[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 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 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 5 | #ifndef NET_QUIC_QUIC_HTTP_STREAM_H_ |
| 6 | #define NET_QUIC_QUIC_HTTP_STREAM_H_ |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 11 | #include <list> |
bnc | 614a92d3 | 2016-04-04 13:56:07 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 14 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 17 | #include "net/base/completion_once_callback.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 18 | #include "net/base/io_buffer.h" |
xunjieli | 100937eb5 | 2016-09-15 20:09:37 | [diff] [blame] | 19 | #include "net/base/load_timing_info.h" |
bnc | 81c46c1f | 2016-10-04 16:25:59 | [diff] [blame] | 20 | #include "net/base/net_export.h" |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 21 | #include "net/http/http_response_info.h" |
rch | 1d3b31a | 2017-03-29 20:34:17 | [diff] [blame] | 22 | #include "net/http/http_server_properties.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 23 | #include "net/log/net_log_with_source.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 24 | #include "net/quic/quic_chromium_client_session.h" |
| 25 | #include "net/quic/quic_chromium_client_stream.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 26 | #include "net/spdy/multiplexed_http_stream.h" |
Victor Vasiliev | 6bb59d2 | 2019-03-08 21:34:51 | [diff] [blame] | 27 | #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] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 29 | |
| 30 | namespace net { |
| 31 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 32 | namespace test { |
| 33 | class QuicHttpStreamPeer; |
| 34 | } // namespace test |
| 35 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 36 | // The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a |
rch | 12fef55 | 2016-01-15 16:26:31 | [diff] [blame] | 37 | // non-owning pointer to a QuicChromiumClientStream which it uses to |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 38 | // send and receive data. |
rch | 56ec40a | 2017-06-23 14:48:44 | [diff] [blame] | 39 | class NET_EXPORT_PRIVATE QuicHttpStream : public MultiplexedHttpStream { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 40 | public: |
zhongyi | 98d6a926 | 2017-05-19 02:47:45 | [diff] [blame] | 41 | explicit QuicHttpStream( |
| 42 | std::unique_ptr<QuicChromiumClientSession::Handle> session); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 43 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 44 | ~QuicHttpStream() override; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 45 | |
| 46 | // HttpStream implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 47 | int InitializeStream(const HttpRequestInfo* request_info, |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 48 | bool can_send_early, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 49 | RequestPriority priority, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 50 | const NetLogWithSource& net_log, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 51 | CompletionOnceCallback callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 52 | int SendRequest(const HttpRequestHeaders& request_headers, |
| 53 | HttpResponseInfo* response, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 54 | CompletionOnceCallback callback) override; |
| 55 | int ReadResponseHeaders(CompletionOnceCallback callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 56 | int ReadResponseBody(IOBuffer* buf, |
| 57 | int buf_len, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 58 | CompletionOnceCallback callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 59 | void Close(bool not_reusable) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 60 | bool IsResponseBodyComplete() const override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 61 | bool IsConnectionReused() const override; |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 62 | int64_t GetTotalReceivedBytes() const override; |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 63 | int64_t GetTotalSentBytes() const override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 64 | bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override; |
rch | cd37901 | 2017-04-12 21:53:32 | [diff] [blame] | 65 | bool GetAlternativeService( |
| 66 | AlternativeService* alternative_service) const override; |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 67 | void PopulateNetErrorDetails(NetErrorDetails* details) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 68 | void SetPriority(RequestPriority priority) override; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 69 | |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 70 | static HttpResponseInfo::ConnectionInfo ConnectionInfoFromQuicVersion( |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 71 | quic::QuicTransportVersion quic_version); |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 72 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 73 | private: |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 74 | friend class test::QuicHttpStreamPeer; |
| 75 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 76 | enum State { |
| 77 | STATE_NONE, |
rch | a08ce10 | 2016-09-15 00:39:47 | [diff] [blame] | 78 | STATE_HANDLE_PROMISE, |
| 79 | STATE_HANDLE_PROMISE_COMPLETE, |
ckrasic | 3865ee0f | 2016-02-29 22:04:56 | [diff] [blame] | 80 | STATE_REQUEST_STREAM, |
rch | a08ce10 | 2016-09-15 00:39:47 | [diff] [blame] | 81 | STATE_REQUEST_STREAM_COMPLETE, |
ckrasic | 3865ee0f | 2016-02-29 22:04:56 | [diff] [blame] | 82 | STATE_SET_REQUEST_PRIORITY, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 83 | 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 | |
bnc | 614a92d3 | 2016-04-04 13:56:07 | [diff] [blame] | 95 | int DoLoop(int rv); |
rch | a08ce10 | 2016-09-15 00:39:47 | [diff] [blame] | 96 | int DoHandlePromise(); |
| 97 | int DoHandlePromiseComplete(int rv); |
| 98 | int DoRequestStream(); |
| 99 | int DoRequestStreamComplete(int rv); |
ckrasic | 3865ee0f | 2016-02-29 22:04:56 | [diff] [blame] | 100 | int DoSetRequestPriority(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 101 | 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] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 107 | |
rch | fb47f71 | 2017-05-21 03:24:00 | [diff] [blame] | 108 | void OnReadResponseHeadersComplete(int rv); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 109 | int ProcessResponseHeaders(const spdy::SpdyHeaderBlock& headers); |
rch | ce24641 | 2017-05-30 13:51:50 | [diff] [blame] | 110 | void ReadTrailingHeaders(); |
| 111 | void OnReadTrailingHeadersComplete(int rv); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 112 | |
rch | 27da045 | 2017-05-26 22:54:54 | [diff] [blame] | 113 | void OnReadBodyComplete(int rv); |
| 114 | int HandleReadComplete(int rv); |
| 115 | |
ckrasic | 3865ee0f | 2016-02-29 22:04:56 | [diff] [blame] | 116 | void EnterStateSendHeaders(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 117 | |
rtenneti | 1e777fa0 | 2015-08-26 00:10:16 | [diff] [blame] | 118 | void ResetStream(); |
| 119 | |
rch | 1bcfddf2 | 2017-06-03 00:26:29 | [diff] [blame] | 120 | // 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 | |
rch | 4cbd5e2 | 2017-03-30 00:13:29 | [diff] [blame] | 124 | // 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 | |
rch | f0b18c8a | 2017-05-05 19:31:57 | [diff] [blame] | 136 | QuicChromiumClientSession::Handle* quic_session() { |
| 137 | return static_cast<QuicChromiumClientSession::Handle*>(session()); |
| 138 | } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 139 | |
rch | f0b18c8a | 2017-05-05 19:31:57 | [diff] [blame] | 140 | const QuicChromiumClientSession::Handle* quic_session() const { |
| 141 | return static_cast<const QuicChromiumClientSession::Handle*>(session()); |
| 142 | } |
| 143 | |
| 144 | State next_state_; |
rch | 1d3b31a | 2017-03-29 20:34:17 | [diff] [blame] | 145 | |
rch | 08e19857 | 2017-05-09 16:56:55 | [diff] [blame] | 146 | std::unique_ptr<QuicChromiumClientStream::Handle> stream_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 147 | |
| 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. |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 152 | // Only valid before the response body is read. |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 153 | const HttpRequestInfo* request_info_; |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 154 | |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 155 | // Whether this request can be sent without confirmation. |
| 156 | bool can_send_early_; |
| 157 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 158 | // The request body to send, if any, owned by the caller. |
| 159 | UploadDataStream* request_body_stream_; |
[email protected] | 6ed6743 | 2014-06-24 01:53:53 | [diff] [blame] | 160 | // Time the request was issued. |
| 161 | base::Time request_time_; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 162 | // The priority of the request. |
| 163 | RequestPriority priority_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 164 | // |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_; |
rch | 4cbd5e2 | 2017-03-30 00:13:29 | [diff] [blame] | 167 | bool has_response_status_; // true if response_status_ as been set. |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 168 | // 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] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 173 | |
[email protected] | 13428d4 | 2013-11-26 18:50:36 | [diff] [blame] | 174 | // Serialized request headers. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 175 | spdy::SpdyHeaderBlock request_headers_; |
[email protected] | 13428d4 | 2013-11-26 18:50:36 | [diff] [blame] | 176 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 177 | spdy::SpdyHeaderBlock response_header_block_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 178 | bool response_headers_received_; |
| 179 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 180 | spdy::SpdyHeaderBlock trailing_header_block_; |
rch | ce24641 | 2017-05-30 13:51:50 | [diff] [blame] | 181 | bool trailing_headers_received_; |
| 182 | |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 183 | // 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] | 14cfbd6 | 2014-01-23 22:59:15 | [diff] [blame] | 188 | // Number of bytes received when the stream was closed. |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 189 | int64_t closed_stream_received_bytes_; |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 190 | // Number of bytes sent when the stream was closed. |
| 191 | int64_t closed_stream_sent_bytes_; |
xunjieli | 100937eb5 | 2016-09-15 20:09:37 | [diff] [blame] | 192 | // 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] | 14cfbd6 | 2014-01-23 22:59:15 | [diff] [blame] | 196 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 197 | // The caller's callback to be used for asynchronous operations. |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 198 | CompletionOnceCallback callback_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 199 | |
| 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 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 209 | NetLogWithSource stream_net_log_; |
[email protected] | 0d10b59 | 2013-02-14 16:09:26 | [diff] [blame] | 210 | |
rch | f0b18c8a | 2017-05-05 19:31:57 | [diff] [blame] | 211 | int session_error_; // Error code from the connection shutdown. |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 212 | |
ckrasic | 3865ee0f | 2016-02-29 22:04:56 | [diff] [blame] | 213 | bool found_promise_; |
ckrasic | 3865ee0f | 2016-02-29 22:04:56 | [diff] [blame] | 214 | |
xunjieli | 1f48e6e | 2016-07-13 22:53:04 | [diff] [blame] | 215 | // Set to true when DoLoop() is being executed, false otherwise. |
| 216 | bool in_loop_; |
| 217 | |
xunjieli | 100937eb5 | 2016-09-15 20:09:37 | [diff] [blame] | 218 | // Session connect timing info. |
| 219 | LoadTimingInfo::ConnectTiming connect_timing_; |
| 220 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame^] | 221 | base::WeakPtrFactory<QuicHttpStream> weak_factory_{this}; |
[email protected] | b99c0fc | 2014-04-22 07:56:52 | [diff] [blame] | 222 | |
| 223 | DISALLOW_COPY_AND_ASSIGN(QuicHttpStream); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | } // namespace net |
| 227 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 228 | #endif // NET_QUIC_QUIC_HTTP_STREAM_H_ |