blob: fbe964d037392bd25da9253e21099260cf8b0857 [file] [log] [blame]
[email protected]6db833d12012-01-21 00:45:191// 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/http/http_stream_parser.h"
6
sclittlebe1ccf62015-09-02 19:40:367#include <stdint.h>
8
[email protected]3cb5fc22013-12-12 19:40:569#include <algorithm>
10#include <string>
11#include <vector>
12
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2914#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2315#include "base/files/scoped_temp_dir.h"
[email protected]4750937f2012-06-15 20:44:2116#include "base/memory/ref_counted.h"
olli.raula6df48b2a2015-11-26 07:40:2217#include "base/memory/scoped_ptr.h"
[email protected]0f972882013-09-20 04:34:2018#include "base/run_loop.h"
[email protected]d069c11a2013-04-13 00:01:5519#include "base/strings/string_piece.h"
[email protected]125ef482013-06-11 18:32:4720#include "base/strings/stringprintf.h"
skyostil4891b25b2015-06-11 11:43:4521#include "base/thread_task_runner_handle.h"
mmenkecbc2b712014-10-09 20:29:0722#include "net/base/chunked_upload_data_stream.h"
23#include "net/base/elements_upload_data_stream.h"
[email protected]4750937f2012-06-15 20:44:2124#include "net/base/io_buffer.h"
[email protected]6db833d12012-01-21 00:45:1925#include "net/base/net_errors.h"
[email protected]4750937f2012-06-15 20:44:2126#include "net/base/test_completion_callback.h"
[email protected]b2d26cfd2012-12-11 10:36:0627#include "net/base/upload_bytes_element_reader.h"
[email protected]b2d26cfd2012-12-11 10:36:0628#include "net/base/upload_file_element_reader.h"
[email protected]4750937f2012-06-15 20:44:2129#include "net/http/http_request_headers.h"
30#include "net/http/http_request_info.h"
[email protected]df8e6382013-11-07 00:19:0631#include "net/http/http_response_headers.h"
[email protected]4750937f2012-06-15 20:44:2132#include "net/http/http_response_info.h"
33#include "net/socket/client_socket_handle.h"
34#include "net/socket/socket_test_util.h"
[email protected]6db833d12012-01-21 00:45:1935#include "testing/gtest/include/gtest/gtest.h"
[email protected]f89276a72013-07-12 06:41:5436#include "url/gurl.h"
[email protected]6db833d12012-01-21 00:45:1937
38namespace net {
39
[email protected]390489b2013-12-09 10:49:0140namespace {
41
[email protected]6db833d12012-01-21 00:45:1942const size_t kOutputSize = 1024; // Just large enough for this test.
43// The number of bytes that can fit in a buffer of kOutputSize.
44const size_t kMaxPayloadSize =
45 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize;
46
mmenke3dc8c88b2015-02-19 15:11:2747// Helper method to create a connected ClientSocketHandle using |data|.
48// Modifies |data|.
49scoped_ptr<ClientSocketHandle> CreateConnectedSocketHandle(
mmenkef22be0ee2015-06-05 15:39:0250 SequencedSocketData* data) {
mmenke3dc8c88b2015-02-19 15:11:2751 data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
52
mmenkef22be0ee2015-06-05 15:39:0253 scoped_ptr<MockTCPClientSocket> socket(
54 new MockTCPClientSocket(net::AddressList(), nullptr, data));
55 data->set_socket(socket.get());
mmenke3dc8c88b2015-02-19 15:11:2756
57 TestCompletionCallback callback;
mmenkef22be0ee2015-06-05 15:39:0258 EXPECT_EQ(OK, socket->Connect(callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:2759
60 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
mmenkef22be0ee2015-06-05 15:39:0261 socket_handle->SetSocket(socket.Pass());
mmenke3dc8c88b2015-02-19 15:11:2762 return socket_handle.Pass();
63}
64
[email protected]6db833d12012-01-21 00:45:1965// The empty payload is how the last chunk is encoded.
66TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
67 char output[kOutputSize];
68
69 const base::StringPiece kPayload = "";
70 const base::StringPiece kExpected = "0\r\n\r\n";
71 const int num_bytes_written =
72 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
73 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
74 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
75}
76
77TEST(HttpStreamParser, EncodeChunk_ShortPayload) {
78 char output[kOutputSize];
79
80 const std::string kPayload("foo\x00\x11\x22", 6);
81 // 11 = payload size + sizeof("6") + CRLF x 2.
82 const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11);
83 const int num_bytes_written =
84 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
85 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
86 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
87}
88
89TEST(HttpStreamParser, EncodeChunk_LargePayload) {
90 char output[kOutputSize];
91
92 const std::string kPayload(1000, '\xff'); // '\xff' x 1000.
93 // 3E8 = 1000 in hex.
94 const std::string kExpected = "3E8\r\n" + kPayload + "\r\n";
95 const int num_bytes_written =
96 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
97 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
98 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
99}
100
101TEST(HttpStreamParser, EncodeChunk_FullPayload) {
102 char output[kOutputSize];
103
104 const std::string kPayload(kMaxPayloadSize, '\xff');
105 // 3F4 = 1012 in hex.
106 const std::string kExpected = "3F4\r\n" + kPayload + "\r\n";
107 const int num_bytes_written =
108 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
109 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
110 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
111}
112
113TEST(HttpStreamParser, EncodeChunk_TooLargePayload) {
114 char output[kOutputSize];
115
116 // The payload is one byte larger the output buffer size.
117 const std::string kPayload(kMaxPayloadSize + 1, '\xff');
118 const int num_bytes_written =
119 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
120 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written);
121}
122
[email protected]75577ec2012-01-24 23:41:50123TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) {
[email protected]7a1fcff2012-01-24 01:07:49124 // Shouldn't be merged if upload data is non-existent.
[email protected]75577ec2012-01-24 23:41:50125 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
126 "some header", NULL));
[email protected]7a1fcff2012-01-24 01:07:49127}
128
[email protected]75577ec2012-01-24 23:41:50129TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) {
olli.raula6df48b2a2015-11-26 07:40:22130 std::vector<scoped_ptr<UploadElementReader>> element_readers;
131 scoped_ptr<UploadDataStream> body(make_scoped_ptr(
132 new ElementsUploadDataStream(std::move(element_readers), 0)));
[email protected]4db27d82012-12-20 11:50:24133 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[email protected]7a1fcff2012-01-24 01:07:49134 // Shouldn't be merged if upload data is empty.
[email protected]75577ec2012-01-24 23:41:50135 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
136 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49137}
138
[email protected]75577ec2012-01-24 23:41:50139TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) {
[email protected]7a1fcff2012-01-24 01:07:49140 const std::string payload = "123";
mmenkecbc2b712014-10-09 20:29:07141 scoped_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0));
142 body->AppendData(payload.data(), payload.size(), true);
143 ASSERT_EQ(OK, body->Init(TestCompletionCallback().callback()));
[email protected]7a1fcff2012-01-24 01:07:49144 // Shouldn't be merged if upload data carries chunked data.
[email protected]75577ec2012-01-24 23:41:50145 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
146 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49147}
148
[email protected]75577ec2012-01-24 23:41:50149TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) {
wkorman30fe3fd2015-11-06 23:43:42150 // Create an empty temporary file.
151 base::ScopedTempDir temp_dir;
152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
153 base::FilePath temp_file_path;
154 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path));
155
[email protected]0f972882013-09-20 04:34:20156 {
olli.raula6df48b2a2015-11-26 07:40:22157 std::vector<scoped_ptr<UploadElementReader>> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49158
olli.raula6df48b2a2015-11-26 07:40:22159 element_readers.push_back(make_scoped_ptr(
skyostil4891b25b2015-06-11 11:43:45160 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
olli.raula6df48b2a2015-11-26 07:40:22161 temp_file_path, 0, 0, base::Time())));
[email protected]7a1fcff2012-01-24 01:07:49162
[email protected]0f972882013-09-20 04:34:20163 scoped_ptr<UploadDataStream> body(
olli.raula6df48b2a2015-11-26 07:40:22164 new ElementsUploadDataStream(std::move(element_readers), 0));
[email protected]0f972882013-09-20 04:34:20165 TestCompletionCallback callback;
166 ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback()));
167 ASSERT_EQ(OK, callback.WaitForResult());
168 // Shouldn't be merged if upload data carries a file, as it's not in-memory.
169 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
170 "some header", body.get()));
171 }
wkorman30fe3fd2015-11-06 23:43:42172
[email protected]0f972882013-09-20 04:34:20173 // UploadFileElementReaders may post clean-up tasks on destruction.
174 base::RunLoop().RunUntilIdle();
[email protected]7a1fcff2012-01-24 01:07:49175}
176
[email protected]75577ec2012-01-24 23:41:50177TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) {
olli.raula6df48b2a2015-11-26 07:40:22178 std::vector<scoped_ptr<UploadElementReader>> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49179 const std::string payload = "123";
olli.raula6df48b2a2015-11-26 07:40:22180 element_readers.push_back(make_scoped_ptr(
181 new UploadBytesElementReader(payload.data(), payload.size())));
[email protected]7a1fcff2012-01-24 01:07:49182
[email protected]96c77a72013-09-24 09:49:20183 scoped_ptr<UploadDataStream> body(
olli.raula6df48b2a2015-11-26 07:40:22184 new ElementsUploadDataStream(std::move(element_readers), 0));
[email protected]4db27d82012-12-20 11:50:24185 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[email protected]7a1fcff2012-01-24 01:07:49186 // Yes, should be merged if the in-memory body is small here.
[email protected]75577ec2012-01-24 23:41:50187 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
188 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49189}
190
[email protected]75577ec2012-01-24 23:41:50191TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) {
olli.raula6df48b2a2015-11-26 07:40:22192 std::vector<scoped_ptr<UploadElementReader>> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49193 const std::string payload(10000, 'a'); // 'a' x 10000.
olli.raula6df48b2a2015-11-26 07:40:22194 element_readers.push_back(make_scoped_ptr(
195 new UploadBytesElementReader(payload.data(), payload.size())));
[email protected]7a1fcff2012-01-24 01:07:49196
[email protected]96c77a72013-09-24 09:49:20197 scoped_ptr<UploadDataStream> body(
olli.raula6df48b2a2015-11-26 07:40:22198 new ElementsUploadDataStream(std::move(element_readers), 0));
[email protected]4db27d82012-12-20 11:50:24199 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[email protected]7a1fcff2012-01-24 01:07:49200 // Shouldn't be merged if the in-memory body is large here.
[email protected]75577ec2012-01-24 23:41:50201 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
202 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49203}
204
sclittlebe1ccf62015-09-02 19:40:36205TEST(HttpStreamParser, SentBytesNoHeaders) {
206 MockWrite writes[] = {
207 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"),
208 };
209
210 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
211 scoped_ptr<ClientSocketHandle> socket_handle =
212 CreateConnectedSocketHandle(&data);
213
214 HttpRequestInfo request;
215 request.method = "GET";
216 request.url = GURL("http://localhost");
217
218 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
219 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
220 BoundNetLog());
221
222 HttpResponseInfo response;
223 TestCompletionCallback callback;
224 EXPECT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", HttpRequestHeaders(),
225 &response, callback.callback()));
226
227 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
228}
229
230TEST(HttpStreamParser, SentBytesWithHeaders) {
231 MockWrite writes[] = {
232 MockWrite(SYNCHRONOUS, 0,
233 "GET / HTTP/1.1\r\n"
234 "Host: localhost\r\n"
235 "Connection: Keep-Alive\r\n\r\n"),
236 };
237
238 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
239 scoped_ptr<ClientSocketHandle> socket_handle =
240 CreateConnectedSocketHandle(&data);
241
242 HttpRequestInfo request;
243 request.method = "GET";
244 request.url = GURL("http://localhost");
245
246 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
247 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
248 BoundNetLog());
249
250 HttpRequestHeaders headers;
251 headers.SetHeader("Host", "localhost");
252 headers.SetHeader("Connection", "Keep-Alive");
253
254 HttpResponseInfo response;
255 TestCompletionCallback callback;
256 EXPECT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", headers, &response,
257 callback.callback()));
258
259 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
260}
261
262TEST(HttpStreamParser, SentBytesWithHeadersMultiWrite) {
263 MockWrite writes[] = {
264 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"),
265 MockWrite(SYNCHRONOUS, 1, "Host: localhost\r\n"),
266 MockWrite(SYNCHRONOUS, 2, "Connection: Keep-Alive\r\n\r\n"),
267 };
268
269 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
270 scoped_ptr<ClientSocketHandle> socket_handle =
271 CreateConnectedSocketHandle(&data);
272
273 HttpRequestInfo request;
274 request.method = "GET";
275 request.url = GURL("http://localhost");
276
277 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
278 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
279 BoundNetLog());
280
281 HttpRequestHeaders headers;
282 headers.SetHeader("Host", "localhost");
283 headers.SetHeader("Connection", "Keep-Alive");
284
285 HttpResponseInfo response;
286 TestCompletionCallback callback;
287
288 EXPECT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", headers, &response,
289 callback.callback()));
290
291 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
292}
293
294TEST(HttpStreamParser, SentBytesWithErrorWritingHeaders) {
295 MockWrite writes[] = {
296 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"),
297 MockWrite(SYNCHRONOUS, 1, "Host: localhost\r\n"),
298 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 2),
299 };
300
301 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
302 scoped_ptr<ClientSocketHandle> socket_handle =
303 CreateConnectedSocketHandle(&data);
304
305 HttpRequestInfo request;
306 request.method = "GET";
307 request.url = GURL("http://localhost");
308
309 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
310 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
311 BoundNetLog());
312
313 HttpRequestHeaders headers;
314 headers.SetHeader("Host", "localhost");
315 headers.SetHeader("Connection", "Keep-Alive");
316
317 HttpResponseInfo response;
318 TestCompletionCallback callback;
319 EXPECT_EQ(ERR_CONNECTION_RESET,
320 parser.SendRequest("GET / HTTP/1.1\r\n", headers, &response,
321 callback.callback()));
322
323 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
324}
325
326TEST(HttpStreamParser, SentBytesPost) {
327 MockWrite writes[] = {
328 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"),
329 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"),
330 MockWrite(SYNCHRONOUS, 2, "hello world!"),
331 };
332
333 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
334 scoped_ptr<ClientSocketHandle> socket_handle =
335 CreateConnectedSocketHandle(&data);
336
olli.raula6df48b2a2015-11-26 07:40:22337 std::vector<scoped_ptr<UploadElementReader>> element_readers;
338 element_readers.push_back(
339 make_scoped_ptr(new UploadBytesElementReader("hello world!", 12)));
340 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
sclittlebe1ccf62015-09-02 19:40:36341 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback()));
342
343 HttpRequestInfo request;
344 request.method = "POST";
345 request.url = GURL("http://localhost");
346 request.upload_data_stream = &upload_data_stream;
347
348 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
349 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
350 BoundNetLog());
351
352 HttpRequestHeaders headers;
353 headers.SetHeader("Content-Length", "12");
354
355 HttpResponseInfo response;
356 TestCompletionCallback callback;
357 EXPECT_EQ(OK, parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
358 callback.callback()));
359
360 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
361}
362
363TEST(HttpStreamParser, SentBytesChunkedPostError) {
364 static const char kChunk[] = "Chunk 1";
365
366 MockWrite writes[] = {
367 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"),
368 MockWrite(ASYNC, 1, "Transfer-Encoding: chunked\r\n\r\n"),
369 MockWrite(ASYNC, 2, "7\r\nChunk 1\r\n"),
370 MockWrite(SYNCHRONOUS, ERR_FAILED, 3),
371 };
372
373 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
374 scoped_ptr<ClientSocketHandle> socket_handle =
375 CreateConnectedSocketHandle(&data);
376
377 ChunkedUploadDataStream upload_data_stream(0);
378 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback()));
379
380 HttpRequestInfo request;
381 request.method = "POST";
382 request.url = GURL("http://localhost");
383 request.upload_data_stream = &upload_data_stream;
384
385 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
386 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
387 BoundNetLog());
388
389 HttpRequestHeaders headers;
390 headers.SetHeader("Transfer-Encoding", "chunked");
391
392 HttpResponseInfo response;
393 TestCompletionCallback callback;
394 EXPECT_EQ(ERR_IO_PENDING, parser.SendRequest("POST / HTTP/1.1\r\n", headers,
395 &response, callback.callback()));
396
397 base::RunLoop().RunUntilIdle();
398 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false);
399
400 base::RunLoop().RunUntilIdle();
401 // This write should fail.
402 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false);
403 EXPECT_EQ(ERR_FAILED, callback.WaitForResult());
404
405 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
406}
407
[email protected]4750937f2012-06-15 20:44:21408// Test to ensure the HttpStreamParser state machine does not get confused
mmenke3dc8c88b2015-02-19 15:11:27409// when sending a request with a chunked body with only one chunk that becomes
410// available asynchronously.
411TEST(HttpStreamParser, AsyncSingleChunkAndAsyncSocket) {
412 static const char kChunk[] = "Chunk";
413
414 MockWrite writes[] = {
415 MockWrite(ASYNC, 0,
416 "GET /one.html HTTP/1.1\r\n"
417 "Transfer-Encoding: chunked\r\n\r\n"),
418 MockWrite(ASYNC, 1, "5\r\nChunk\r\n"),
419 MockWrite(ASYNC, 2, "0\r\n\r\n"),
420 };
421
422 // The size of the response body, as reflected in the Content-Length of the
423 // MockRead below.
424 static const int kBodySize = 8;
425
426 MockRead reads[] = {
427 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"),
428 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"),
429 MockRead(ASYNC, 5, "one.html"),
430 MockRead(SYNCHRONOUS, 0, 6), // EOF
431 };
432
433 ChunkedUploadDataStream upload_stream(0);
434 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
435
mmenkef22be0ee2015-06-05 15:39:02436 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
mmenke3dc8c88b2015-02-19 15:11:27437 scoped_ptr<ClientSocketHandle> socket_handle =
438 CreateConnectedSocketHandle(&data);
439
440 HttpRequestInfo request_info;
441 request_info.method = "GET";
442 request_info.url = GURL("http://localhost");
443 request_info.upload_data_stream = &upload_stream;
444
445 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
446 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
447 BoundNetLog());
448
449 HttpRequestHeaders request_headers;
450 request_headers.SetHeader("Transfer-Encoding", "chunked");
451
452 HttpResponseInfo response_info;
453 TestCompletionCallback callback;
454 // This will attempt to Write() the initial request and headers, which will
455 // complete asynchronously.
456 ASSERT_EQ(ERR_IO_PENDING,
457 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
458 &response_info, callback.callback()));
459
mmenkef22be0ee2015-06-05 15:39:02460 // Complete the initial request write. Callback should not have been invoked.
461 base::RunLoop().RunUntilIdle();
mmenke3dc8c88b2015-02-19 15:11:27462 ASSERT_FALSE(callback.have_result());
463
mmenkef22be0ee2015-06-05 15:39:02464 // Now append the only chunk and wait for the callback.
mmenke3dc8c88b2015-02-19 15:11:27465 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true);
mmenke3dc8c88b2015-02-19 15:11:27466 ASSERT_EQ(OK, callback.WaitForResult());
467
468 // Attempt to read the response status and the response headers.
469 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27470 ASSERT_GT(callback.WaitForResult(), 0);
471
472 // Finally, attempt to read the response body.
473 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
474 ASSERT_EQ(ERR_IO_PENDING,
475 parser.ReadResponseBody(body_buffer.get(), kBodySize,
476 callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27477 ASSERT_EQ(kBodySize, callback.WaitForResult());
sclittlebe1ccf62015-09-02 19:40:36478
479 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
480 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
mmenke3dc8c88b2015-02-19 15:11:27481}
482
483// Test to ensure the HttpStreamParser state machine does not get confused
484// when sending a request with a chunked body with only one chunk that is
485// available synchronously.
486TEST(HttpStreamParser, SyncSingleChunkAndAsyncSocket) {
487 static const char kChunk[] = "Chunk";
488
489 MockWrite writes[] = {
490 MockWrite(ASYNC, 0,
491 "GET /one.html HTTP/1.1\r\n"
492 "Transfer-Encoding: chunked\r\n\r\n"),
493 MockWrite(ASYNC, 1, "5\r\nChunk\r\n"),
494 MockWrite(ASYNC, 2, "0\r\n\r\n"),
495 };
496
497 // The size of the response body, as reflected in the Content-Length of the
498 // MockRead below.
499 static const int kBodySize = 8;
500
501 MockRead reads[] = {
502 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"),
503 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"),
504 MockRead(ASYNC, 5, "one.html"),
505 MockRead(SYNCHRONOUS, 0, 6), // EOF
506 };
507
508 ChunkedUploadDataStream upload_stream(0);
509 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
510 // Append the only chunk.
511 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true);
512
mmenkef22be0ee2015-06-05 15:39:02513 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
mmenke3dc8c88b2015-02-19 15:11:27514 scoped_ptr<ClientSocketHandle> socket_handle =
515 CreateConnectedSocketHandle(&data);
516
517 HttpRequestInfo request_info;
518 request_info.method = "GET";
519 request_info.url = GURL("http://localhost");
520 request_info.upload_data_stream = &upload_stream;
521
522 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
523 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
524 BoundNetLog());
525
526 HttpRequestHeaders request_headers;
527 request_headers.SetHeader("Transfer-Encoding", "chunked");
528
529 HttpResponseInfo response_info;
530 TestCompletionCallback callback;
531 // This will attempt to Write() the initial request and headers, which will
532 // complete asynchronously.
533 ASSERT_EQ(ERR_IO_PENDING,
534 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
535 &response_info, callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27536 ASSERT_EQ(OK, callback.WaitForResult());
537
538 // Attempt to read the response status and the response headers.
539 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27540 ASSERT_GT(callback.WaitForResult(), 0);
541
542 // Finally, attempt to read the response body.
543 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
544 ASSERT_EQ(ERR_IO_PENDING,
545 parser.ReadResponseBody(body_buffer.get(), kBodySize,
546 callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27547 ASSERT_EQ(kBodySize, callback.WaitForResult());
sclittlebe1ccf62015-09-02 19:40:36548
549 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
550 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
mmenke3dc8c88b2015-02-19 15:11:27551}
552
553// Test to ensure the HttpStreamParser state machine does not get confused
[email protected]4750937f2012-06-15 20:44:21554// when sending a request with a chunked body, where chunks become available
555// asynchronously, over a socket where writes may also complete
556// asynchronously.
557// This is a regression test for http://crbug.com/132243
mmenke3dc8c88b2015-02-19 15:11:27558TEST(HttpStreamParser, AsyncChunkAndAsyncSocketWithMultipleChunks) {
[email protected]4750937f2012-06-15 20:44:21559 // The chunks that will be written in the request, as reflected in the
560 // MockWrites below.
561 static const char kChunk1[] = "Chunk 1";
562 static const char kChunk2[] = "Chunky 2";
563 static const char kChunk3[] = "Test 3";
564
565 MockWrite writes[] = {
mmenke3dc8c88b2015-02-19 15:11:27566 MockWrite(ASYNC, 0,
567 "GET /one.html HTTP/1.1\r\n"
568 "Transfer-Encoding: chunked\r\n\r\n"),
569 MockWrite(ASYNC, 1, "7\r\nChunk 1\r\n"),
570 MockWrite(ASYNC, 2, "8\r\nChunky 2\r\n"),
571 MockWrite(ASYNC, 3, "6\r\nTest 3\r\n"),
572 MockWrite(ASYNC, 4, "0\r\n\r\n"),
[email protected]4750937f2012-06-15 20:44:21573 };
574
575 // The size of the response body, as reflected in the Content-Length of the
576 // MockRead below.
577 static const int kBodySize = 8;
578
579 MockRead reads[] = {
580 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"),
581 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"),
582 MockRead(ASYNC, 7, "one.html"),
[email protected]d55b30a2012-07-21 00:35:39583 MockRead(SYNCHRONOUS, 0, 8), // EOF
[email protected]4750937f2012-06-15 20:44:21584 };
585
mmenkecbc2b712014-10-09 20:29:07586 ChunkedUploadDataStream upload_stream(0);
587 upload_stream.AppendData(kChunk1, arraysize(kChunk1) - 1, false);
588 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
[email protected]9a963892012-11-01 11:48:13589
mmenkef22be0ee2015-06-05 15:39:02590 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
mmenke3dc8c88b2015-02-19 15:11:27591 scoped_ptr<ClientSocketHandle> socket_handle =
592 CreateConnectedSocketHandle(&data);
[email protected]4750937f2012-06-15 20:44:21593
594 HttpRequestInfo request_info;
595 request_info.method = "GET";
596 request_info.url = GURL("http://localhost");
[email protected]bf3eb002012-11-15 05:50:11597 request_info.upload_data_stream = &upload_stream;
[email protected]4750937f2012-06-15 20:44:21598
599 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
[email protected]90499482013-06-01 00:39:50600 HttpStreamParser parser(
601 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog());
[email protected]4750937f2012-06-15 20:44:21602
[email protected]4750937f2012-06-15 20:44:21603 HttpRequestHeaders request_headers;
[email protected]4750937f2012-06-15 20:44:21604 request_headers.SetHeader("Transfer-Encoding", "chunked");
[email protected]4750937f2012-06-15 20:44:21605
606 HttpResponseInfo response_info;
mmenke3dc8c88b2015-02-19 15:11:27607 TestCompletionCallback callback;
[email protected]4750937f2012-06-15 20:44:21608 // This will attempt to Write() the initial request and headers, which will
609 // complete asynchronously.
mmenke3dc8c88b2015-02-19 15:11:27610 ASSERT_EQ(ERR_IO_PENDING,
611 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
612 &response_info, callback.callback()));
[email protected]4750937f2012-06-15 20:44:21613 ASSERT_FALSE(callback.have_result());
614
mmenkef22be0ee2015-06-05 15:39:02615 // Sending the request and the first chunk completes.
616 base::RunLoop().RunUntilIdle();
617 ASSERT_FALSE(callback.have_result());
618
619 // Now append another chunk.
mmenkecbc2b712014-10-09 20:29:07620 upload_stream.AppendData(kChunk2, arraysize(kChunk2) - 1, false);
[email protected]4750937f2012-06-15 20:44:21621 ASSERT_FALSE(callback.have_result());
622
mmenkef22be0ee2015-06-05 15:39:02623 // Add the final chunk, while the write for the second is still pending,
624 // which should not confuse the state machine.
mmenkecbc2b712014-10-09 20:29:07625 upload_stream.AppendData(kChunk3, arraysize(kChunk3) - 1, true);
[email protected]4750937f2012-06-15 20:44:21626 ASSERT_FALSE(callback.have_result());
627
mmenkef22be0ee2015-06-05 15:39:02628 // Wait for writes to complete.
mmenke3dc8c88b2015-02-19 15:11:27629 ASSERT_EQ(OK, callback.WaitForResult());
[email protected]4750937f2012-06-15 20:44:21630
631 // Attempt to read the response status and the response headers.
mmenke3dc8c88b2015-02-19 15:11:27632 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27633 ASSERT_GT(callback.WaitForResult(), 0);
[email protected]4750937f2012-06-15 20:44:21634
635 // Finally, attempt to read the response body.
636 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
mmenke3dc8c88b2015-02-19 15:11:27637 ASSERT_EQ(ERR_IO_PENDING,
638 parser.ReadResponseBody(body_buffer.get(), kBodySize,
639 callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27640 ASSERT_EQ(kBodySize, callback.WaitForResult());
sclittlebe1ccf62015-09-02 19:40:36641
642 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
643 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
mmenke3dc8c88b2015-02-19 15:11:27644}
645
646// Test to ensure the HttpStreamParser state machine does not get confused
647// when there's only one "chunk" with 0 bytes, and is received from the
648// UploadStream only after sending the request headers successfully.
649TEST(HttpStreamParser, AsyncEmptyChunkedUpload) {
650 MockWrite writes[] = {
651 MockWrite(ASYNC, 0,
652 "GET /one.html HTTP/1.1\r\n"
653 "Transfer-Encoding: chunked\r\n\r\n"),
654 MockWrite(ASYNC, 1, "0\r\n\r\n"),
655 };
656
657 // The size of the response body, as reflected in the Content-Length of the
658 // MockRead below.
659 const int kBodySize = 8;
660
661 MockRead reads[] = {
662 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"),
663 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"),
664 MockRead(ASYNC, 4, "one.html"),
665 MockRead(SYNCHRONOUS, 0, 5), // EOF
666 };
667
668 ChunkedUploadDataStream upload_stream(0);
669 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
670
mmenkef22be0ee2015-06-05 15:39:02671 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
mmenke3dc8c88b2015-02-19 15:11:27672 scoped_ptr<ClientSocketHandle> socket_handle =
673 CreateConnectedSocketHandle(&data);
674
675 HttpRequestInfo request_info;
676 request_info.method = "GET";
677 request_info.url = GURL("http://localhost");
678 request_info.upload_data_stream = &upload_stream;
679
680 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
681 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
682 BoundNetLog());
683
684 HttpRequestHeaders request_headers;
685 request_headers.SetHeader("Transfer-Encoding", "chunked");
686
687 HttpResponseInfo response_info;
688 TestCompletionCallback callback;
689 // This will attempt to Write() the initial request and headers, which will
690 // complete asynchronously.
691 ASSERT_EQ(ERR_IO_PENDING,
692 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
693 &response_info, callback.callback()));
694
mmenke3dc8c88b2015-02-19 15:11:27695 // Now append the terminal 0-byte "chunk".
696 upload_stream.AppendData(nullptr, 0, true);
697 ASSERT_FALSE(callback.have_result());
698
mmenke3dc8c88b2015-02-19 15:11:27699 ASSERT_EQ(OK, callback.WaitForResult());
700
701 // Attempt to read the response status and the response headers.
702 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27703 ASSERT_GT(callback.WaitForResult(), 0);
704
705 // Finally, attempt to read the response body.
706 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
707 ASSERT_EQ(ERR_IO_PENDING,
708 parser.ReadResponseBody(body_buffer.get(), kBodySize,
709 callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27710 ASSERT_EQ(kBodySize, callback.WaitForResult());
sclittlebe1ccf62015-09-02 19:40:36711
712 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
713 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
mmenke3dc8c88b2015-02-19 15:11:27714}
715
716// Test to ensure the HttpStreamParser state machine does not get confused
717// when there's only one "chunk" with 0 bytes, which was already appended before
718// the request was started.
719TEST(HttpStreamParser, SyncEmptyChunkedUpload) {
720 MockWrite writes[] = {
721 MockWrite(ASYNC, 0,
722 "GET /one.html HTTP/1.1\r\n"
723 "Transfer-Encoding: chunked\r\n\r\n"),
724 MockWrite(ASYNC, 1, "0\r\n\r\n"),
725 };
726
727 // The size of the response body, as reflected in the Content-Length of the
728 // MockRead below.
729 const int kBodySize = 8;
730
731 MockRead reads[] = {
732 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"),
733 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"),
734 MockRead(ASYNC, 4, "one.html"),
735 MockRead(SYNCHRONOUS, 0, 5), // EOF
736 };
737
738 ChunkedUploadDataStream upload_stream(0);
739 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
740 // Append final empty chunk.
741 upload_stream.AppendData(nullptr, 0, true);
742
mmenkef22be0ee2015-06-05 15:39:02743 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
mmenke3dc8c88b2015-02-19 15:11:27744 scoped_ptr<ClientSocketHandle> socket_handle =
745 CreateConnectedSocketHandle(&data);
746
747 HttpRequestInfo request_info;
748 request_info.method = "GET";
749 request_info.url = GURL("http://localhost");
750 request_info.upload_data_stream = &upload_stream;
751
752 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
753 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
754 BoundNetLog());
755
756 HttpRequestHeaders request_headers;
757 request_headers.SetHeader("Transfer-Encoding", "chunked");
758
759 HttpResponseInfo response_info;
760 TestCompletionCallback callback;
761 // This will attempt to Write() the initial request and headers, which will
762 // complete asynchronously.
763 ASSERT_EQ(ERR_IO_PENDING,
764 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
765 &response_info, callback.callback()));
766
767 // Complete writing the request headers and body.
mmenke3dc8c88b2015-02-19 15:11:27768 ASSERT_EQ(OK, callback.WaitForResult());
769
770 // Attempt to read the response status and the response headers.
771 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27772 ASSERT_GT(callback.WaitForResult(), 0);
773
774 // Finally, attempt to read the response body.
775 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
776 ASSERT_EQ(ERR_IO_PENDING,
777 parser.ReadResponseBody(body_buffer.get(), kBodySize,
778 callback.callback()));
mmenke3dc8c88b2015-02-19 15:11:27779 ASSERT_EQ(kBodySize, callback.WaitForResult());
sclittlebe1ccf62015-09-02 19:40:36780
781 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
782 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
[email protected]4750937f2012-06-15 20:44:21783}
784
[email protected]9c18dbc2013-05-29 19:06:53785TEST(HttpStreamParser, TruncatedHeaders) {
786 MockRead truncated_status_reads[] = {
787 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 20"),
788 MockRead(SYNCHRONOUS, 0, 2), // EOF
789 };
790
791 MockRead truncated_after_status_reads[] = {
792 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\n"),
793 MockRead(SYNCHRONOUS, 0, 2), // EOF
794 };
795
796 MockRead truncated_in_header_reads[] = {
797 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHead"),
798 MockRead(SYNCHRONOUS, 0, 2), // EOF
799 };
800
801 MockRead truncated_after_header_reads[] = {
802 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHeader: foo\r\n"),
803 MockRead(SYNCHRONOUS, 0, 2), // EOF
804 };
805
806 MockRead truncated_after_final_newline_reads[] = {
807 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHeader: foo\r\n\r"),
808 MockRead(SYNCHRONOUS, 0, 2), // EOF
809 };
810
811 MockRead not_truncated_reads[] = {
812 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHeader: foo\r\n\r\n"),
813 MockRead(SYNCHRONOUS, 0, 2), // EOF
814 };
815
816 MockRead* reads[] = {
817 truncated_status_reads,
818 truncated_after_status_reads,
819 truncated_in_header_reads,
820 truncated_after_header_reads,
821 truncated_after_final_newline_reads,
822 not_truncated_reads,
823 };
824
825 MockWrite writes[] = {
826 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"),
827 };
828
zmo9528c9f42015-08-04 22:12:08829 enum {
830 HTTP = 0,
831 HTTPS,
832 NUM_PROTOCOLS,
833 };
[email protected]9c18dbc2013-05-29 19:06:53834
zmo9528c9f42015-08-04 22:12:08835 for (size_t protocol = 0; protocol < NUM_PROTOCOLS; protocol++) {
836 SCOPED_TRACE(protocol);
[email protected]9c18dbc2013-05-29 19:06:53837
zmo9528c9f42015-08-04 22:12:08838 for (size_t i = 0; i < arraysize(reads); i++) {
839 SCOPED_TRACE(i);
840 SequencedSocketData data(reads[i], 2, writes, arraysize(writes));
841 scoped_ptr<ClientSocketHandle> socket_handle(
842 CreateConnectedSocketHandle(&data));
[email protected]9c18dbc2013-05-29 19:06:53843
zmo9528c9f42015-08-04 22:12:08844 HttpRequestInfo request_info;
845 request_info.method = "GET";
846 if (protocol == HTTP) {
847 request_info.url = GURL("http://localhost");
848 } else {
849 request_info.url = GURL("https://localhost");
850 }
851 request_info.load_flags = LOAD_NORMAL;
[email protected]9c18dbc2013-05-29 19:06:53852
zmo9528c9f42015-08-04 22:12:08853 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
854 HttpStreamParser parser(
855 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog());
856
857 HttpRequestHeaders request_headers;
858 HttpResponseInfo response_info;
859 TestCompletionCallback callback;
860 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers,
861 &response_info, callback.callback()));
862
863 int rv = parser.ReadResponseHeaders(callback.callback());
sclittlebe1ccf62015-09-02 19:40:36864 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
865 parser.sent_bytes());
zmo9528c9f42015-08-04 22:12:08866 if (i == arraysize(reads) - 1) {
867 EXPECT_EQ(OK, rv);
868 EXPECT_TRUE(response_info.headers.get());
sclittlebe1ccf62015-09-02 19:40:36869 EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes());
zmo9528c9f42015-08-04 22:12:08870 } else {
871 if (protocol == HTTP) {
872 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv);
873 EXPECT_TRUE(response_info.headers.get());
sclittlebe1ccf62015-09-02 19:40:36874 EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes());
zmo9528c9f42015-08-04 22:12:08875 } else {
876 EXPECT_EQ(ERR_RESPONSE_HEADERS_TRUNCATED, rv);
877 EXPECT_FALSE(response_info.headers.get());
sclittlebe1ccf62015-09-02 19:40:36878 EXPECT_EQ(0, parser.received_bytes());
zmo9528c9f42015-08-04 22:12:08879 }
880 }
[email protected]9c18dbc2013-05-29 19:06:53881 }
882 }
883}
884
[email protected]df8e6382013-11-07 00:19:06885// Confirm that on 101 response, the headers are parsed but the data that
886// follows remains in the buffer.
887TEST(HttpStreamParser, Websocket101Response) {
888 MockRead reads[] = {
889 MockRead(SYNCHRONOUS, 1,
890 "HTTP/1.1 101 Switching Protocols\r\n"
891 "Upgrade: websocket\r\n"
892 "Connection: Upgrade\r\n"
893 "\r\n"
894 "a fake websocket frame"),
895 };
896
897 MockWrite writes[] = {
898 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"),
899 };
900
mmenkef22be0ee2015-06-05 15:39:02901 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
902 scoped_ptr<ClientSocketHandle> socket_handle =
903 CreateConnectedSocketHandle(&data);
[email protected]df8e6382013-11-07 00:19:06904
905 HttpRequestInfo request_info;
906 request_info.method = "GET";
907 request_info.url = GURL("http://localhost");
908 request_info.load_flags = LOAD_NORMAL;
909
910 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
911 HttpStreamParser parser(
912 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog());
913
914 HttpRequestHeaders request_headers;
915 HttpResponseInfo response_info;
mmenkef22be0ee2015-06-05 15:39:02916 TestCompletionCallback callback;
917 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers,
918 &response_info, callback.callback()));
[email protected]df8e6382013-11-07 00:19:06919
mmenkef22be0ee2015-06-05 15:39:02920 EXPECT_EQ(OK, parser.ReadResponseHeaders(callback.callback()));
[email protected]df8e6382013-11-07 00:19:06921 ASSERT_TRUE(response_info.headers.get());
922 EXPECT_EQ(101, response_info.headers->response_code());
923 EXPECT_TRUE(response_info.headers->HasHeaderValue("Connection", "Upgrade"));
924 EXPECT_TRUE(response_info.headers->HasHeaderValue("Upgrade", "websocket"));
925 EXPECT_EQ(read_buffer->capacity(), read_buffer->offset());
926 EXPECT_EQ("a fake websocket frame",
927 base::StringPiece(read_buffer->StartOfBuffer(),
928 read_buffer->capacity()));
sclittlebe1ccf62015-09-02 19:40:36929
930 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
931 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)) -
932 static_cast<int64_t>(strlen("a fake websocket frame")),
933 parser.received_bytes());
[email protected]df8e6382013-11-07 00:19:06934}
935
[email protected]390489b2013-12-09 10:49:01936// Helper class for constructing HttpStreamParser and running GET requests.
937class SimpleGetRunner {
938 public:
939 SimpleGetRunner() : read_buffer_(new GrowableIOBuffer), sequence_number_(0) {
940 writes_.push_back(MockWrite(
941 SYNCHRONOUS, sequence_number_++, "GET / HTTP/1.1\r\n\r\n"));
942 }
943
944 HttpStreamParser* parser() { return parser_.get(); }
945 GrowableIOBuffer* read_buffer() { return read_buffer_.get(); }
946 HttpResponseInfo* response_info() { return &response_info_; }
947
948 void AddInitialData(const std::string& data) {
949 int offset = read_buffer_->offset();
950 int size = data.size();
951 read_buffer_->SetCapacity(offset + size);
952 memcpy(read_buffer_->StartOfBuffer() + offset, data.data(), size);
953 read_buffer_->set_offset(offset + size);
954 }
955
956 void AddRead(const std::string& data) {
957 reads_.push_back(MockRead(SYNCHRONOUS, sequence_number_++, data.data()));
958 }
959
960 void SetupParserAndSendRequest() {
961 reads_.push_back(MockRead(SYNCHRONOUS, 0, sequence_number_++)); // EOF
962
mmenkef22be0ee2015-06-05 15:39:02963 data_.reset(new SequencedSocketData(&reads_.front(), reads_.size(),
964 &writes_.front(), writes_.size()));
965 socket_handle_ = CreateConnectedSocketHandle(data_.get());
[email protected]390489b2013-12-09 10:49:01966
967 request_info_.method = "GET";
968 request_info_.url = GURL("http://localhost");
969 request_info_.load_flags = LOAD_NORMAL;
970
971 parser_.reset(new HttpStreamParser(
972 socket_handle_.get(), &request_info_, read_buffer(), BoundNetLog()));
973
mmenkef22be0ee2015-06-05 15:39:02974 TestCompletionCallback callback;
975 ASSERT_EQ(OK, parser_->SendRequest("GET / HTTP/1.1\r\n", request_headers_,
976 &response_info_, callback.callback()));
[email protected]390489b2013-12-09 10:49:01977 }
978
979 void ReadHeaders() {
980 TestCompletionCallback callback;
981 EXPECT_EQ(OK, parser_->ReadResponseHeaders(callback.callback()));
982 }
983
984 void ReadBody(int user_buf_len, int* read_lengths) {
985 TestCompletionCallback callback;
986 scoped_refptr<IOBuffer> buffer = new IOBuffer(user_buf_len);
987 int rv;
988 int i = 0;
989 while (true) {
dcheng48459ac22014-08-26 00:46:41990 rv = parser_->ReadResponseBody(
991 buffer.get(), user_buf_len, callback.callback());
[email protected]390489b2013-12-09 10:49:01992 EXPECT_EQ(read_lengths[i], rv);
993 i++;
994 if (rv <= 0)
995 return;
996 }
997 }
998
999 private:
1000 HttpRequestHeaders request_headers_;
1001 HttpResponseInfo response_info_;
1002 HttpRequestInfo request_info_;
1003 scoped_refptr<GrowableIOBuffer> read_buffer_;
1004 std::vector<MockRead> reads_;
1005 std::vector<MockWrite> writes_;
1006 scoped_ptr<ClientSocketHandle> socket_handle_;
mmenkef22be0ee2015-06-05 15:39:021007 scoped_ptr<SequencedSocketData> data_;
[email protected]390489b2013-12-09 10:49:011008 scoped_ptr<HttpStreamParser> parser_;
1009 int sequence_number_;
1010};
1011
1012// Test that HTTP/0.9 response size is correctly calculated.
1013TEST(HttpStreamParser, ReceivedBytesNoHeaders) {
1014 std::string response = "hello\r\nworld\r\n";
1015
1016 SimpleGetRunner get_runner;
1017 get_runner.AddRead(response);
1018 get_runner.SetupParserAndSendRequest();
1019 get_runner.ReadHeaders();
1020 EXPECT_EQ(0, get_runner.parser()->received_bytes());
[email protected]3cb5fc22013-12-12 19:40:561021 int response_size = response.size();
[email protected]390489b2013-12-09 10:49:011022 int read_lengths[] = {response_size, 0};
1023 get_runner.ReadBody(response_size, read_lengths);
1024 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1025}
1026
1027// Test basic case where there is no keep-alive or extra data from the socket,
1028// and the entire response is received in a single read.
1029TEST(HttpStreamParser, ReceivedBytesNormal) {
1030 std::string headers = "HTTP/1.1 200 OK\r\n"
1031 "Content-Length: 7\r\n\r\n";
1032 std::string body = "content";
1033 std::string response = headers + body;
1034
1035 SimpleGetRunner get_runner;
1036 get_runner.AddRead(response);
1037 get_runner.SetupParserAndSendRequest();
1038 get_runner.ReadHeaders();
1039 int64 headers_size = headers.size();
1040 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
[email protected]3cb5fc22013-12-12 19:40:561041 int body_size = body.size();
[email protected]390489b2013-12-09 10:49:011042 int read_lengths[] = {body_size, 0};
1043 get_runner.ReadBody(body_size, read_lengths);
1044 int64 response_size = response.size();
1045 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1046}
1047
1048// Test that bytes that represent "next" response are not counted
1049// as current response "received_bytes".
1050TEST(HttpStreamParser, ReceivedBytesExcludesNextResponse) {
1051 std::string headers = "HTTP/1.1 200 OK\r\n"
1052 "Content-Length: 8\r\n\r\n";
1053 std::string body = "content8";
1054 std::string response = headers + body;
1055 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
1056 std::string data = response + next_response;
1057
1058 SimpleGetRunner get_runner;
1059 get_runner.AddRead(data);
1060 get_runner.SetupParserAndSendRequest();
1061 get_runner.ReadHeaders();
1062 EXPECT_EQ(39, get_runner.parser()->received_bytes());
1063 int64 headers_size = headers.size();
1064 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
[email protected]3cb5fc22013-12-12 19:40:561065 int body_size = body.size();
[email protected]390489b2013-12-09 10:49:011066 int read_lengths[] = {body_size, 0};
1067 get_runner.ReadBody(body_size, read_lengths);
1068 int64 response_size = response.size();
1069 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1070 int64 next_response_size = next_response.size();
1071 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1072}
1073
1074// Test that "received_bytes" calculation works fine when last read
1075// contains more data than requested by user.
1076// We send data in two reads:
1077// 1) Headers + beginning of response
1078// 2) remaining part of response + next response start
1079// We setup user read buffer so it fully accepts the beginnig of response
1080// body, but it is larger that remaining part of body.
1081TEST(HttpStreamParser, ReceivedBytesMultiReadExcludesNextResponse) {
1082 std::string headers = "HTTP/1.1 200 OK\r\n"
1083 "Content-Length: 36\r\n\r\n";
1084 int64 user_buf_len = 32;
1085 std::string body_start = std::string(user_buf_len, '#');
[email protected]3cb5fc22013-12-12 19:40:561086 int body_start_size = body_start.size();
[email protected]390489b2013-12-09 10:49:011087 EXPECT_EQ(user_buf_len, body_start_size);
1088 std::string response_start = headers + body_start;
1089 std::string body_end = "abcd";
1090 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
1091 std::string response_end = body_end + next_response;
1092
1093 SimpleGetRunner get_runner;
1094 get_runner.AddRead(response_start);
1095 get_runner.AddRead(response_end);
1096 get_runner.SetupParserAndSendRequest();
1097 get_runner.ReadHeaders();
1098 int64 headers_size = headers.size();
1099 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
[email protected]3cb5fc22013-12-12 19:40:561100 int body_end_size = body_end.size();
[email protected]390489b2013-12-09 10:49:011101 int read_lengths[] = {body_start_size, body_end_size, 0};
1102 get_runner.ReadBody(body_start_size, read_lengths);
1103 int64 response_size = response_start.size() + body_end_size;
1104 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1105 int64 next_response_size = next_response.size();
1106 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1107}
1108
1109// Test that "received_bytes" calculation works fine when there is no
1110// network activity at all; that is when all data is read from read buffer.
1111// In this case read buffer contains two responses. We expect that only
1112// bytes that correspond to the first one are taken into account.
1113TEST(HttpStreamParser, ReceivedBytesFromReadBufExcludesNextResponse) {
1114 std::string headers = "HTTP/1.1 200 OK\r\n"
1115 "Content-Length: 7\r\n\r\n";
1116 std::string body = "content";
1117 std::string response = headers + body;
1118 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
1119 std::string data = response + next_response;
1120
1121 SimpleGetRunner get_runner;
1122 get_runner.AddInitialData(data);
1123 get_runner.SetupParserAndSendRequest();
1124 get_runner.ReadHeaders();
1125 int64 headers_size = headers.size();
1126 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
[email protected]3cb5fc22013-12-12 19:40:561127 int body_size = body.size();
[email protected]390489b2013-12-09 10:49:011128 int read_lengths[] = {body_size, 0};
1129 get_runner.ReadBody(body_size, read_lengths);
1130 int64 response_size = response.size();
1131 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1132 int64 next_response_size = next_response.size();
1133 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1134}
1135
1136// Test calculating "received_bytes" when part of request has been already
1137// loaded and placed to read buffer by previous stream parser.
1138TEST(HttpStreamParser, ReceivedBytesUseReadBuf) {
1139 std::string buffer = "HTTP/1.1 200 OK\r\n";
1140 std::string remaining_headers = "Content-Length: 7\r\n\r\n";
1141 int64 headers_size = buffer.size() + remaining_headers.size();
1142 std::string body = "content";
1143 std::string response = remaining_headers + body;
1144
1145 SimpleGetRunner get_runner;
1146 get_runner.AddInitialData(buffer);
1147 get_runner.AddRead(response);
1148 get_runner.SetupParserAndSendRequest();
1149 get_runner.ReadHeaders();
1150 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
[email protected]3cb5fc22013-12-12 19:40:561151 int body_size = body.size();
[email protected]390489b2013-12-09 10:49:011152 int read_lengths[] = {body_size, 0};
1153 get_runner.ReadBody(body_size, read_lengths);
1154 EXPECT_EQ(headers_size + body_size, get_runner.parser()->received_bytes());
1155 EXPECT_EQ(0, get_runner.read_buffer()->offset());
1156}
1157
1158// Test the case when the resulting read_buf contains both unused bytes and
1159// bytes ejected by chunked-encoding filter.
1160TEST(HttpStreamParser, ReceivedBytesChunkedTransferExcludesNextResponse) {
1161 std::string response = "HTTP/1.1 200 OK\r\n"
1162 "Transfer-Encoding: chunked\r\n\r\n"
1163 "7\r\nChunk 1\r\n"
1164 "8\r\nChunky 2\r\n"
1165 "6\r\nTest 3\r\n"
1166 "0\r\n\r\n";
1167 std::string next_response = "foo bar\r\n";
1168 std::string data = response + next_response;
1169
1170 SimpleGetRunner get_runner;
1171 get_runner.AddInitialData(data);
1172 get_runner.SetupParserAndSendRequest();
1173 get_runner.ReadHeaders();
1174 int read_lengths[] = {4, 3, 6, 2, 6, 0};
1175 get_runner.ReadBody(7, read_lengths);
1176 int64 response_size = response.size();
1177 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1178 int64 next_response_size = next_response.size();
1179 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
1180}
1181
1182// Test that data transfered in multiple reads is correctly processed.
1183// We feed data into 4-bytes reads. Also we set length of read
1184// buffer to 5-bytes to test all possible buffer misaligments.
1185TEST(HttpStreamParser, ReceivedBytesMultipleReads) {
1186 std::string headers = "HTTP/1.1 200 OK\r\n"
1187 "Content-Length: 33\r\n\r\n";
1188 std::string body = "foo bar baz\r\n"
1189 "sputnik mir babushka";
1190 std::string response = headers + body;
1191
1192 size_t receive_length = 4;
1193 std::vector<std::string> blocks;
1194 for (size_t i = 0; i < response.size(); i += receive_length) {
1195 size_t length = std::min(receive_length, response.size() - i);
1196 blocks.push_back(response.substr(i, length));
1197 }
1198
1199 SimpleGetRunner get_runner;
[email protected]3cb5fc22013-12-12 19:40:561200 for (std::vector<std::string>::size_type i = 0; i < blocks.size(); ++i)
[email protected]390489b2013-12-09 10:49:011201 get_runner.AddRead(blocks[i]);
1202 get_runner.SetupParserAndSendRequest();
1203 get_runner.ReadHeaders();
1204 int64 headers_size = headers.size();
1205 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1206 int read_lengths[] = {1, 4, 4, 4, 4, 4, 4, 4, 4, 0};
1207 get_runner.ReadBody(receive_length + 1, read_lengths);
1208 int64 response_size = response.size();
1209 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1210}
1211
1212// Test that "continue" HTTP header is counted as "received_bytes".
1213TEST(HttpStreamParser, ReceivedBytesIncludesContinueHeader) {
1214 std::string status100 = "HTTP/1.1 100 OK\r\n\r\n";
1215 std::string headers = "HTTP/1.1 200 OK\r\n"
1216 "Content-Length: 7\r\n\r\n";
1217 int64 headers_size = status100.size() + headers.size();
1218 std::string body = "content";
1219 std::string response = headers + body;
1220
1221 SimpleGetRunner get_runner;
1222 get_runner.AddRead(status100);
1223 get_runner.AddRead(response);
1224 get_runner.SetupParserAndSendRequest();
1225 get_runner.ReadHeaders();
1226 EXPECT_EQ(100, get_runner.response_info()->headers->response_code());
1227 int64 status100_size = status100.size();
1228 EXPECT_EQ(status100_size, get_runner.parser()->received_bytes());
1229 get_runner.ReadHeaders();
1230 EXPECT_EQ(200, get_runner.response_info()->headers->response_code());
1231 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1232 int64 response_size = headers_size + body.size();
[email protected]3cb5fc22013-12-12 19:40:561233 int body_size = body.size();
[email protected]390489b2013-12-09 10:49:011234 int read_lengths[] = {body_size, 0};
1235 get_runner.ReadBody(body_size, read_lengths);
1236 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1237}
1238
[email protected]ecab6e052014-05-16 14:58:121239// Test that an HttpStreamParser can be read from after it's received headers
1240// and data structures owned by its owner have been deleted. This happens
1241// when a ResponseBodyDrainer is used.
1242TEST(HttpStreamParser, ReadAfterUnownedObjectsDestroyed) {
1243 MockWrite writes[] = {
1244 MockWrite(SYNCHRONOUS, 0,
1245 "GET /foo.html HTTP/1.1\r\n\r\n"),
[email protected]ecab6e052014-05-16 14:58:121246 };
1247
1248 const int kBodySize = 1;
1249 MockRead reads[] = {
mmenkef22be0ee2015-06-05 15:39:021250 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 OK\r\n"),
sclittlebe1ccf62015-09-02 19:40:361251 MockRead(SYNCHRONOUS, 2, "Content-Length: 1\r\n"),
mmenkef22be0ee2015-06-05 15:39:021252 MockRead(SYNCHRONOUS, 3, "Connection: Keep-Alive\r\n\r\n"),
1253 MockRead(SYNCHRONOUS, 4, "1"),
1254 MockRead(SYNCHRONOUS, 0, 5), // EOF
[email protected]ecab6e052014-05-16 14:58:121255 };
1256
mmenkef22be0ee2015-06-05 15:39:021257 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
1258 scoped_ptr<ClientSocketHandle> socket_handle =
1259 CreateConnectedSocketHandle(&data);
[email protected]ecab6e052014-05-16 14:58:121260
1261 scoped_ptr<HttpRequestInfo> request_info(new HttpRequestInfo());
1262 request_info->method = "GET";
1263 request_info->url = GURL("http://somewhere/foo.html");
1264
1265 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
1266 HttpStreamParser parser(socket_handle.get(), request_info.get(),
1267 read_buffer.get(), BoundNetLog());
1268
1269 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders());
1270 scoped_ptr<HttpResponseInfo> response_info(new HttpResponseInfo());
mmenkef22be0ee2015-06-05 15:39:021271 TestCompletionCallback callback;
[email protected]ecab6e052014-05-16 14:58:121272 ASSERT_EQ(OK, parser.SendRequest("GET /foo.html HTTP/1.1\r\n",
1273 *request_headers, response_info.get(), callback.callback()));
1274 ASSERT_EQ(OK, parser.ReadResponseHeaders(callback.callback()));
1275
1276 // If the object that owns the HttpStreamParser is deleted, it takes the
1277 // objects passed to the HttpStreamParser with it.
1278 request_info.reset();
1279 request_headers.reset();
1280 response_info.reset();
1281
1282 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
1283 ASSERT_EQ(kBodySize, parser.ReadResponseBody(
1284 body_buffer.get(), kBodySize, callback.callback()));
sclittlebe1ccf62015-09-02 19:40:361285
1286 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
1287 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
[email protected]ecab6e052014-05-16 14:58:121288}
1289
[email protected]390489b2013-12-09 10:49:011290} // namespace
[email protected]df8e6382013-11-07 00:19:061291
[email protected]6db833d12012-01-21 00:45:191292} // namespace net