Skip to content

Commit

Permalink
Gets the peer address (if available) in server requests.
Browse files Browse the repository at this point in the history
R=byungchul@chromium.org, lcwu@chromium.org, pfeldman@chromium.org, rsleevi@chromium.org
BUG=347770

Review URL: https://codereview.chromium.org/181063006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258424 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
gunsch@chromium.org committed Mar 20, 2014
1 parent c5f45ba commit 0cbc29b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions net/server/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ void HttpServer::DidRead(StreamListenSocket* socket,
if (!ParseHeaders(connection, &request, &pos))
break;

// Sets peer address if exists.
socket->GetPeerAddress(&request.peer);

std::string connection_header = request.GetHeaderValue("connection");
if (connection_header == "Upgrade") {
connection->web_socket_.reset(WebSocket::CreateWebSocket(connection,
Expand Down
5 changes: 5 additions & 0 deletions net/server/http_server_request_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <map>
#include <string>

#include "net/base/ip_endpoint.h"

namespace net {

// Meta information about an HTTP request.
Expand All @@ -23,6 +25,9 @@ class HttpServerRequestInfo {
// lower case.
std::string GetHeaderValue(const std::string& header_name) const;

// Request peer address.
IPEndPoint peer;

// Request method.
std::string method;

Expand Down
1 change: 1 addition & 0 deletions net/server/http_server_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ TEST_F(HttpServerTest, Request) {
ASSERT_EQ("/test", requests_[0].path);
ASSERT_EQ("", requests_[0].data);
ASSERT_EQ(0u, requests_[0].headers.size());
ASSERT_TRUE(StartsWithASCII(requests_[0].peer.ToString(), "127.0.0.1", true));
}

TEST_F(HttpServerTest, RequestWithHeaders) {
Expand Down
17 changes: 17 additions & 0 deletions net/socket/stream_listen_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ int StreamListenSocket::GetLocalAddress(IPEndPoint* address) {
return OK;
}

int StreamListenSocket::GetPeerAddress(IPEndPoint* address) {
SockaddrStorage storage;
if (getpeername(socket_, storage.addr, &storage.addr_len)) {
#if defined(OS_WIN)
int err = WSAGetLastError();
#else
int err = errno;
#endif
return MapSystemError(err);
}

if (!address->FromSockAddr(storage.addr, storage.addr_len))
return ERR_ADDRESS_INVALID;

return OK;
}

SocketDescriptor StreamListenSocket::AcceptSocket() {
SocketDescriptor conn = HANDLE_EINTR(accept(socket_, NULL, NULL));
if (conn == kInvalidSocket)
Expand Down
2 changes: 2 additions & 0 deletions net/socket/stream_listen_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class NET_EXPORT StreamListenSocket

// Copies the local address to |address|. Returns a network error code.
int GetLocalAddress(IPEndPoint* address);
// Copies the peer address to |address|. Returns a network error code.
int GetPeerAddress(IPEndPoint* address);

static const int kSocketError;

Expand Down

0 comments on commit 0cbc29b

Please sign in to comment.