| OLD | NEW |
| (Empty) |
| 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/tools/quic/quic_simple_client_stream.h" | |
| 6 | |
| 7 #include "net/http/http_response_info.h" | |
| 8 #include "net/spdy/spdy_framer.h" | |
| 9 #include "net/spdy/spdy_http_utils.h" | |
| 10 #include "net/tools/quic/quic_simple_client_session.h" | |
| 11 | |
| 12 using base::StringPiece; | |
| 13 using std::string; | |
| 14 | |
| 15 namespace net { | |
| 16 namespace tools { | |
| 17 | |
| 18 static const size_t kHeaderBufInitialSize = 4096; | |
| 19 | |
| 20 QuicSimpleClientStream::QuicSimpleClientStream(QuicStreamId id, | |
| 21 QuicSimpleClientSession* session) | |
| 22 : QuicDataStream(id, session), | |
| 23 read_buf_(new GrowableIOBuffer()), | |
| 24 response_headers_received_(false), | |
| 25 header_bytes_read_(0), | |
| 26 header_bytes_written_(0) { | |
| 27 read_buf_->SetCapacity(kHeaderBufInitialSize); | |
| 28 } | |
| 29 | |
| 30 QuicSimpleClientStream::~QuicSimpleClientStream() { | |
| 31 } | |
| 32 | |
| 33 void QuicSimpleClientStream::OnStreamFrame(const QuicStreamFrame& frame) { | |
| 34 if (!write_side_closed()) { | |
| 35 DVLOG(1) << "Got a response before the request was complete. " | |
| 36 << "Aborting request."; | |
| 37 CloseWriteSide(); | |
| 38 } | |
| 39 QuicDataStream::OnStreamFrame(frame); | |
| 40 } | |
| 41 | |
| 42 void QuicSimpleClientStream::OnStreamHeadersComplete(bool fin, | |
| 43 size_t frame_len) { | |
| 44 header_bytes_read_ = frame_len; | |
| 45 QuicDataStream::OnStreamHeadersComplete(fin, frame_len); | |
| 46 } | |
| 47 | |
| 48 uint32 QuicSimpleClientStream::ProcessData(const char* data, | |
| 49 uint32 data_len) { | |
| 50 int total_bytes_processed = 0; | |
| 51 | |
| 52 // Are we still reading the response headers. | |
| 53 if (!response_headers_received_) { | |
| 54 // Grow the read buffer if necessary. | |
| 55 if (read_buf_->RemainingCapacity() < (int)data_len) { | |
| 56 read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize); | |
| 57 } | |
| 58 memcpy(read_buf_->data(), data, data_len); | |
| 59 read_buf_->set_offset(read_buf_->offset() + data_len); | |
| 60 ParseResponseHeaders(); | |
| 61 } else { | |
| 62 data_.append(data + total_bytes_processed, | |
| 63 data_len - total_bytes_processed); | |
| 64 } | |
| 65 return data_len; | |
| 66 } | |
| 67 | |
| 68 void QuicSimpleClientStream::OnFinRead() { | |
| 69 ReliableQuicStream::OnFinRead(); | |
| 70 if (!response_headers_received_) { | |
| 71 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | |
| 72 } else if (headers()->GetContentLength() != -1 && | |
| 73 data_.size() != | |
| 74 static_cast<size_t>(headers()->GetContentLength())) { | |
| 75 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 size_t QuicSimpleClientStream::SendRequest(const HttpRequestInfo& headers, | |
| 80 StringPiece body, | |
| 81 bool fin) { | |
| 82 SpdyHeaderBlock header_block; | |
| 83 CreateSpdyHeadersFromHttpRequest(headers, | |
| 84 headers.extra_headers, | |
| 85 SPDY3, | |
| 86 /*direct=*/ true, | |
| 87 &header_block); | |
| 88 | |
| 89 bool send_fin_with_headers = fin && body.empty(); | |
| 90 size_t bytes_sent = body.size(); | |
| 91 header_bytes_written_ = | |
| 92 WriteHeaders(header_block, send_fin_with_headers, nullptr); | |
| 93 bytes_sent += header_bytes_written_; | |
| 94 | |
| 95 if (!body.empty()) { | |
| 96 WriteOrBufferData(body, fin, nullptr); | |
| 97 } | |
| 98 | |
| 99 return bytes_sent; | |
| 100 } | |
| 101 | |
| 102 int QuicSimpleClientStream::ParseResponseHeaders() { | |
| 103 size_t read_buf_len = static_cast<size_t>(read_buf_->offset()); | |
| 104 SpdyFramer framer(SPDY3); | |
| 105 SpdyHeaderBlock headers; | |
| 106 char* data = read_buf_->StartOfBuffer(); | |
| 107 size_t len = framer.ParseHeaderBlockInBuffer(data, read_buf_->offset(), | |
| 108 &headers); | |
| 109 if (len == 0) { | |
| 110 return -1; | |
| 111 } | |
| 112 | |
| 113 HttpResponseInfo info; | |
| 114 if (!SpdyHeadersToHttpResponse(headers, SPDY3, &info)) { | |
| 115 Reset(QUIC_BAD_APPLICATION_PAYLOAD); | |
| 116 return -1; | |
| 117 } | |
| 118 headers_ = info.headers; | |
| 119 | |
| 120 response_headers_received_ = true; | |
| 121 | |
| 122 size_t delta = read_buf_len - len; | |
| 123 if (delta > 0) { | |
| 124 data_.append(data + len, delta); | |
| 125 } | |
| 126 | |
| 127 return static_cast<int>(len); | |
| 128 } | |
| 129 | |
| 130 void QuicSimpleClientStream::SendBody(const string& data, bool fin) { | |
| 131 SendBody(data, fin, nullptr); | |
| 132 } | |
| 133 | |
| 134 void QuicSimpleClientStream::SendBody( | |
| 135 const string& data, | |
| 136 bool fin, | |
| 137 QuicAckNotifier::DelegateInterface* delegate) { | |
| 138 WriteOrBufferData(data, fin, delegate); | |
| 139 } | |
| 140 | |
| 141 } // namespace tools | |
| 142 } // namespace net | |
| OLD | NEW |