blob: e4dfa0d00d9a7433c7fac3d994237b3e4f2e6562 [file] [log] [blame]
[email protected]f702d572012-12-04 15:56:201// 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
Ryan Hamiltona3ee93a72018-08-01 22:03:085#ifndef NET_QUIC_QUIC_HTTP_STREAM_H_
6#define NET_QUIC_QUIC_HTTP_STREAM_H_
[email protected]f702d572012-12-04 15:56:207
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
sclittlebe1ccf62015-09-02 19:40:369#include <stdint.h>
10
Eric Orthac661912022-01-10 21:44:1711#include <set>
bnc614a92d32016-04-04 13:56:0712#include <string>
[email protected]f702d572012-12-04 15:56:2013
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
[email protected]f702d572012-12-04 15:56:2015#include "base/memory/weak_ptr.h"
Bence Béky334ddfc22021-03-12 15:18:1316#include "base/strings/string_piece.h"
Gabriel Charetted87f10f2022-03-31 00:44:2217#include "base/time/time.h"
Bence Békya25e3f72018-02-13 21:13:3918#include "net/base/completion_once_callback.h"
Yu Suba2a1d72020-12-04 02:14:4119#include "net/base/idempotency.h"
[email protected]f702d572012-12-04 15:56:2020#include "net/base/io_buffer.h"
xunjieli100937eb52016-09-15 20:09:3721#include "net/base/load_timing_info.h"
bnc81c46c1f2016-10-04 16:25:5922#include "net/base/net_export.h"
bnc90be5dd782016-11-09 16:28:4423#include "net/http/http_response_info.h"
rch1d3b31a2017-03-29 20:34:1724#include "net/http/http_server_properties.h"
mikecironef22f9812016-10-04 03:40:1925#include "net/log/net_log_with_source.h"
Ryan Hamiltona3ee93a72018-08-01 22:03:0826#include "net/quic/quic_chromium_client_session.h"
27#include "net/quic/quic_chromium_client_stream.h"
Bence Béky94658bf2018-05-11 19:22:5828#include "net/spdy/multiplexed_http_stream.h"
Ryan Hamiltonea4fa192022-04-12 18:30:4929#include "net/third_party/quiche/src/quiche/quic/core/http/quic_client_push_promise_index.h"
30#include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
[email protected]f702d572012-12-04 15:56:2031
32namespace net {
33
[email protected]24e5bc52013-09-18 15:36:5834namespace test {
35class QuicHttpStreamPeer;
36} // namespace test
37
[email protected]f702d572012-12-04 15:56:2038// The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a
Kenichi Ishibashi5153f0632021-03-11 14:10:1139// handle of QuicChromiumClientStream which it uses to send and receive data.
40// The handle hides the details of the underlying stream's lifetime and can be
41// used even after the underlying stream is destroyed.
rch56ec40a2017-06-23 14:48:4442class NET_EXPORT_PRIVATE QuicHttpStream : public MultiplexedHttpStream {
[email protected]f702d572012-12-04 15:56:2043 public:
zhongyi98d6a9262017-05-19 02:47:4544 explicit QuicHttpStream(
Cammie Smith Barnes0f38aff2021-03-02 23:14:3545 std::unique_ptr<QuicChromiumClientSession::Handle> session,
Eric Orthac661912022-01-10 21:44:1746 std::set<std::string> dns_aliases);
[email protected]f702d572012-12-04 15:56:2047
Peter Boström293b1342021-09-22 17:31:4348 QuicHttpStream(const QuicHttpStream&) = delete;
49 QuicHttpStream& operator=(const QuicHttpStream&) = delete;
50
dchengb03027d2014-10-21 12:00:2051 ~QuicHttpStream() override;
[email protected]f702d572012-12-04 15:56:2052
53 // HttpStream implementation.
Ali Beyada0b1a1c2022-04-08 20:08:1454 void RegisterRequest(const HttpRequestInfo* request_info) override;
55 int InitializeStream(bool can_send_early,
dchengb03027d2014-10-21 12:00:2056 RequestPriority priority,
tfarina42834112016-09-22 13:38:2057 const NetLogWithSource& net_log,
Bence Békya25e3f72018-02-13 21:13:3958 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2059 int SendRequest(const HttpRequestHeaders& request_headers,
60 HttpResponseInfo* response,
Bence Békya25e3f72018-02-13 21:13:3961 CompletionOnceCallback callback) override;
62 int ReadResponseHeaders(CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2063 int ReadResponseBody(IOBuffer* buf,
64 int buf_len,
Bence Békya25e3f72018-02-13 21:13:3965 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2066 void Close(bool not_reusable) override;
dchengb03027d2014-10-21 12:00:2067 bool IsResponseBodyComplete() const override;
dchengb03027d2014-10-21 12:00:2068 bool IsConnectionReused() const override;
sclittle4de1bab92015-09-22 21:28:2469 int64_t GetTotalReceivedBytes() const override;
sclittlebe1ccf62015-09-02 19:40:3670 int64_t GetTotalSentBytes() const override;
dchengb03027d2014-10-21 12:00:2071 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
rchcd379012017-04-12 21:53:3272 bool GetAlternativeService(
73 AlternativeService* alternative_service) const override;
zhongyica364fbb2015-12-12 03:39:1274 void PopulateNetErrorDetails(NetErrorDetails* details) override;
dchengb03027d2014-10-21 12:00:2075 void SetPriority(RequestPriority priority) override;
Yu Suba2a1d72020-12-04 02:14:4176 void SetRequestIdempotency(Idempotency idempotency) override;
Eric Orthac661912022-01-10 21:44:1777 const std::set<std::string>& GetDnsAliases() const override;
Bence Béky334ddfc22021-03-12 15:18:1378 base::StringPiece GetAcceptChViaAlps() const override;
[email protected]f702d572012-12-04 15:56:2079
bnc90be5dd782016-11-09 16:28:4480 static HttpResponseInfo::ConnectionInfo ConnectionInfoFromQuicVersion(
Nick Harper63da9422019-10-22 22:41:0881 quic::ParsedQuicVersion quic_version);
bnc90be5dd782016-11-09 16:28:4482
[email protected]f702d572012-12-04 15:56:2083 private:
[email protected]24e5bc52013-09-18 15:36:5884 friend class test::QuicHttpStreamPeer;
85
[email protected]f702d572012-12-04 15:56:2086 enum State {
87 STATE_NONE,
rcha08ce102016-09-15 00:39:4788 STATE_HANDLE_PROMISE,
89 STATE_HANDLE_PROMISE_COMPLETE,
ckrasic3865ee0f2016-02-29 22:04:5690 STATE_REQUEST_STREAM,
rcha08ce102016-09-15 00:39:4791 STATE_REQUEST_STREAM_COMPLETE,
ckrasic3865ee0f2016-02-29 22:04:5692 STATE_SET_REQUEST_PRIORITY,
[email protected]f702d572012-12-04 15:56:2093 STATE_SEND_HEADERS,
94 STATE_SEND_HEADERS_COMPLETE,
95 STATE_READ_REQUEST_BODY,
96 STATE_READ_REQUEST_BODY_COMPLETE,
97 STATE_SEND_BODY,
98 STATE_SEND_BODY_COMPLETE,
99 STATE_OPEN,
100 };
101
102 void OnIOComplete(int rv);
103 void DoCallback(int rv);
104
bnc614a92d32016-04-04 13:56:07105 int DoLoop(int rv);
rcha08ce102016-09-15 00:39:47106 int DoHandlePromise();
107 int DoHandlePromiseComplete(int rv);
108 int DoRequestStream();
109 int DoRequestStreamComplete(int rv);
ckrasic3865ee0f2016-02-29 22:04:56110 int DoSetRequestPriority();
[email protected]f702d572012-12-04 15:56:20111 int DoSendHeaders();
112 int DoSendHeadersComplete(int rv);
113 int DoReadRequestBody();
114 int DoReadRequestBodyComplete(int rv);
115 int DoSendBody();
116 int DoSendBodyComplete(int rv);
[email protected]f702d572012-12-04 15:56:20117
rchfb47f712017-05-21 03:24:00118 void OnReadResponseHeadersComplete(int rv);
Bence Béky4c325e52020-10-22 20:48:01119 int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers);
rchce246412017-05-30 13:51:50120 void ReadTrailingHeaders();
121 void OnReadTrailingHeadersComplete(int rv);
[email protected]f702d572012-12-04 15:56:20122
rch27da0452017-05-26 22:54:54123 void OnReadBodyComplete(int rv);
124 int HandleReadComplete(int rv);
125
ckrasic3865ee0f2016-02-29 22:04:56126 void EnterStateSendHeaders();
[email protected]f702d572012-12-04 15:56:20127
rtenneti1e777fa02015-08-26 00:10:16128 void ResetStream();
129
rch1bcfddf22017-06-03 00:26:29130 // Returns ERR_QUIC_HANDSHAKE_FAILED, if |rv| is ERR_QUIC_PROTOCOL_ERROR and
131 // the handshake was never confirmed. Otherwise, returns |rv|.
132 int MapStreamError(int rv);
133
rch4cbd5e22017-03-30 00:13:29134 // If |has_response_status_| is false, sets |response_status| to the result
135 // of ComputeResponseStatus(). Returns |response_status_|.
136 int GetResponseStatus();
137 // Sets the result of |ComputeResponseStatus()| as the |response_status_|.
138 void SaveResponseStatus();
139 // Sets |response_status_| to |response_status| and sets
140 // |has_response_status_| to true.
141 void SetResponseStatus(int response_status);
142 // Computes the correct response status based on the status of the handshake,
143 // |session_error|, |connection_error| and |stream_error|.
144 int ComputeResponseStatus() const;
145
rchf0b18c8a2017-05-05 19:31:57146 QuicChromiumClientSession::Handle* quic_session() {
147 return static_cast<QuicChromiumClientSession::Handle*>(session());
148 }
[email protected]f702d572012-12-04 15:56:20149
rchf0b18c8a2017-05-05 19:31:57150 const QuicChromiumClientSession::Handle* quic_session() const {
151 return static_cast<const QuicChromiumClientSession::Handle*>(session());
152 }
153
Tsuyoshi Horo4478fd32022-06-09 01:41:25154 State next_state_ = STATE_NONE;
rch1d3b31a2017-03-29 20:34:17155
rch08e198572017-05-09 16:56:55156 std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
[email protected]f702d572012-12-04 15:56:20157
158 // The following three fields are all owned by the caller and must
159 // outlive this object, according to the HttpStream contract.
160
161 // The request to send.
shivanisha0b440852016-10-18 15:48:15162 // Only valid before the response body is read.
Keishi Hattori0e45c022021-11-27 09:25:52163 raw_ptr<const HttpRequestInfo> request_info_;
shivanisha0b440852016-10-18 15:48:15164
Steven Valdezb4ff0412018-01-18 22:39:27165 // Whether this request can be sent without confirmation.
Tsuyoshi Horo4478fd32022-06-09 01:41:25166 bool can_send_early_ = false;
Steven Valdezb4ff0412018-01-18 22:39:27167
[email protected]f702d572012-12-04 15:56:20168 // The request body to send, if any, owned by the caller.
Keishi Hattori0e45c022021-11-27 09:25:52169 raw_ptr<UploadDataStream> request_body_stream_;
[email protected]6ed67432014-06-24 01:53:53170 // Time the request was issued.
171 base::Time request_time_;
[email protected]24e5bc52013-09-18 15:36:58172 // The priority of the request.
Tsuyoshi Horo4478fd32022-06-09 01:41:25173 RequestPriority priority_ = MINIMUM_PRIORITY;
[email protected]f702d572012-12-04 15:56:20174 // |response_info_| is the HTTP response data object which is filled in
175 // when a the response headers are read. It is not owned by this stream.
Keishi Hattori0e45c022021-11-27 09:25:52176 raw_ptr<HttpResponseInfo> response_info_;
Tsuyoshi Horo4478fd32022-06-09 01:41:25177 bool has_response_status_ = false; // true if response_status_ as been set.
[email protected]56dfb902013-01-03 23:17:55178 // Because response data is buffered, also buffer the response status if the
179 // stream is explicitly closed via OnError or OnClose with an error.
180 // Once all buffered data has been returned, this will be used as the final
181 // response.
Tsuyoshi Horo4478fd32022-06-09 01:41:25182 int response_status_ = ERR_UNEXPECTED;
[email protected]f702d572012-12-04 15:56:20183
[email protected]13428d42013-11-26 18:50:36184 // Serialized request headers.
Bence Béky4c325e52020-10-22 20:48:01185 spdy::Http2HeaderBlock request_headers_;
[email protected]13428d42013-11-26 18:50:36186
Bence Béky4c325e52020-10-22 20:48:01187 spdy::Http2HeaderBlock response_header_block_;
Tsuyoshi Horo4478fd32022-06-09 01:41:25188 bool response_headers_received_ = false;
[email protected]f702d572012-12-04 15:56:20189
Bence Béky4c325e52020-10-22 20:48:01190 spdy::Http2HeaderBlock trailing_header_block_;
Tsuyoshi Horo4478fd32022-06-09 01:41:25191 bool trailing_headers_received_ = false;
rchce246412017-05-30 13:51:50192
sclittlec4dc1a32015-09-24 00:15:45193 // Number of bytes received by the headers stream on behalf of this stream.
Tsuyoshi Horo4478fd32022-06-09 01:41:25194 int64_t headers_bytes_received_ = 0;
sclittlec4dc1a32015-09-24 00:15:45195 // Number of bytes sent by the headers stream on behalf of this stream.
Tsuyoshi Horo4478fd32022-06-09 01:41:25196 int64_t headers_bytes_sent_ = 0;
sclittlec4dc1a32015-09-24 00:15:45197
[email protected]14cfbd62014-01-23 22:59:15198 // Number of bytes received when the stream was closed.
Tsuyoshi Horo4478fd32022-06-09 01:41:25199 int64_t closed_stream_received_bytes_ = 0;
sclittle1edeeb22015-09-02 20:46:10200 // Number of bytes sent when the stream was closed.
Tsuyoshi Horo4478fd32022-06-09 01:41:25201 int64_t closed_stream_sent_bytes_ = 0;
xunjieli100937eb52016-09-15 20:09:37202 // True if the stream is the first stream negotiated on the session. Set when
203 // the stream was closed. If |stream_| is failed to be created, this takes on
204 // the default value of false.
Tsuyoshi Horo4478fd32022-06-09 01:41:25205 bool closed_is_first_stream_ = false;
[email protected]14cfbd62014-01-23 22:59:15206
[email protected]f702d572012-12-04 15:56:20207 // The caller's callback to be used for asynchronous operations.
Bence Békya25e3f72018-02-13 21:13:39208 CompletionOnceCallback callback_;
[email protected]f702d572012-12-04 15:56:20209
210 // Caller provided buffer for the ReadResponseBody() response.
211 scoped_refptr<IOBuffer> user_buffer_;
Tsuyoshi Horo4478fd32022-06-09 01:41:25212 int user_buffer_len_ = 0;
[email protected]f702d572012-12-04 15:56:20213
214 // Temporary buffer used to read the request body from UploadDataStream.
215 scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
216 // Wraps raw_request_body_buf_ to read the remaining data progressively.
217 scoped_refptr<DrainableIOBuffer> request_body_buf_;
218
tfarina42834112016-09-22 13:38:20219 NetLogWithSource stream_net_log_;
[email protected]0d10b592013-02-14 16:09:26220
Tsuyoshi Horo4478fd32022-06-09 01:41:25221 int session_error_ =
222 ERR_UNEXPECTED; // Error code from the connection shutdown.
zhongyica364fbb2015-12-12 03:39:12223
Tsuyoshi Horo4478fd32022-06-09 01:41:25224 bool found_promise_ = false;
ckrasic3865ee0f2016-02-29 22:04:56225
xunjieli1f48e6e2016-07-13 22:53:04226 // Set to true when DoLoop() is being executed, false otherwise.
Tsuyoshi Horo4478fd32022-06-09 01:41:25227 bool in_loop_ = false;
xunjieli1f48e6e2016-07-13 22:53:04228
xunjieli100937eb52016-09-15 20:09:37229 // Session connect timing info.
230 LoadTimingInfo::ConnectTiming connect_timing_;
231
Eric Orthac661912022-01-10 21:44:17232 // Stores any DNS aliases for the remote endpoint. Includes all known
233 // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for
234 // the connection, in no particular order. These are stored in the stream
235 // instead of the session due to complications related to IP-pooling.
236 std::set<std::string> dns_aliases_;
Cammie Smith Barnes0f38aff2021-03-02 23:14:35237
Jeremy Romand54000b22019-07-08 18:40:16238 base::WeakPtrFactory<QuicHttpStream> weak_factory_{this};
[email protected]f702d572012-12-04 15:56:20239};
240
241} // namespace net
242
Ryan Hamiltona3ee93a72018-08-01 22:03:08243#endif // NET_QUIC_QUIC_HTTP_STREAM_H_