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