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.

201 lines
7.7 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. // Copyright 2005 Google Inc. All Rights Reserved.
  28. //
  29. #ifndef TALK_BASE_HTTPBASE_H__
  30. #define TALK_BASE_HTTPBASE_H__
  31. #include "talk/base/httpcommon.h"
  32. namespace talk_base {
  33. class StreamInterface;
  34. ///////////////////////////////////////////////////////////////////////////////
  35. // HttpParser - Parses an HTTP stream provided via Process and end_of_input, and
  36. // generates events for:
  37. // Structural Elements: Leader, Headers, Document Data
  38. // Events: End of Headers, End of Document, Errors
  39. ///////////////////////////////////////////////////////////////////////////////
  40. class HttpParser {
  41. public:
  42. enum ProcessResult { PR_CONTINUE, PR_BLOCK, PR_COMPLETE };
  43. HttpParser();
  44. virtual ~HttpParser();
  45. void reset();
  46. ProcessResult Process(const char* buffer, size_t len, size_t* processed,
  47. HttpError* error);
  48. bool is_valid_end_of_input() const;
  49. void complete(HttpError err);
  50. size_t GetDataRemaining() const { return data_size_; }
  51. protected:
  52. ProcessResult ProcessLine(const char* line, size_t len, HttpError* error);
  53. // HttpParser Interface
  54. virtual ProcessResult ProcessLeader(const char* line, size_t len,
  55. HttpError* error) = 0;
  56. virtual ProcessResult ProcessHeader(const char* name, size_t nlen,
  57. const char* value, size_t vlen,
  58. HttpError* error) = 0;
  59. virtual ProcessResult ProcessHeaderComplete(bool chunked, size_t& data_size,
  60. HttpError* error) = 0;
  61. virtual ProcessResult ProcessData(const char* data, size_t len, size_t& read,
  62. HttpError* error) = 0;
  63. virtual void OnComplete(HttpError err) = 0;
  64. private:
  65. enum State {
  66. ST_LEADER, ST_HEADERS,
  67. ST_CHUNKSIZE, ST_CHUNKTERM, ST_TRAILERS,
  68. ST_DATA, ST_COMPLETE
  69. } state_;
  70. bool chunked_;
  71. size_t data_size_;
  72. };
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // IHttpNotify
  75. ///////////////////////////////////////////////////////////////////////////////
  76. enum HttpMode { HM_NONE, HM_CONNECT, HM_RECV, HM_SEND };
  77. class IHttpNotify {
  78. public:
  79. virtual ~IHttpNotify() {}
  80. virtual HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) = 0;
  81. virtual void onHttpComplete(HttpMode mode, HttpError err) = 0;
  82. virtual void onHttpClosed(HttpError err) = 0;
  83. };
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // HttpBase - Provides a state machine for implementing HTTP-based components.
  86. // Attach HttpBase to a StreamInterface which represents a bidirectional HTTP
  87. // stream, and then call send() or recv() to initiate sending or receiving one
  88. // side of an HTTP transaction. By default, HttpBase operates as an I/O pump,
  89. // moving data from the HTTP stream to the HttpData object and vice versa.
  90. // However, it can also operate in stream mode, in which case the user of the
  91. // stream interface drives I/O via calls to Read().
  92. ///////////////////////////////////////////////////////////////////////////////
  93. class HttpBase
  94. : private HttpParser,
  95. public sigslot::has_slots<>
  96. {
  97. public:
  98. HttpBase();
  99. virtual ~HttpBase();
  100. void notify(IHttpNotify* notify) { notify_ = notify; }
  101. bool attach(StreamInterface* stream);
  102. StreamInterface* stream() { return http_stream_; }
  103. StreamInterface* detach();
  104. bool isConnected() const;
  105. void send(HttpData* data);
  106. void recv(HttpData* data);
  107. void abort(HttpError err);
  108. HttpMode mode() const { return mode_; }
  109. void set_ignore_data(bool ignore) { ignore_data_ = ignore; }
  110. bool ignore_data() const { return ignore_data_; }
  111. // Obtaining this stream puts HttpBase into stream mode until the stream
  112. // is closed. HttpBase can only expose one open stream interface at a time.
  113. // Further calls will return NULL.
  114. StreamInterface* GetDocumentStream();
  115. protected:
  116. // Do cleanup when the http stream closes (error may be 0 for a clean
  117. // shutdown), and return the error code to signal.
  118. HttpError HandleStreamClose(int error);
  119. // DoReceiveLoop acts as a data pump, pulling data from the http stream,
  120. // pushing it through the HttpParser, and then populating the HttpData object
  121. // based on the callbacks from the parser. One of the most interesting
  122. // callbacks is ProcessData, which provides the actual http document body.
  123. // This data is then written to the HttpData::document. As a result, data
  124. // flows from the network to the document, with some incidental protocol
  125. // parsing in between.
  126. // Ideally, we would pass in the document* to DoReceiveLoop, to more easily
  127. // support GetDocumentStream(). However, since the HttpParser is callback
  128. // driven, we are forced to store the pointer somewhere until the callback
  129. // is triggered.
  130. // Returns true if the received document has finished, and
  131. // HttpParser::complete should be called.
  132. bool DoReceiveLoop(HttpError* err);
  133. void read_and_process_data();
  134. void flush_data();
  135. bool queue_headers();
  136. void do_complete(HttpError err = HE_NONE);
  137. void OnHttpStreamEvent(StreamInterface* stream, int events, int error);
  138. void OnDocumentEvent(StreamInterface* stream, int events, int error);
  139. // HttpParser Interface
  140. virtual ProcessResult ProcessLeader(const char* line, size_t len,
  141. HttpError* error);
  142. virtual ProcessResult ProcessHeader(const char* name, size_t nlen,
  143. const char* value, size_t vlen,
  144. HttpError* error);
  145. virtual ProcessResult ProcessHeaderComplete(bool chunked, size_t& data_size,
  146. HttpError* error);
  147. virtual ProcessResult ProcessData(const char* data, size_t len, size_t& read,
  148. HttpError* error);
  149. virtual void OnComplete(HttpError err);
  150. private:
  151. class DocumentStream;
  152. friend class DocumentStream;
  153. enum { kBufferSize = 32 * 1024 };
  154. HttpMode mode_;
  155. HttpData* data_;
  156. IHttpNotify* notify_;
  157. StreamInterface* http_stream_;
  158. DocumentStream* doc_stream_;
  159. char buffer_[kBufferSize];
  160. size_t len_;
  161. bool ignore_data_, chunk_data_;
  162. HttpData::const_iterator header_;
  163. };
  164. ///////////////////////////////////////////////////////////////////////////////
  165. } // namespace talk_base
  166. #endif // TALK_BASE_HTTPBASE_H__