base: Make it possible to replace the MessageLoop's task runner
[chromium-blink-merge.git] / net / tools / quic / quic_spdy_client_stream.cc
blobeffb619e737bce1c14c58bcc9eb653ef04a6d1d8
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.
5 #include "net/tools/quic/quic_spdy_client_stream.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"
12 #include "net/tools/quic/quic_client_session.h"
13 #include "net/tools/quic/spdy_balsa_utils.h"
15 using base::StringPiece;
16 using std::string;
17 using base::StringToInt;
19 namespace net {
20 namespace tools {
22 QuicSpdyClientStream::QuicSpdyClientStream(QuicStreamId id,
23 QuicClientSession* session)
24 : QuicDataStream(id, session),
25 content_length_(-1),
26 response_code_(0),
27 header_bytes_read_(0),
28 header_bytes_written_(0) {
31 QuicSpdyClientStream::~QuicSpdyClientStream() {
34 void QuicSpdyClientStream::OnStreamFrame(const QuicStreamFrame& frame) {
35 if (!write_side_closed()) {
36 DVLOG(1) << "Got a response before the request was complete. "
37 << "Aborting request.";
38 CloseWriteSide();
40 QuicDataStream::OnStreamFrame(frame);
43 void QuicSpdyClientStream::OnStreamHeadersComplete(bool fin,
44 size_t frame_len) {
45 header_bytes_read_ = frame_len;
46 QuicDataStream::OnStreamHeadersComplete(fin, frame_len);
47 if (!ParseResponseHeaders(decompressed_headers().data(),
48 decompressed_headers().length())) {
49 Reset(QUIC_BAD_APPLICATION_PAYLOAD);
50 return;
52 MarkHeadersConsumed(decompressed_headers().length());
55 uint32 QuicSpdyClientStream::ProcessData(const char* data, uint32 data_len) {
56 data_.append(data, data_len);
57 DCHECK(!response_headers_.empty());
58 if (content_length_ >= 0 &&
59 static_cast<int>(data_.size()) > content_length_) {
60 Reset(QUIC_BAD_APPLICATION_PAYLOAD);
61 return 0;
63 DVLOG(1) << "Client processed " << data_len << " bytes for stream " << id();
64 return data_len;
67 bool QuicSpdyClientStream::ParseResponseHeaders(const char* data,
68 uint32 data_len) {
69 DCHECK(headers_decompressed());
70 SpdyFramer framer(SPDY3);
71 size_t len = framer.ParseHeaderBlockInBuffer(data,
72 data_len,
73 &response_headers_);
74 DCHECK_LE(len, data_len);
75 if (len == 0 || response_headers_.empty()) {
76 return false; // Headers were invalid.
79 if (data_len > len) {
80 data_.append(data + len, data_len - len);
82 if (ContainsKey(response_headers_, "content-length") &&
83 !StringToInt(response_headers_["content-length"], &content_length_)) {
84 return false; // Invalid content-length.
86 string status = response_headers_[":status"];
87 size_t end = status.find(" ");
88 if (end != string::npos) {
89 status.erase(end);
91 if (!StringToInt(status, &response_code_)) {
92 return false; // Invalid response code.
94 return true;
97 size_t QuicSpdyClientStream::SendRequest(const SpdyHeaderBlock& headers,
98 StringPiece body,
99 bool fin) {
100 bool send_fin_with_headers = fin && body.empty();
101 size_t bytes_sent = body.size();
102 header_bytes_written_ =
103 WriteHeaders(headers, send_fin_with_headers, nullptr);
104 bytes_sent += header_bytes_written_;
106 if (!body.empty()) {
107 WriteOrBufferData(body, fin, nullptr);
110 return bytes_sent;
113 void QuicSpdyClientStream::SendBody(const string& data, bool fin) {
114 SendBody(data, fin, nullptr);
117 void QuicSpdyClientStream::SendBody(
118 const string& data, bool fin,
119 QuicAckNotifier::DelegateInterface* delegate) {
120 WriteOrBufferData(data, fin, delegate);
123 } // namespace tools
124 } // namespace net