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