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.
5 #ifndef NET_HTTP_HTTP_STREAM_PARSER_H_
6 #define NET_HTTP_HTTP_STREAM_PARSER_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/strings/string_piece.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/net_export.h"
19 #include "net/base/upload_progress.h"
20 #include "net/log/net_log.h"
24 class ClientSocketHandle
;
25 class DrainableIOBuffer
;
26 class GrowableIOBuffer
;
27 class HttpChunkedDecoder
;
28 struct HttpRequestInfo
;
29 class HttpRequestHeaders
;
30 class HttpResponseInfo
;
32 class IOBufferWithSize
;
33 class SSLCertRequestInfo
;
35 class UploadDataStream
;
37 class NET_EXPORT_PRIVATE HttpStreamParser
{
39 // Any data in |read_buffer| will be used before reading from the socket
40 // and any data left over after parsing the stream will be put into
41 // |read_buffer|. The left over data will start at offset 0 and the
42 // buffer's offset will be set to the first free byte. |read_buffer| may
43 // have its capacity changed.
44 HttpStreamParser(ClientSocketHandle
* connection
,
45 const HttpRequestInfo
* request
,
46 GrowableIOBuffer
* read_buffer
,
47 const BoundNetLog
& net_log
);
48 virtual ~HttpStreamParser();
50 // These functions implement the interface described in HttpStream with
51 // some additional functionality
52 int SendRequest(const std::string
& request_line
,
53 const HttpRequestHeaders
& headers
,
54 HttpResponseInfo
* response
,
55 const CompletionCallback
& callback
);
57 int ReadResponseHeaders(const CompletionCallback
& callback
);
59 int ReadResponseBody(IOBuffer
* buf
, int buf_len
,
60 const CompletionCallback
& callback
);
62 void Close(bool not_reusable
);
64 // Returns the progress of uploading. When data is chunked, size is set to
65 // zero, but position will not be.
66 UploadProgress
GetUploadProgress() const;
68 bool IsResponseBodyComplete() const;
70 bool CanFindEndOfResponse() const;
72 bool IsMoreDataBuffered() const;
74 bool IsConnectionReused() const;
76 void SetConnectionReused();
78 // Returns true if the underlying connection can be reused.
79 // The connection can be reused if:
80 // * It's still connected.
81 // * The response headers indicate the connection can be kept alive.
82 // * The end of the response can be found.
84 // Note that if response headers have yet to be received, this will return
86 bool CanReuseConnection() const;
88 int64
received_bytes() const { return received_bytes_
; }
90 int64_t sent_bytes() const { return sent_bytes_
; }
92 void GetSSLInfo(SSLInfo
* ssl_info
);
94 void GetSSLCertRequestInfo(SSLCertRequestInfo
* cert_request_info
);
96 // Encodes the given |payload| in the chunked format to |output|.
97 // Returns the number of bytes written to |output|. |output_size| should
98 // be large enough to store the encoded chunk, which is payload.size() +
99 // kChunkHeaderFooterSize. Returns ERR_INVALID_ARGUMENT if |output_size|
100 // is not large enough.
102 // The output will look like: "HEX\r\n[payload]\r\n"
103 // where HEX is a length in hexdecimal (without the "0x" prefix).
104 static int EncodeChunk(const base::StringPiece
& payload
,
108 // Returns true if request headers and body should be merged (i.e. the
109 // sum is small enough and the body is in memory, and not chunked).
110 static bool ShouldMergeRequestHeadersAndBody(
111 const std::string
& request_headers
,
112 const UploadDataStream
* request_body
);
114 // The number of extra bytes required to encode a chunk.
115 static const size_t kChunkHeaderFooterSize
;
118 class SeekableIOBuffer
;
120 // FOO_COMPLETE states implement the second half of potentially asynchronous
121 // operations and don't necessarily mean that FOO is complete.
123 // STATE_NONE indicates that this is waiting on an external call before
127 STATE_SEND_HEADERS_COMPLETE
,
129 STATE_SEND_BODY_COMPLETE
,
130 STATE_SEND_REQUEST_READ_BODY_COMPLETE
,
132 STATE_READ_HEADERS_COMPLETE
,
134 STATE_READ_BODY_COMPLETE
,
138 // The number of bytes by which the header buffer is grown when it reaches
140 static const int kHeaderBufInitialSize
= 4 * 1024; // 4K
142 // |kMaxHeaderBufSize| is the number of bytes that the response headers can
143 // grow to. If the body start is not found within this range of the
144 // response, the transaction will fail with ERR_RESPONSE_HEADERS_TOO_BIG.
145 // Note: |kMaxHeaderBufSize| should be a multiple of |kHeaderBufInitialSize|.
146 static const int kMaxHeaderBufSize
= kHeaderBufInitialSize
* 64; // 256K
148 // The maximum sane buffer size.
149 static const int kMaxBufSize
= 2 * 1024 * 1024; // 2M
152 void OnIOComplete(int result
);
154 // Try to make progress sending/receiving the request/response.
155 int DoLoop(int result
);
157 // The implementations of each state of the state machine.
159 int DoSendHeadersComplete(int result
);
161 int DoSendBodyComplete(int result
);
162 int DoSendRequestReadBodyComplete(int result
);
164 int DoReadHeadersComplete(int result
);
166 int DoReadBodyComplete(int result
);
168 // This handles most of the logic for DoReadHeadersComplete.
169 int HandleReadHeaderResult(int result
);
171 // Examines |read_buf_| to find the start and end of the headers. If they are
172 // found, parse them with DoParseResponseHeaders(). Return the offset for
173 // the end of the headers, or -1 if the complete headers were not found, or
174 // with a net::Error if we encountered an error during parsing.
175 int FindAndParseResponseHeaders();
177 // Parse the headers into response_. Returns OK on success or a net::Error on
179 int ParseResponseHeaders(int end_of_header_offset
);
181 // Examine the parsed headers to try to determine the response body size.
182 void CalculateResponseBodySize();
184 // Uploads statistics about status line compliance with RFC 7230.
185 void ValidateStatusLine(const std::string
& status_line
);
187 // Next state of the request, when the current one completes.
190 // The request to send.
191 const HttpRequestInfo
* request_
;
193 // The request header data. May include a merged request body.
194 scoped_refptr
<DrainableIOBuffer
> request_headers_
;
196 // Size of just the request headers. May be less than the length of
197 // |request_headers_| if the body was merged with the headers.
198 int request_headers_length_
;
200 // Temporary buffer for reading.
201 scoped_refptr
<GrowableIOBuffer
> read_buf_
;
203 // Offset of the first unused byte in |read_buf_|. May be nonzero due to
204 // body data in the same packet as header data but is zero when reading
206 int read_buf_unused_offset_
;
208 // The amount beyond |read_buf_unused_offset_| where the status line starts;
209 // -1 if not found yet.
210 int response_header_start_offset_
;
212 // The amount of received data. If connection is reused then intermediate
213 // value may be bigger than final.
214 int64 received_bytes_
;
216 // The amount of sent data.
219 // The parsed response headers. Owned by the caller of SendRequest. This
220 // cannot be safely accessed after reading the final set of headers, as the
221 // caller of SendRequest may have been destroyed - this happens in the case an
222 // HttpResponseBodyDrainer is used.
223 HttpResponseInfo
* response_
;
225 // Indicates the content length. If this value is less than zero
226 // (and chunked_decoder_ is null), then we must read until the server
227 // closes the connection.
228 int64 response_body_length_
;
230 // Keep track of the number of response body bytes read so far.
231 int64 response_body_read_
;
233 // Helper if the data is chunked.
234 scoped_ptr
<HttpChunkedDecoder
> chunked_decoder_
;
236 // Where the caller wants the body data.
237 scoped_refptr
<IOBuffer
> user_read_buf_
;
238 int user_read_buf_len_
;
240 // The callback to notify a user that their request or response is
241 // complete or there was an error
242 CompletionCallback callback_
;
244 // In the client callback, the client can do anything, including
245 // destroying this class, so any pending callback must be issued
246 // after everything else is done. When it is time to issue the client
247 // callback, move it from |callback_| to |scheduled_callback_|.
248 CompletionCallback scheduled_callback_
;
250 // The underlying socket.
251 ClientSocketHandle
* const connection_
;
253 BoundNetLog net_log_
;
255 // Callback to be used when doing IO.
256 CompletionCallback io_callback_
;
258 // Buffer used to read the request body from UploadDataStream.
259 scoped_refptr
<SeekableIOBuffer
> request_body_read_buf_
;
260 // Buffer used to send the request body. This points the same buffer as
261 // |request_body_read_buf_| unless the data is chunked.
262 scoped_refptr
<SeekableIOBuffer
> request_body_send_buf_
;
263 bool sent_last_chunk_
;
265 // Error received when uploading the body, if any.
268 base::WeakPtrFactory
<HttpStreamParser
> weak_ptr_factory_
;
270 DISALLOW_COPY_AND_ASSIGN(HttpStreamParser
);
275 #endif // NET_HTTP_HTTP_STREAM_PARSER_H_