3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_HTTPSERVER_H__
29 #define TALK_BASE_HTTPSERVER_H__
32 #include "talk/base/httpbase.h"
40 //////////////////////////////////////////////////////////////////////
42 //////////////////////////////////////////////////////////////////////
44 const int HTTP_INVALID_CONNECTION_ID = 0;
46 struct HttpServerTransaction : public HttpTransaction {
48 HttpServerTransaction(int id) : connection_id_(id) { }
49 int connection_id() const { return connection_id_; }
58 virtual ~HttpServer();
60 int HandleConnection(StreamInterface* stream);
61 // Due to sigslot issues, we can't destroy some streams at an arbitrary time.
62 sigslot::signal3<HttpServer*, int, StreamInterface*> SignalConnectionClosed;
64 // This signal occurs when the HTTP request headers have been received, but
65 // before the request body is written to the request document. By default,
66 // the request document is a MemoryStream. By handling this signal, the
67 // document can be overridden, in which case the third signal argument should
68 // be set to true. In the case where the request body should be ignored,
69 // the document can be set to NULL. Note that the transaction object is still
70 // owened by the HttpServer at this point.
71 sigslot::signal3<HttpServer*, HttpServerTransaction*, bool*>
72 SignalHttpRequestHeader;
74 // An HTTP request has been made, and is available in the transaction object.
75 // Populate the transaction's response, and then return the object via the
76 // Respond method. Note that during this time, ownership of the transaction
77 // object is transferred, so it may be passed between threads, although
78 // respond must be called on the server's active thread.
79 sigslot::signal2<HttpServer*, HttpServerTransaction*> SignalHttpRequest;
80 void Respond(HttpServerTransaction* transaction);
82 // If you want to know when a request completes, listen to this event.
83 sigslot::signal3<HttpServer*, HttpServerTransaction*, int>
84 SignalHttpRequestComplete;
86 // Stop processing the connection indicated by connection_id.
87 // Unless force is true, the server will complete sending a response that is
89 void Close(int connection_id, bool force);
90 void CloseAll(bool force);
92 // After calling CloseAll, this event is signalled to indicate that all
93 // outstanding connections have closed.
94 sigslot::signal1<HttpServer*> SignalCloseAllComplete;
97 class Connection : private IHttpNotify {
99 Connection(int connection_id, HttpServer* server);
100 virtual ~Connection();
102 void BeginProcess(StreamInterface* stream);
103 StreamInterface* EndProcess();
105 void Respond(HttpServerTransaction* transaction);
106 void InitiateClose(bool force);
108 // IHttpNotify Interface
109 virtual HttpError onHttpHeaderComplete(bool chunked, size_t& data_size);
110 virtual void onHttpComplete(HttpMode mode, HttpError err);
111 virtual void onHttpClosed(HttpError err);
116 HttpServerTransaction* current_;
117 bool signalling_, close_;
120 Connection* Find(int connection_id);
121 void Remove(int connection_id);
123 friend class Connection;
124 typedef std::map<int,Connection*> ConnectionMap;
126 ConnectionMap connections_;
127 int next_connection_id_;
131 //////////////////////////////////////////////////////////////////////
133 class HttpListenServer : public HttpServer, public sigslot::has_slots<> {
136 virtual ~HttpListenServer();
138 int Listen(const SocketAddress& address);
139 bool GetAddress(SocketAddress* address) const;
140 void StopListening();
143 void OnReadEvent(AsyncSocket* socket);
144 void OnConnectionClosed(HttpServer* server, int connection_id,
145 StreamInterface* stream);
147 scoped_ptr<AsyncSocket> listener_;
150 //////////////////////////////////////////////////////////////////////
152 } // namespace talk_base
154 #endif // TALK_BASE_HTTPSERVER_H__