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