blob: 61442a93f349366409d02f139ca1b6fea8a0d591 [file] [log] [blame]
deadbeeff137e972017-03-23 15:45:49 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_HTTPSERVER_H_
12#define RTC_BASE_HTTPSERVER_H_
deadbeeff137e972017-03-23 15:45:49 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <map>
15#include <memory>
deadbeeff137e972017-03-23 15:45:49 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/httpbase.h"
deadbeeff137e972017-03-23 15:45:49 -070018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019namespace rtc {
20
21class AsyncSocket;
22class HttpServer;
23class SocketAddress;
24
25//////////////////////////////////////////////////////////////////////
26// HttpServer
27//////////////////////////////////////////////////////////////////////
28
29const int HTTP_INVALID_CONNECTION_ID = 0;
30
31struct HttpServerTransaction : public HttpTransaction {
32public:
33 HttpServerTransaction(int id) : connection_id_(id) { }
34 int connection_id() const { return connection_id_; }
35
36private:
37 int connection_id_;
38};
39
40class HttpServer {
41public:
42 HttpServer();
43 virtual ~HttpServer();
44
45 int HandleConnection(StreamInterface* stream);
46 // Due to sigslot issues, we can't destroy some streams at an arbitrary time.
47 sigslot::signal3<HttpServer*, int, StreamInterface*> SignalConnectionClosed;
48
49 // This signal occurs when the HTTP request headers have been received, but
50 // before the request body is written to the request document. By default,
51 // the request document is a MemoryStream. By handling this signal, the
52 // document can be overridden, in which case the third signal argument should
53 // be set to true. In the case where the request body should be ignored,
54 // the document can be set to null. Note that the transaction object is still
55 // owened by the HttpServer at this point.
56 sigslot::signal3<HttpServer*, HttpServerTransaction*, bool*>
57 SignalHttpRequestHeader;
58
59 // An HTTP request has been made, and is available in the transaction object.
60 // Populate the transaction's response, and then return the object via the
61 // Respond method. Note that during this time, ownership of the transaction
62 // object is transferred, so it may be passed between threads, although
63 // respond must be called on the server's active thread.
64 sigslot::signal2<HttpServer*, HttpServerTransaction*> SignalHttpRequest;
65 void Respond(HttpServerTransaction* transaction);
66
67 // If you want to know when a request completes, listen to this event.
68 sigslot::signal3<HttpServer*, HttpServerTransaction*, int>
69 SignalHttpRequestComplete;
70
71 // Stop processing the connection indicated by connection_id.
72 // Unless force is true, the server will complete sending a response that is
73 // in progress.
74 void Close(int connection_id, bool force);
75 void CloseAll(bool force);
76
77 // After calling CloseAll, this event is signalled to indicate that all
78 // outstanding connections have closed.
79 sigslot::signal1<HttpServer*> SignalCloseAllComplete;
80
81private:
82 class Connection : private IHttpNotify {
83 public:
84 Connection(int connection_id, HttpServer* server);
85 ~Connection() override;
86
87 void BeginProcess(StreamInterface* stream);
88 StreamInterface* EndProcess();
Henrik Kjellanderc0362762017-06-29 08:03:04 +020089
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090 void Respond(HttpServerTransaction* transaction);
91 void InitiateClose(bool force);
92
93 // IHttpNotify Interface
94 HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) override;
95 void onHttpComplete(HttpMode mode, HttpError err) override;
96 void onHttpClosed(HttpError err) override;
97
98 int connection_id_;
99 HttpServer* server_;
100 HttpBase base_;
101 HttpServerTransaction* current_;
102 bool signalling_, close_;
103 };
104
105 Connection* Find(int connection_id);
106 void Remove(int connection_id);
107
108 friend class Connection;
109 typedef std::map<int,Connection*> ConnectionMap;
110
111 ConnectionMap connections_;
112 int next_connection_id_;
113 bool closing_;
114};
115
116//////////////////////////////////////////////////////////////////////
117
118class HttpListenServer : public HttpServer, public sigslot::has_slots<> {
119public:
120 HttpListenServer();
121 ~HttpListenServer() override;
122
123 int Listen(const SocketAddress& address);
124 bool GetAddress(SocketAddress* address) const;
125 void StopListening();
126
127private:
128 void OnReadEvent(AsyncSocket* socket);
129 void OnConnectionClosed(HttpServer* server, int connection_id,
130 StreamInterface* stream);
131
132 std::unique_ptr<AsyncSocket> listener_;
133};
134
135//////////////////////////////////////////////////////////////////////
136
137} // namespace rtc
138
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200139#endif // RTC_BASE_HTTPSERVER_H_