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