blob: 3724ae171b79aed8283e07adc3e0e4e545409fbe [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_util.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.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]4db27d82012-12-20 11:50:24101 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[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]4db27d82012-12-20 11:50:24112 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[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());
[email protected]6cdfd7f2013-02-08 20:40:15124 base::FilePath temp_file_path;
[email protected]7a1fcff2012-01-24 01:07:49125 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(
[email protected]671a7142013-01-10 14:08:07129 base::MessageLoopProxy::current(), 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]4db27d82012-12-20 11:50:24132 TestCompletionCallback callback;
133 ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback()));
134 ASSERT_EQ(OK, callback.WaitForResult());
[email protected]7a1fcff2012-01-24 01:07:49135 // Shouldn't be merged if upload data carries a file, as it's not in-memory.
[email protected]75577ec2012-01-24 23:41:50136 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
137 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49138}
139
[email protected]75577ec2012-01-24 23:41:50140TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) {
[email protected]b2d26cfd2012-12-11 10:36:06141 ScopedVector<UploadElementReader> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49142 const std::string payload = "123";
[email protected]b2d26cfd2012-12-11 10:36:06143 element_readers.push_back(new UploadBytesElementReader(
144 payload.data(), payload.size()));
[email protected]7a1fcff2012-01-24 01:07:49145
[email protected]b2d26cfd2012-12-11 10:36:06146 scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
[email protected]4db27d82012-12-20 11:50:24147 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[email protected]7a1fcff2012-01-24 01:07:49148 // Yes, should be merged if the in-memory body is small here.
[email protected]75577ec2012-01-24 23:41:50149 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
150 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49151}
152
[email protected]75577ec2012-01-24 23:41:50153TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) {
[email protected]b2d26cfd2012-12-11 10:36:06154 ScopedVector<UploadElementReader> element_readers;
[email protected]7a1fcff2012-01-24 01:07:49155 const std::string payload(10000, 'a'); // 'a' x 10000.
[email protected]b2d26cfd2012-12-11 10:36:06156 element_readers.push_back(new UploadBytesElementReader(
157 payload.data(), payload.size()));
[email protected]7a1fcff2012-01-24 01:07:49158
[email protected]b2d26cfd2012-12-11 10:36:06159 scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
[email protected]4db27d82012-12-20 11:50:24160 ASSERT_EQ(OK, body->Init(CompletionCallback()));
[email protected]7a1fcff2012-01-24 01:07:49161 // Shouldn't be merged if the in-memory body is large here.
[email protected]75577ec2012-01-24 23:41:50162 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
163 "some header", body.get()));
[email protected]7a1fcff2012-01-24 01:07:49164}
165
[email protected]4750937f2012-06-15 20:44:21166// Test to ensure the HttpStreamParser state machine does not get confused
167// when sending a request with a chunked body, where chunks become available
168// asynchronously, over a socket where writes may also complete
169// asynchronously.
170// This is a regression test for http://crbug.com/132243
171TEST(HttpStreamParser, AsyncChunkAndAsyncSocket) {
172 // The chunks that will be written in the request, as reflected in the
173 // MockWrites below.
174 static const char kChunk1[] = "Chunk 1";
175 static const char kChunk2[] = "Chunky 2";
176 static const char kChunk3[] = "Test 3";
177
178 MockWrite writes[] = {
179 MockWrite(ASYNC, 0,
180 "GET /one.html HTTP/1.1\r\n"
181 "Host: localhost\r\n"
182 "Transfer-Encoding: chunked\r\n"
183 "Connection: keep-alive\r\n\r\n"),
184 MockWrite(ASYNC, 1, "7\r\nChunk 1\r\n"),
185 MockWrite(ASYNC, 2, "8\r\nChunky 2\r\n"),
186 MockWrite(ASYNC, 3, "6\r\nTest 3\r\n"),
187 MockWrite(ASYNC, 4, "0\r\n\r\n"),
188 };
189
190 // The size of the response body, as reflected in the Content-Length of the
191 // MockRead below.
192 static const int kBodySize = 8;
193
194 MockRead reads[] = {
195 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"),
196 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"),
197 MockRead(ASYNC, 7, "one.html"),
[email protected]d55b30a2012-07-21 00:35:39198 MockRead(SYNCHRONOUS, 0, 8), // EOF
[email protected]4750937f2012-06-15 20:44:21199 };
200
[email protected]b2d26cfd2012-12-11 10:36:06201 UploadDataStream upload_stream(UploadDataStream::CHUNKED, 0);
202 upload_stream.AppendChunk(kChunk1, arraysize(kChunk1) - 1, false);
[email protected]4db27d82012-12-20 11:50:24203 ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
[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.
[email protected]b2d26cfd2012-12-11 10:36:06250 upload_stream.AppendChunk(kChunk2, arraysize(kChunk2) - 1, false);
[email protected]4750937f2012-06-15 20:44:21251 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.
[email protected]b2d26cfd2012-12-11 10:36:06267 upload_stream.AppendChunk(kChunk3, arraysize(kChunk3) - 1, true);
[email protected]4750937f2012-06-15 20:44:21268 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