OLD | NEW |
(Empty) | |
| 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 <memory> |
| 16 |
| 17 #include "webrtc/base/httpbase.h" |
| 18 |
| 19 namespace rtc { |
| 20 |
| 21 class AsyncSocket; |
| 22 class HttpServer; |
| 23 class SocketAddress; |
| 24 |
| 25 ////////////////////////////////////////////////////////////////////// |
| 26 // HttpServer |
| 27 ////////////////////////////////////////////////////////////////////// |
| 28 |
| 29 const int HTTP_INVALID_CONNECTION_ID = 0; |
| 30 |
| 31 struct HttpServerTransaction : public HttpTransaction { |
| 32 public: |
| 33 HttpServerTransaction(int id) : connection_id_(id) { } |
| 34 int connection_id() const { return connection_id_; } |
| 35 |
| 36 private: |
| 37 int connection_id_; |
| 38 }; |
| 39 |
| 40 class HttpServer { |
| 41 public: |
| 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 |
| 81 private: |
| 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(); |
| 89 |
| 90 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 |
| 118 class HttpListenServer : public HttpServer, public sigslot::has_slots<> { |
| 119 public: |
| 120 HttpListenServer(); |
| 121 ~HttpListenServer() override; |
| 122 |
| 123 int Listen(const SocketAddress& address); |
| 124 bool GetAddress(SocketAddress* address) const; |
| 125 void StopListening(); |
| 126 |
| 127 private: |
| 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 |
| 139 #endif // WEBRTC_BASE_HTTPSERVER_H__ |
OLD | NEW |