blob: b62f53918a1dc64062847fb2d4847cd597003442 [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
[email protected]7a1fcff2012-01-24 01:07:497#include "base/file_path.h"
8#include "base/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
[email protected]4750937f2012-06-15 20:44:2110#include "base/memory/ref_counted.h"
[email protected]6db833d12012-01-21 00:45:1911#include "base/string_piece.h"
12#include "base/stringprintf.h"
[email protected]4750937f2012-06-15 20:44:2113#include "googleurl/src/gurl.h"
14#include "net/base/io_buffer.h"
[email protected]6db833d12012-01-21 00:45:1915#include "net/base/net_errors.h"
[email protected]4750937f2012-06-15 20:44:2116#include "net/base/test_completion_callback.h"
[email protected]b2d26cfd2012-12-11 10:36:0617#include "net/base/upload_bytes_element_reader.h"
[email protected]7a1fcff2012-01-24 01:07:4918#include "net/base/upload_data_stream.h"
[email protected]b2d26cfd2012-12-11 10:36:0619#include "net/base/upload_file_element_reader.h"
[email protected]4750937f2012-06-15 20:44:2120#include "net/http/http_request_headers.h"
21#include "net/http/http_request_info.h"
22#include "net/http/http_response_info.h"
23#include "net/socket/client_socket_handle.h"
24#include "net/socket/socket_test_util.h"
[email protected]6db833d12012-01-21 00:45:1925#include "testing/gtest/include/gtest/gtest.h"
26
27namespace net {
28
29const size_t kOutputSize = 1024; // Just large enough for this test.
30// The number of bytes that can fit in a buffer of kOutputSize.
31const size_t kMaxPayloadSize =
32 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize;
33
34// The empty payload is how the last chunk is encoded.
35TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
36 char output[kOutputSize];
37
38 const base::StringPiece kPayload = "";
39 const base::StringPiece kExpected = "0\r\n\r\n";
40 const int num_bytes_written =
41 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
42 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
43 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
44}
45
46TEST(HttpStreamParser, EncodeChunk_ShortPayload) {
47 char output[kOutputSize];
48
49 const std::string kPayload("foo\x00\x11\x22", 6);
50 // 11 = payload size + sizeof("6") + CRLF x 2.
51 const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11);
52 const int num_bytes_written =
53 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
54 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
55 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
56}
57
58TEST(HttpStreamParser, EncodeChunk_LargePayload) {
59 char output[kOutputSize];
60
61 const std::string kPayload(1000, '\xff'); // '\xff' x 1000.
62 // 3E8 = 1000 in hex.
63 const std::string kExpected = "3E8\r\n" + kPayload + "\r\n";
64 const int num_bytes_written =
65 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
66 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
67 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
68}
69
70TEST(HttpStreamParser, EncodeChunk_FullPayload) {
71 char output[kOutputSize];
72
73 const std::string kPayload(kMaxPayloadSize, '\xff');
74 // 3F4 = 1012 in hex.
75 const std::string kExpected = "3F4\r\n" + kPayload + "\r\n";
76 const int num_bytes_written =
77 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
78 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
79 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
80}
81
82TEST(HttpStreamParser, EncodeChunk_TooLargePayload) {
83 char output[kOutputSize];
84
85 // The payload is one byte larger the output buffer size.
86 const std::string kPayload(kMaxPayloadSize + 1, '\xff');
87 const int num_bytes_written =
88 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
89 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written);
90}
91
[email protected]75577ec2012-01-24 23:41:5092TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) {
[email protected]7a1fcff2012-01-24 01:07:4993 // Shouldn't be merged if upload data is non-existent.
[email protected]75577ec2012-01-24 23:41:5094 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
95 "some header", NULL));
[email protected]7a1fcff2012-01-24 01:07:4996}
97
[email protected]75577ec2012-01-24 23:41:5098TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) {
[email protected]b2d26cfd2012-12-11 10:36:0699 ScopedVector<UploadElementReader> element_readers;
100 scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
[email protected]df7adc62012-09-18 14:01:53101 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49102 // Shouldn't be merged if upload data is empty.
[email protected]75577ec2012-01-24 23:41:50103 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
104 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49105}
106
[email protected]75577ec2012-01-24 23:41:50107TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) {
[email protected]7a1fcff2012-01-24 01:07:49108 const std::string payload = "123";
[email protected]b2d26cfd2012-12-11 10:36:06109 scoped_ptr<UploadDataStream> body(
110 new UploadDataStream(UploadDataStream::CHUNKED, 0));
111 body->AppendChunk(payload.data(), payload.size(), true);
[email protected]df7adc62012-09-18 14:01:53112 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49113 // Shouldn't be merged if upload data carries chunked data.
[email protected]75577ec2012-01-24 23:41:50114 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
115 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49116}
117
[email protected]75577ec2012-01-24 23:41:50118TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) {
[email protected]b2d26cfd2012-12-11 10:36:06119 ScopedVector<UploadElementReader> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49120
121 // Create an empty temporary file.
[email protected]ea1a3f62012-11-16 20:34:23122 base::ScopedTempDir temp_dir;
[email protected]7a1fcff2012-01-24 01:07:49123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
124 FilePath temp_file_path;
125 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(),
126 &temp_file_path));
127
[email protected]b2d26cfd2012-12-11 10:36:06128 element_readers.push_back(new UploadFileElementReader(
129 temp_file_path, 0, 0, base::Time()));
[email protected]7a1fcff2012-01-24 01:07:49130
[email protected]b2d26cfd2012-12-11 10:36:06131 scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
[email protected]df7adc62012-09-18 14:01:53132 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49133 // Shouldn't be merged if upload data carries a file, as it's not in-memory.
[email protected]75577ec2012-01-24 23:41:50134 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
135 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49136}
137
[email protected]75577ec2012-01-24 23:41:50138TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) {
[email protected]b2d26cfd2012-12-11 10:36:06139 ScopedVector<UploadElementReader> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49140 const std::string payload = "123";
[email protected]b2d26cfd2012-12-11 10:36:06141 element_readers.push_back(new UploadBytesElementReader(
142 payload.data(), payload.size()));
[email protected]7a1fcff2012-01-24 01:07:49143
[email protected]b2d26cfd2012-12-11 10:36:06144 scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
[email protected]df7adc62012-09-18 14:01:53145 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49146 // Yes, should be merged if the in-memory body is small here.
[email protected]75577ec2012-01-24 23:41:50147 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
148 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49149}
150
[email protected]75577ec2012-01-24 23:41:50151TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) {
[email protected]b2d26cfd2012-12-11 10:36:06152 ScopedVector<UploadElementReader> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49153 const std::string payload(10000, 'a'); // 'a' x 10000.
[email protected]b2d26cfd2012-12-11 10:36:06154 element_readers.push_back(new UploadBytesElementReader(
155 payload.data(), payload.size()));
[email protected]7a1fcff2012-01-24 01:07:49156
[email protected]b2d26cfd2012-12-11 10:36:06157 scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
[email protected]df7adc62012-09-18 14:01:53158 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49159 // Shouldn't be merged if the in-memory body is large here.
[email protected]75577ec2012-01-24 23:41:50160 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
161 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49162}
163
[email protected]4750937f2012-06-15 20:44:21164// Test to ensure the HttpStreamParser state machine does not get confused
165// when sending a request with a chunked body, where chunks become available
166// asynchronously, over a socket where writes may also complete
167// asynchronously.
168// This is a regression test for http://crbug.com/132243
169TEST(HttpStreamParser, AsyncChunkAndAsyncSocket) {
170 // The chunks that will be written in the request, as reflected in the
171 // MockWrites below.
172 static const char kChunk1[] = "Chunk 1";
173 static const char kChunk2[] = "Chunky 2";
174 static const char kChunk3[] = "Test 3";
175
176 MockWrite writes[] = {
177 MockWrite(ASYNC, 0,
178 "GET /one.html HTTP/1.1\r\n"
179 "Host: localhost\r\n"
180 "Transfer-Encoding: chunked\r\n"
181 "Connection: keep-alive\r\n\r\n"),
182 MockWrite(ASYNC, 1, "7\r\nChunk 1\r\n"),
183 MockWrite(ASYNC, 2, "8\r\nChunky 2\r\n"),
184 MockWrite(ASYNC, 3, "6\r\nTest 3\r\n"),
185 MockWrite(ASYNC, 4, "0\r\n\r\n"),
186 };
187
188 // The size of the response body, as reflected in the Content-Length of the
189 // MockRead below.
190 static const int kBodySize = 8;
191
192 MockRead reads[] = {
193 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"),
194 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"),
195 MockRead(ASYNC, 7, "one.html"),
[email protected]d55b30a2012-07-21 00:35:39196 MockRead(SYNCHRONOUS, 0, 8), // EOF
[email protected]4750937f2012-06-15 20:44:21197 };
198
[email protected]b2d26cfd2012-12-11 10:36:06199 UploadDataStream upload_stream(UploadDataStream::CHUNKED, 0);
200 upload_stream.AppendChunk(kChunk1, arraysize(kChunk1) - 1, false);
[email protected]bf3eb002012-11-15 05:50:11201 ASSERT_EQ(OK, upload_stream.InitSync());
[email protected]9a963892012-11-01 11:48:13202
[email protected]dd54bd82012-07-19 23:44:57203 DeterministicSocketData data(reads, arraysize(reads),
204 writes, arraysize(writes));
205 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
[email protected]4750937f2012-06-15 20:44:21206
207 scoped_ptr<DeterministicMockTCPClientSocket> transport(
[email protected]dd54bd82012-07-19 23:44:57208 new DeterministicMockTCPClientSocket(NULL, &data));
209 data.set_socket(transport->AsWeakPtr());
[email protected]4750937f2012-06-15 20:44:21210
211 TestCompletionCallback callback;
212 int rv = transport->Connect(callback.callback());
213 rv = callback.GetResult(rv);
214 ASSERT_EQ(OK, rv);
215
216 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
217 socket_handle->set_socket(transport.release());
218
219 HttpRequestInfo request_info;
220 request_info.method = "GET";
221 request_info.url = GURL("http://localhost");
222 request_info.load_flags = LOAD_NORMAL;
[email protected]bf3eb002012-11-15 05:50:11223 request_info.upload_data_stream = &upload_stream;
[email protected]4750937f2012-06-15 20:44:21224
225 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
226 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer,
227 BoundNetLog());
228
[email protected]4750937f2012-06-15 20:44:21229 HttpRequestHeaders request_headers;
230 request_headers.SetHeader("Host", "localhost");
231 request_headers.SetHeader("Transfer-Encoding", "chunked");
232 request_headers.SetHeader("Connection", "keep-alive");
233
234 HttpResponseInfo response_info;
235 // This will attempt to Write() the initial request and headers, which will
236 // complete asynchronously.
237 rv = parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
[email protected]bf3eb002012-11-15 05:50:11238 &response_info, callback.callback());
[email protected]4750937f2012-06-15 20:44:21239 ASSERT_EQ(ERR_IO_PENDING, rv);
240
241 // Complete the initial request write. Additionally, this should enqueue the
242 // first chunk.
[email protected]dd54bd82012-07-19 23:44:57243 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21244 ASSERT_FALSE(callback.have_result());
245
246 // Now append another chunk (while the first write is still pending), which
247 // should not confuse the state machine.
[email protected]b2d26cfd2012-12-11 10:36:06248 upload_stream.AppendChunk(kChunk2, arraysize(kChunk2) - 1, false);
[email protected]4750937f2012-06-15 20:44:21249 ASSERT_FALSE(callback.have_result());
250
251 // Complete writing the first chunk, which should then enqueue the second
252 // chunk for writing and return, because it is set to complete
253 // asynchronously.
[email protected]dd54bd82012-07-19 23:44:57254 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21255 ASSERT_FALSE(callback.have_result());
256
257 // Complete writing the second chunk. However, because no chunks are
258 // available yet, no further writes should be called until a new chunk is
259 // added.
[email protected]dd54bd82012-07-19 23:44:57260 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21261 ASSERT_FALSE(callback.have_result());
262
263 // Add the final chunk. This will enqueue another write, but it will not
264 // complete due to the async nature.
[email protected]b2d26cfd2012-12-11 10:36:06265 upload_stream.AppendChunk(kChunk3, arraysize(kChunk3) - 1, true);
[email protected]4750937f2012-06-15 20:44:21266 ASSERT_FALSE(callback.have_result());
267
268 // Finalize writing the last chunk, which will enqueue the trailer.
[email protected]dd54bd82012-07-19 23:44:57269 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21270 ASSERT_FALSE(callback.have_result());
271
272 // Finalize writing the trailer.
[email protected]dd54bd82012-07-19 23:44:57273 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21274 ASSERT_TRUE(callback.have_result());
275
276 // Warning: This will hang if the callback doesn't already have a result,
277 // due to the deterministic socket provider. Do not remove the above
278 // ASSERT_TRUE, which will avoid this hang.
279 rv = callback.WaitForResult();
280 ASSERT_EQ(OK, rv);
281
282 // Attempt to read the response status and the response headers.
283 rv = parser.ReadResponseHeaders(callback.callback());
284 ASSERT_EQ(ERR_IO_PENDING, rv);
[email protected]dd54bd82012-07-19 23:44:57285 data.RunFor(2);
[email protected]4750937f2012-06-15 20:44:21286
287 ASSERT_TRUE(callback.have_result());
288 rv = callback.WaitForResult();
289 ASSERT_GT(rv, 0);
290
291 // Finally, attempt to read the response body.
292 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
293 rv = parser.ReadResponseBody(body_buffer, kBodySize, callback.callback());
294 ASSERT_EQ(ERR_IO_PENDING, rv);
[email protected]dd54bd82012-07-19 23:44:57295 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21296
297 ASSERT_TRUE(callback.have_result());
298 rv = callback.WaitForResult();
299 ASSERT_EQ(kBodySize, rv);
300}
301
[email protected]6db833d12012-01-21 00:45:19302} // namespace net