quic_http_stream.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_QUIC_QUIC_HTTP_STREAM_H_
  5. #define NET_QUIC_QUIC_HTTP_STREAM_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <set>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/time/time.h"
  14. #include "net/base/completion_once_callback.h"
  15. #include "net/base/idempotency.h"
  16. #include "net/base/io_buffer.h"
  17. #include "net/base/load_timing_info.h"
  18. #include "net/base/net_export.h"
  19. #include "net/http/http_response_info.h"
  20. #include "net/http/http_server_properties.h"
  21. #include "net/log/net_log_with_source.h"
  22. #include "net/quic/quic_chromium_client_session.h"
  23. #include "net/quic/quic_chromium_client_stream.h"
  24. #include "net/spdy/multiplexed_http_stream.h"
  25. #include "net/third_party/quiche/src/quiche/quic/core/http/quic_client_push_promise_index.h"
  26. #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
  27. namespace net {
  28. namespace test {
  29. class QuicHttpStreamPeer;
  30. } // namespace test
  31. // The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a
  32. // handle of QuicChromiumClientStream which it uses to send and receive data.
  33. // The handle hides the details of the underlying stream's lifetime and can be
  34. // used even after the underlying stream is destroyed.
  35. class NET_EXPORT_PRIVATE QuicHttpStream : public MultiplexedHttpStream {
  36. public:
  37. explicit QuicHttpStream(
  38. std::unique_ptr<QuicChromiumClientSession::Handle> session,
  39. std::set<std::string> dns_aliases);
  40. QuicHttpStream(const QuicHttpStream&) = delete;
  41. QuicHttpStream& operator=(const QuicHttpStream&) = delete;
  42. ~QuicHttpStream() override;
  43. // HttpStream implementation.
  44. void RegisterRequest(const HttpRequestInfo* request_info) override;
  45. int InitializeStream(bool can_send_early,
  46. RequestPriority priority,
  47. const NetLogWithSource& net_log,
  48. CompletionOnceCallback callback) override;
  49. int SendRequest(const HttpRequestHeaders& request_headers,
  50. HttpResponseInfo* response,
  51. CompletionOnceCallback callback) override;
  52. int ReadResponseHeaders(CompletionOnceCallback callback) override;
  53. int ReadResponseBody(IOBuffer* buf,
  54. int buf_len,
  55. CompletionOnceCallback callback) override;
  56. void Close(bool not_reusable) override;
  57. bool IsResponseBodyComplete() const override;
  58. bool IsConnectionReused() const override;
  59. int64_t GetTotalReceivedBytes() const override;
  60. int64_t GetTotalSentBytes() const override;
  61. bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
  62. bool GetAlternativeService(
  63. AlternativeService* alternative_service) const override;
  64. void PopulateNetErrorDetails(NetErrorDetails* details) override;
  65. void SetPriority(RequestPriority priority) override;
  66. void SetRequestIdempotency(Idempotency idempotency) override;
  67. const std::set<std::string>& GetDnsAliases() const override;
  68. base::StringPiece GetAcceptChViaAlps() const override;
  69. static HttpResponseInfo::ConnectionInfo ConnectionInfoFromQuicVersion(
  70. quic::ParsedQuicVersion quic_version);
  71. private:
  72. friend class test::QuicHttpStreamPeer;
  73. enum State {
  74. STATE_NONE,
  75. STATE_HANDLE_PROMISE,
  76. STATE_HANDLE_PROMISE_COMPLETE,
  77. STATE_REQUEST_STREAM,
  78. STATE_REQUEST_STREAM_COMPLETE,
  79. STATE_SET_REQUEST_PRIORITY,
  80. STATE_SEND_HEADERS,
  81. STATE_SEND_HEADERS_COMPLETE,
  82. STATE_READ_REQUEST_BODY,
  83. STATE_READ_REQUEST_BODY_COMPLETE,
  84. STATE_SEND_BODY,
  85. STATE_SEND_BODY_COMPLETE,
  86. STATE_OPEN,
  87. };
  88. void OnIOComplete(int rv);
  89. void DoCallback(int rv);
  90. int DoLoop(int rv);
  91. int DoHandlePromise();
  92. int DoHandlePromiseComplete(int rv);
  93. int DoRequestStream();
  94. int DoRequestStreamComplete(int rv);
  95. int DoSetRequestPriority();
  96. int DoSendHeaders();
  97. int DoSendHeadersComplete(int rv);
  98. int DoReadRequestBody();
  99. int DoReadRequestBodyComplete(int rv);
  100. int DoSendBody();
  101. int DoSendBodyComplete(int rv);
  102. void OnReadResponseHeadersComplete(int rv);
  103. int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers);
  104. void ReadTrailingHeaders();
  105. void OnReadTrailingHeadersComplete(int rv);
  106. void OnReadBodyComplete(int rv);
  107. int HandleReadComplete(int rv);
  108. void EnterStateSendHeaders();
  109. void ResetStream();
  110. // Returns ERR_QUIC_HANDSHAKE_FAILED, if |rv| is ERR_QUIC_PROTOCOL_ERROR and
  111. // the handshake was never confirmed. Otherwise, returns |rv|.
  112. int MapStreamError(int rv);
  113. // If |has_response_status_| is false, sets |response_status| to the result
  114. // of ComputeResponseStatus(). Returns |response_status_|.
  115. int GetResponseStatus();
  116. // Sets the result of |ComputeResponseStatus()| as the |response_status_|.
  117. void SaveResponseStatus();
  118. // Sets |response_status_| to |response_status| and sets
  119. // |has_response_status_| to true.
  120. void SetResponseStatus(int response_status);
  121. // Computes the correct response status based on the status of the handshake,
  122. // |session_error|, |connection_error| and |stream_error|.
  123. int ComputeResponseStatus() const;
  124. QuicChromiumClientSession::Handle* quic_session() {
  125. return static_cast<QuicChromiumClientSession::Handle*>(session());
  126. }
  127. const QuicChromiumClientSession::Handle* quic_session() const {
  128. return static_cast<const QuicChromiumClientSession::Handle*>(session());
  129. }
  130. State next_state_ = STATE_NONE;
  131. std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
  132. // The following three fields are all owned by the caller and must
  133. // outlive this object, according to the HttpStream contract.
  134. // The request to send.
  135. // Only valid before the response body is read.
  136. raw_ptr<const HttpRequestInfo> request_info_ = nullptr;
  137. // Whether this request can be sent without confirmation.
  138. bool can_send_early_ = false;
  139. // The request body to send, if any, owned by the caller.
  140. raw_ptr<UploadDataStream> request_body_stream_ = nullptr;
  141. // Time the request was issued.
  142. base::Time request_time_;
  143. // The priority of the request.
  144. RequestPriority priority_ = MINIMUM_PRIORITY;
  145. // |response_info_| is the HTTP response data object which is filled in
  146. // when a the response headers are read. It is not owned by this stream.
  147. raw_ptr<HttpResponseInfo> response_info_ = nullptr;
  148. bool has_response_status_ = false; // true if response_status_ as been set.
  149. // Because response data is buffered, also buffer the response status if the
  150. // stream is explicitly closed via OnError or OnClose with an error.
  151. // Once all buffered data has been returned, this will be used as the final
  152. // response.
  153. int response_status_ = ERR_UNEXPECTED;
  154. // Serialized request headers.
  155. spdy::Http2HeaderBlock request_headers_;
  156. spdy::Http2HeaderBlock response_header_block_;
  157. bool response_headers_received_ = false;
  158. spdy::Http2HeaderBlock trailing_header_block_;
  159. bool trailing_headers_received_ = false;
  160. // Number of bytes received by the headers stream on behalf of this stream.
  161. int64_t headers_bytes_received_ = 0;
  162. // Number of bytes sent by the headers stream on behalf of this stream.
  163. int64_t headers_bytes_sent_ = 0;
  164. // Number of bytes received when the stream was closed.
  165. int64_t closed_stream_received_bytes_ = 0;
  166. // Number of bytes sent when the stream was closed.
  167. int64_t closed_stream_sent_bytes_ = 0;
  168. // True if the stream is the first stream negotiated on the session. Set when
  169. // the stream was closed. If |stream_| is failed to be created, this takes on
  170. // the default value of false.
  171. bool closed_is_first_stream_ = false;
  172. // The caller's callback to be used for asynchronous operations.
  173. CompletionOnceCallback callback_;
  174. // Caller provided buffer for the ReadResponseBody() response.
  175. scoped_refptr<IOBuffer> user_buffer_;
  176. int user_buffer_len_ = 0;
  177. // Temporary buffer used to read the request body from UploadDataStream.
  178. scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
  179. // Wraps raw_request_body_buf_ to read the remaining data progressively.
  180. scoped_refptr<DrainableIOBuffer> request_body_buf_;
  181. NetLogWithSource stream_net_log_;
  182. int session_error_ =
  183. ERR_UNEXPECTED; // Error code from the connection shutdown.
  184. bool found_promise_ = false;
  185. // Set to true when DoLoop() is being executed, false otherwise.
  186. bool in_loop_ = false;
  187. // Session connect timing info.
  188. LoadTimingInfo::ConnectTiming connect_timing_;
  189. // Stores any DNS aliases for the remote endpoint. Includes all known
  190. // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for
  191. // the connection, in no particular order. These are stored in the stream
  192. // instead of the session due to complications related to IP-pooling.
  193. std::set<std::string> dns_aliases_;
  194. base::WeakPtrFactory<QuicHttpStream> weak_factory_{this};
  195. };
  196. } // namespace net
  197. #endif // NET_QUIC_QUIC_HTTP_STREAM_H_