OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 for (std::list<Connection*>::const_iterator it = connections.begin(); | 86 for (std::list<Connection*>::const_iterator it = connections.begin(); |
87 it != connections.end(); ++it) { | 87 it != connections.end(); ++it) { |
88 (*it)->InitiateClose(force); | 88 (*it)->InitiateClose(force); |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 HttpServer::Connection* | 92 HttpServer::Connection* |
93 HttpServer::Find(int connection_id) { | 93 HttpServer::Find(int connection_id) { |
94 ConnectionMap::iterator it = connections_.find(connection_id); | 94 ConnectionMap::iterator it = connections_.find(connection_id); |
95 if (it == connections_.end()) | 95 if (it == connections_.end()) |
96 return NULL; | 96 return nullptr; |
97 return it->second; | 97 return it->second; |
98 } | 98 } |
99 | 99 |
100 void | 100 void |
101 HttpServer::Remove(int connection_id) { | 101 HttpServer::Remove(int connection_id) { |
102 ConnectionMap::iterator it = connections_.find(connection_id); | 102 ConnectionMap::iterator it = connections_.find(connection_id); |
103 if (it == connections_.end()) { | 103 if (it == connections_.end()) { |
104 RTC_NOTREACHED(); | 104 RTC_NOTREACHED(); |
105 return; | 105 return; |
106 } | 106 } |
107 Connection* connection = it->second; | 107 Connection* connection = it->second; |
108 connections_.erase(it); | 108 connections_.erase(it); |
109 SignalConnectionClosed(this, connection_id, connection->EndProcess()); | 109 SignalConnectionClosed(this, connection_id, connection->EndProcess()); |
110 delete connection; | 110 delete connection; |
111 if (closing_ && connections_.empty()) { | 111 if (closing_ && connections_.empty()) { |
112 closing_ = false; | 112 closing_ = false; |
113 SignalCloseAllComplete(this); | 113 SignalCloseAllComplete(this); |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
118 // HttpServer::Connection | 118 // HttpServer::Connection |
119 /////////////////////////////////////////////////////////////////////////////// | 119 /////////////////////////////////////////////////////////////////////////////// |
120 | 120 |
121 HttpServer::Connection::Connection(int connection_id, HttpServer* server) | 121 HttpServer::Connection::Connection(int connection_id, HttpServer* server) |
122 : connection_id_(connection_id), server_(server), | 122 : connection_id_(connection_id), |
123 current_(NULL), signalling_(false), close_(false) { | 123 server_(server), |
124 } | 124 current_(nullptr), |
| 125 signalling_(false), |
| 126 close_(false) {} |
125 | 127 |
126 HttpServer::Connection::~Connection() { | 128 HttpServer::Connection::~Connection() { |
127 // It's possible that an object hosted inside this transaction signalled | 129 // It's possible that an object hosted inside this transaction signalled |
128 // an event which caused the connection to close. | 130 // an event which caused the connection to close. |
129 Thread::Current()->Dispose(current_); | 131 Thread::Current()->Dispose(current_); |
130 } | 132 } |
131 | 133 |
132 void | 134 void |
133 HttpServer::Connection::BeginProcess(StreamInterface* stream) { | 135 HttpServer::Connection::BeginProcess(StreamInterface* stream) { |
134 base_.notify(this); | 136 base_.notify(this); |
135 base_.attach(stream); | 137 base_.attach(stream); |
136 current_ = new HttpServerTransaction(connection_id_); | 138 current_ = new HttpServerTransaction(connection_id_); |
137 if (base_.mode() != HM_CONNECT) | 139 if (base_.mode() != HM_CONNECT) |
138 base_.recv(¤t_->request); | 140 base_.recv(¤t_->request); |
139 } | 141 } |
140 | 142 |
141 StreamInterface* | 143 StreamInterface* |
142 HttpServer::Connection::EndProcess() { | 144 HttpServer::Connection::EndProcess() { |
143 base_.notify(NULL); | 145 base_.notify(nullptr); |
144 base_.abort(HE_DISCONNECTED); | 146 base_.abort(HE_DISCONNECTED); |
145 return base_.detach(); | 147 return base_.detach(); |
146 } | 148 } |
147 | 149 |
148 void | 150 void |
149 HttpServer::Connection::Respond(HttpServerTransaction* transaction) { | 151 HttpServer::Connection::Respond(HttpServerTransaction* transaction) { |
150 RTC_DCHECK(current_ == NULL); | 152 RTC_DCHECK(current_ == nullptr); |
151 current_ = transaction; | 153 current_ = transaction; |
152 if (current_->response.begin() == current_->response.end()) { | 154 if (current_->response.begin() == current_->response.end()) { |
153 current_->response.set_error(HC_INTERNAL_SERVER_ERROR); | 155 current_->response.set_error(HC_INTERNAL_SERVER_ERROR); |
154 } | 156 } |
155 bool keep_alive = HttpShouldKeepAlive(current_->request); | 157 bool keep_alive = HttpShouldKeepAlive(current_->request); |
156 current_->response.setHeader(HH_CONNECTION, | 158 current_->response.setHeader(HH_CONNECTION, |
157 keep_alive ? "Keep-Alive" : "Close", | 159 keep_alive ? "Keep-Alive" : "Close", |
158 false); | 160 false); |
159 close_ = !HttpShouldKeepAlive(current_->response); | 161 close_ = !HttpShouldKeepAlive(current_->response); |
160 base_.send(¤t_->response); | 162 base_.send(¤t_->response); |
161 } | 163 } |
162 | 164 |
163 void | 165 void |
164 HttpServer::Connection::InitiateClose(bool force) { | 166 HttpServer::Connection::InitiateClose(bool force) { |
165 bool request_in_progress = (HM_SEND == base_.mode()) || (NULL == current_); | 167 bool request_in_progress = (HM_SEND == base_.mode()) || (nullptr == current_); |
166 if (!signalling_ && (force || !request_in_progress)) { | 168 if (!signalling_ && (force || !request_in_progress)) { |
167 server_->Remove(connection_id_); | 169 server_->Remove(connection_id_); |
168 } else { | 170 } else { |
169 close_ = true; | 171 close_ = true; |
170 } | 172 } |
171 } | 173 } |
172 | 174 |
173 // | 175 // |
174 // IHttpNotify Implementation | 176 // IHttpNotify Implementation |
175 // | 177 // |
176 | 178 |
177 HttpError | 179 HttpError |
178 HttpServer::Connection::onHttpHeaderComplete(bool chunked, size_t& data_size) { | 180 HttpServer::Connection::onHttpHeaderComplete(bool chunked, size_t& data_size) { |
179 if (data_size == SIZE_UNKNOWN) { | 181 if (data_size == SIZE_UNKNOWN) { |
180 data_size = 0; | 182 data_size = 0; |
181 } | 183 } |
182 RTC_DCHECK(current_ != NULL); | 184 RTC_DCHECK(current_ != nullptr); |
183 bool custom_document = false; | 185 bool custom_document = false; |
184 server_->SignalHttpRequestHeader(server_, current_, &custom_document); | 186 server_->SignalHttpRequestHeader(server_, current_, &custom_document); |
185 if (!custom_document) { | 187 if (!custom_document) { |
186 current_->request.document.reset(new MemoryStream); | 188 current_->request.document.reset(new MemoryStream); |
187 } | 189 } |
188 return HE_NONE; | 190 return HE_NONE; |
189 } | 191 } |
190 | 192 |
191 void | 193 void |
192 HttpServer::Connection::onHttpComplete(HttpMode mode, HttpError err) { | 194 HttpServer::Connection::onHttpComplete(HttpMode mode, HttpError err) { |
193 if (mode == HM_SEND) { | 195 if (mode == HM_SEND) { |
194 RTC_DCHECK(current_ != NULL); | 196 RTC_DCHECK(current_ != nullptr); |
195 signalling_ = true; | 197 signalling_ = true; |
196 server_->SignalHttpRequestComplete(server_, current_, err); | 198 server_->SignalHttpRequestComplete(server_, current_, err); |
197 signalling_ = false; | 199 signalling_ = false; |
198 if (close_) { | 200 if (close_) { |
199 // Force a close | 201 // Force a close |
200 err = HE_DISCONNECTED; | 202 err = HE_DISCONNECTED; |
201 } | 203 } |
202 } | 204 } |
203 if (err != HE_NONE) { | 205 if (err != HE_NONE) { |
204 server_->Remove(connection_id_); | 206 server_->Remove(connection_id_); |
205 } else if (mode == HM_CONNECT) { | 207 } else if (mode == HM_CONNECT) { |
206 base_.recv(¤t_->request); | 208 base_.recv(¤t_->request); |
207 } else if (mode == HM_RECV) { | 209 } else if (mode == HM_RECV) { |
208 RTC_DCHECK(current_ != NULL); | 210 RTC_DCHECK(current_ != nullptr); |
209 // TODO: do we need this? | 211 // TODO: do we need this? |
210 //request_.document_->rewind(); | 212 //request_.document_->rewind(); |
211 HttpServerTransaction* transaction = current_; | 213 HttpServerTransaction* transaction = current_; |
212 current_ = NULL; | 214 current_ = nullptr; |
213 server_->SignalHttpRequest(server_, transaction); | 215 server_->SignalHttpRequest(server_, transaction); |
214 } else if (mode == HM_SEND) { | 216 } else if (mode == HM_SEND) { |
215 Thread::Current()->Dispose(current_->response.document.release()); | 217 Thread::Current()->Dispose(current_->response.document.release()); |
216 current_->request.clear(true); | 218 current_->request.clear(true); |
217 current_->response.clear(true); | 219 current_->response.clear(true); |
218 base_.recv(¤t_->request); | 220 base_.recv(¤t_->request); |
219 } else { | 221 } else { |
220 RTC_NOTREACHED(); | 222 RTC_NOTREACHED(); |
221 } | 223 } |
222 } | 224 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 } | 264 } |
263 | 265 |
264 void HttpListenServer::StopListening() { | 266 void HttpListenServer::StopListening() { |
265 if (listener_) { | 267 if (listener_) { |
266 listener_->Close(); | 268 listener_->Close(); |
267 } | 269 } |
268 } | 270 } |
269 | 271 |
270 void HttpListenServer::OnReadEvent(AsyncSocket* socket) { | 272 void HttpListenServer::OnReadEvent(AsyncSocket* socket) { |
271 RTC_DCHECK(socket == listener_.get()); | 273 RTC_DCHECK(socket == listener_.get()); |
272 AsyncSocket* incoming = listener_->Accept(NULL); | 274 AsyncSocket* incoming = listener_->Accept(nullptr); |
273 if (incoming) { | 275 if (incoming) { |
274 StreamInterface* stream = new SocketStream(incoming); | 276 StreamInterface* stream = new SocketStream(incoming); |
275 //stream = new LoggingAdapter(stream, LS_VERBOSE, "HttpServer", false); | 277 //stream = new LoggingAdapter(stream, LS_VERBOSE, "HttpServer", false); |
276 HandleConnection(stream); | 278 HandleConnection(stream); |
277 } | 279 } |
278 } | 280 } |
279 | 281 |
280 void HttpListenServer::OnConnectionClosed(HttpServer* server, | 282 void HttpListenServer::OnConnectionClosed(HttpServer* server, |
281 int connection_id, | 283 int connection_id, |
282 StreamInterface* stream) { | 284 StreamInterface* stream) { |
283 Thread::Current()->Dispose(stream); | 285 Thread::Current()->Dispose(stream); |
284 } | 286 } |
285 | 287 |
286 /////////////////////////////////////////////////////////////////////////////// | 288 /////////////////////////////////////////////////////////////////////////////// |
287 | 289 |
288 } // namespace rtc | 290 } // namespace rtc |
OLD | NEW |