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_server_stream.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_piece.h"
11 #include "net/quic/quic_data_stream.h"
12 #include "net/quic/quic_spdy_session.h"
13 #include "net/quic/spdy_utils.h"
14 #include "net/spdy/spdy_protocol.h"
15 #include "net/tools/quic/quic_in_memory_cache.h"
17 using base::StringPiece
;
18 using base::StringToInt
;
24 QuicSpdyServerStream::QuicSpdyServerStream(QuicStreamId id
,
25 QuicSpdySession
* session
)
26 : QuicDataStream(id
, session
), content_length_(-1) {
29 QuicSpdyServerStream::~QuicSpdyServerStream() {
32 void QuicSpdyServerStream::OnStreamHeadersComplete(bool fin
, size_t frame_len
) {
33 QuicDataStream::OnStreamHeadersComplete(fin
, frame_len
);
34 if (!ParseRequestHeaders(decompressed_headers().data(),
35 decompressed_headers().length())) {
36 // Headers were invalid.
39 MarkHeadersConsumed(decompressed_headers().length());
42 void QuicSpdyServerStream::OnDataAvailable() {
43 while (HasBytesToRead()) {
45 if (GetReadableRegions(&iov
, 1) == 0) {
46 // No more data to read.
49 DVLOG(1) << "Processed " << iov
.iov_len
<< " bytes for stream " << id();
50 body_
.append(static_cast<char*>(iov
.iov_base
), iov
.iov_len
);
52 if (content_length_
>= 0 &&
53 static_cast<int>(body_
.size()) > content_length_
) {
57 MarkConsumed(iov
.iov_len
);
59 if (!sequencer()->IsClosed()) {
60 sequencer()->SetUnblocked();
64 // If the sequencer is closed, then the all the body, including the fin,
68 if (write_side_closed() || fin_buffered()) {
72 if (request_headers_
.empty()) {
77 if (content_length_
> 0 &&
78 content_length_
!= static_cast<int>(body_
.size())) {
86 bool QuicSpdyServerStream::ParseRequestHeaders(const char* data
,
88 DCHECK(headers_decompressed());
89 SpdyFramer
framer(HTTP2
);
90 size_t len
= framer
.ParseHeaderBlockInBuffer(data
,
93 DCHECK_LE(len
, data_len
);
94 if (len
== 0 || request_headers_
.empty()) {
95 return false; // Headers were invalid.
99 body_
.append(data
+ len
, data_len
- len
);
101 if (ContainsKey(request_headers_
, "content-length")) {
102 // Historically, if an input to SimpleAtoi contained null byte, anything
103 // past it would be silently ignored. This behavior is being removed, but
104 // this method relies on it (see cl/101239633). Hence, we explicitly call
105 // c_str() on request headers to simulate the old behavior.
106 // TODO(rch): Correctly handle null-separated value in content-length.
108 StringPiece
trimmed_header(request_headers_
["content-length"].c_str());
109 if (!StringToInt(trimmed_header
, &content_length_
)) {
110 return false; // Invalid content-length.
116 void QuicSpdyServerStream::SendResponse() {
117 if (!ContainsKey(request_headers_
, GetHostKey()) ||
118 !ContainsKey(request_headers_
, ":path")) {
123 // Find response in cache. If not found, send error response.
124 const QuicInMemoryCache::Response
* response
=
125 QuicInMemoryCache::GetInstance()->GetResponse(
126 request_headers_
[GetHostKey()], request_headers_
[":path"]);
127 if (response
== nullptr) {
132 if (response
->response_type() == QuicInMemoryCache::CLOSE_CONNECTION
) {
133 DVLOG(1) << "Special response: closing connection.";
134 CloseConnection(QUIC_NO_ERROR
);
138 if (response
->response_type() == QuicInMemoryCache::IGNORE_REQUEST
) {
139 DVLOG(1) << "Special response: ignoring request.";
143 DVLOG(1) << "Sending response for stream " << id();
144 if (version() > QUIC_VERSION_24
) {
146 SpdyUtils::ConvertSpdy3ResponseHeadersToSpdy4(response
->headers()),
151 SendHeadersAndBody(response
->headers(), response
->body());
154 void QuicSpdyServerStream::SendErrorResponse() {
155 DVLOG(1) << "Sending error response for stream " << id();
156 SpdyHeaderBlock headers
;
157 if (version() <= QUIC_VERSION_24
) {
158 headers
[":version"] = "HTTP/1.1";
159 headers
[":status"] = "500 Server Error";
161 headers
[":status"] = "500";
163 headers
["content-length"] = "3";
164 SendHeadersAndBody(headers
, "bad");
167 void QuicSpdyServerStream::SendHeadersAndBody(
168 const SpdyHeaderBlock
& response_headers
,
170 // We only support SPDY and HTTP, and neither handles bidirectional streaming.
171 if (!read_side_closed()) {
175 WriteHeaders(response_headers
, body
.empty(), nullptr);
178 WriteOrBufferData(body
, true, nullptr);
182 const string
QuicSpdyServerStream::GetHostKey() {
183 // SPDY/4 uses ":authority" instead of ":host".
184 return version() > QUIC_VERSION_24
? ":authority" : ":host";