blob: fee7a2c4c61779fda9509eefd214b34c3cb502cf [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <algorithm>
12
13#include "webrtc/base/httpcommon-inl.h"
14
15#include "webrtc/base/asyncsocket.h"
nissec80e7412017-01-11 05:56:46 -080016#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/common.h"
18#include "webrtc/base/httpserver.h"
19#include "webrtc/base/logging.h"
20#include "webrtc/base/socketstream.h"
21#include "webrtc/base/thread.h"
22
23namespace rtc {
24
25///////////////////////////////////////////////////////////////////////////////
26// HttpServer
27///////////////////////////////////////////////////////////////////////////////
28
29HttpServer::HttpServer() : next_connection_id_(1), closing_(false) {
30}
31
32HttpServer::~HttpServer() {
33 if (closing_) {
34 LOG(LS_WARNING) << "HttpServer::CloseAll has not completed";
35 }
36 for (ConnectionMap::iterator it = connections_.begin();
37 it != connections_.end();
38 ++it) {
39 StreamInterface* stream = it->second->EndProcess();
40 delete stream;
41 delete it->second;
42 }
43}
44
45int
46HttpServer::HandleConnection(StreamInterface* stream) {
47 int connection_id = next_connection_id_++;
48 ASSERT(connection_id != HTTP_INVALID_CONNECTION_ID);
49 Connection* connection = new Connection(connection_id, this);
50 connections_.insert(ConnectionMap::value_type(connection_id, connection));
51 connection->BeginProcess(stream);
52 return connection_id;
53}
54
55void
56HttpServer::Respond(HttpServerTransaction* transaction) {
57 int connection_id = transaction->connection_id();
58 if (Connection* connection = Find(connection_id)) {
59 connection->Respond(transaction);
60 } else {
61 delete transaction;
62 // We may be tempted to SignalHttpComplete, but that implies that a
63 // connection still exists.
64 }
65}
66
67void
68HttpServer::Close(int connection_id, bool force) {
69 if (Connection* connection = Find(connection_id)) {
70 connection->InitiateClose(force);
71 }
72}
73
74void
75HttpServer::CloseAll(bool force) {
76 if (connections_.empty()) {
77 SignalCloseAllComplete(this);
78 return;
79 }
80 closing_ = true;
81 std::list<Connection*> connections;
82 for (ConnectionMap::const_iterator it = connections_.begin();
83 it != connections_.end(); ++it) {
84 connections.push_back(it->second);
85 }
86 for (std::list<Connection*>::const_iterator it = connections.begin();
87 it != connections.end(); ++it) {
88 (*it)->InitiateClose(force);
89 }
90}
91
92HttpServer::Connection*
93HttpServer::Find(int connection_id) {
94 ConnectionMap::iterator it = connections_.find(connection_id);
95 if (it == connections_.end())
96 return NULL;
97 return it->second;
98}
99
100void
101HttpServer::Remove(int connection_id) {
102 ConnectionMap::iterator it = connections_.find(connection_id);
103 if (it == connections_.end()) {
nissec80e7412017-01-11 05:56:46 -0800104 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 return;
106 }
107 Connection* connection = it->second;
108 connections_.erase(it);
109 SignalConnectionClosed(this, connection_id, connection->EndProcess());
110 delete connection;
111 if (closing_ && connections_.empty()) {
112 closing_ = false;
113 SignalCloseAllComplete(this);
114 }
115}
116
117///////////////////////////////////////////////////////////////////////////////
118// HttpServer::Connection
119///////////////////////////////////////////////////////////////////////////////
120
121HttpServer::Connection::Connection(int connection_id, HttpServer* server)
122 : connection_id_(connection_id), server_(server),
123 current_(NULL), signalling_(false), close_(false) {
124}
125
126HttpServer::Connection::~Connection() {
127 // It's possible that an object hosted inside this transaction signalled
128 // an event which caused the connection to close.
129 Thread::Current()->Dispose(current_);
130}
131
132void
133HttpServer::Connection::BeginProcess(StreamInterface* stream) {
134 base_.notify(this);
135 base_.attach(stream);
136 current_ = new HttpServerTransaction(connection_id_);
137 if (base_.mode() != HM_CONNECT)
138 base_.recv(&current_->request);
139}
140
141StreamInterface*
142HttpServer::Connection::EndProcess() {
143 base_.notify(NULL);
144 base_.abort(HE_DISCONNECTED);
145 return base_.detach();
146}
147
148void
149HttpServer::Connection::Respond(HttpServerTransaction* transaction) {
150 ASSERT(current_ == NULL);
151 current_ = transaction;
152 if (current_->response.begin() == current_->response.end()) {
153 current_->response.set_error(HC_INTERNAL_SERVER_ERROR);
154 }
155 bool keep_alive = HttpShouldKeepAlive(current_->request);
156 current_->response.setHeader(HH_CONNECTION,
157 keep_alive ? "Keep-Alive" : "Close",
158 false);
159 close_ = !HttpShouldKeepAlive(current_->response);
160 base_.send(&current_->response);
161}
162
163void
164HttpServer::Connection::InitiateClose(bool force) {
165 bool request_in_progress = (HM_SEND == base_.mode()) || (NULL == current_);
166 if (!signalling_ && (force || !request_in_progress)) {
167 server_->Remove(connection_id_);
168 } else {
169 close_ = true;
170 }
171}
172
173//
174// IHttpNotify Implementation
175//
176
177HttpError
178HttpServer::Connection::onHttpHeaderComplete(bool chunked, size_t& data_size) {
179 if (data_size == SIZE_UNKNOWN) {
180 data_size = 0;
181 }
182 ASSERT(current_ != NULL);
183 bool custom_document = false;
184 server_->SignalHttpRequestHeader(server_, current_, &custom_document);
185 if (!custom_document) {
186 current_->request.document.reset(new MemoryStream);
187 }
188 return HE_NONE;
189}
190
191void
192HttpServer::Connection::onHttpComplete(HttpMode mode, HttpError err) {
193 if (mode == HM_SEND) {
194 ASSERT(current_ != NULL);
195 signalling_ = true;
196 server_->SignalHttpRequestComplete(server_, current_, err);
197 signalling_ = false;
198 if (close_) {
199 // Force a close
200 err = HE_DISCONNECTED;
201 }
202 }
203 if (err != HE_NONE) {
204 server_->Remove(connection_id_);
205 } else if (mode == HM_CONNECT) {
206 base_.recv(&current_->request);
207 } else if (mode == HM_RECV) {
208 ASSERT(current_ != NULL);
209 // TODO: do we need this?
210 //request_.document_->rewind();
211 HttpServerTransaction* transaction = current_;
212 current_ = NULL;
213 server_->SignalHttpRequest(server_, transaction);
214 } else if (mode == HM_SEND) {
215 Thread::Current()->Dispose(current_->response.document.release());
216 current_->request.clear(true);
217 current_->response.clear(true);
218 base_.recv(&current_->request);
219 } else {
nissec80e7412017-01-11 05:56:46 -0800220 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221 }
222}
223
224void
225HttpServer::Connection::onHttpClosed(HttpError err) {
henrike@webrtc.org14abcc72014-05-16 16:54:44 +0000226 RTC_UNUSED(err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227 server_->Remove(connection_id_);
228}
229
230///////////////////////////////////////////////////////////////////////////////
231// HttpListenServer
232///////////////////////////////////////////////////////////////////////////////
233
234HttpListenServer::HttpListenServer() {
235 SignalConnectionClosed.connect(this, &HttpListenServer::OnConnectionClosed);
236}
237
238HttpListenServer::~HttpListenServer() {
239}
240
241int HttpListenServer::Listen(const SocketAddress& address) {
242 AsyncSocket* sock =
243 Thread::Current()->socketserver()->CreateAsyncSocket(address.family(),
244 SOCK_STREAM);
245 if (!sock) {
246 return SOCKET_ERROR;
247 }
248 listener_.reset(sock);
249 listener_->SignalReadEvent.connect(this, &HttpListenServer::OnReadEvent);
250 if ((listener_->Bind(address) != SOCKET_ERROR) &&
251 (listener_->Listen(5) != SOCKET_ERROR))
252 return 0;
253 return listener_->GetError();
254}
255
256bool HttpListenServer::GetAddress(SocketAddress* address) const {
257 if (!listener_) {
258 return false;
259 }
260 *address = listener_->GetLocalAddress();
261 return !address->IsNil();
262}
263
264void HttpListenServer::StopListening() {
265 if (listener_) {
266 listener_->Close();
267 }
268}
269
270void HttpListenServer::OnReadEvent(AsyncSocket* socket) {
271 ASSERT(socket == listener_.get());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 AsyncSocket* incoming = listener_->Accept(NULL);
273 if (incoming) {
274 StreamInterface* stream = new SocketStream(incoming);
275 //stream = new LoggingAdapter(stream, LS_VERBOSE, "HttpServer", false);
276 HandleConnection(stream);
277 }
278}
279
280void HttpListenServer::OnConnectionClosed(HttpServer* server,
281 int connection_id,
282 StreamInterface* stream) {
283 Thread::Current()->Dispose(stream);
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
288} // namespace rtc