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_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "net/server/http_connection.h" | 14 #include "net/server/http_connection.h" |
15 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
| 16 #include "net/server/http_server_response_info.h" |
16 #include "net/server/web_socket.h" | 17 #include "net/server/web_socket.h" |
17 #include "net/socket/tcp_listen_socket.h" | 18 #include "net/socket/tcp_listen_socket.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 HttpServer::HttpServer(const StreamListenSocketFactory& factory, | 22 HttpServer::HttpServer(const StreamListenSocketFactory& factory, |
22 HttpServer::Delegate* delegate) | 23 HttpServer::Delegate* delegate) |
23 : delegate_(delegate), | 24 : delegate_(delegate), |
24 server_(factory.CreateAndListen(this)) { | 25 server_(factory.CreateAndListen(this)) { |
25 DCHECK(server_.get()); | 26 DCHECK(server_.get()); |
(...skipping 12 matching lines...) Expand all Loading... |
38 | 39 |
39 void HttpServer::SendOverWebSocket(int connection_id, | 40 void HttpServer::SendOverWebSocket(int connection_id, |
40 const std::string& data) { | 41 const std::string& data) { |
41 HttpConnection* connection = FindConnection(connection_id); | 42 HttpConnection* connection = FindConnection(connection_id); |
42 if (connection == NULL) | 43 if (connection == NULL) |
43 return; | 44 return; |
44 DCHECK(connection->web_socket_.get()); | 45 DCHECK(connection->web_socket_.get()); |
45 connection->web_socket_->Send(data); | 46 connection->web_socket_->Send(data); |
46 } | 47 } |
47 | 48 |
| 49 void HttpServer::SendResponse(int connection_id, |
| 50 const HttpServerResponseInfo& response) { |
| 51 HttpConnection* connection = FindConnection(connection_id); |
| 52 if (connection == NULL) |
| 53 return; |
| 54 connection->Send(response); |
| 55 } |
| 56 |
48 void HttpServer::Send(int connection_id, | 57 void HttpServer::Send(int connection_id, |
49 HttpStatusCode status_code, | 58 HttpStatusCode status_code, |
50 const std::string& data, | 59 const std::string& data, |
51 const std::string& content_type) { | 60 const std::string& content_type) { |
52 HttpConnection* connection = FindConnection(connection_id); | 61 HttpServerResponseInfo response(status_code); |
53 if (connection == NULL) | 62 response.SetBody(data, content_type); |
54 return; | 63 SendResponse(connection_id, response); |
55 connection->Send(status_code, data, content_type); | |
56 } | 64 } |
57 | 65 |
58 void HttpServer::Send200(int connection_id, | 66 void HttpServer::Send200(int connection_id, |
59 const std::string& data, | 67 const std::string& data, |
60 const std::string& content_type) { | 68 const std::string& content_type) { |
61 Send(connection_id, HTTP_OK, data, content_type); | 69 Send(connection_id, HTTP_OK, data, content_type); |
62 } | 70 } |
63 | 71 |
64 void HttpServer::Send404(int connection_id) { | 72 void HttpServer::Send404(int connection_id) { |
65 Send(connection_id, HTTP_NOT_FOUND, std::string(), "text/html"); | 73 SendResponse(connection_id, HttpServerResponseInfo::CreateFor404()); |
66 } | 74 } |
67 | 75 |
68 void HttpServer::Send500(int connection_id, const std::string& message) { | 76 void HttpServer::Send500(int connection_id, const std::string& message) { |
69 Send(connection_id, HTTP_INTERNAL_SERVER_ERROR, message, "text/html"); | 77 SendResponse(connection_id, HttpServerResponseInfo::CreateFor500(message)); |
70 } | 78 } |
71 | 79 |
72 void HttpServer::Close(int connection_id) { | 80 void HttpServer::Close(int connection_id) { |
73 HttpConnection* connection = FindConnection(connection_id); | 81 HttpConnection* connection = FindConnection(connection_id); |
74 if (connection == NULL) | 82 if (connection == NULL) |
75 return; | 83 return; |
76 | 84 |
77 // Initiating close from server-side does not lead to the DidClose call. | 85 // Initiating close from server-side does not lead to the DidClose call. |
78 // Do it manually here. | 86 // Do it manually here. |
79 DidClose(connection->socket_.get()); | 87 DidClose(connection->socket_.get()); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 } | 299 } |
292 | 300 |
293 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { | 301 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { |
294 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); | 302 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); |
295 if (it == socket_to_connection_.end()) | 303 if (it == socket_to_connection_.end()) |
296 return NULL; | 304 return NULL; |
297 return it->second; | 305 return it->second; |
298 } | 306 } |
299 | 307 |
300 } // namespace net | 308 } // namespace net |
OLD | NEW |