http_stream_parser.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #ifndef NET_HTTP_HTTP_STREAM_PARSER_H_
  5. #define NET_HTTP_HTTP_STREAM_PARSER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/strings/string_piece.h"
  14. #include "base/time/time.h"
  15. #include "crypto/ec_private_key.h"
  16. #include "net/base/completion_once_callback.h"
  17. #include "net/base/completion_repeating_callback.h"
  18. #include "net/base/net_errors.h"
  19. #include "net/base/net_export.h"
  20. #include "net/log/net_log_with_source.h"
  21. #include "net/traffic_annotation/network_traffic_annotation.h"
  22. namespace net {
  23. class DrainableIOBuffer;
  24. class GrowableIOBuffer;
  25. class HttpChunkedDecoder;
  26. struct HttpRequestInfo;
  27. class HttpRequestHeaders;
  28. class HttpResponseInfo;
  29. class IOBuffer;
  30. class SSLCertRequestInfo;
  31. class SSLInfo;
  32. class StreamSocket;
  33. class UploadDataStream;
  34. class NET_EXPORT_PRIVATE HttpStreamParser {
  35. public:
  36. // |connection_is_reused| must be |true| if |stream_socket| has previously
  37. // been used successfully for an HTTP/1.x request.
  38. //
  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. //
  45. // It is not safe to call into the HttpStreamParser after destroying the
  46. // |stream_socket|.
  47. HttpStreamParser(StreamSocket* stream_socket,
  48. bool connection_is_reused,
  49. const HttpRequestInfo* request,
  50. GrowableIOBuffer* read_buffer,
  51. const NetLogWithSource& net_log);
  52. HttpStreamParser(const HttpStreamParser&) = delete;
  53. HttpStreamParser& operator=(const HttpStreamParser&) = delete;
  54. virtual ~HttpStreamParser();
  55. // These functions implement the interface described in HttpStream with
  56. // some additional functionality
  57. int SendRequest(const std::string& request_line,
  58. const HttpRequestHeaders& headers,
  59. const NetworkTrafficAnnotationTag& traffic_annotation,
  60. HttpResponseInfo* response,
  61. CompletionOnceCallback callback);
  62. int ConfirmHandshake(CompletionOnceCallback callback);
  63. int ReadResponseHeaders(CompletionOnceCallback callback);
  64. int ReadResponseBody(IOBuffer* buf,
  65. int buf_len,
  66. CompletionOnceCallback callback);
  67. bool IsResponseBodyComplete() const;
  68. bool CanFindEndOfResponse() const;
  69. bool IsMoreDataBuffered() const;
  70. // Returns true if the underlying connection can be reused.
  71. // The connection can be reused if:
  72. // * It's still connected.
  73. // * The response headers indicate the connection can be kept alive.
  74. // * The end of the response can be found, though it may not have yet been
  75. // received.
  76. //
  77. // Note that if response headers have yet to be received, this will return
  78. // false.
  79. bool CanReuseConnection() const;
  80. // Called when stream is closed.
  81. void OnConnectionClose();
  82. int64_t received_bytes() const { return received_bytes_; }
  83. int64_t sent_bytes() const { return sent_bytes_; }
  84. base::TimeTicks first_response_start_time() const {
  85. return first_response_start_time_;
  86. }
  87. base::TimeTicks non_informational_response_start_time() const {
  88. return non_informational_response_start_time_;
  89. }
  90. base::TimeTicks first_early_hints_time() { return first_early_hints_time_; }
  91. void GetSSLInfo(SSLInfo* ssl_info);
  92. void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
  93. // Encodes the given |payload| in the chunked format to |output|.
  94. // Returns the number of bytes written to |output|. |output_size| should
  95. // be large enough to store the encoded chunk, which is payload.size() +
  96. // kChunkHeaderFooterSize. Returns ERR_INVALID_ARGUMENT if |output_size|
  97. // is not large enough.
  98. //
  99. // The output will look like: "HEX\r\n[payload]\r\n"
  100. // where HEX is a length in hexdecimal (without the "0x" prefix).
  101. static int EncodeChunk(const base::StringPiece& payload,
  102. char* output,
  103. size_t output_size);
  104. // Returns true if request headers and body should be merged (i.e. the
  105. // sum is small enough and the body is in memory, and not chunked).
  106. static bool ShouldMergeRequestHeadersAndBody(
  107. const std::string& request_headers,
  108. const UploadDataStream* request_body);
  109. // The number of extra bytes required to encode a chunk.
  110. static const size_t kChunkHeaderFooterSize;
  111. private:
  112. class SeekableIOBuffer;
  113. // FOO_COMPLETE states implement the second half of potentially asynchronous
  114. // operations and don't necessarily mean that FOO is complete.
  115. enum State {
  116. // STATE_NONE indicates that this is waiting on an external call before
  117. // continuing.
  118. STATE_NONE,
  119. STATE_SEND_HEADERS,
  120. STATE_SEND_HEADERS_COMPLETE,
  121. STATE_SEND_BODY,
  122. STATE_SEND_BODY_COMPLETE,
  123. STATE_SEND_REQUEST_READ_BODY_COMPLETE,
  124. STATE_SEND_REQUEST_COMPLETE,
  125. STATE_READ_HEADERS,
  126. STATE_READ_HEADERS_COMPLETE,
  127. STATE_READ_BODY,
  128. STATE_READ_BODY_COMPLETE,
  129. STATE_DONE
  130. };
  131. // The number of bytes by which the header buffer is grown when it reaches
  132. // capacity.
  133. static const int kHeaderBufInitialSize = 4 * 1024; // 4K
  134. // |kMaxHeaderBufSize| is the number of bytes that the response headers can
  135. // grow to. If the body start is not found within this range of the
  136. // response, the transaction will fail with ERR_RESPONSE_HEADERS_TOO_BIG.
  137. // Note: |kMaxHeaderBufSize| should be a multiple of |kHeaderBufInitialSize|.
  138. static const int kMaxHeaderBufSize = kHeaderBufInitialSize * 64; // 256K
  139. // The maximum sane buffer size.
  140. static const int kMaxBufSize = 2 * 1024 * 1024; // 2M
  141. // Handle callbacks.
  142. void OnIOComplete(int result);
  143. // Try to make progress sending/receiving the request/response.
  144. int DoLoop(int result);
  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 DoSendRequestComplete(int result);
  152. int DoReadHeaders();
  153. int DoReadHeadersComplete(int result);
  154. int DoReadBody();
  155. int DoReadBodyComplete(int result);
  156. // This handles most of the logic for DoReadHeadersComplete.
  157. int HandleReadHeaderResult(int result);
  158. void RunConfirmHandshakeCallback(int rv);
  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. //
  164. // |new_bytes| is the number of new bytes that have been appended to the end
  165. // of |read_buf_| since the last call to this method (which must have returned
  166. // -1).
  167. int FindAndParseResponseHeaders(int new_bytes);
  168. // Parse the headers into response_. Returns OK on success or a net::Error on
  169. // failure.
  170. int ParseResponseHeaders(int end_of_header_offset);
  171. // Examine the parsed headers to try to determine the response body size.
  172. void CalculateResponseBodySize();
  173. // Check if buffers used to send the request are empty.
  174. bool SendRequestBuffersEmpty();
  175. // Next state of the request, when the current one completes.
  176. State io_state_ = STATE_NONE;
  177. // Null when read state machine is invoked.
  178. raw_ptr<const HttpRequestInfo> request_;
  179. // The request header data. May include a merged request body.
  180. scoped_refptr<DrainableIOBuffer> request_headers_;
  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_ = 0;
  184. // Temporary buffer for reading.
  185. scoped_refptr<GrowableIOBuffer> read_buf_;
  186. // Offset of the first unused byte in |read_buf_|. May be nonzero due to
  187. // body data in the same packet as header data but is zero when reading
  188. // headers.
  189. int read_buf_unused_offset_ = 0;
  190. // The amount beyond |read_buf_unused_offset_| where the status line starts;
  191. // std::string::npos if not found yet.
  192. size_t response_header_start_offset_;
  193. // The amount of received data. If connection is reused then intermediate
  194. // value may be bigger than final.
  195. int64_t received_bytes_ = 0;
  196. // The amount of sent data.
  197. int64_t sent_bytes_ = 0;
  198. // The parsed response headers. Owned by the caller of SendRequest. This
  199. // cannot be safely accessed after reading the final set of headers, as the
  200. // caller of SendRequest may have been destroyed - this happens in the case an
  201. // HttpResponseBodyDrainer is used.
  202. raw_ptr<HttpResponseInfo> response_ = nullptr;
  203. // Time at which the first bytes of the first header response including
  204. // informational responses (1xx) are about to be parsed. This corresponds to
  205. // |LoadTimingInfo::receive_headers_start|. See also comments there.
  206. base::TimeTicks first_response_start_time_;
  207. // Time at which the first bytes of the current header response are about to
  208. // be parsed. This is reset every time new response headers including
  209. // non-informational responses (1xx) are parsed.
  210. base::TimeTicks current_response_start_time_;
  211. // Time at which the first byte of the non-informational header response
  212. // (non-1xx) are about to be parsed. This corresponds to
  213. // |LoadTimingInfo::receive_non_informational_headers_start|. See also
  214. // comments there.
  215. base::TimeTicks non_informational_response_start_time_;
  216. // Time at which the first 103 Early Hints response is received. This
  217. // corresponds to |LoadTimingInfo::first_early_hints_time|.
  218. base::TimeTicks first_early_hints_time_;
  219. // Indicates the content length. If this value is less than zero
  220. // (and chunked_decoder_ is null), then we must read until the server
  221. // closes the connection.
  222. int64_t response_body_length_ = -1;
  223. // True if reading a keep-alive response. False if not, or if don't yet know.
  224. bool response_is_keep_alive_ = false;
  225. // True if we've seen a response that has an HTTP status line. This is
  226. // persistent across multiple response parsing. If we see a status line
  227. // for a response, this will remain true forever.
  228. bool has_seen_status_line_ = false;
  229. // Keep track of the number of response body bytes read so far.
  230. int64_t response_body_read_ = 0;
  231. // Helper if the data is chunked.
  232. std::unique_ptr<HttpChunkedDecoder> chunked_decoder_;
  233. // Where the caller wants the body data.
  234. scoped_refptr<IOBuffer> user_read_buf_;
  235. int user_read_buf_len_ = 0;
  236. // The callback to notify a user that the handshake has been confirmed.
  237. CompletionOnceCallback confirm_handshake_callback_;
  238. // The callback to notify a user that their request or response is
  239. // complete or there was an error
  240. CompletionOnceCallback callback_;
  241. // The underlying socket, owned by the caller. The HttpStreamParser must be
  242. // destroyed before the caller destroys the socket, or relinquishes ownership
  243. // of it.
  244. raw_ptr<StreamSocket> stream_socket_;
  245. // Whether the socket has already been used. Only used in HTTP/0.9 detection
  246. // logic.
  247. const bool connection_is_reused_;
  248. NetLogWithSource net_log_;
  249. // Callback to be used when doing IO.
  250. CompletionRepeatingCallback io_callback_;
  251. // Buffer used to read the request body from UploadDataStream.
  252. scoped_refptr<SeekableIOBuffer> request_body_read_buf_;
  253. // Buffer used to send the request body. This points the same buffer as
  254. // |request_body_read_buf_| unless the data is chunked.
  255. scoped_refptr<SeekableIOBuffer> request_body_send_buf_;
  256. bool sent_last_chunk_ = false;
  257. // Error received when uploading the body, if any.
  258. int upload_error_ = OK;
  259. MutableNetworkTrafficAnnotationTag traffic_annotation_;
  260. base::WeakPtrFactory<HttpStreamParser> weak_ptr_factory_{this};
  261. };
  262. } // namespace net
  263. #endif // NET_HTTP_HTTP_STREAM_PARSER_H_