You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.5 KiB

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