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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/stl_util.h" | |
16 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
19 #include "base/sys_byteorder.h" | 18 #include "base/sys_byteorder.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
21 #include "build/build_config.h" | 20 #include "build/build_config.h" |
22 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
23 #include "net/server/http_connection.h" | 22 #include "net/server/http_connection.h" |
24 #include "net/server/http_server_request_info.h" | 23 #include "net/server/http_server_request_info.h" |
25 #include "net/server/http_server_response_info.h" | 24 #include "net/server/http_server_response_info.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
38 weak_ptr_factory_(this) { | 37 weak_ptr_factory_(this) { |
39 DCHECK(server_socket_); | 38 DCHECK(server_socket_); |
40 // Start accepting connections in next run loop in case when delegate is not | 39 // Start accepting connections in next run loop in case when delegate is not |
41 // ready to get callbacks. | 40 // ready to get callbacks. |
42 base::ThreadTaskRunnerHandle::Get()->PostTask( | 41 base::ThreadTaskRunnerHandle::Get()->PostTask( |
43 FROM_HERE, | 42 FROM_HERE, |
44 base::Bind(&HttpServer::DoAcceptLoop, weak_ptr_factory_.GetWeakPtr())); | 43 base::Bind(&HttpServer::DoAcceptLoop, weak_ptr_factory_.GetWeakPtr())); |
45 } | 44 } |
46 | 45 |
47 HttpServer::~HttpServer() { | 46 HttpServer::~HttpServer() { |
48 base::STLDeleteContainerPairSecondPointers(id_to_connection_.begin(), | |
49 id_to_connection_.end()); | |
50 } | 47 } |
51 | 48 |
52 void HttpServer::AcceptWebSocket( | 49 void HttpServer::AcceptWebSocket( |
53 int connection_id, | 50 int connection_id, |
54 const HttpServerRequestInfo& request) { | 51 const HttpServerRequestInfo& request) { |
55 HttpConnection* connection = FindConnection(connection_id); | 52 HttpConnection* connection = FindConnection(connection_id); |
56 if (connection == NULL) | 53 if (connection == NULL) |
57 return; | 54 return; |
58 DCHECK(connection->web_socket()); | 55 DCHECK(connection->web_socket()); |
59 connection->web_socket()->Accept(request); | 56 connection->web_socket()->Accept(request); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 | 98 |
102 void HttpServer::Send404(int connection_id) { | 99 void HttpServer::Send404(int connection_id) { |
103 SendResponse(connection_id, HttpServerResponseInfo::CreateFor404()); | 100 SendResponse(connection_id, HttpServerResponseInfo::CreateFor404()); |
104 } | 101 } |
105 | 102 |
106 void HttpServer::Send500(int connection_id, const std::string& message) { | 103 void HttpServer::Send500(int connection_id, const std::string& message) { |
107 SendResponse(connection_id, HttpServerResponseInfo::CreateFor500(message)); | 104 SendResponse(connection_id, HttpServerResponseInfo::CreateFor500(message)); |
108 } | 105 } |
109 | 106 |
110 void HttpServer::Close(int connection_id) { | 107 void HttpServer::Close(int connection_id) { |
111 HttpConnection* connection = FindConnection(connection_id); | 108 auto it = id_to_connection_.find(connection_id); |
112 if (connection == NULL) | 109 if (it == id_to_connection_.end()) |
113 return; | 110 return; |
114 | 111 |
112 std::unique_ptr<HttpConnection> connection = std::move(it->second); | |
115 id_to_connection_.erase(connection_id); | 113 id_to_connection_.erase(connection_id); |
davidben
2016/09/16 22:11:13
Nit: id_to_connection_.erase(it) to save a lookup?
Avi (use Gerrit)
2016/09/16 22:14:40
Done.
| |
116 delegate_->OnClose(connection_id); | 114 delegate_->OnClose(connection_id); |
117 | 115 |
118 // The call stack might have callbacks which still have the pointer of | 116 // The call stack might have callbacks which still have the pointer of |
119 // connection. Instead of referencing connection with ID all the time, | 117 // connection. Instead of referencing connection with ID all the time, |
120 // destroys the connection in next run loop to make sure any pending | 118 // destroys the connection in next run loop to make sure any pending |
121 // callbacks in the call stack return. | 119 // callbacks in the call stack return. |
122 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, connection); | 120 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, |
121 connection.release()); | |
123 } | 122 } |
124 | 123 |
125 int HttpServer::GetLocalAddress(IPEndPoint* address) { | 124 int HttpServer::GetLocalAddress(IPEndPoint* address) { |
126 return server_socket_->GetLocalAddress(address); | 125 return server_socket_->GetLocalAddress(address); |
127 } | 126 } |
128 | 127 |
129 void HttpServer::SetReceiveBufferSize(int connection_id, int32_t size) { | 128 void HttpServer::SetReceiveBufferSize(int connection_id, int32_t size) { |
130 HttpConnection* connection = FindConnection(connection_id); | 129 HttpConnection* connection = FindConnection(connection_id); |
131 if (connection) | 130 if (connection) |
132 connection->read_buf()->set_max_buffer_size(size); | 131 connection->read_buf()->set_max_buffer_size(size); |
(...skipping 21 matching lines...) Expand all Loading... | |
154 if (HandleAcceptResult(rv) == OK) | 153 if (HandleAcceptResult(rv) == OK) |
155 DoAcceptLoop(); | 154 DoAcceptLoop(); |
156 } | 155 } |
157 | 156 |
158 int HttpServer::HandleAcceptResult(int rv) { | 157 int HttpServer::HandleAcceptResult(int rv) { |
159 if (rv < 0) { | 158 if (rv < 0) { |
160 LOG(ERROR) << "Accept error: rv=" << rv; | 159 LOG(ERROR) << "Accept error: rv=" << rv; |
161 return rv; | 160 return rv; |
162 } | 161 } |
163 | 162 |
164 HttpConnection* connection = | 163 std::unique_ptr<HttpConnection> connection_ptr = |
165 new HttpConnection(++last_id_, std::move(accepted_socket_)); | 164 base::MakeUnique<HttpConnection>(++last_id_, std::move(accepted_socket_)); |
166 id_to_connection_[connection->id()] = connection; | 165 HttpConnection* connection = connection_ptr.get(); |
166 id_to_connection_[connection->id()] = std::move(connection_ptr); | |
167 delegate_->OnConnect(connection->id()); | 167 delegate_->OnConnect(connection->id()); |
168 if (!HasClosedConnection(connection)) | 168 if (!HasClosedConnection(connection)) |
169 DoReadLoop(connection); | 169 DoReadLoop(connection); |
170 return OK; | 170 return OK; |
171 } | 171 } |
172 | 172 |
173 void HttpServer::DoReadLoop(HttpConnection* connection) { | 173 void HttpServer::DoReadLoop(HttpConnection* connection) { |
174 int rv; | 174 int rv; |
175 do { | 175 do { |
176 HttpConnection::ReadIOBuffer* read_buf = connection->read_buf(); | 176 HttpConnection::ReadIOBuffer* read_buf = connection->read_buf(); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
446 case ST_ERR: | 446 case ST_ERR: |
447 return false; | 447 return false; |
448 } | 448 } |
449 } | 449 } |
450 } | 450 } |
451 // No more characters, but we haven't finished parsing yet. | 451 // No more characters, but we haven't finished parsing yet. |
452 return false; | 452 return false; |
453 } | 453 } |
454 | 454 |
455 HttpConnection* HttpServer::FindConnection(int connection_id) { | 455 HttpConnection* HttpServer::FindConnection(int connection_id) { |
456 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id); | 456 auto it = id_to_connection_.find(connection_id); |
457 if (it == id_to_connection_.end()) | 457 if (it == id_to_connection_.end()) |
458 return NULL; | 458 return nullptr; |
459 return it->second; | 459 return it->second.get(); |
460 } | 460 } |
461 | 461 |
462 // This is called after any delegate callbacks are called to check if Close() | 462 // This is called after any delegate callbacks are called to check if Close() |
463 // has been called during callback processing. Using the pointer of connection, | 463 // has been called during callback processing. Using the pointer of connection, |
464 // |connection| is safe here because Close() deletes the connection in next run | 464 // |connection| is safe here because Close() deletes the connection in next run |
465 // loop. | 465 // loop. |
466 bool HttpServer::HasClosedConnection(HttpConnection* connection) { | 466 bool HttpServer::HasClosedConnection(HttpConnection* connection) { |
467 return FindConnection(connection->id()) != connection; | 467 return FindConnection(connection->id()) != connection; |
468 } | 468 } |
469 | 469 |
470 } // namespace net | 470 } // namespace net |
OLD | NEW |