[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [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 | |
| 5 | #include "net/quic/reliable_quic_stream.h" |
| 6 | |
| 7 | #include "net/quic/quic_session.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 8 | #include "net/quic/quic_spdy_decompressor.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 9 | #include "net/spdy/write_blocked_list.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 10 | |
| 11 | using base::StringPiece; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 12 | using std::min; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // This is somewhat arbitrary. It's possible, but unlikely, we will either fail |
| 19 | // to set a priority client-side, or cancel a stream before stripping the |
| 20 | // priority from the wire server-side. In either case, start out with a |
| 21 | // priority in the middle. |
| 22 | QuicPriority kDefaultPriority = 3; |
| 23 | |
| 24 | // Appends bytes from data into partial_data_buffer. Once partial_data_buffer |
| 25 | // reaches 4 bytes, copies the data into 'result' and clears |
| 26 | // partial_data_buffer. |
| 27 | // Returns the number of bytes consumed. |
| 28 | uint32 StripUint32(const char* data, uint32 data_len, |
| 29 | string* partial_data_buffer, |
| 30 | uint32* result) { |
| 31 | DCHECK_GT(4u, partial_data_buffer->length()); |
| 32 | size_t missing_size = 4 - partial_data_buffer->length(); |
| 33 | if (data_len < missing_size) { |
| 34 | StringPiece(data, data_len).AppendToString(partial_data_buffer); |
| 35 | return data_len; |
| 36 | } |
| 37 | StringPiece(data, missing_size).AppendToString(partial_data_buffer); |
| 38 | DCHECK_EQ(4u, partial_data_buffer->length()); |
| 39 | memcpy(result, partial_data_buffer->data(), 4); |
| 40 | partial_data_buffer->clear(); |
| 41 | return missing_size; |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 46 | ReliableQuicStream::ReliableQuicStream(QuicStreamId id, |
| 47 | QuicSession* session) |
| 48 | : sequencer_(this), |
| 49 | id_(id), |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 50 | session_(session), |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 51 | visitor_(NULL), |
| 52 | stream_bytes_read_(0), |
| 53 | stream_bytes_written_(0), |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 54 | headers_decompressed_(false), |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 55 | priority_(kDefaultPriority), |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 56 | headers_id_(0), |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 57 | decompression_failed_(false), |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 58 | stream_error_(QUIC_STREAM_NO_ERROR), |
| 59 | connection_error_(QUIC_NO_ERROR), |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 60 | read_side_closed_(false), |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 61 | write_side_closed_(false), |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 62 | priority_parsed_(false), |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 63 | fin_buffered_(false), |
| 64 | fin_sent_(false) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ReliableQuicStream::~ReliableQuicStream() { |
| 68 | } |
| 69 | |
| 70 | bool ReliableQuicStream::WillAcceptStreamFrame( |
| 71 | const QuicStreamFrame& frame) const { |
| 72 | if (read_side_closed_) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 73 | return true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 74 | } |
| 75 | if (frame.stream_id != id_) { |
| 76 | LOG(ERROR) << "Error!"; |
| 77 | return false; |
| 78 | } |
| 79 | return sequencer_.WillAcceptStreamFrame(frame); |
| 80 | } |
| 81 | |
| 82 | bool ReliableQuicStream::OnStreamFrame(const QuicStreamFrame& frame) { |
| 83 | DCHECK_EQ(frame.stream_id, id_); |
| 84 | if (read_side_closed_) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 85 | DLOG(INFO) << "Ignoring frame " << frame.stream_id; |
| 86 | // We don't want to be reading: blackhole the data. |
| 87 | return true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 88 | } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 89 | // Note: This count include duplicate data received. |
| 90 | stream_bytes_read_ += frame.data.length(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 91 | |
| 92 | bool accepted = sequencer_.OnStreamFrame(frame); |
| 93 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 94 | return accepted; |
| 95 | } |
| 96 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 97 | void ReliableQuicStream::OnStreamReset(QuicRstStreamErrorCode error) { |
| 98 | stream_error_ = error; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 99 | TerminateFromPeer(false); // Full close. |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void ReliableQuicStream::ConnectionClose(QuicErrorCode error, bool from_peer) { |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 103 | if (read_side_closed_ && write_side_closed_) { |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 104 | return; |
| 105 | } |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 106 | if (error != QUIC_NO_ERROR) { |
| 107 | stream_error_ = QUIC_STREAM_CONNECTION_ERROR; |
| 108 | connection_error_ = error; |
| 109 | } |
| 110 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 111 | if (from_peer) { |
| 112 | TerminateFromPeer(false); |
| 113 | } else { |
| 114 | CloseWriteSide(); |
| 115 | CloseReadSide(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void ReliableQuicStream::TerminateFromPeer(bool half_close) { |
| 120 | if (!half_close) { |
| 121 | CloseWriteSide(); |
| 122 | } |
| 123 | CloseReadSide(); |
| 124 | } |
| 125 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 126 | void ReliableQuicStream::Close(QuicRstStreamErrorCode error) { |
| 127 | stream_error_ = error; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 128 | if (error != QUIC_STREAM_NO_ERROR) { |
| 129 | // Sending a RstStream results in calling CloseStream. |
| 130 | session()->SendRstStream(id(), error); |
| 131 | } else { |
| 132 | session_->CloseStream(id()); |
| 133 | } |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | d6ce2eb | 2013-06-11 13:36:57 | [diff] [blame] | 136 | size_t ReliableQuicStream::Readv(const struct iovec* iov, size_t iov_len) { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 137 | if (headers_decompressed_ && decompressed_headers_.empty()) { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 138 | return sequencer_.Readv(iov, iov_len); |
| 139 | } |
| 140 | size_t bytes_consumed = 0; |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 141 | size_t iov_index = 0; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 142 | while (iov_index < iov_len && |
| 143 | decompressed_headers_.length() > bytes_consumed) { |
[email protected] | d6ce2eb | 2013-06-11 13:36:57 | [diff] [blame] | 144 | size_t bytes_to_read = min(iov[iov_index].iov_len, |
| 145 | decompressed_headers_.length() - bytes_consumed); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 146 | char* iov_ptr = static_cast<char*>(iov[iov_index].iov_base); |
| 147 | memcpy(iov_ptr, |
| 148 | decompressed_headers_.data() + bytes_consumed, bytes_to_read); |
| 149 | bytes_consumed += bytes_to_read; |
| 150 | ++iov_index; |
| 151 | } |
| 152 | decompressed_headers_.erase(0, bytes_consumed); |
| 153 | return bytes_consumed; |
| 154 | } |
| 155 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 156 | int ReliableQuicStream::GetReadableRegions(iovec* iov, size_t iov_len) { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 157 | if (headers_decompressed_ && decompressed_headers_.empty()) { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 158 | return sequencer_.GetReadableRegions(iov, iov_len); |
| 159 | } |
| 160 | if (iov_len == 0) { |
| 161 | return 0; |
| 162 | } |
| 163 | iov[0].iov_base = static_cast<void*>( |
| 164 | const_cast<char*>(decompressed_headers_.data())); |
| 165 | iov[0].iov_len = decompressed_headers_.length(); |
| 166 | return 1; |
| 167 | } |
| 168 | |
[email protected] | 2ff600a | 2012-11-11 19:22:19 | [diff] [blame] | 169 | bool ReliableQuicStream::IsHalfClosed() const { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 170 | if (!headers_decompressed_ || !decompressed_headers_.empty()) { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 171 | return false; |
| 172 | } |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 173 | return sequencer_.IsHalfClosed(); |
| 174 | } |
| 175 | |
[email protected] | 2ff600a | 2012-11-11 19:22:19 | [diff] [blame] | 176 | bool ReliableQuicStream::HasBytesToRead() const { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 177 | return !decompressed_headers_.empty() || sequencer_.HasBytesToRead(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 180 | const IPEndPoint& ReliableQuicStream::GetPeerAddress() const { |
| 181 | return session_->peer_address(); |
| 182 | } |
| 183 | |
[email protected] | 821555c | 2013-05-16 20:20:17 | [diff] [blame] | 184 | QuicSpdyCompressor* ReliableQuicStream::compressor() { |
| 185 | return session_->compressor(); |
| 186 | } |
| 187 | |
[email protected] | a69af052 | 2013-07-12 19:23:47 | [diff] [blame] | 188 | bool ReliableQuicStream::GetSSLInfo(SSLInfo* ssl_info) { |
| 189 | return session_->GetSSLInfo(ssl_info); |
| 190 | } |
| 191 | |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 192 | QuicConsumedData ReliableQuicStream::WriteData(StringPiece data, bool fin) { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 193 | DCHECK(data.size() > 0 || fin); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 194 | return WriteOrBuffer(data, fin); |
| 195 | } |
| 196 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 197 | |
| 198 | void ReliableQuicStream::set_priority(QuicPriority priority) { |
| 199 | DCHECK_EQ(0u, stream_bytes_written_); |
| 200 | priority_ = priority; |
| 201 | } |
| 202 | |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 203 | QuicConsumedData ReliableQuicStream::WriteOrBuffer(StringPiece data, bool fin) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 204 | DCHECK(!fin_buffered_); |
| 205 | |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 206 | QuicConsumedData consumed_data(0, false); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 207 | fin_buffered_ = fin; |
| 208 | |
| 209 | if (queued_data_.empty()) { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 210 | consumed_data = WriteDataInternal(string(data.data(), data.length()), fin); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 211 | DCHECK_LE(consumed_data.bytes_consumed, data.length()); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 212 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 213 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 214 | // If there's unconsumed data or an unconsumed fin, queue it. |
| 215 | if (consumed_data.bytes_consumed < data.length() || |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 216 | (fin && !consumed_data.fin_consumed)) { |
| 217 | queued_data_.push_back( |
| 218 | string(data.data() + consumed_data.bytes_consumed, |
| 219 | data.length() - consumed_data.bytes_consumed)); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 220 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 221 | |
| 222 | return QuicConsumedData(data.size(), true); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void ReliableQuicStream::OnCanWrite() { |
| 226 | bool fin = false; |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 227 | while (!queued_data_.empty()) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 228 | const string& data = queued_data_.front(); |
| 229 | if (queued_data_.size() == 1 && fin_buffered_) { |
| 230 | fin = true; |
| 231 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 232 | QuicConsumedData consumed_data = WriteDataInternal(data, fin); |
| 233 | if (consumed_data.bytes_consumed == data.size() && |
| 234 | fin == consumed_data.fin_consumed) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 235 | queued_data_.pop_front(); |
| 236 | } else { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 237 | queued_data_.front().erase(0, consumed_data.bytes_consumed); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 238 | break; |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 243 | QuicConsumedData ReliableQuicStream::WriteDataInternal( |
| 244 | StringPiece data, bool fin) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 245 | struct iovec iov = {const_cast<char*>(data.data()), |
| 246 | static_cast<size_t>(data.size())}; |
| 247 | return WritevDataInternal(&iov, 1, fin); |
| 248 | } |
| 249 | |
| 250 | QuicConsumedData ReliableQuicStream::WritevDataInternal(const struct iovec* iov, |
| 251 | int count, |
| 252 | bool fin) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 253 | if (write_side_closed_) { |
| 254 | DLOG(ERROR) << "Attempt to write when the write side is closed"; |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 255 | return QuicConsumedData(0, false); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 258 | size_t write_length = 0u; |
| 259 | for (int i = 0; i < count; ++i) { |
| 260 | write_length += iov[i].iov_len; |
| 261 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 262 | QuicConsumedData consumed_data = |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 263 | session()->WritevData(id(), iov, count, stream_bytes_written_, fin); |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 264 | stream_bytes_written_ += consumed_data.bytes_consumed; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 265 | if (consumed_data.bytes_consumed == write_length) { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 266 | if (fin && consumed_data.fin_consumed) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 267 | fin_sent_ = true; |
| 268 | CloseWriteSide(); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 269 | } else if (fin && !consumed_data.fin_consumed) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 270 | session_->MarkWriteBlocked(id(), EffectivePriority()); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 271 | } |
| 272 | } else { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 273 | session_->MarkWriteBlocked(id(), EffectivePriority()); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 274 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 275 | return consumed_data; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 276 | } |
| 277 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 278 | QuicPriority ReliableQuicStream::EffectivePriority() const { |
| 279 | return priority(); |
| 280 | } |
| 281 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 282 | void ReliableQuicStream::CloseReadSide() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 283 | if (read_side_closed_) { |
| 284 | return; |
| 285 | } |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 286 | DLOG(INFO) << "Done reading from stream " << id(); |
| 287 | |
| 288 | read_side_closed_ = true; |
| 289 | if (write_side_closed_) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 290 | DLOG(INFO) << "Closing stream: " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 291 | session_->CloseStream(id()); |
| 292 | } |
| 293 | } |
| 294 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 295 | uint32 ReliableQuicStream::ProcessRawData(const char* data, uint32 data_len) { |
| 296 | if (id() == kCryptoStreamId) { |
[email protected] | dbeea41 | 2013-07-16 17:38:46 | [diff] [blame] | 297 | if (data_len == 0) { |
| 298 | return 0; |
| 299 | } |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 300 | // The crypto stream does not use compression. |
| 301 | return ProcessData(data, data_len); |
| 302 | } |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 303 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 304 | uint32 total_bytes_consumed = 0; |
| 305 | if (headers_id_ == 0u) { |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 306 | total_bytes_consumed += StripPriorityAndHeaderId(data, data_len); |
| 307 | data += total_bytes_consumed; |
| 308 | data_len -= total_bytes_consumed; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 309 | if (data_len == 0 || !session_->connection()->connected()) { |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 310 | return total_bytes_consumed; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 311 | } |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 312 | } |
| 313 | DCHECK_NE(0u, headers_id_); |
| 314 | |
| 315 | // Once the headers are finished, we simply pass the data through. |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 316 | if (headers_decompressed_) { |
| 317 | // Some buffered header data remains. |
| 318 | if (!decompressed_headers_.empty()) { |
| 319 | ProcessHeaderData(); |
| 320 | } |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 321 | if (decompressed_headers_.empty()) { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 322 | DVLOG(1) << "Delegating procesing to ProcessData"; |
| 323 | total_bytes_consumed += ProcessData(data, data_len); |
| 324 | } |
| 325 | return total_bytes_consumed; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | QuicHeaderId current_header_id = |
| 329 | session_->decompressor()->current_header_id(); |
| 330 | // Ensure that this header id looks sane. |
| 331 | if (headers_id_ < current_header_id || |
| 332 | headers_id_ > kMaxHeaderIdDelta + current_header_id) { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 333 | DVLOG(1) << "Invalid headers for stream: " << id() |
| 334 | << " header_id: " << headers_id_ |
| 335 | << " current_header_id: " << current_header_id; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 336 | session_->connection()->SendConnectionClose(QUIC_INVALID_HEADER_ID); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 337 | return total_bytes_consumed; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // If we are head-of-line blocked on decompression, then back up. |
| 341 | if (current_header_id != headers_id_) { |
| 342 | session_->MarkDecompressionBlocked(headers_id_, id()); |
[email protected] | 821555c | 2013-05-16 20:20:17 | [diff] [blame] | 343 | DVLOG(1) << "Unable to decompress header data for stream: " << id() |
| 344 | << " header_id: " << headers_id_; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 345 | return total_bytes_consumed; |
| 346 | } |
| 347 | |
| 348 | // Decompressed data will be delivered to decompressed_headers_. |
| 349 | size_t bytes_consumed = session_->decompressor()->DecompressData( |
| 350 | StringPiece(data, data_len), this); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 351 | DCHECK_NE(0u, bytes_consumed); |
| 352 | if (bytes_consumed > data_len) { |
| 353 | DCHECK(false) << "DecompressData returned illegal value"; |
| 354 | OnDecompressionError(); |
| 355 | return total_bytes_consumed; |
| 356 | } |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 357 | total_bytes_consumed += bytes_consumed; |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 358 | data += bytes_consumed; |
| 359 | data_len -= bytes_consumed; |
| 360 | |
| 361 | if (decompression_failed_) { |
| 362 | // The session will have been closed in OnDecompressionError. |
| 363 | return total_bytes_consumed; |
| 364 | } |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 365 | |
| 366 | // Headers are complete if the decompressor has moved on to the |
| 367 | // next stream. |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 368 | headers_decompressed_ = |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 369 | session_->decompressor()->current_header_id() != headers_id_; |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 370 | if (!headers_decompressed_) { |
| 371 | DCHECK_EQ(0u, data_len); |
| 372 | } |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 373 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 374 | ProcessHeaderData(); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 375 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 376 | if (!headers_decompressed_ || !decompressed_headers_.empty()) { |
| 377 | return total_bytes_consumed; |
| 378 | } |
| 379 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 380 | // We have processed all of the decompressed data but we might |
| 381 | // have some more raw data to process. |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 382 | if (data_len > 0) { |
| 383 | total_bytes_consumed += ProcessData(data, data_len); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // The sequencer will push any additional buffered frames if this data |
| 387 | // has been completely consumed. |
| 388 | return total_bytes_consumed; |
| 389 | } |
| 390 | |
| 391 | uint32 ReliableQuicStream::ProcessHeaderData() { |
| 392 | if (decompressed_headers_.empty()) { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | size_t bytes_processed = ProcessData(decompressed_headers_.data(), |
| 397 | decompressed_headers_.length()); |
| 398 | if (bytes_processed == decompressed_headers_.length()) { |
| 399 | decompressed_headers_.clear(); |
| 400 | } else { |
| 401 | decompressed_headers_ = decompressed_headers_.erase(0, bytes_processed); |
| 402 | } |
| 403 | return bytes_processed; |
| 404 | } |
| 405 | |
| 406 | void ReliableQuicStream::OnDecompressorAvailable() { |
| 407 | DCHECK_EQ(headers_id_, |
| 408 | session_->decompressor()->current_header_id()); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 409 | DCHECK(!headers_decompressed_); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 410 | DCHECK(!decompression_failed_); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 411 | DCHECK_EQ(0u, decompressed_headers_.length()); |
| 412 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 413 | while (!headers_decompressed_) { |
[email protected] | 8fb9adb | 2013-07-28 14:04:37 | [diff] [blame] | 414 | struct iovec iovec; |
| 415 | if (sequencer_.GetReadableRegions(&iovec, 1) == 0) { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 416 | return; |
| 417 | } |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 418 | |
[email protected] | 8fb9adb | 2013-07-28 14:04:37 | [diff] [blame] | 419 | size_t bytes_consumed = session_->decompressor()->DecompressData( |
| 420 | StringPiece(static_cast<char*>(iovec.iov_base), |
| 421 | iovec.iov_len), |
| 422 | this); |
| 423 | DCHECK_LE(bytes_consumed, iovec.iov_len); |
| 424 | if (decompression_failed_) { |
| 425 | return; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 426 | } |
[email protected] | 8fb9adb | 2013-07-28 14:04:37 | [diff] [blame] | 427 | sequencer_.MarkConsumed(bytes_consumed); |
| 428 | |
| 429 | headers_decompressed_ = |
| 430 | session_->decompressor()->current_header_id() != headers_id_; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Either the headers are complete, or the all data as been consumed. |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 434 | ProcessHeaderData(); // Unprocessed headers remain in decompressed_headers_. |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 435 | if (IsHalfClosed()) { |
| 436 | TerminateFromPeer(true); |
| 437 | } else if (headers_decompressed_ && decompressed_headers_.empty()) { |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 438 | sequencer_.FlushBufferedFrames(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | bool ReliableQuicStream::OnDecompressedData(StringPiece data) { |
| 443 | data.AppendToString(&decompressed_headers_); |
| 444 | return true; |
| 445 | } |
| 446 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 447 | void ReliableQuicStream::OnDecompressionError() { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 448 | DCHECK(!decompression_failed_); |
| 449 | decompression_failed_ = true; |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 450 | session_->connection()->SendConnectionClose(QUIC_DECOMPRESSION_FAILURE); |
| 451 | } |
| 452 | |
| 453 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 454 | void ReliableQuicStream::CloseWriteSide() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 455 | if (write_side_closed_) { |
| 456 | return; |
| 457 | } |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 458 | DLOG(INFO) << "Done writing to stream " << id(); |
| 459 | |
| 460 | write_side_closed_ = true; |
| 461 | if (read_side_closed_) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 462 | DLOG(INFO) << "Closing stream: " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 463 | session_->CloseStream(id()); |
| 464 | } |
| 465 | } |
| 466 | |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 467 | bool ReliableQuicStream::HasBufferedData() { |
| 468 | return !queued_data_.empty(); |
| 469 | } |
| 470 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 471 | void ReliableQuicStream::OnClose() { |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 472 | CloseReadSide(); |
| 473 | CloseWriteSide(); |
| 474 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 475 | if (visitor_) { |
| 476 | Visitor* visitor = visitor_; |
| 477 | // Calling Visitor::OnClose() may result the destruction of the visitor, |
| 478 | // so we need to ensure we don't call it again. |
| 479 | visitor_ = NULL; |
| 480 | visitor->OnClose(this); |
| 481 | } |
| 482 | } |
| 483 | |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 484 | uint32 ReliableQuicStream::StripPriorityAndHeaderId( |
| 485 | const char* data, uint32 data_len) { |
| 486 | uint32 total_bytes_parsed = 0; |
| 487 | |
| 488 | if (!priority_parsed_ && |
| 489 | session_->connection()->version() >= QUIC_VERSION_9 && |
| 490 | session_->connection()->is_server()) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 491 | QuicPriority temporary_priority = priority_; |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 492 | total_bytes_parsed = StripUint32( |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 493 | data, data_len, &headers_id_and_priority_buffer_, &temporary_priority); |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 494 | if (total_bytes_parsed > 0 && headers_id_and_priority_buffer_.size() == 0) { |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 495 | priority_parsed_ = true; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 496 | // Spdy priorities are inverted, so the highest numerical value is the |
| 497 | // lowest legal priority. |
| 498 | if (temporary_priority > static_cast<QuicPriority>(kLowestPriority)) { |
| 499 | session_->connection()->SendConnectionClose(QUIC_INVALID_PRIORITY); |
| 500 | return 0; |
| 501 | } |
| 502 | priority_ = temporary_priority; |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 503 | } |
| 504 | data += total_bytes_parsed; |
| 505 | data_len -= total_bytes_parsed; |
| 506 | } |
| 507 | if (data_len > 0 && headers_id_ == 0u) { |
| 508 | // The headers ID has not yet been read. Strip it from the beginning of |
| 509 | // the data stream. |
| 510 | total_bytes_parsed += StripUint32( |
| 511 | data, data_len, &headers_id_and_priority_buffer_, &headers_id_); |
| 512 | } |
| 513 | return total_bytes_parsed; |
| 514 | } |
| 515 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 516 | } // namespace net |