blob: bfe2d46afa300ef1e9e5c6c9cdd794890677a9a7 [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]4750937f2012-06-15 20:44:219#include "base/memory/ref_counted.h"
[email protected]7a1fcff2012-01-24 01:07:4910#include "base/scoped_temp_dir.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]7a1fcff2012-01-24 01:07:4917#include "net/base/upload_data.h"
18#include "net/base/upload_data_stream.h"
[email protected]4750937f2012-06-15 20:44:2119#include "net/http/http_request_headers.h"
20#include "net/http/http_request_info.h"
21#include "net/http/http_response_info.h"
22#include "net/socket/client_socket_handle.h"
23#include "net/socket/socket_test_util.h"
[email protected]6db833d12012-01-21 00:45:1924#include "testing/gtest/include/gtest/gtest.h"
25
26namespace net {
27
28const size_t kOutputSize = 1024; // Just large enough for this test.
29// The number of bytes that can fit in a buffer of kOutputSize.
30const size_t kMaxPayloadSize =
31 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize;
32
33// The empty payload is how the last chunk is encoded.
34TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
35 char output[kOutputSize];
36
37 const base::StringPiece kPayload = "";
38 const base::StringPiece kExpected = "0\r\n\r\n";
39 const int num_bytes_written =
40 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
41 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
42 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
43}
44
45TEST(HttpStreamParser, EncodeChunk_ShortPayload) {
46 char output[kOutputSize];
47
48 const std::string kPayload("foo\x00\x11\x22", 6);
49 // 11 = payload size + sizeof("6") + CRLF x 2.
50 const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11);
51 const int num_bytes_written =
52 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
53 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
54 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
55}
56
57TEST(HttpStreamParser, EncodeChunk_LargePayload) {
58 char output[kOutputSize];
59
60 const std::string kPayload(1000, '\xff'); // '\xff' x 1000.
61 // 3E8 = 1000 in hex.
62 const std::string kExpected = "3E8\r\n" + kPayload + "\r\n";
63 const int num_bytes_written =
64 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
65 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
66 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
67}
68
69TEST(HttpStreamParser, EncodeChunk_FullPayload) {
70 char output[kOutputSize];
71
72 const std::string kPayload(kMaxPayloadSize, '\xff');
73 // 3F4 = 1012 in hex.
74 const std::string kExpected = "3F4\r\n" + kPayload + "\r\n";
75 const int num_bytes_written =
76 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
77 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
78 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
79}
80
81TEST(HttpStreamParser, EncodeChunk_TooLargePayload) {
82 char output[kOutputSize];
83
84 // The payload is one byte larger the output buffer size.
85 const std::string kPayload(kMaxPayloadSize + 1, '\xff');
86 const int num_bytes_written =
87 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
88 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written);
89}
90
[email protected]75577ec2012-01-24 23:41:5091TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) {
[email protected]7a1fcff2012-01-24 01:07:4992 // Shouldn't be merged if upload data is non-existent.
[email protected]75577ec2012-01-24 23:41:5093 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
94 "some header", NULL));
[email protected]7a1fcff2012-01-24 01:07:4995}
96
[email protected]75577ec2012-01-24 23:41:5097TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) {
[email protected]7a1fcff2012-01-24 01:07:4998 scoped_refptr<UploadData> upload_data = new UploadData;
[email protected]49ed6cc2012-02-02 08:59:1699 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data));
[email protected]df7adc62012-09-18 14:01:53100 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49101 // Shouldn't be merged if upload data is empty.
[email protected]75577ec2012-01-24 23:41:50102 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
103 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49104}
105
[email protected]75577ec2012-01-24 23:41:50106TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) {
[email protected]7a1fcff2012-01-24 01:07:49107 scoped_refptr<UploadData> upload_data = new UploadData;
108 upload_data->set_is_chunked(true);
109 const std::string payload = "123";
110 upload_data->AppendChunk(payload.data(), payload.size(), true);
111
[email protected]49ed6cc2012-02-02 08:59:16112 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data));
[email protected]df7adc62012-09-18 14:01:53113 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49114 // Shouldn't be merged if upload data carries chunked data.
[email protected]75577ec2012-01-24 23:41:50115 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
116 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49117}
118
[email protected]75577ec2012-01-24 23:41:50119TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) {
[email protected]7a1fcff2012-01-24 01:07:49120 scoped_refptr<UploadData> upload_data = new UploadData;
121
122 // Create an empty temporary file.
123 ScopedTempDir temp_dir;
124 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
125 FilePath temp_file_path;
126 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(),
127 &temp_file_path));
128
129 upload_data->AppendFileRange(temp_file_path, 0, 0, base::Time());
130
[email protected]49ed6cc2012-02-02 08:59:16131 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data));
[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]7a1fcff2012-01-24 01:07:49139 scoped_refptr<UploadData> upload_data = new UploadData;
140 const std::string payload = "123";
141 upload_data->AppendBytes(payload.data(), payload.size());
142
[email protected]49ed6cc2012-02-02 08:59:16143 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data));
[email protected]df7adc62012-09-18 14:01:53144 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49145 // Yes, should be merged if the in-memory body is small here.
[email protected]75577ec2012-01-24 23:41:50146 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
147 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49148}
149
[email protected]75577ec2012-01-24 23:41:50150TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) {
[email protected]7a1fcff2012-01-24 01:07:49151 scoped_refptr<UploadData> upload_data = new UploadData;
152 const std::string payload(10000, 'a'); // 'a' x 10000.
153 upload_data->AppendBytes(payload.data(), payload.size());
154
[email protected]49ed6cc2012-02-02 08:59:16155 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data));
[email protected]df7adc62012-09-18 14:01:53156 ASSERT_EQ(OK, body->InitSync());
[email protected]7a1fcff2012-01-24 01:07:49157 // Shouldn't be merged if the in-memory body is large here.
[email protected]75577ec2012-01-24 23:41:50158 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
159 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49160}
161
[email protected]4750937f2012-06-15 20:44:21162// Test to ensure the HttpStreamParser state machine does not get confused
163// when sending a request with a chunked body, where chunks become available
164// asynchronously, over a socket where writes may also complete
165// asynchronously.
166// This is a regression test for http://crbug.com/132243
167TEST(HttpStreamParser, AsyncChunkAndAsyncSocket) {
168 // The chunks that will be written in the request, as reflected in the
169 // MockWrites below.
170 static const char kChunk1[] = "Chunk 1";
171 static const char kChunk2[] = "Chunky 2";
172 static const char kChunk3[] = "Test 3";
173
174 MockWrite writes[] = {
175 MockWrite(ASYNC, 0,
176 "GET /one.html HTTP/1.1\r\n"
177 "Host: localhost\r\n"
178 "Transfer-Encoding: chunked\r\n"
179 "Connection: keep-alive\r\n\r\n"),
180 MockWrite(ASYNC, 1, "7\r\nChunk 1\r\n"),
181 MockWrite(ASYNC, 2, "8\r\nChunky 2\r\n"),
182 MockWrite(ASYNC, 3, "6\r\nTest 3\r\n"),
183 MockWrite(ASYNC, 4, "0\r\n\r\n"),
184 };
185
186 // The size of the response body, as reflected in the Content-Length of the
187 // MockRead below.
188 static const int kBodySize = 8;
189
190 MockRead reads[] = {
191 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"),
192 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"),
193 MockRead(ASYNC, 7, "one.html"),
[email protected]d55b30a2012-07-21 00:35:39194 MockRead(SYNCHRONOUS, 0, 8), // EOF
[email protected]4750937f2012-06-15 20:44:21195 };
196
[email protected]9a963892012-11-01 11:48:13197 scoped_refptr<UploadData> upload_data(new UploadData);
198 upload_data->set_is_chunked(true);
199
200 upload_data->AppendChunk(kChunk1, arraysize(kChunk1) - 1, false);
201
[email protected]bf3eb002012-11-15 05:50:11202 UploadDataStream upload_stream(upload_data);
203 ASSERT_EQ(OK, upload_stream.InitSync());
[email protected]9a963892012-11-01 11:48:13204
[email protected]dd54bd82012-07-19 23:44:57205 DeterministicSocketData data(reads, arraysize(reads),
206 writes, arraysize(writes));
207 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
[email protected]4750937f2012-06-15 20:44:21208
209 scoped_ptr<DeterministicMockTCPClientSocket> transport(
[email protected]dd54bd82012-07-19 23:44:57210 new DeterministicMockTCPClientSocket(NULL, &data));
211 data.set_socket(transport->AsWeakPtr());
[email protected]4750937f2012-06-15 20:44:21212
213 TestCompletionCallback callback;
214 int rv = transport->Connect(callback.callback());
215 rv = callback.GetResult(rv);
216 ASSERT_EQ(OK, rv);
217
218 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
219 socket_handle->set_socket(transport.release());
220
221 HttpRequestInfo request_info;
222 request_info.method = "GET";
223 request_info.url = GURL("http://localhost");
224 request_info.load_flags = LOAD_NORMAL;
[email protected]bf3eb002012-11-15 05:50:11225 request_info.upload_data_stream = &upload_stream;
[email protected]4750937f2012-06-15 20:44:21226
227 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
228 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer,
229 BoundNetLog());
230
[email protected]4750937f2012-06-15 20:44:21231 HttpRequestHeaders request_headers;
232 request_headers.SetHeader("Host", "localhost");
233 request_headers.SetHeader("Transfer-Encoding", "chunked");
234 request_headers.SetHeader("Connection", "keep-alive");
235
236 HttpResponseInfo response_info;
237 // This will attempt to Write() the initial request and headers, which will
238 // complete asynchronously.
239 rv = parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
[email protected]bf3eb002012-11-15 05:50:11240 &response_info, callback.callback());
[email protected]4750937f2012-06-15 20:44:21241 ASSERT_EQ(ERR_IO_PENDING, rv);
242
243 // Complete the initial request write. Additionally, this should enqueue the
244 // first chunk.
[email protected]dd54bd82012-07-19 23:44:57245 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21246 ASSERT_FALSE(callback.have_result());
247
248 // Now append another chunk (while the first write is still pending), which
249 // should not confuse the state machine.
250 upload_data->AppendChunk(kChunk2, arraysize(kChunk2) - 1, false);
251 ASSERT_FALSE(callback.have_result());
252
253 // Complete writing the first chunk, which should then enqueue the second
254 // chunk for writing and return, because it is set to complete
255 // asynchronously.
[email protected]dd54bd82012-07-19 23:44:57256 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21257 ASSERT_FALSE(callback.have_result());
258
259 // Complete writing the second chunk. However, because no chunks are
260 // available yet, no further writes should be called until a new chunk is
261 // added.
[email protected]dd54bd82012-07-19 23:44:57262 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21263 ASSERT_FALSE(callback.have_result());
264
265 // Add the final chunk. This will enqueue another write, but it will not
266 // complete due to the async nature.
267 upload_data->AppendChunk(kChunk3, arraysize(kChunk3) - 1, true);
268 ASSERT_FALSE(callback.have_result());
269
270 // Finalize writing the last chunk, which will enqueue the trailer.
[email protected]dd54bd82012-07-19 23:44:57271 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21272 ASSERT_FALSE(callback.have_result());
273
274 // Finalize writing the trailer.
[email protected]dd54bd82012-07-19 23:44:57275 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21276 ASSERT_TRUE(callback.have_result());
277
278 // Warning: This will hang if the callback doesn't already have a result,
279 // due to the deterministic socket provider. Do not remove the above
280 // ASSERT_TRUE, which will avoid this hang.
281 rv = callback.WaitForResult();
282 ASSERT_EQ(OK, rv);
283
284 // Attempt to read the response status and the response headers.
285 rv = parser.ReadResponseHeaders(callback.callback());
286 ASSERT_EQ(ERR_IO_PENDING, rv);
[email protected]dd54bd82012-07-19 23:44:57287 data.RunFor(2);
[email protected]4750937f2012-06-15 20:44:21288
289 ASSERT_TRUE(callback.have_result());
290 rv = callback.WaitForResult();
291 ASSERT_GT(rv, 0);
292
293 // Finally, attempt to read the response body.
294 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
295 rv = parser.ReadResponseBody(body_buffer, kBodySize, callback.callback());
296 ASSERT_EQ(ERR_IO_PENDING, rv);
[email protected]dd54bd82012-07-19 23:44:57297 data.RunFor(1);
[email protected]4750937f2012-06-15 20:44:21298
299 ASSERT_TRUE(callback.have_result());
300 rv = callback.WaitForResult();
301 ASSERT_EQ(kBodySize, rv);
302}
303
[email protected]6db833d12012-01-21 00:45:19304} // namespace net