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