1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_HTTP_HTTP_STREAM_PARSER_H_
6#define NET_HTTP_HTTP_STREAM_PARSER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/strings/string_piece.h"
15#include "net/base/completion_callback.h"
16#include "net/base/net_export.h"
17#include "net/base/net_log.h"
18#include "net/base/upload_progress.h"
19
20namespace net {
21
22class ClientSocketHandle;
23class DrainableIOBuffer;
24class GrowableIOBuffer;
25class HttpChunkedDecoder;
26struct HttpRequestInfo;
27class HttpRequestHeaders;
28class HttpResponseInfo;
29class IOBuffer;
30class IOBufferWithSize;
31class SSLCertRequestInfo;
32class SSLInfo;
33class UploadDataStream;
34
35class NET_EXPORT_PRIVATE HttpStreamParser {
36 public:
37  // Any data in |read_buffer| will be used before reading from the socket
38  // and any data left over after parsing the stream will be put into
39  // |read_buffer|.  The left over data will start at offset 0 and the
40  // buffer's offset will be set to the first free byte. |read_buffer| may
41  // have its capacity changed.
42  HttpStreamParser(ClientSocketHandle* connection,
43                   const HttpRequestInfo* request,
44                   GrowableIOBuffer* read_buffer,
45                   const BoundNetLog& net_log);
46  virtual ~HttpStreamParser();
47
48  // These functions implement the interface described in HttpStream with
49  // some additional functionality
50  int SendRequest(const std::string& request_line,
51                  const HttpRequestHeaders& headers,
52                  HttpResponseInfo* response,
53                  const CompletionCallback& callback);
54
55  int ReadResponseHeaders(const CompletionCallback& callback);
56
57  int ReadResponseBody(IOBuffer* buf, int buf_len,
58                       const CompletionCallback& callback);
59
60  void Close(bool not_reusable);
61
62  // Returns the progress of uploading. When data is chunked, size is set to
63  // zero, but position will not be.
64  UploadProgress GetUploadProgress() const;
65
66  bool IsResponseBodyComplete() const;
67
68  bool CanFindEndOfResponse() const;
69
70  bool IsMoreDataBuffered() const;
71
72  bool IsConnectionReused() const;
73
74  void SetConnectionReused();
75
76  bool IsConnectionReusable() const;
77
78  int64 received_bytes() const { return received_bytes_; }
79
80  void GetSSLInfo(SSLInfo* ssl_info);
81
82  void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
83
84  // Encodes the given |payload| in the chunked format to |output|.
85  // Returns the number of bytes written to |output|. |output_size| should
86  // be large enough to store the encoded chunk, which is payload.size() +
87  // kChunkHeaderFooterSize. Returns ERR_INVALID_ARGUMENT if |output_size|
88  // is not large enough.
89  //
90  // The output will look like: "HEX\r\n[payload]\r\n"
91  // where HEX is a length in hexdecimal (without the "0x" prefix).
92  static int EncodeChunk(const base::StringPiece& payload,
93                         char* output,
94                         size_t output_size);
95
96  // Returns true if request headers and body should be merged (i.e. the
97  // sum is small enough and the body is in memory, and not chunked).
98  static bool ShouldMergeRequestHeadersAndBody(
99      const std::string& request_headers,
100      const UploadDataStream* request_body);
101
102  // The number of extra bytes required to encode a chunk.
103  static const size_t kChunkHeaderFooterSize;
104
105 private:
106  class SeekableIOBuffer;
107
108  // FOO_COMPLETE states implement the second half of potentially asynchronous
109  // operations and don't necessarily mean that FOO is complete.
110  enum State {
111    // STATE_NONE indicates that this is waiting on an external call before
112    // continuing.
113    STATE_NONE,
114    STATE_SEND_HEADERS,
115    STATE_SEND_HEADERS_COMPLETE,
116    STATE_SEND_BODY,
117    STATE_SEND_BODY_COMPLETE,
118    STATE_SEND_REQUEST_READ_BODY_COMPLETE,
119    STATE_READ_HEADERS,
120    STATE_READ_HEADERS_COMPLETE,
121    STATE_READ_BODY,
122    STATE_READ_BODY_COMPLETE,
123    STATE_DONE
124  };
125
126  // The number of bytes by which the header buffer is grown when it reaches
127  // capacity.
128  static const int kHeaderBufInitialSize = 4 * 1024;  // 4K
129
130  // |kMaxHeaderBufSize| is the number of bytes that the response headers can
131  // grow to. If the body start is not found within this range of the
132  // response, the transaction will fail with ERR_RESPONSE_HEADERS_TOO_BIG.
133  // Note: |kMaxHeaderBufSize| should be a multiple of |kHeaderBufInitialSize|.
134  static const int kMaxHeaderBufSize = kHeaderBufInitialSize * 64;  // 256K
135
136  // The maximum sane buffer size.
137  static const int kMaxBufSize = 2 * 1024 * 1024;  // 2M
138
139  // Handle callbacks.
140  void OnIOComplete(int result);
141
142  // Try to make progress sending/receiving the request/response.
143  int DoLoop(int result);
144
145  // The implementations of each state of the state machine.
146  int DoSendHeaders();
147  int DoSendHeadersComplete(int result);
148  int DoSendBody();
149  int DoSendBodyComplete(int result);
150  int DoSendRequestReadBodyComplete(int result);
151  int DoReadHeaders();
152  int DoReadHeadersComplete(int result);
153  int DoReadBody();
154  int DoReadBodyComplete(int result);
155
156  // This handles most of the logic for DoReadHeadersComplete.
157  int HandleReadHeaderResult(int result);
158
159  // Examines |read_buf_| to find the start and end of the headers. If they are
160  // found, parse them with DoParseResponseHeaders().  Return the offset for
161  // the end of the headers, or -1 if the complete headers were not found, or
162  // with a net::Error if we encountered an error during parsing.
163  int ParseResponseHeaders();
164
165  // Parse the headers into response_.  Returns OK on success or a net::Error on
166  // failure.
167  int DoParseResponseHeaders(int end_of_header_offset);
168
169  // Examine the parsed headers to try to determine the response body size.
170  void CalculateResponseBodySize();
171
172  // Next state of the request, when the current one completes.
173  State io_state_;
174
175  // The request to send.
176  const HttpRequestInfo* request_;
177
178  // The request header data.  May include a merged request body.
179  scoped_refptr<DrainableIOBuffer> request_headers_;
180
181  // Size of just the request headers.  May be less than the length of
182  // |request_headers_| if the body was merged with the headers.
183  int request_headers_length_;
184
185  // Temporary buffer for reading.
186  scoped_refptr<GrowableIOBuffer> read_buf_;
187
188  // Offset of the first unused byte in |read_buf_|.  May be nonzero due to
189  // body data in the same packet as header data but is zero when reading
190  // headers.
191  int read_buf_unused_offset_;
192
193  // The amount beyond |read_buf_unused_offset_| where the status line starts;
194  // -1 if not found yet.
195  int response_header_start_offset_;
196
197  // The amount of received data.  If connection is reused then intermediate
198  // value may be bigger than final.
199  int64 received_bytes_;
200
201  // The parsed response headers.  Owned by the caller of SendRequest.  This
202  // cannot be safely accessed after reading the final set of headers, as the
203  // caller of SendRequest may have been destroyed - this happens in the case an
204  // HttpResponseBodyDrainer is used.
205  HttpResponseInfo* response_;
206
207  // Indicates the content length.  If this value is less than zero
208  // (and chunked_decoder_ is null), then we must read until the server
209  // closes the connection.
210  int64 response_body_length_;
211
212  // Keep track of the number of response body bytes read so far.
213  int64 response_body_read_;
214
215  // Helper if the data is chunked.
216  scoped_ptr<HttpChunkedDecoder> chunked_decoder_;
217
218  // Where the caller wants the body data.
219  scoped_refptr<IOBuffer> user_read_buf_;
220  int user_read_buf_len_;
221
222  // The callback to notify a user that their request or response is
223  // complete or there was an error
224  CompletionCallback callback_;
225
226  // In the client callback, the client can do anything, including
227  // destroying this class, so any pending callback must be issued
228  // after everything else is done.  When it is time to issue the client
229  // callback, move it from |callback_| to |scheduled_callback_|.
230  CompletionCallback scheduled_callback_;
231
232  // The underlying socket.
233  ClientSocketHandle* const connection_;
234
235  BoundNetLog net_log_;
236
237  // Callback to be used when doing IO.
238  CompletionCallback io_callback_;
239
240  // Buffer used to read the request body from UploadDataStream.
241  scoped_refptr<SeekableIOBuffer> request_body_read_buf_;
242  // Buffer used to send the request body. This points the same buffer as
243  // |request_body_read_buf_| unless the data is chunked.
244  scoped_refptr<SeekableIOBuffer> request_body_send_buf_;
245  bool sent_last_chunk_;
246
247  // Error received when uploading the body, if any.
248  int upload_error_;
249
250  base::WeakPtrFactory<HttpStreamParser> weak_ptr_factory_;
251
252  DISALLOW_COPY_AND_ASSIGN(HttpStreamParser);
253};
254
255}  // namespace net
256
257#endif  // NET_HTTP_HTTP_STREAM_PARSER_H_
258