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_server_stream.h" | 5 #include "net/tools/quic/quic_spdy_server_stream.h" |
6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" |
7 #include "net/quic/quic_session.h" | 8 #include "net/quic/quic_session.h" |
8 #include "net/spdy/spdy_framer.h" | 9 #include "net/spdy/spdy_framer.h" |
9 #include "net/tools/quic/quic_in_memory_cache.h" | 10 #include "net/tools/quic/quic_in_memory_cache.h" |
10 #include "net/tools/quic/spdy_utils.h" | 11 #include "net/tools/quic/spdy_utils.h" |
11 #include "url/gurl.h" | 12 #include "url/gurl.h" |
12 | 13 |
13 using base::StringPiece; | 14 using base::StringPiece; |
14 using std::string; | 15 using std::string; |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 namespace tools { | 18 namespace tools { |
18 | 19 |
19 static const size_t kHeaderBufInitialSize = 4096; | |
20 | |
21 QuicSpdyServerStream::QuicSpdyServerStream(QuicStreamId id, | 20 QuicSpdyServerStream::QuicSpdyServerStream(QuicStreamId id, |
22 QuicSession* session) | 21 QuicSession* session) |
23 : QuicDataStream(id, session), | 22 : QuicDataStream(id, session), |
24 read_buf_(new GrowableIOBuffer()), | 23 content_length_(-1) { |
25 request_headers_received_(false) { | |
26 } | 24 } |
27 | 25 |
28 QuicSpdyServerStream::~QuicSpdyServerStream() { | 26 QuicSpdyServerStream::~QuicSpdyServerStream() { |
29 } | 27 } |
30 | 28 |
31 uint32 QuicSpdyServerStream::ProcessData(const char* data, uint32 data_len) { | 29 uint32 QuicSpdyServerStream::ProcessData(const char* data, uint32 data_len) { |
32 uint32 total_bytes_processed = 0; | 30 if (!headers_decompressed()) { |
33 | 31 // Let the headers data accumulate in the underlying QuicDataStream. |
34 // Are we still reading the request headers. | 32 return 0; |
35 if (!request_headers_received_) { | 33 } |
36 // Grow the read buffer if necessary. | 34 if (request_headers_.empty()) { |
37 if (read_buf_->RemainingCapacity() < (int)data_len) { | 35 if (!ParseRequestHeaders(data, data_len)) { |
38 read_buf_->SetCapacity(read_buf_->capacity() + kHeaderBufInitialSize); | 36 // Headers were invalid. |
| 37 SendErrorResponse(); |
| 38 return 0; |
39 } | 39 } |
40 memcpy(read_buf_->data(), data, data_len); | |
41 read_buf_->set_offset(read_buf_->offset() + data_len); | |
42 ParseRequestHeaders(); | |
43 } else { | 40 } else { |
44 body_.append(data + total_bytes_processed, | 41 body_.append(data, data_len); |
45 data_len - total_bytes_processed); | |
46 } | 42 } |
| 43 DCHECK(!request_headers_.empty()); |
| 44 if (content_length_ > 0 && static_cast<int>(body_.size()) > content_length_) { |
| 45 SendErrorResponse(); |
| 46 return 0; |
| 47 } |
| 48 DVLOG(1) << "Processed " << data_len << " bytes for stream " << id(); |
47 return data_len; | 49 return data_len; |
48 } | 50 } |
49 | 51 |
50 void QuicSpdyServerStream::OnFinRead() { | 52 void QuicSpdyServerStream::OnFinRead() { |
51 ReliableQuicStream::OnFinRead(); | 53 ReliableQuicStream::OnFinRead(); |
52 if (write_side_closed() || fin_buffered()) { | 54 if (write_side_closed() || fin_buffered()) { |
53 return; | 55 return; |
54 } | 56 } |
55 | 57 |
56 if (!request_headers_received_) { | 58 if (request_headers_.empty()) { |
57 SendErrorResponse(); // We're not done reading headers. | |
58 } else if ((headers_.content_length_status() == | |
59 BalsaHeadersEnums::VALID_CONTENT_LENGTH) && | |
60 body_.size() != headers_.content_length()) { | |
61 SendErrorResponse(); // Invalid content length | |
62 } else { | |
63 SendResponse(); | |
64 } | |
65 } | |
66 | |
67 void QuicSpdyServerStream::ParseRequestHeaders() { | |
68 size_t read_buf_len = static_cast<size_t>(read_buf_->offset()); | |
69 SpdyFramer framer(SPDY3); | |
70 SpdyHeaderBlock headers; | |
71 char* data = read_buf_->StartOfBuffer(); | |
72 size_t len = framer.ParseHeaderBlockInBuffer(data, read_buf_->offset(), | |
73 &headers); | |
74 if (len == 0) { | |
75 return; | |
76 } | |
77 | |
78 if (!SpdyUtils::FillBalsaRequestHeaders(headers, &headers_)) { | |
79 SendErrorResponse(); | 59 SendErrorResponse(); |
80 return; | 60 return; |
81 } | 61 } |
82 | 62 |
83 size_t delta = read_buf_len - len; | 63 if (content_length_ > 0 && |
84 if (delta > 0) { | 64 content_length_ != static_cast<int>(body_.size())) { |
85 body_.append(data + len, delta); | 65 SendErrorResponse(); |
| 66 return; |
86 } | 67 } |
87 | 68 |
88 request_headers_received_ = true; | 69 SendResponse(); |
| 70 } |
| 71 |
| 72 bool QuicSpdyServerStream::ParseRequestHeaders(const char* data, |
| 73 uint32 data_len) { |
| 74 DCHECK(headers_decompressed()); |
| 75 SpdyFramer framer(SPDY3); |
| 76 size_t len = framer.ParseHeaderBlockInBuffer(data, |
| 77 data_len, |
| 78 &request_headers_); |
| 79 DCHECK_LE(len, data_len); |
| 80 if (len == 0 || request_headers_.empty()) { |
| 81 return false; // Headers were invalid. |
| 82 } |
| 83 |
| 84 if (data_len > len) { |
| 85 body_.append(data + len, data_len - len); |
| 86 } |
| 87 if (ContainsKey(request_headers_, "content-length") && |
| 88 !base::StringToInt(request_headers_["content-length"], &content_length_))
{ |
| 89 return false; // Invalid content-length. |
| 90 } |
| 91 return true; |
89 } | 92 } |
90 | 93 |
91 void QuicSpdyServerStream::SendResponse() { | 94 void QuicSpdyServerStream::SendResponse() { |
92 // Find response in cache. If not found, send error response. | 95 if (!ContainsKey(request_headers_, ":host") || |
93 GURL url(headers_.request_uri().as_string()); | 96 !ContainsKey(request_headers_, ":path")) { |
94 if (!url.is_valid()) { | |
95 SendErrorResponse(); | 97 SendErrorResponse(); |
96 return; | 98 return; |
97 } | 99 } |
| 100 |
| 101 // Find response in cache. If not found, send error response. |
98 const QuicInMemoryCache::Response* response = | 102 const QuicInMemoryCache::Response* response = |
99 QuicInMemoryCache::GetInstance()->GetResponse( | 103 QuicInMemoryCache::GetInstance()->GetResponse( |
100 url.host(), | 104 request_headers_[":host"], |
101 url.PathForRequest()); | 105 request_headers_[":path"]); |
102 if (response == nullptr) { | 106 if (response == nullptr) { |
103 SendErrorResponse(); | 107 SendErrorResponse(); |
104 return; | 108 return; |
105 } | 109 } |
106 | 110 |
107 if (response->response_type() == QuicInMemoryCache::CLOSE_CONNECTION) { | 111 if (response->response_type() == QuicInMemoryCache::CLOSE_CONNECTION) { |
108 DVLOG(1) << "Special response: closing connection."; | 112 DVLOG(1) << "Special response: closing connection."; |
109 CloseConnection(QUIC_NO_ERROR); | 113 CloseConnection(QUIC_NO_ERROR); |
110 return; | 114 return; |
111 } | 115 } |
(...skipping 26 matching lines...) Expand all Loading... |
138 | 142 |
139 WriteHeaders(response_headers, body.empty(), nullptr); | 143 WriteHeaders(response_headers, body.empty(), nullptr); |
140 | 144 |
141 if (!body.empty()) { | 145 if (!body.empty()) { |
142 WriteOrBufferData(body, true, nullptr); | 146 WriteOrBufferData(body, true, nullptr); |
143 } | 147 } |
144 } | 148 } |
145 | 149 |
146 } // namespace tools | 150 } // namespace tools |
147 } // namespace net | 151 } // namespace net |
OLD | NEW |