Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/server/http_server.h" | 5 #include "net/server/http_server.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "net/base/ip_endpoint.h" | |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/server/http_connection.h" | 17 #include "net/server/http_connection.h" |
| 17 #include "net/server/http_server_request_info.h" | 18 #include "net/server/http_server_request_info.h" |
| 18 #include "net/server/http_server_response_info.h" | 19 #include "net/server/http_server_response_info.h" |
| 19 #include "net/server/web_socket.h" | 20 #include "net/server/web_socket.h" |
| 20 #include "net/socket/tcp_listen_socket.h" | 21 #include "net/socket/tcp_listen_socket.h" |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| 23 | 24 |
| 24 HttpServer::HttpServer(const StreamListenSocketFactory& factory, | 25 HttpServer::HttpServer(const StreamListenSocketFactory& factory, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 } | 126 } |
| 126 delegate_->OnWebSocketMessage(connection->id(), message); | 127 delegate_->OnWebSocketMessage(connection->id(), message); |
| 127 continue; | 128 continue; |
| 128 } | 129 } |
| 129 | 130 |
| 130 HttpServerRequestInfo request; | 131 HttpServerRequestInfo request; |
| 131 size_t pos = 0; | 132 size_t pos = 0; |
| 132 if (!ParseHeaders(connection, &request, &pos)) | 133 if (!ParseHeaders(connection, &request, &pos)) |
| 133 break; | 134 break; |
| 134 | 135 |
| 136 // Sets peer address if exists. | |
| 137 IPEndPoint peer; | |
| 138 if (socket->GetPeerAddress(&peer) == OK) { | |
| 139 request.peer = peer; | |
|
Ryan Sleevi
2014/03/11 01:32:43
Why not socket->GetPeerAddress(&request.peer)?
Se
gunsch
2014/03/17 16:57:15
Done.
| |
| 140 } | |
| 141 | |
| 135 std::string connection_header = request.GetHeaderValue("connection"); | 142 std::string connection_header = request.GetHeaderValue("connection"); |
| 136 if (connection_header == "Upgrade") { | 143 if (connection_header == "Upgrade") { |
| 137 connection->web_socket_.reset(WebSocket::CreateWebSocket(connection, | 144 connection->web_socket_.reset(WebSocket::CreateWebSocket(connection, |
| 138 request, | 145 request, |
| 139 &pos)); | 146 &pos)); |
| 140 | 147 |
| 141 if (!connection->web_socket_.get()) // Not enough data was received. | 148 if (!connection->web_socket_.get()) // Not enough data was received. |
| 142 break; | 149 break; |
| 143 delegate_->OnWebSocketRequest(connection->id(), request); | 150 delegate_->OnWebSocketRequest(connection->id(), request); |
| 144 connection->Shift(pos); | 151 connection->Shift(pos); |
| 145 continue; | 152 continue; |
| 146 } | 153 } |
| 147 | 154 |
| 155 // Rejects non "HTTP/1.1" requests and terminate connection. | |
| 156 if (request.proto != "HTTP/1.1") { | |
| 157 connection->Send(HttpServerResponseInfo(HTTP_NOT_IMPLEMENTED)); | |
| 158 Close(connection->id()); | |
| 159 break; | |
| 160 } | |
|
Ryan Sleevi
2014/03/11 01:32:43
Unrelated?
gunsch
2014/03/17 16:57:15
This was in our local changes to this class, but I
| |
| 161 | |
| 148 const char kContentLength[] = "content-length"; | 162 const char kContentLength[] = "content-length"; |
| 149 if (request.headers.count(kContentLength)) { | 163 if (request.headers.count(kContentLength)) { |
| 150 size_t content_length = 0; | 164 size_t content_length = 0; |
| 151 const size_t kMaxBodySize = 100 << 20; | 165 const size_t kMaxBodySize = 100 << 20; |
| 152 if (!base::StringToSizeT(request.GetHeaderValue(kContentLength), | 166 if (!base::StringToSizeT(request.GetHeaderValue(kContentLength), |
| 153 &content_length) || | 167 &content_length) || |
| 154 content_length > kMaxBodySize) { | 168 content_length > kMaxBodySize) { |
| 155 connection->Send(HttpServerResponseInfo::CreateFor500( | 169 connection->Send(HttpServerResponseInfo::CreateFor500( |
| 156 "request content-length too big or unknown: " + | 170 "request content-length too big or unknown: " + |
| 157 request.GetHeaderValue(kContentLength))); | 171 request.GetHeaderValue(kContentLength))); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 switch (state) { | 279 switch (state) { |
| 266 case ST_METHOD: | 280 case ST_METHOD: |
| 267 info->method = buffer; | 281 info->method = buffer; |
| 268 buffer.clear(); | 282 buffer.clear(); |
| 269 break; | 283 break; |
| 270 case ST_URL: | 284 case ST_URL: |
| 271 info->path = buffer; | 285 info->path = buffer; |
| 272 buffer.clear(); | 286 buffer.clear(); |
| 273 break; | 287 break; |
| 274 case ST_PROTO: | 288 case ST_PROTO: |
| 275 // TODO(mbelshe): Deal better with parsing protocol. | 289 info->proto = buffer; |
|
Ryan Sleevi
2014/03/11 01:32:43
ditto
gunsch
2014/03/17 16:57:15
Done.
| |
| 276 DCHECK(buffer == "HTTP/1.1"); | |
| 277 buffer.clear(); | 290 buffer.clear(); |
| 278 break; | 291 break; |
| 279 case ST_NAME: | 292 case ST_NAME: |
| 280 header_name = StringToLowerASCII(buffer); | 293 header_name = StringToLowerASCII(buffer); |
| 281 buffer.clear(); | 294 buffer.clear(); |
| 282 break; | 295 break; |
| 283 case ST_VALUE: | 296 case ST_VALUE: |
| 284 TrimWhitespaceASCII(buffer, TRIM_LEADING, &header_value); | 297 TrimWhitespaceASCII(buffer, TRIM_LEADING, &header_value); |
| 285 // TODO(mbelshe): Deal better with duplicate headers | 298 // TODO(mbelshe): Deal better with duplicate headers |
| 286 DCHECK(info->headers.find(header_name) == info->headers.end()); | 299 DCHECK(info->headers.find(header_name) == info->headers.end()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 } | 334 } |
| 322 | 335 |
| 323 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { | 336 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { |
| 324 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); | 337 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); |
| 325 if (it == socket_to_connection_.end()) | 338 if (it == socket_to_connection_.end()) |
| 326 return NULL; | 339 return NULL; |
| 327 return it->second; | 340 return it->second; |
| 328 } | 341 } |
| 329 | 342 |
| 330 } // namespace net | 343 } // namespace net |
| OLD | NEW |