blob: 424a61f9f7f5c64bc8c362d606466b8cb7fad4ce [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
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 virtual ~HttpBase();
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 virtual ProcessResult ProcessLeader(const char* line, size_t len,
149 HttpError* error);
150 virtual ProcessResult ProcessHeader(const char* name, size_t nlen,
151 const char* value, size_t vlen,
152 HttpError* error);
153 virtual ProcessResult ProcessHeaderComplete(bool chunked, size_t& data_size,
154 HttpError* error);
155 virtual ProcessResult ProcessData(const char* data, size_t len, size_t& read,
156 HttpError* error);
157 virtual void OnComplete(HttpError err);
158
159private:
160 class DocumentStream;
161 friend class DocumentStream;
162
163 enum { kBufferSize = 32 * 1024 };
164
165 HttpMode mode_;
166 HttpData* data_;
167 IHttpNotify* notify_;
168 StreamInterface* http_stream_;
169 DocumentStream* doc_stream_;
170 char buffer_[kBufferSize];
171 size_t len_;
172
173 bool ignore_data_, chunk_data_;
174 HttpData::const_iterator header_;
175};
176
177///////////////////////////////////////////////////////////////////////////////
178
179} // namespace rtc
180
181#endif // WEBRTC_BASE_HTTPBASE_H__