| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "net/quic/spdy_utils.h" | 10 #include "net/quic/spdy_utils.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 << "Aborting request."; | 37 << "Aborting request."; |
| 38 CloseWriteSide(); | 38 CloseWriteSide(); |
| 39 } | 39 } |
| 40 QuicSpdyStream::OnStreamFrame(frame); | 40 QuicSpdyStream::OnStreamFrame(frame); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void QuicSpdyClientStream::OnStreamHeadersComplete(bool fin, | 43 void QuicSpdyClientStream::OnStreamHeadersComplete(bool fin, |
| 44 size_t frame_len) { | 44 size_t frame_len) { |
| 45 header_bytes_read_ = frame_len; | 45 header_bytes_read_ = frame_len; |
| 46 QuicSpdyStream::OnStreamHeadersComplete(fin, frame_len); | 46 QuicSpdyStream::OnStreamHeadersComplete(fin, frame_len); |
| 47 if (!ParseResponseHeaders(decompressed_headers().data(), | 47 if (!SpdyUtils::ParseHeaders(decompressed_headers().data(), |
| 48 decompressed_headers().length())) { | 48 decompressed_headers().length(), |
| 49 &content_length_, &response_headers_)) { |
| 49 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | 50 Reset(QUIC_BAD_APPLICATION_PAYLOAD); |
| 50 return; | 51 return; |
| 51 } | 52 } |
| 53 |
| 54 string status = response_headers_[":status"].as_string(); |
| 55 size_t end = status.find(" "); |
| 56 if (end != string::npos) { |
| 57 status.erase(end); |
| 58 } |
| 59 if (!StringToInt(status, &response_code_)) { |
| 60 // Invalid response code. |
| 61 Reset(QUIC_BAD_APPLICATION_PAYLOAD); |
| 62 return; |
| 63 } |
| 64 |
| 52 MarkHeadersConsumed(decompressed_headers().length()); | 65 MarkHeadersConsumed(decompressed_headers().length()); |
| 53 } | 66 } |
| 54 | 67 |
| 55 void QuicSpdyClientStream::OnDataAvailable() { | 68 void QuicSpdyClientStream::OnDataAvailable() { |
| 56 while (HasBytesToRead()) { | 69 while (HasBytesToRead()) { |
| 57 struct iovec iov; | 70 struct iovec iov; |
| 58 if (GetReadableRegions(&iov, 1) == 0) { | 71 if (GetReadableRegions(&iov, 1) == 0) { |
| 59 // No more data to read. | 72 // No more data to read. |
| 60 break; | 73 break; |
| 61 } | 74 } |
| 62 DVLOG(1) << "Client processed " << iov.iov_len << " bytes for stream " | 75 DVLOG(1) << "Client processed " << iov.iov_len << " bytes for stream " |
| 63 << id(); | 76 << id(); |
| 64 data_.append(static_cast<char*>(iov.iov_base), iov.iov_len); | 77 data_.append(static_cast<char*>(iov.iov_base), iov.iov_len); |
| 65 | 78 |
| 66 if (content_length_ >= 0 && | 79 if (content_length_ >= 0 && |
| 67 static_cast<int>(data_.size()) > content_length_) { | 80 static_cast<int>(data_.size()) > content_length_) { |
| 68 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | 81 Reset(QUIC_BAD_APPLICATION_PAYLOAD); |
| 69 return; | 82 return; |
| 70 } | 83 } |
| 71 MarkConsumed(iov.iov_len); | 84 MarkConsumed(iov.iov_len); |
| 72 } | 85 } |
| 73 if (sequencer()->IsClosed()) { | 86 if (sequencer()->IsClosed()) { |
| 74 OnFinRead(); | 87 OnFinRead(); |
| 75 } else { | 88 } else { |
| 76 sequencer()->SetUnblocked(); | 89 sequencer()->SetUnblocked(); |
| 77 } | 90 } |
| 78 } | 91 } |
| 79 | 92 |
| 80 bool QuicSpdyClientStream::ParseResponseHeaders(const char* data, | |
| 81 uint32 data_len) { | |
| 82 DCHECK(headers_decompressed()); | |
| 83 SpdyFramer framer(HTTP2); | |
| 84 if (!framer.ParseHeaderBlockInBuffer(data, data_len, &response_headers_) || | |
| 85 response_headers_.empty()) { | |
| 86 return false; // Headers were invalid. | |
| 87 } | |
| 88 | |
| 89 if (ContainsKey(response_headers_, "content-length") && | |
| 90 !StringToInt(StringPiece(response_headers_["content-length"]), | |
| 91 &content_length_)) { | |
| 92 return false; // Invalid content-length. | |
| 93 } | |
| 94 string status = response_headers_[":status"].as_string(); | |
| 95 size_t end = status.find(" "); | |
| 96 if (end != string::npos) { | |
| 97 status.erase(end); | |
| 98 } | |
| 99 if (!StringToInt(status, &response_code_)) { | |
| 100 return false; // Invalid response code. | |
| 101 } | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 size_t QuicSpdyClientStream::SendRequest(const SpdyHeaderBlock& headers, | 93 size_t QuicSpdyClientStream::SendRequest(const SpdyHeaderBlock& headers, |
| 106 StringPiece body, | 94 StringPiece body, |
| 107 bool fin) { | 95 bool fin) { |
| 108 bool send_fin_with_headers = fin && body.empty(); | 96 bool send_fin_with_headers = fin && body.empty(); |
| 109 size_t bytes_sent = body.size(); | 97 size_t bytes_sent = body.size(); |
| 110 header_bytes_written_ = | 98 header_bytes_written_ = |
| 111 WriteHeaders(headers, send_fin_with_headers, nullptr); | 99 WriteHeaders(headers, send_fin_with_headers, nullptr); |
| 112 bytes_sent += header_bytes_written_; | 100 bytes_sent += header_bytes_written_; |
| 113 | 101 |
| 114 if (!body.empty()) { | 102 if (!body.empty()) { |
| 115 WriteOrBufferData(body, fin, nullptr); | 103 WriteOrBufferData(body, fin, nullptr); |
| 116 } | 104 } |
| 117 | 105 |
| 118 return bytes_sent; | 106 return bytes_sent; |
| 119 } | 107 } |
| 120 | 108 |
| 121 void QuicSpdyClientStream::SendBody(const string& data, bool fin) { | 109 void QuicSpdyClientStream::SendBody(const string& data, bool fin) { |
| 122 SendBody(data, fin, nullptr); | 110 SendBody(data, fin, nullptr); |
| 123 } | 111 } |
| 124 | 112 |
| 125 void QuicSpdyClientStream::SendBody(const string& data, | 113 void QuicSpdyClientStream::SendBody(const string& data, |
| 126 bool fin, | 114 bool fin, |
| 127 QuicAckListenerInterface* listener) { | 115 QuicAckListenerInterface* listener) { |
| 128 WriteOrBufferData(data, fin, listener); | 116 WriteOrBufferData(data, fin, listener); |
| 129 } | 117 } |
| 130 | 118 |
| 131 } // namespace tools | 119 } // namespace tools |
| 132 } // namespace net | 120 } // namespace net |
| OLD | NEW |