| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/quic/quic_spdy_client_stream.h" | 5 #include "net/tools/quic/quic_spdy_client_stream.h" |
| 6 | 6 |
| 7 #include "net/spdy/spdy_framer.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "net/quic/spdy_utils.h" |
| 11 #include "net/spdy/spdy_protocol.h" |
| 8 #include "net/tools/quic/quic_client_session.h" | 12 #include "net/tools/quic/quic_client_session.h" |
| 9 #include "net/tools/quic/spdy_utils.h" | 13 #include "net/tools/quic/spdy_utils.h" |
| 10 | 14 |
| 11 using base::StringPiece; | 15 using base::StringPiece; |
| 12 using std::string; | 16 using std::string; |
| 17 using base::StringToInt; |
| 13 | 18 |
| 14 namespace net { | 19 namespace net { |
| 15 namespace tools { | 20 namespace tools { |
| 16 | 21 |
| 17 static const size_t kHeaderBufInitialSize = 4096; | |
| 18 | |
| 19 QuicSpdyClientStream::QuicSpdyClientStream(QuicStreamId id, | 22 QuicSpdyClientStream::QuicSpdyClientStream(QuicStreamId id, |
| 20 QuicClientSession* session) | 23 QuicClientSession* session) |
| 21 : QuicDataStream(id, session), | 24 : QuicDataStream(id, session), |
| 22 read_buf_(new GrowableIOBuffer()), | 25 content_length_(-1), |
| 23 response_headers_received_(false), | 26 response_code_(0), |
| 24 header_bytes_read_(0), | 27 header_bytes_read_(0), |
| 25 header_bytes_written_(0) { | 28 header_bytes_written_(0) { |
| 26 } | 29 } |
| 27 | 30 |
| 28 QuicSpdyClientStream::~QuicSpdyClientStream() { | 31 QuicSpdyClientStream::~QuicSpdyClientStream() { |
| 29 } | 32 } |
| 30 | 33 |
| 31 void QuicSpdyClientStream::OnStreamFrame(const QuicStreamFrame& frame) { | 34 void QuicSpdyClientStream::OnStreamFrame(const QuicStreamFrame& frame) { |
| 32 if (!write_side_closed()) { | 35 if (!write_side_closed()) { |
| 33 DVLOG(1) << "Got a response before the request was complete. " | 36 DVLOG(1) << "Got a response before the request was complete. " |
| 34 << "Aborting request."; | 37 << "Aborting request."; |
| 35 CloseWriteSide(); | 38 CloseWriteSide(); |
| 36 } | 39 } |
| 37 QuicDataStream::OnStreamFrame(frame); | 40 QuicDataStream::OnStreamFrame(frame); |
| 38 } | 41 } |
| 39 | 42 |
| 40 void QuicSpdyClientStream::OnStreamHeadersComplete(bool fin, | 43 void QuicSpdyClientStream::OnStreamHeadersComplete(bool fin, |
| 41 size_t frame_len) { | 44 size_t frame_len) { |
| 42 header_bytes_read_ = frame_len; | 45 header_bytes_read_ = frame_len; |
| 43 QuicDataStream::OnStreamHeadersComplete(fin, frame_len); | 46 QuicDataStream::OnStreamHeadersComplete(fin, frame_len); |
| 44 } | 47 } |
| 45 | 48 |
| 46 uint32 QuicSpdyClientStream::ProcessData(const char* data, | 49 uint32 QuicSpdyClientStream::ProcessData(const char* data, uint32 data_len) { |
| 47 uint32 data_len) { | 50 if (!headers_decompressed()) { |
| 48 int total_bytes_processed = 0; | 51 // Let the headers data accumulate in the underlying QuicDataStream. |
| 49 | 52 return 0; |
| 50 // Are we still reading the response headers. | 53 } |
| 51 if (!response_headers_received_) { | 54 if (response_headers_.empty()) { |
| 52 // Grow the read buffer if necessary. | 55 if (!ParseResponseHeaders(data, data_len)) { |
| 53 if (read_buf_->RemainingCapacity() < (int)data_len) { | 56 // Headers were invalid. |
| 54 read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize); | 57 Reset(QUIC_BAD_APPLICATION_PAYLOAD); |
| 58 return 0; |
| 55 } | 59 } |
| 56 memcpy(read_buf_->data(), data, data_len); | |
| 57 read_buf_->set_offset(read_buf_->offset() + data_len); | |
| 58 ParseResponseHeaders(); | |
| 59 } else { | 60 } else { |
| 60 data_.append(data + total_bytes_processed, | 61 data_.append(data, data_len); |
| 61 data_len - total_bytes_processed); | |
| 62 } | 62 } |
| 63 DCHECK(!response_headers_.empty()); |
| 64 if (content_length_ >= 0 && |
| 65 static_cast<int>(data_.size()) > content_length_) { |
| 66 Reset(QUIC_BAD_APPLICATION_PAYLOAD); |
| 67 return 0; |
| 68 } |
| 69 DVLOG(1) << "Client processed " << data_len << " bytes for stream " << id(); |
| 63 return data_len; | 70 return data_len; |
| 64 } | 71 } |
| 65 | 72 |
| 66 void QuicSpdyClientStream::OnFinRead() { | 73 bool QuicSpdyClientStream::ParseResponseHeaders(const char* data, |
| 67 ReliableQuicStream::OnFinRead(); | 74 uint32 data_len) { |
| 68 if (!response_headers_received_) { | 75 DCHECK(headers_decompressed()); |
| 69 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | 76 SpdyFramer framer(SPDY3); |
| 70 } else if ((headers().content_length_status() == | 77 size_t len = framer.ParseHeaderBlockInBuffer(data, |
| 71 BalsaHeadersEnums::VALID_CONTENT_LENGTH) && | 78 data_len, |
| 72 data_.size() != headers().content_length()) { | 79 &response_headers_); |
| 73 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | 80 DCHECK_LE(len, data_len); |
| 81 if (len == 0 || response_headers_.empty()) { |
| 82 return false; // Headers were invalid. |
| 74 } | 83 } |
| 84 |
| 85 if (data_len > len) { |
| 86 data_.append(data + len, data_len - len); |
| 87 } |
| 88 if (ContainsKey(response_headers_, "content-length") && |
| 89 !StringToInt(response_headers_["content-length"], &content_length_)) { |
| 90 return false; // Invalid content-length. |
| 91 } |
| 92 string status = response_headers_[":status"]; |
| 93 size_t end = status.find(" "); |
| 94 if (end != string::npos) { |
| 95 status.erase(end); |
| 96 } |
| 97 if (!StringToInt(status, &response_code_)) { |
| 98 return false; // Invalid response code. |
| 99 } |
| 100 return true; |
| 75 } | 101 } |
| 76 | 102 |
| 77 ssize_t QuicSpdyClientStream::SendRequest(const BalsaHeaders& headers, | 103 ssize_t QuicSpdyClientStream::SendRequest(const SpdyHeaderBlock& headers, |
| 78 StringPiece body, | 104 StringPiece body, |
| 79 bool fin) { | 105 bool fin) { |
| 80 SpdyHeaderBlock header_block = | |
| 81 SpdyUtils::RequestHeadersToSpdyHeaders(headers); | |
| 82 | |
| 83 bool send_fin_with_headers = fin && body.empty(); | 106 bool send_fin_with_headers = fin && body.empty(); |
| 84 size_t bytes_sent = body.size(); | 107 size_t bytes_sent = body.size(); |
| 85 header_bytes_written_ = | 108 header_bytes_written_ = |
| 86 WriteHeaders(header_block, send_fin_with_headers, nullptr); | 109 WriteHeaders(headers, send_fin_with_headers, nullptr); |
| 87 bytes_sent += header_bytes_written_; | 110 bytes_sent += header_bytes_written_; |
| 88 | 111 |
| 89 if (!body.empty()) { | 112 if (!body.empty()) { |
| 90 WriteOrBufferData(body, fin, nullptr); | 113 WriteOrBufferData(body, fin, nullptr); |
| 91 } | 114 } |
| 92 | 115 |
| 93 return bytes_sent; | 116 return bytes_sent; |
| 94 } | 117 } |
| 95 | 118 |
| 96 int QuicSpdyClientStream::ParseResponseHeaders() { | |
| 97 size_t read_buf_len = static_cast<size_t>(read_buf_->offset()); | |
| 98 SpdyFramer framer(SPDY3); | |
| 99 SpdyHeaderBlock headers; | |
| 100 char* data = read_buf_->StartOfBuffer(); | |
| 101 size_t len = framer.ParseHeaderBlockInBuffer(data, read_buf_->offset(), | |
| 102 &headers); | |
| 103 if (len == 0) { | |
| 104 return -1; | |
| 105 } | |
| 106 | |
| 107 if (!SpdyUtils::FillBalsaResponseHeaders(headers, &headers_)) { | |
| 108 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | |
| 109 return -1; | |
| 110 } | |
| 111 response_headers_received_ = true; | |
| 112 | |
| 113 size_t delta = read_buf_len - len; | |
| 114 if (delta > 0) { | |
| 115 data_.append(data + len, delta); | |
| 116 } | |
| 117 | |
| 118 return len; | |
| 119 } | |
| 120 | |
| 121 void QuicSpdyClientStream::SendBody(const string& data, bool fin) { | 119 void QuicSpdyClientStream::SendBody(const string& data, bool fin) { |
| 122 SendBody(data, fin, nullptr); | 120 SendBody(data, fin, nullptr); |
| 123 } | 121 } |
| 124 | 122 |
| 125 void QuicSpdyClientStream::SendBody( | 123 void QuicSpdyClientStream::SendBody( |
| 126 const string& data, | 124 const string& data, bool fin, |
| 127 bool fin, | |
| 128 QuicAckNotifier::DelegateInterface* delegate) { | 125 QuicAckNotifier::DelegateInterface* delegate) { |
| 129 WriteOrBufferData(data, fin, delegate); | 126 WriteOrBufferData(data, fin, delegate); |
| 130 } | 127 } |
| 131 | 128 |
| 132 } // namespace tools | 129 } // namespace tools |
| 133 } // namespace net | 130 } // namespace net |
| OLD | NEW |