@@ -157,6 +157,7 @@ class Stream {
157157 virtual int read (char * ptr, size_t size) = 0;
158158 virtual int write (const char * ptr, size_t size1) = 0;
159159 virtual int write (const char * ptr) = 0;
160+ virtual std::string get_remote_addr () = 0;
160161
161162 template <typename ...Args>
162163 void write_format (const char * fmt, const Args& ...args);
@@ -170,6 +171,26 @@ class SocketStream : public Stream {
170171 virtual int read (char * ptr, size_t size);
171172 virtual int write (const char * ptr, size_t size);
172173 virtual int write (const char * ptr);
174+
175+ std::string get_remote_addr () {
176+ socklen_t len;
177+ struct sockaddr_storage addr;
178+ char ipstr[INET6_ADDRSTRLEN];
179+
180+ len = sizeof addr;
181+ getpeername (sock_, (struct sockaddr *)&addr, &len);
182+
183+ // deal with both IPv4 and IPv6:
184+ if (addr.ss_family == AF_INET) {
185+ struct sockaddr_in *s = (struct sockaddr_in *)&addr;
186+ inet_ntop (AF_INET, &s->sin_addr , ipstr, sizeof ipstr);
187+ } else { // AF_INET6
188+ struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
189+ inet_ntop (AF_INET6, &s->sin6_addr , ipstr, sizeof ipstr);
190+ }
191+
192+ return ipstr;
193+ }
173194
174195private:
175196 socket_t sock_;
@@ -1583,6 +1604,8 @@ inline bool Server::process_request(Stream& strm, bool last_connection)
15831604 ret = false ;
15841605 }
15851606
1607+ req.set_header (" REMOTE_ADDR" , strm.get_remote_addr ().c_str ());
1608+
15861609 // Body
15871610 if (req.method == " POST" ) {
15881611 if (!detail::read_content (strm, req)) {
0 commit comments