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 5 #ifndef NET_QUIC_QUIC_HTTP_STREAM_H_ 6 #define NET_QUIC_QUIC_HTTP_STREAM_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <list> 12 #include <string> 13 #include <vector> 14 15 #include "base/macros.h" 16 #include "base/memory/weak_ptr.h" 17 #include "net/base/completion_once_callback.h" 18 #include "net/base/io_buffer.h" 19 #include "net/base/load_timing_info.h" 20 #include "net/base/net_export.h" 21 #include "net/http/http_response_info.h" 22 #include "net/http/http_server_properties.h" 23 #include "net/log/net_log_with_source.h" 24 #include "net/quic/quic_chromium_client_session.h" 25 #include "net/quic/quic_chromium_client_stream.h" 26 #include "net/spdy/multiplexed_http_stream.h" 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" 29 30 namespace net { 31 32 namespace test { 33 class QuicHttpStreamPeer; 34 } // namespace test 35 36 // The QuicHttpStream is a QUIC-specific HttpStream subclass. It holds a 37 // non-owning pointer to a QuicChromiumClientStream which it uses to 38 // send and receive data. 39 class NET_EXPORT_PRIVATE QuicHttpStream : public MultiplexedHttpStream { 40 public: 41 explicit QuicHttpStream( 42 std::unique_ptr<QuicChromiumClientSession::Handle> session); 43 44 ~QuicHttpStream() override; 45 46 // HttpStream implementation. 47 int InitializeStream(const HttpRequestInfo* request_info, 48 bool can_send_early, 49 RequestPriority priority, 50 const NetLogWithSource& net_log, 51 CompletionOnceCallback callback) override; 52 int SendRequest(const HttpRequestHeaders& request_headers, 53 HttpResponseInfo* response, 54 CompletionOnceCallback callback) override; 55 int ReadResponseHeaders(CompletionOnceCallback callback) override; 56 int ReadResponseBody(IOBuffer* buf, 57 int buf_len, 58 CompletionOnceCallback callback) override; 59 void Close(bool not_reusable) override; 60 bool IsResponseBodyComplete() const override; 61 bool IsConnectionReused() const override; 62 int64_t GetTotalReceivedBytes() const override; 63 int64_t GetTotalSentBytes() const override; 64 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override; 65 bool GetAlternativeService( 66 AlternativeService* alternative_service) const override; 67 void PopulateNetErrorDetails(NetErrorDetails* details) override; 68 void SetPriority(RequestPriority priority) override; 69 70 static HttpResponseInfo::ConnectionInfo ConnectionInfoFromQuicVersion( 71 quic::ParsedQuicVersion quic_version); 72 73 private: 74 friend class test::QuicHttpStreamPeer; 75 76 enum State { 77 STATE_NONE, 78 STATE_HANDLE_PROMISE, 79 STATE_HANDLE_PROMISE_COMPLETE, 80 STATE_REQUEST_STREAM, 81 STATE_REQUEST_STREAM_COMPLETE, 82 STATE_SET_REQUEST_PRIORITY, 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 95 int DoLoop(int rv); 96 int DoHandlePromise(); 97 int DoHandlePromiseComplete(int rv); 98 int DoRequestStream(); 99 int DoRequestStreamComplete(int rv); 100 int DoSetRequestPriority(); 101 int DoSendHeaders(); 102 int DoSendHeadersComplete(int rv); 103 int DoReadRequestBody(); 104 int DoReadRequestBodyComplete(int rv); 105 int DoSendBody(); 106 int DoSendBodyComplete(int rv); 107 108 void OnReadResponseHeadersComplete(int rv); 109 int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers); 110 void ReadTrailingHeaders(); 111 void OnReadTrailingHeadersComplete(int rv); 112 113 void OnReadBodyComplete(int rv); 114 int HandleReadComplete(int rv); 115 116 void EnterStateSendHeaders(); 117 118 void ResetStream(); 119 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 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 quic_session()136 QuicChromiumClientSession::Handle* quic_session() { 137 return static_cast<QuicChromiumClientSession::Handle*>(session()); 138 } 139 quic_session()140 const QuicChromiumClientSession::Handle* quic_session() const { 141 return static_cast<const QuicChromiumClientSession::Handle*>(session()); 142 } 143 144 State next_state_; 145 146 std::unique_ptr<QuicChromiumClientStream::Handle> stream_; 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. 152 // Only valid before the response body is read. 153 const HttpRequestInfo* request_info_; 154 155 // Whether this request can be sent without confirmation. 156 bool can_send_early_; 157 158 // The request body to send, if any, owned by the caller. 159 UploadDataStream* request_body_stream_; 160 // Time the request was issued. 161 base::Time request_time_; 162 // The priority of the request. 163 RequestPriority priority_; 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_; 167 bool has_response_status_; // true if response_status_ as been set. 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_; 173 174 // Serialized request headers. 175 spdy::Http2HeaderBlock request_headers_; 176 177 spdy::Http2HeaderBlock response_header_block_; 178 bool response_headers_received_; 179 180 spdy::Http2HeaderBlock trailing_header_block_; 181 bool trailing_headers_received_; 182 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 188 // Number of bytes received when the stream was closed. 189 int64_t closed_stream_received_bytes_; 190 // Number of bytes sent when the stream was closed. 191 int64_t closed_stream_sent_bytes_; 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_; 196 197 // The caller's callback to be used for asynchronous operations. 198 CompletionOnceCallback callback_; 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 209 NetLogWithSource stream_net_log_; 210 211 int session_error_; // Error code from the connection shutdown. 212 213 bool found_promise_; 214 215 // Set to true when DoLoop() is being executed, false otherwise. 216 bool in_loop_; 217 218 // Session connect timing info. 219 LoadTimingInfo::ConnectTiming connect_timing_; 220 221 base::WeakPtrFactory<QuicHttpStream> weak_factory_{this}; 222 223 DISALLOW_COPY_AND_ASSIGN(QuicHttpStream); 224 }; 225 226 } // namespace net 227 228 #endif // NET_QUIC_QUIC_HTTP_STREAM_H_ 229