Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      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.
     15  *
     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.
     26  */
     27 
     28 #ifndef TALK_BASE_HTTPSERVER_H__
     29 #define TALK_BASE_HTTPSERVER_H__
     30 
     31 #include <map>
     32 #include "talk/base/httpbase.h"
     33 
     34 namespace talk_base {
     35 
     36 class AsyncSocket;
     37 class HttpServer;
     38 class SocketAddress;
     39 
     40 //////////////////////////////////////////////////////////////////////
     41 // HttpServer
     42 //////////////////////////////////////////////////////////////////////
     43 
     44 const int HTTP_INVALID_CONNECTION_ID = 0;
     45 
     46 struct HttpServerTransaction : public HttpTransaction {
     47 public:
     48   HttpServerTransaction(int id) : connection_id_(id) { }
     49   int connection_id() const { return connection_id_; }
     50 
     51 private:
     52   int connection_id_;
     53 };
     54 
     55 class HttpServer {
     56 public:
     57   HttpServer();
     58   virtual ~HttpServer();
     59 
     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;
     63 
     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;
     73 
     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);
     81 
     82   // If you want to know when a request completes, listen to this event.
     83   sigslot::signal3<HttpServer*, HttpServerTransaction*, int>
     84     SignalHttpRequestComplete;
     85 
     86   // Stop processing the connection indicated by connection_id.
     87   // Unless force is true, the server will complete sending a response that is
     88   // in progress.
     89   void Close(int connection_id, bool force);
     90   void CloseAll(bool force);
     91 
     92   // After calling CloseAll, this event is signalled to indicate that all
     93   // outstanding connections have closed.
     94   sigslot::signal1<HttpServer*> SignalCloseAllComplete;
     95 
     96 private:
     97   class Connection : private IHttpNotify {
     98   public:
     99     Connection(int connection_id, HttpServer* server);
    100     virtual ~Connection();
    101 
    102     void BeginProcess(StreamInterface* stream);
    103     StreamInterface* EndProcess();
    104 
    105     void Respond(HttpServerTransaction* transaction);
    106     void InitiateClose(bool force);
    107 
    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);
    112 
    113     int connection_id_;
    114     HttpServer* server_;
    115     HttpBase base_;
    116     HttpServerTransaction* current_;
    117     bool signalling_, close_;
    118   };
    119 
    120   Connection* Find(int connection_id);
    121   void Remove(int connection_id);
    122 
    123   friend class Connection;
    124   typedef std::map<int,Connection*> ConnectionMap;
    125 
    126   ConnectionMap connections_;
    127   int next_connection_id_;
    128   bool closing_;
    129 };
    130 
    131 //////////////////////////////////////////////////////////////////////
    132 
    133 class HttpListenServer : public HttpServer, public sigslot::has_slots<> {
    134 public:
    135   HttpListenServer();
    136   virtual ~HttpListenServer();
    137 
    138   int Listen(const SocketAddress& address);
    139   bool GetAddress(SocketAddress* address) const;
    140   void StopListening();
    141 
    142 private:
    143   void OnReadEvent(AsyncSocket* socket);
    144   void OnConnectionClosed(HttpServer* server, int connection_id,
    145                           StreamInterface* stream);
    146 
    147   scoped_ptr<AsyncSocket> listener_;
    148 };
    149 
    150 //////////////////////////////////////////////////////////////////////
    151 
    152 }  // namespace talk_base
    153 
    154 #endif // TALK_BASE_HTTPSERVER_H__
    155