OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 #include "net/quic/bidirectional_stream_quic_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/timer/timer.h" | |
11 #include "net/http/bidirectional_stream_request_info.h" | |
12 #include "net/socket/next_proto.h" | |
13 #include "net/spdy/spdy_header_block.h" | |
14 #include "net/spdy/spdy_http_utils.h" | |
15 | |
16 namespace net { | |
17 | |
18 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl( | |
19 const base::WeakPtr<QuicChromiumClientSession>& session) | |
20 : session_(session), | |
21 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()), | |
22 stream_(nullptr), | |
23 request_info_(nullptr), | |
24 delegate_(nullptr), | |
25 response_status_(OK), | |
26 negotiated_protocol_(kProtoUnknown), | |
27 read_buffer_len_(0), | |
28 headers_bytes_received_(0), | |
29 headers_bytes_sent_(0), | |
30 closed_stream_received_bytes_(0), | |
31 closed_stream_sent_bytes_(0), | |
32 has_sent_headers_(false), | |
33 has_received_headers_(false), | |
34 weak_factory_(this) { | |
35 DCHECK(session_); | |
36 session_->AddObserver(this); | |
37 } | |
38 | |
39 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() { | |
40 Cancel(); | |
41 if (session_) | |
42 session_->RemoveObserver(this); | |
43 } | |
44 | |
45 void BidirectionalStreamQuicImpl::Start( | |
46 const BidirectionalStreamRequestInfo* request_info, | |
47 const BoundNetLog& net_log, | |
48 BidirectionalStreamJob::Delegate* delegate, | |
49 scoped_ptr<base::Timer> /* timer */) { | |
50 DCHECK(!stream_); | |
51 | |
52 if (!session_) { | |
53 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR | |
54 : ERR_QUIC_HANDSHAKE_FAILED); | |
55 return; | |
56 } | |
57 | |
58 delegate_ = delegate; | |
59 request_info_ = request_info; | |
60 | |
61 int rv = stream_request_.StartRequest( | |
62 session_, &stream_, | |
63 base::Bind(&BidirectionalStreamQuicImpl::OnStreamReady, | |
64 weak_factory_.GetWeakPtr())); | |
65 if (rv == OK) { | |
66 OnStreamReady(rv); | |
67 } else if (!was_handshake_confirmed_) { | |
68 NotifyError(ERR_QUIC_HANDSHAKE_FAILED); | |
69 } | |
70 } | |
71 | |
72 int BidirectionalStreamQuicImpl::ReadData(IOBuffer* buffer, int buffer_len) { | |
73 DCHECK(buffer); | |
74 DCHECK(buffer_len); | |
75 | |
76 if (!stream_) { | |
77 // If the stream is already closed, there is no body to read. | |
78 return response_status_; | |
79 } | |
80 int rv = stream_->Read(buffer, buffer_len); | |
81 if (rv != ERR_IO_PENDING) { | |
82 if (stream_->IsDoneReading()) { | |
83 // If the write side is closed, OnFinRead() will call | |
84 // BidirectionalStreamQuicImpl::OnClose(). | |
85 stream_->OnFinRead(); | |
86 } | |
87 return rv; | |
88 } | |
89 // Read will complete asynchronously and Delegate::OnReadCompleted will be | |
90 // called upon completion. | |
91 read_buffer_ = buffer; | |
92 read_buffer_len_ = buffer_len; | |
93 return ERR_IO_PENDING; | |
94 } | |
95 | |
96 void BidirectionalStreamQuicImpl::SendData(IOBuffer* data, | |
97 int length, | |
98 bool end_stream) { | |
99 DCHECK(stream_); | |
100 DCHECK(length > 0 || (length == 0 && end_stream)); | |
101 | |
102 base::StringPiece string_data(data->data(), length); | |
103 int rv = stream_->WriteStreamData( | |
104 string_data, end_stream, | |
105 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | |
106 weak_factory_.GetWeakPtr())); | |
107 DCHECK(rv == OK || rv == ERR_IO_PENDING); | |
108 if (rv == OK) { | |
109 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
110 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | |
111 weak_factory_.GetWeakPtr(), OK)); | |
112 } | |
113 } | |
114 | |
115 void BidirectionalStreamQuicImpl::Cancel() { | |
116 if (stream_) { | |
117 stream_->SetDelegate(nullptr); | |
118 stream_->Reset(QUIC_STREAM_CANCELLED); | |
119 ResetStream(); | |
120 } | |
121 } | |
122 | |
123 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { | |
124 return negotiated_protocol_; | |
125 } | |
126 | |
127 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const { | |
128 if (stream_) | |
129 return headers_bytes_received_ + stream_->stream_bytes_read(); | |
130 return headers_bytes_received_ + closed_stream_received_bytes_; | |
131 } | |
132 | |
133 int64_t BidirectionalStreamQuicImpl::GetTotalSentBytes() const { | |
134 if (stream_) | |
135 return headers_bytes_sent_ + stream_->stream_bytes_written(); | |
136 return headers_bytes_sent_ + closed_stream_sent_bytes_; | |
137 } | |
138 | |
139 void BidirectionalStreamQuicImpl::OnHeadersAvailable( | |
140 const SpdyHeaderBlock& headers, | |
141 size_t frame_len) { | |
142 headers_bytes_received_ += frame_len; | |
143 negotiated_protocol_ = kProtoQUIC1SPDY3; | |
144 if (!has_received_headers_) { | |
145 has_received_headers_ = true; | |
146 delegate_->OnHeadersReceived(headers); | |
147 } else { | |
148 if (stream_->IsDoneReading()) { | |
149 // If the write side is closed, OnFinRead() will call | |
150 // BidirectionalStreamQuicImpl::OnClose(). | |
151 stream_->OnFinRead(); | |
152 } | |
153 delegate_->OnTrailersReceived(headers); | |
154 } | |
155 } | |
156 | |
157 void BidirectionalStreamQuicImpl::OnDataAvailable() { | |
158 // Return early if ReadData has not been called. | |
159 if (!read_buffer_) | |
160 return; | |
161 | |
162 CHECK(read_buffer_); | |
163 CHECK_NE(0, read_buffer_len_); | |
164 int rv = ReadData(read_buffer_.get(), read_buffer_len_); | |
165 if (rv == ERR_IO_PENDING) { | |
166 // Spurrious notification. Wait for the next one. | |
167 return; | |
168 } | |
169 read_buffer_ = nullptr; | |
170 read_buffer_len_ = 0; | |
171 delegate_->OnDataRead(rv); | |
172 } | |
173 | |
174 void BidirectionalStreamQuicImpl::OnClose(QuicErrorCode error) { | |
175 DCHECK(stream_); | |
176 if (error == QUIC_NO_ERROR && | |
177 stream_->stream_error() == QUIC_STREAM_NO_ERROR) { | |
178 ResetStream(); | |
179 return; | |
180 } | |
181 ResetStream(); | |
182 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR | |
183 : ERR_QUIC_HANDSHAKE_FAILED); | |
184 } | |
185 | |
186 void BidirectionalStreamQuicImpl::OnError(int error) { | |
187 NotifyError(error); | |
188 } | |
189 | |
190 bool BidirectionalStreamQuicImpl::HasSendHeadersComplete() { | |
191 return has_sent_headers_; | |
192 } | |
193 | |
194 void BidirectionalStreamQuicImpl::OnCryptoHandshakeConfirmed() { | |
195 was_handshake_confirmed_ = true; | |
196 } | |
197 | |
198 void BidirectionalStreamQuicImpl::OnSessionClosed(int error) { | |
199 DCHECK_NE(OK, error); | |
200 session_.reset(); | |
201 NotifyError(error); | |
202 } | |
203 | |
204 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { | |
205 DCHECK_NE(ERR_IO_PENDING, rv); | |
206 DCHECK(rv == OK || !stream_); | |
207 if (rv == OK) { | |
208 stream_->SetDelegate(this); | |
209 SendRequestHeaders(); | |
210 } else { | |
211 NotifyError(rv); | |
212 } | |
213 } | |
214 | |
215 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) { | |
216 DCHECK(rv == OK || !stream_); | |
217 if (rv == OK) { | |
218 delegate_->OnDataSent(); | |
219 } else { | |
220 NotifyError(rv); | |
221 } | |
222 } | |
223 | |
224 void BidirectionalStreamQuicImpl::SendRequestHeaders() { | |
225 DCHECK(!has_sent_headers_); | |
226 DCHECK(stream_); | |
227 | |
228 SpdyHeaderBlock headers; | |
229 HttpRequestInfo http_request_info; | |
230 http_request_info.url = request_info_->url; | |
231 http_request_info.method = request_info_->method; | |
232 http_request_info.extra_headers = request_info_->extra_headers; | |
233 | |
234 CreateSpdyHeadersFromHttpRequest(http_request_info, | |
235 http_request_info.extra_headers, HTTP2, true, | |
236 &headers); | |
237 size_t frame_len = stream_->WriteHeaders( | |
238 headers, request_info_->end_stream_on_headers, nullptr); | |
239 headers_bytes_sent_ += frame_len; | |
240 has_sent_headers_ = true; | |
241 delegate_->OnHeadersSent(); | |
242 } | |
243 | |
244 void BidirectionalStreamQuicImpl::NotifyError(int error) { | |
245 DCHECK_NE(OK, error); | |
246 DCHECK_NE(ERR_IO_PENDING, error); | |
247 | |
248 response_status_ = error; | |
249 ResetStream(); | |
250 delegate_->OnFailed(error); | |
251 } | |
252 | |
253 void BidirectionalStreamQuicImpl::ResetStream() { | |
254 if (!stream_) | |
255 return; | |
256 closed_stream_received_bytes_ = stream_->stream_bytes_read(); | |
257 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); | |
258 stream_->SetDelegate(nullptr); | |
259 stream_ = nullptr; | |
260 } | |
261 | |
262 } // namespace net | |
OLD | NEW |