-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquic_simple_server_stream.cc
More file actions
315 lines (265 loc) · 10.5 KB
/
quic_simple_server_stream.cc
File metadata and controls
315 lines (265 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/tools/quic/quic_simple_server_stream.h"
#include <list>
#include <utility>
#include "net/quic/core/quic_spdy_stream.h"
#include "net/quic/core/spdy_utils.h"
#include "net/quic/platform/api/quic_bug_tracker.h"
#include "net/quic/platform/api/quic_flags.h"
#include "net/quic/platform/api/quic_logging.h"
#include "net/quic/platform/api/quic_map_util.h"
#include "net/quic/platform/api/quic_text_utils.h"
#include "net/spdy/core/spdy_protocol.h"
#include "net/tools/quic/quic_http_response_cache.h"
#include "net/tools/quic/quic_simple_server_session.h"
#include <iostream>
using std::cerr;
using std::endl;
using std::string;
namespace net {
QuicSimpleServerStream::QuicSimpleServerStream(
QuicStreamId id,
QuicSpdySession* session, QuicHttpResponseCache* response_cache) :
QuicSpdyServerStreamBase(id, session), content_length_(-1), response_cache_(
response_cache) {
}
QuicSimpleServerStream::~QuicSimpleServerStream() {
}
void QuicSimpleServerStream::OnInitialHeadersComplete(bool fin,
size_t frame_len, const QuicHeaderList& header_list) {
QuicSpdyStream::OnInitialHeadersComplete(fin, frame_len, header_list);
if (!SpdyUtils::CopyAndValidateHeaders(header_list, &content_length_,
&request_headers_)) {
QUIC_DVLOG(1) << "Invalid headers";
SendErrorResponse();
}
ConsumeHeaderList();
}
void QuicSimpleServerStream::OnTrailingHeadersComplete(bool fin,
size_t frame_len, const QuicHeaderList& header_list) {
QUIC_BUG << "Server does not support receiving Trailers.";
SendErrorResponse();
}
void QuicSimpleServerStream::OnDataAvailable() {
while (HasBytesToRead()) {
struct iovec iov;
if (GetReadableRegions(&iov, 1) == 0) {
// No more data to read.
break;
}
QUIC_DVLOG(1) << "Stream " << id() << " processed " << iov.iov_len
<< " bytes.";
body_.append(static_cast<char*>(iov.iov_base), iov.iov_len);
if (content_length_ >= 0
&& body_.size() > static_cast<uint64_t>(content_length_)) {
QUIC_DVLOG(1) << "Body size (" << body_.size()
<< ") > content length (" << content_length_
<< ").";
SendErrorResponse();
return;
}
MarkConsumed(iov.iov_len);
}
if (!sequencer()->IsClosed()) {
sequencer()->SetUnblocked();
return;
}
// If the sequencer is closed, then all the body, including the fin, has been
// consumed.
OnFinRead();
if (write_side_closed() || fin_buffered()) {
return;
}
SendResponse();
// std::cout << this->spdy_session()->connection()->GetStats() << std::endl;
}
void QuicSimpleServerStream::PushResponse(
SpdyHeaderBlock push_request_headers) {
if (id() % 2 != 0) {
QUIC_BUG
<< "Client initiated stream shouldn't be used as promised stream.";
return;
}
// Change the stream state to emulate a client request.
request_headers_ = std::move(push_request_headers);
content_length_ = 0;
QUIC_DVLOG(1) << "Stream " << id()
<< " ready to receive server push response.";
// Set as if stream decompresed the headers and received fin.
QuicSpdyStream::OnInitialHeadersComplete(/*fin=*/true, 0, QuicHeaderList());
}
void QuicSimpleServerStream::SendResponse() {
if (request_headers_.empty()) {
QUIC_DVLOG(1) << "Request headers empty.";
SendErrorResponse();
return;
}
if (content_length_ > 0
&& static_cast<uint64_t>(content_length_) != body_.size()) {
QUIC_DVLOG(1) << "Content length (" << content_length_
<< ") != body size (" << body_.size() << ").";
SendErrorResponse();
return;
}
{ // WE DID THIS
uint32_t n;
assert(QuicTextUtils::StringToUint32(request_headers_.find("max_bytes")->second, &n));
// std::cout << "============"<<n<<"=========" << std::endl;
unsigned seed = 48151623;
SpdyHeaderBlock headers;
string s;
for(int i = 0; i < n; i++) {
s.push_back(seed % 256);
seed = (seed * 47u); // overflow is % 2^32
}
headers["content-length"] = QuicTextUtils::Uint64ToString(n);
headers[":status"] = "200";
// cerr << "Sending packet of size " << n << endl;
SendHeadersAndBody(std::move(headers), s);
return;
}
// std::cout << "===0000====" <<std::endl;
if (!QuicContainsKey(request_headers_, ":authority")
|| !QuicContainsKey(request_headers_, ":path")) {
QUIC_DVLOG(1) << "Request headers do not contain :authority or :path.";
SendErrorResponse();
return;
}
// Find response in cache. If not found, send error response.
const QuicHttpResponseCache::Response* response = nullptr;
auto authority = request_headers_.find(":authority");
auto path = request_headers_.find(":path");
if (authority != request_headers_.end() && path != request_headers_.end()) {
response = response_cache_->GetResponse(authority->second,
path->second);
}
if (response == nullptr) {
QUIC_DVLOG(1) << "Response not found in cache.";
SendNotFoundResponse();
return;
}
if (response->response_type() == QuicHttpResponseCache::CLOSE_CONNECTION) {
QUIC_DVLOG(1) << "Special response: closing connection.";
CloseConnectionWithDetails(QUIC_NO_ERROR, "Toy server forcing close");
return;
}
if (response->response_type() == QuicHttpResponseCache::IGNORE_REQUEST) {
QUIC_DVLOG(1) << "Special response: ignoring request.";
return;
}
// Examing response status, if it was not pure integer as typical h2
// response status, send error response. Notice that
// QuicHttpResponseCache push urls are strictly authority + path only,
// scheme is not included (see |QuicHttpResponseCache::GetKey()|).
string request_url = request_headers_[":authority"].as_string()
+ request_headers_[":path"].as_string();
int response_code;
const SpdyHeaderBlock& response_headers = response->headers();
if (!ParseHeaderStatusCode(response_headers, &response_code)) {
auto status = response_headers.find(":status");
if (status == response_headers.end()) {
QUIC_LOG(WARNING)
<< ":status not present in response from cache for request "
<< request_url;
} else {
QUIC_LOG(WARNING)
<< "Illegal (non-integer) response :status from cache: "
<< status->second << " for request "
<< request_url;
}
SendErrorResponse();
return;
}
if (id() % 2 == 0) {
// std::cout << "=======" <<std::endl;
// A server initiated stream is only used for a server push response,
// and only 200 and 30X response codes are supported for server push.
// This behavior mirrors the HTTP/2 implementation.
bool is_redirection = response_code / 100 == 3;
if (response_code != 200 && !is_redirection) {
QUIC_LOG(WARNING) << "Response to server push request "
<< request_url
<< " result in response code "
<< response_code;
Reset(QUIC_STREAM_CANCELLED);
return;
}
}
std::list<QuicHttpResponseCache::ServerPushInfo> resources =
response_cache_->GetServerPushResources(request_url);
QUIC_DVLOG(1) << "Stream " << id() << " found " << resources.size()
<< " push resources.";
if (!resources.empty()) {
// std::cout << "++++" <<std::endl;
QuicSimpleServerSession* session =
static_cast<QuicSimpleServerSession*>(spdy_session());
session->PromisePushResources(request_url, resources, id(),
request_headers_);
}
QUIC_DVLOG(1) << "Stream " << id() << " sending response.";
SendHeadersAndBodyAndTrailers(response->headers().Clone(), response->body(),
response->trailers().Clone());
// std::cout << "!!!!!!!!" <<std::endl;
}
void QuicSimpleServerStream::SendNotFoundResponse() {
QUIC_DVLOG(1) << "Stream " << id() << " sending not found response.";
SpdyHeaderBlock headers;
headers[":status"] = "404";
headers["content-length"] = QuicTextUtils::Uint64ToString(
strlen(kNotFoundResponseBody));
SendHeadersAndBody(std::move(headers), kNotFoundResponseBody);
}
void QuicSimpleServerStream::SendErrorResponse() {
QUIC_DVLOG(1) << "Stream " << id() << " sending error response.";
SpdyHeaderBlock headers;
headers[":status"] = "500";
headers["content-length"] = QuicTextUtils::Uint64ToString(
strlen(kErrorResponseBody));
SendHeadersAndBody(std::move(headers), kErrorResponseBody);
}
void QuicSimpleServerStream::SendHeadersAndBody(
SpdyHeaderBlock response_headers, QuicStringPiece body) {
// std::cout << "SendHeadersAndBody" << std::endl;
SendHeadersAndBodyAndTrailers(std::move(response_headers), body,
SpdyHeaderBlock());
}
void QuicSimpleServerStream::SendHeadersAndBodyAndTrailers(
SpdyHeaderBlock response_headers, QuicStringPiece body,
SpdyHeaderBlock response_trailers) {
// Send the headers, with a FIN if there's nothing else to send.
bool send_fin = (body.empty() && response_trailers.empty());
// if (send_fin){
//
// }
QUIC_DLOG(INFO) << "Stream " << id() << " writing headers (fin = "
<< send_fin << ") : "
<< response_headers.DebugString();
WriteHeaders(std::move(response_headers), send_fin, nullptr);
if (send_fin) {
// Nothing else to send.
return;
}
// Send the body, with a FIN if there's no trailers to send.
send_fin = response_trailers.empty();
QUIC_DLOG(INFO) << "Stream " << id() << " writing body (fin = " << send_fin
<< ") with size: " << body.size();
// std::cout << "Stream " << id() << " writing body (fin = " << send_fin
// << ") with size: " << body.size() << std::endl;
if (!body.empty() || send_fin) {
WriteOrBufferData(body, send_fin, nullptr);
}
if (send_fin) {
// Nothing else to send.
return;
}
// Send the trailers. A FIN is always sent with trailers.
QUIC_DLOG(INFO) << "Stream " << id() << " writing trailers (fin = true): "
<< response_trailers.DebugString();
WriteTrailers(std::move(response_trailers), nullptr);
}
const char* const QuicSimpleServerStream::kErrorResponseBody = "bad";
const char* const QuicSimpleServerStream::kNotFoundResponseBody =
"file not found";
} // namespace net