blob: 0440c765387ebf860f535daaa28b636ee49fde41 [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
[email protected]f702d572012-12-04 15:56:2011#include <list>
bnc614a92d32016-04-04 13:56:0712#include <string>
13#include <vector>
[email protected]f702d572012-12-04 15:56:2014
Avi Drissman13fc8932015-12-20 04:40:4615#include "base/macros.h"
[email protected]f702d572012-12-04 15:56:2016#include "base/memory/weak_ptr.h"
Bence Békya25e3f72018-02-13 21:13:3917#include "net/base/completion_once_callback.h"
[email protected]f702d572012-12-04 15:56:2018#include "net/base/io_buffer.h"
xunjieli100937eb52016-09-15 20:09:3719#include "net/base/load_timing_info.h"
bnc81c46c1f2016-10-04 16:25:5920#include "net/base/net_export.h"
bnc90be5dd782016-11-09 16:28:4421#include "net/http/http_response_info.h"
rch1d3b31a2017-03-29 20:34:1722#include "net/http/http_server_properties.h"
mikecironef22f9812016-10-04 03:40:1923#include "net/log/net_log_with_source.h"
Ryan Hamiltona3ee93a72018-08-01 22:03:0824#include "net/quic/quic_chromium_client_session.h"
25#include "net/quic/quic_chromium_client_stream.h"
Bence Béky94658bf2018-05-11 19:22:5826#include "net/spdy/multiplexed_http_stream.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5127#include "net/third_party/quiche/src/quic/core/http/quic_client_push_promise_index.h"
28#include "net/third_party/quiche/src/quic/core/quic_packets.h"
[email protected]f702d572012-12-04 15:56:2029
30namespace net {
31
[email protected]24e5bc52013-09-18 15:36:5832namespace test {
33class QuicHttpStreamPeer;
34} // namespace test
35
[email protected]f702d572012-12-04 15:56:2036// The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a
rch12fef552016-01-15 16:26:3137// non-owning pointer to a QuicChromiumClientStream which it uses to
[email protected]f702d572012-12-04 15:56:2038// send and receive data.
rch56ec40a2017-06-23 14:48:4439class NET_EXPORT_PRIVATE QuicHttpStream : public MultiplexedHttpStream {
[email protected]f702d572012-12-04 15:56:2040 public:
zhongyi98d6a9262017-05-19 02:47:4541 explicit QuicHttpStream(
42 std::unique_ptr<QuicChromiumClientSession::Handle> session);
[email protected]f702d572012-12-04 15:56:2043
dchengb03027d2014-10-21 12:00:2044 ~QuicHttpStream() override;
[email protected]f702d572012-12-04 15:56:2045
46 // HttpStream implementation.
dchengb03027d2014-10-21 12:00:2047 int InitializeStream(const HttpRequestInfo* request_info,
Steven Valdezb4ff0412018-01-18 22:39:2748 bool can_send_early,
dchengb03027d2014-10-21 12:00:2049 RequestPriority priority,
tfarina42834112016-09-22 13:38:2050 const NetLogWithSource& net_log,
Bence Békya25e3f72018-02-13 21:13:3951 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2052 int SendRequest(const HttpRequestHeaders& request_headers,
53 HttpResponseInfo* response,
Bence Békya25e3f72018-02-13 21:13:3954 CompletionOnceCallback callback) override;
55 int ReadResponseHeaders(CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2056 int ReadResponseBody(IOBuffer* buf,
57 int buf_len,
Bence Békya25e3f72018-02-13 21:13:3958 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2059 void Close(bool not_reusable) override;
dchengb03027d2014-10-21 12:00:2060 bool IsResponseBodyComplete() const override;
dchengb03027d2014-10-21 12:00:2061 bool IsConnectionReused() const override;
sclittle4de1bab92015-09-22 21:28:2462 int64_t GetTotalReceivedBytes() const override;
sclittlebe1ccf62015-09-02 19:40:3663 int64_t GetTotalSentBytes() const override;
dchengb03027d2014-10-21 12:00:2064 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
rchcd379012017-04-12 21:53:3265 bool GetAlternativeService(
66 AlternativeService* alternative_service) const override;
zhongyica364fbb2015-12-12 03:39:1267 void PopulateNetErrorDetails(NetErrorDetails* details) override;
dchengb03027d2014-10-21 12:00:2068 void SetPriority(RequestPriority priority) override;
[email protected]f702d572012-12-04 15:56:2069
bnc90be5dd782016-11-09 16:28:4470 static HttpResponseInfo::ConnectionInfo ConnectionInfoFromQuicVersion(
Ryan Hamilton8d9ee76e2018-05-29 23:52:5271 quic::QuicTransportVersion quic_version);
bnc90be5dd782016-11-09 16:28:4472
[email protected]f702d572012-12-04 15:56:2073 private:
[email protected]24e5bc52013-09-18 15:36:5874 friend class test::QuicHttpStreamPeer;
75
[email protected]f702d572012-12-04 15:56:2076 enum State {
77 STATE_NONE,
rcha08ce102016-09-15 00:39:4778 STATE_HANDLE_PROMISE,
79 STATE_HANDLE_PROMISE_COMPLETE,
ckrasic3865ee0f2016-02-29 22:04:5680 STATE_REQUEST_STREAM,
rcha08ce102016-09-15 00:39:4781 STATE_REQUEST_STREAM_COMPLETE,
ckrasic3865ee0f2016-02-29 22:04:5682 STATE_SET_REQUEST_PRIORITY,
[email protected]f702d572012-12-04 15:56:2083 STATE_SEND_HEADERS,
84 STATE_SEND_HEADERS_COMPLETE,
85 STATE_READ_REQUEST_BODY,
86 STATE_READ_REQUEST_BODY_COMPLETE,
87 STATE_SEND_BODY,
88 STATE_SEND_BODY_COMPLETE,
89 STATE_OPEN,
90 };
91
92 void OnIOComplete(int rv);
93 void DoCallback(int rv);
94
bnc614a92d32016-04-04 13:56:0795 int DoLoop(int rv);
rcha08ce102016-09-15 00:39:4796 int DoHandlePromise();
97 int DoHandlePromiseComplete(int rv);
98 int DoRequestStream();
99 int DoRequestStreamComplete(int rv);
ckrasic3865ee0f2016-02-29 22:04:56100 int DoSetRequestPriority();
[email protected]f702d572012-12-04 15:56:20101 int DoSendHeaders();
102 int DoSendHeadersComplete(int rv);
103 int DoReadRequestBody();
104 int DoReadRequestBodyComplete(int rv);
105 int DoSendBody();
106 int DoSendBodyComplete(int rv);
[email protected]f702d572012-12-04 15:56:20107
rchfb47f712017-05-21 03:24:00108 void OnReadResponseHeadersComplete(int rv);
Ryan Hamilton0239aac2018-05-19 00:03:13109 int ProcessResponseHeaders(const spdy::SpdyHeaderBlock& headers);
rchce246412017-05-30 13:51:50110 void ReadTrailingHeaders();
111 void OnReadTrailingHeadersComplete(int rv);
[email protected]f702d572012-12-04 15:56:20112
rch27da0452017-05-26 22:54:54113 void OnReadBodyComplete(int rv);
114 int HandleReadComplete(int rv);
115
ckrasic3865ee0f2016-02-29 22:04:56116 void EnterStateSendHeaders();
[email protected]f702d572012-12-04 15:56:20117
rtenneti1e777fa02015-08-26 00:10:16118 void ResetStream();
119
rch1bcfddf22017-06-03 00:26:29120 // Returns ERR_QUIC_HANDSHAKE_FAILED, if |rv| is ERR_QUIC_PROTOCOL_ERROR and
121 // the handshake was never confirmed. Otherwise, returns |rv|.
122 int MapStreamError(int rv);
123
rch4cbd5e22017-03-30 00:13:29124 // If |has_response_status_| is false, sets |response_status| to the result
125 // of ComputeResponseStatus(). Returns |response_status_|.
126 int GetResponseStatus();
127 // Sets the result of |ComputeResponseStatus()| as the |response_status_|.
128 void SaveResponseStatus();
129 // Sets |response_status_| to |response_status| and sets
130 // |has_response_status_| to true.
131 void SetResponseStatus(int response_status);
132 // Computes the correct response status based on the status of the handshake,
133 // |session_error|, |connection_error| and |stream_error|.
134 int ComputeResponseStatus() const;
135
rchf0b18c8a2017-05-05 19:31:57136 QuicChromiumClientSession::Handle* quic_session() {
137 return static_cast<QuicChromiumClientSession::Handle*>(session());
138 }
[email protected]f702d572012-12-04 15:56:20139
rchf0b18c8a2017-05-05 19:31:57140 const QuicChromiumClientSession::Handle* quic_session() const {
141 return static_cast<const QuicChromiumClientSession::Handle*>(session());
142 }
143
144 State next_state_;
rch1d3b31a2017-03-29 20:34:17145
rch08e198572017-05-09 16:56:55146 std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
[email protected]f702d572012-12-04 15:56:20147
148 // The following three fields are all owned by the caller and must
149 // outlive this object, according to the HttpStream contract.
150
151 // The request to send.
shivanisha0b440852016-10-18 15:48:15152 // Only valid before the response body is read.
[email protected]f702d572012-12-04 15:56:20153 const HttpRequestInfo* request_info_;
shivanisha0b440852016-10-18 15:48:15154
Steven Valdezb4ff0412018-01-18 22:39:27155 // Whether this request can be sent without confirmation.
156 bool can_send_early_;
157
[email protected]f702d572012-12-04 15:56:20158 // The request body to send, if any, owned by the caller.
159 UploadDataStream* request_body_stream_;
[email protected]6ed67432014-06-24 01:53:53160 // Time the request was issued.
161 base::Time request_time_;
[email protected]24e5bc52013-09-18 15:36:58162 // The priority of the request.
163 RequestPriority priority_;
[email protected]f702d572012-12-04 15:56:20164 // |response_info_| is the HTTP response data object which is filled in
165 // when a the response headers are read. It is not owned by this stream.
166 HttpResponseInfo* response_info_;
rch4cbd5e22017-03-30 00:13:29167 bool has_response_status_; // true if response_status_ as been set.
[email protected]56dfb902013-01-03 23:17:55168 // Because response data is buffered, also buffer the response status if the
169 // stream is explicitly closed via OnError or OnClose with an error.
170 // Once all buffered data has been returned, this will be used as the final
171 // response.
172 int response_status_;
[email protected]f702d572012-12-04 15:56:20173
[email protected]13428d42013-11-26 18:50:36174 // Serialized request headers.
Ryan Hamilton0239aac2018-05-19 00:03:13175 spdy::SpdyHeaderBlock request_headers_;
[email protected]13428d42013-11-26 18:50:36176
Ryan Hamilton0239aac2018-05-19 00:03:13177 spdy::SpdyHeaderBlock response_header_block_;
[email protected]f702d572012-12-04 15:56:20178 bool response_headers_received_;
179
Ryan Hamilton0239aac2018-05-19 00:03:13180 spdy::SpdyHeaderBlock trailing_header_block_;
rchce246412017-05-30 13:51:50181 bool trailing_headers_received_;
182
sclittlec4dc1a32015-09-24 00:15:45183 // Number of bytes received by the headers stream on behalf of this stream.
184 int64_t headers_bytes_received_;
185 // Number of bytes sent by the headers stream on behalf of this stream.
186 int64_t headers_bytes_sent_;
187
[email protected]14cfbd62014-01-23 22:59:15188 // Number of bytes received when the stream was closed.
sclittle4de1bab92015-09-22 21:28:24189 int64_t closed_stream_received_bytes_;
sclittle1edeeb22015-09-02 20:46:10190 // Number of bytes sent when the stream was closed.
191 int64_t closed_stream_sent_bytes_;
xunjieli100937eb52016-09-15 20:09:37192 // True if the stream is the first stream negotiated on the session. Set when
193 // the stream was closed. If |stream_| is failed to be created, this takes on
194 // the default value of false.
195 bool closed_is_first_stream_;
[email protected]14cfbd62014-01-23 22:59:15196
[email protected]f702d572012-12-04 15:56:20197 // The caller's callback to be used for asynchronous operations.
Bence Békya25e3f72018-02-13 21:13:39198 CompletionOnceCallback callback_;
[email protected]f702d572012-12-04 15:56:20199
200 // Caller provided buffer for the ReadResponseBody() response.
201 scoped_refptr<IOBuffer> user_buffer_;
202 int user_buffer_len_;
203
204 // Temporary buffer used to read the request body from UploadDataStream.
205 scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
206 // Wraps raw_request_body_buf_ to read the remaining data progressively.
207 scoped_refptr<DrainableIOBuffer> request_body_buf_;
208
tfarina42834112016-09-22 13:38:20209 NetLogWithSource stream_net_log_;
[email protected]0d10b592013-02-14 16:09:26210
rchf0b18c8a2017-05-05 19:31:57211 int session_error_; // Error code from the connection shutdown.
zhongyica364fbb2015-12-12 03:39:12212
ckrasic3865ee0f2016-02-29 22:04:56213 bool found_promise_;
ckrasic3865ee0f2016-02-29 22:04:56214
xunjieli1f48e6e2016-07-13 22:53:04215 // Set to true when DoLoop() is being executed, false otherwise.
216 bool in_loop_;
217
xunjieli100937eb52016-09-15 20:09:37218 // Session connect timing info.
219 LoadTimingInfo::ConnectTiming connect_timing_;
220
Jeremy Romand54000b22019-07-08 18:40:16221 base::WeakPtrFactory<QuicHttpStream> weak_factory_{this};
[email protected]b99c0fc2014-04-22 07:56:52222
223 DISALLOW_COPY_AND_ASSIGN(QuicHttpStream);
[email protected]f702d572012-12-04 15:56:20224};
225
226} // namespace net
227
Ryan Hamiltona3ee93a72018-08-01 22:03:08228#endif // NET_QUIC_QUIC_HTTP_STREAM_H_