[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 1 | // 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 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 15 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 17 | #include "base/memory/scoped_ptr.h" |
[email protected] | 0f97288 | 2013-09-20 04:34:20 | [diff] [blame] | 18 | #include "base/run_loop.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 19 | #include "base/strings/string_piece.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 20 | #include "base/strings/stringprintf.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 21 | #include "base/thread_task_runner_handle.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 22 | #include "net/base/chunked_upload_data_stream.h" |
| 23 | #include "net/base/elements_upload_data_stream.h" |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 24 | #include "net/base/io_buffer.h" |
[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 25 | #include "net/base/net_errors.h" |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 26 | #include "net/base/test_completion_callback.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 27 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 28 | #include "net/base/upload_file_element_reader.h" |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 29 | #include "net/http/http_request_headers.h" |
| 30 | #include "net/http/http_request_info.h" |
[email protected] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 31 | #include "net/http/http_response_headers.h" |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 32 | #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] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 35 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 36 | #include "url/gurl.h" |
[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 37 | |
| 38 | namespace net { |
| 39 | |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 40 | namespace { |
| 41 | |
[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 42 | const size_t kOutputSize = 1024; // Just large enough for this test. |
| 43 | // The number of bytes that can fit in a buffer of kOutputSize. |
| 44 | const size_t kMaxPayloadSize = |
| 45 | kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; |
| 46 | |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 47 | // Helper method to create a connected ClientSocketHandle using |data|. |
| 48 | // Modifies |data|. |
| 49 | scoped_ptr<ClientSocketHandle> CreateConnectedSocketHandle( |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 50 | SequencedSocketData* data) { |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 51 | data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 52 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 53 | scoped_ptr<MockTCPClientSocket> socket( |
| 54 | new MockTCPClientSocket(net::AddressList(), nullptr, data)); |
| 55 | data->set_socket(socket.get()); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 56 | |
| 57 | TestCompletionCallback callback; |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 58 | EXPECT_EQ(OK, socket->Connect(callback.callback())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 59 | |
| 60 | scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 61 | socket_handle->SetSocket(socket.Pass()); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 62 | return socket_handle.Pass(); |
| 63 | } |
| 64 | |
[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 65 | // The empty payload is how the last chunk is encoded. |
| 66 | TEST(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 | |
| 77 | TEST(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 | |
| 89 | TEST(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 | |
| 101 | TEST(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 | |
| 113 | TEST(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] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 123 | TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 124 | // Shouldn't be merged if upload data is non-existent. |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 125 | ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 126 | "some header", NULL)); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 129 | TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 130 | 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] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 133 | ASSERT_EQ(OK, body->Init(CompletionCallback())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 134 | // Shouldn't be merged if upload data is empty. |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 135 | ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 136 | "some header", body.get())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 139 | TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 140 | const std::string payload = "123"; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 141 | 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] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 144 | // Shouldn't be merged if upload data carries chunked data. |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 145 | ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 146 | "some header", body.get())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 149 | TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { |
wkorman | 30fe3fd | 2015-11-06 23:43:42 | [diff] [blame] | 150 | // 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] | 0f97288 | 2013-09-20 04:34:20 | [diff] [blame] | 156 | { |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 157 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 158 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 159 | element_readers.push_back(make_scoped_ptr( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 160 | new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 161 | temp_file_path, 0, 0, base::Time()))); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 162 | |
[email protected] | 0f97288 | 2013-09-20 04:34:20 | [diff] [blame] | 163 | scoped_ptr<UploadDataStream> body( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 164 | new ElementsUploadDataStream(std::move(element_readers), 0)); |
[email protected] | 0f97288 | 2013-09-20 04:34:20 | [diff] [blame] | 165 | 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 | } |
wkorman | 30fe3fd | 2015-11-06 23:43:42 | [diff] [blame] | 172 | |
[email protected] | 0f97288 | 2013-09-20 04:34:20 | [diff] [blame] | 173 | // UploadFileElementReaders may post clean-up tasks on destruction. |
| 174 | base::RunLoop().RunUntilIdle(); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 177 | TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 178 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 179 | const std::string payload = "123"; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 180 | element_readers.push_back(make_scoped_ptr( |
| 181 | new UploadBytesElementReader(payload.data(), payload.size()))); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 182 | |
[email protected] | 96c77a7 | 2013-09-24 09:49:20 | [diff] [blame] | 183 | scoped_ptr<UploadDataStream> body( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 184 | new ElementsUploadDataStream(std::move(element_readers), 0)); |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 185 | ASSERT_EQ(OK, body->Init(CompletionCallback())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 186 | // Yes, should be merged if the in-memory body is small here. |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 187 | ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 188 | "some header", body.get())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 191 | TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 192 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 193 | const std::string payload(10000, 'a'); // 'a' x 10000. |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 194 | element_readers.push_back(make_scoped_ptr( |
| 195 | new UploadBytesElementReader(payload.data(), payload.size()))); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 196 | |
[email protected] | 96c77a7 | 2013-09-24 09:49:20 | [diff] [blame] | 197 | scoped_ptr<UploadDataStream> body( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 198 | new ElementsUploadDataStream(std::move(element_readers), 0)); |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 199 | ASSERT_EQ(OK, body->Init(CompletionCallback())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 200 | // Shouldn't be merged if the in-memory body is large here. |
[email protected] | 75577ec | 2012-01-24 23:41:50 | [diff] [blame] | 201 | ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 202 | "some header", body.get())); |
[email protected] | 7a1fcff | 2012-01-24 01:07:49 | [diff] [blame] | 203 | } |
| 204 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 205 | TEST(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 | |
| 230 | TEST(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 | |
| 262 | TEST(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 | |
| 294 | TEST(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 | |
| 326 | TEST(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.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame^] | 337 | 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); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 341 | 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 | |
| 363 | TEST(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] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 408 | // Test to ensure the HttpStreamParser state machine does not get confused |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 409 | // when sending a request with a chunked body with only one chunk that becomes |
| 410 | // available asynchronously. |
| 411 | TEST(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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 436 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 437 | 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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 460 | // Complete the initial request write. Callback should not have been invoked. |
| 461 | base::RunLoop().RunUntilIdle(); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 462 | ASSERT_FALSE(callback.have_result()); |
| 463 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 464 | // Now append the only chunk and wait for the callback. |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 465 | upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 466 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 470 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 477 | ASSERT_EQ(kBodySize, callback.WaitForResult()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 478 | |
| 479 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 480 | EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 481 | } |
| 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. |
| 486 | TEST(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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 513 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 514 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 536 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 540 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 547 | ASSERT_EQ(kBodySize, callback.WaitForResult()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 548 | |
| 549 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 550 | EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | // Test to ensure the HttpStreamParser state machine does not get confused |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 554 | // 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 |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 558 | TEST(HttpStreamParser, AsyncChunkAndAsyncSocketWithMultipleChunks) { |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 559 | // 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[] = { |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 566 | 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] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 573 | }; |
| 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] | d55b30a | 2012-07-21 00:35:39 | [diff] [blame] | 583 | MockRead(SYNCHRONOUS, 0, 8), // EOF |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 584 | }; |
| 585 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 586 | ChunkedUploadDataStream upload_stream(0); |
| 587 | upload_stream.AppendData(kChunk1, arraysize(kChunk1) - 1, false); |
| 588 | ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); |
[email protected] | 9a96389 | 2012-11-01 11:48:13 | [diff] [blame] | 589 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 590 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 591 | scoped_ptr<ClientSocketHandle> socket_handle = |
| 592 | CreateConnectedSocketHandle(&data); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 593 | |
| 594 | HttpRequestInfo request_info; |
| 595 | request_info.method = "GET"; |
| 596 | request_info.url = GURL("http://localhost"); |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 597 | request_info.upload_data_stream = &upload_stream; |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 598 | |
| 599 | scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 600 | HttpStreamParser parser( |
| 601 | socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog()); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 602 | |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 603 | HttpRequestHeaders request_headers; |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 604 | request_headers.SetHeader("Transfer-Encoding", "chunked"); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 605 | |
| 606 | HttpResponseInfo response_info; |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 607 | TestCompletionCallback callback; |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 608 | // This will attempt to Write() the initial request and headers, which will |
| 609 | // complete asynchronously. |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 610 | 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] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 613 | ASSERT_FALSE(callback.have_result()); |
| 614 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 615 | // Sending the request and the first chunk completes. |
| 616 | base::RunLoop().RunUntilIdle(); |
| 617 | ASSERT_FALSE(callback.have_result()); |
| 618 | |
| 619 | // Now append another chunk. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 620 | upload_stream.AppendData(kChunk2, arraysize(kChunk2) - 1, false); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 621 | ASSERT_FALSE(callback.have_result()); |
| 622 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 623 | // Add the final chunk, while the write for the second is still pending, |
| 624 | // which should not confuse the state machine. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 625 | upload_stream.AppendData(kChunk3, arraysize(kChunk3) - 1, true); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 626 | ASSERT_FALSE(callback.have_result()); |
| 627 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 628 | // Wait for writes to complete. |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 629 | ASSERT_EQ(OK, callback.WaitForResult()); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 630 | |
| 631 | // Attempt to read the response status and the response headers. |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 632 | ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 633 | ASSERT_GT(callback.WaitForResult(), 0); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 634 | |
| 635 | // Finally, attempt to read the response body. |
| 636 | scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 637 | ASSERT_EQ(ERR_IO_PENDING, |
| 638 | parser.ReadResponseBody(body_buffer.get(), kBodySize, |
| 639 | callback.callback())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 640 | ASSERT_EQ(kBodySize, callback.WaitForResult()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 641 | |
| 642 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 643 | EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 644 | } |
| 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. |
| 649 | TEST(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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 671 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 672 | 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 | |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 695 | // Now append the terminal 0-byte "chunk". |
| 696 | upload_stream.AppendData(nullptr, 0, true); |
| 697 | ASSERT_FALSE(callback.have_result()); |
| 698 | |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 699 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 703 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 710 | ASSERT_EQ(kBodySize, callback.WaitForResult()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 711 | |
| 712 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 713 | EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 714 | } |
| 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. |
| 719 | TEST(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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 743 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 744 | 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. |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 768 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 772 | 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())); |
mmenke | 3dc8c88b | 2015-02-19 15:11:27 | [diff] [blame] | 779 | ASSERT_EQ(kBodySize, callback.WaitForResult()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 780 | |
| 781 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 782 | EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
[email protected] | 4750937f | 2012-06-15 20:44:21 | [diff] [blame] | 783 | } |
| 784 | |
[email protected] | 9c18dbc | 2013-05-29 19:06:53 | [diff] [blame] | 785 | TEST(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 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 829 | enum { |
| 830 | HTTP = 0, |
| 831 | HTTPS, |
| 832 | NUM_PROTOCOLS, |
| 833 | }; |
[email protected] | 9c18dbc | 2013-05-29 19:06:53 | [diff] [blame] | 834 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 835 | for (size_t protocol = 0; protocol < NUM_PROTOCOLS; protocol++) { |
| 836 | SCOPED_TRACE(protocol); |
[email protected] | 9c18dbc | 2013-05-29 19:06:53 | [diff] [blame] | 837 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 838 | 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] | 9c18dbc | 2013-05-29 19:06:53 | [diff] [blame] | 843 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 844 | 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] | 9c18dbc | 2013-05-29 19:06:53 | [diff] [blame] | 852 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 853 | 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()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 864 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 865 | parser.sent_bytes()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 866 | if (i == arraysize(reads) - 1) { |
| 867 | EXPECT_EQ(OK, rv); |
| 868 | EXPECT_TRUE(response_info.headers.get()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 869 | EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 870 | } else { |
| 871 | if (protocol == HTTP) { |
| 872 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
| 873 | EXPECT_TRUE(response_info.headers.get()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 874 | EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 875 | } else { |
| 876 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TRUNCATED, rv); |
| 877 | EXPECT_FALSE(response_info.headers.get()); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 878 | EXPECT_EQ(0, parser.received_bytes()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 879 | } |
| 880 | } |
[email protected] | 9c18dbc | 2013-05-29 19:06:53 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
[email protected] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 885 | // Confirm that on 101 response, the headers are parsed but the data that |
| 886 | // follows remains in the buffer. |
| 887 | TEST(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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 901 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 902 | scoped_ptr<ClientSocketHandle> socket_handle = |
| 903 | CreateConnectedSocketHandle(&data); |
[email protected] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 904 | |
| 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; |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 916 | TestCompletionCallback callback; |
| 917 | ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, |
| 918 | &response_info, callback.callback())); |
[email protected] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 919 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 920 | EXPECT_EQ(OK, parser.ReadResponseHeaders(callback.callback())); |
[email protected] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 921 | 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())); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 929 | |
| 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] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 934 | } |
| 935 | |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 936 | // Helper class for constructing HttpStreamParser and running GET requests. |
| 937 | class 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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 963 | data_.reset(new SequencedSocketData(&reads_.front(), reads_.size(), |
| 964 | &writes_.front(), writes_.size())); |
| 965 | socket_handle_ = CreateConnectedSocketHandle(data_.get()); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 966 | |
| 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 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 974 | TestCompletionCallback callback; |
| 975 | ASSERT_EQ(OK, parser_->SendRequest("GET / HTTP/1.1\r\n", request_headers_, |
| 976 | &response_info_, callback.callback())); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 977 | } |
| 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) { |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 990 | rv = parser_->ReadResponseBody( |
| 991 | buffer.get(), user_buf_len, callback.callback()); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 992 | 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_; |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 1007 | scoped_ptr<SequencedSocketData> data_; |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1008 | scoped_ptr<HttpStreamParser> parser_; |
| 1009 | int sequence_number_; |
| 1010 | }; |
| 1011 | |
| 1012 | // Test that HTTP/0.9 response size is correctly calculated. |
| 1013 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1021 | int response_size = response.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1022 | 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. |
| 1029 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1041 | int body_size = body.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1042 | 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". |
| 1050 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1065 | int body_size = body.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1066 | 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. |
| 1081 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1086 | int body_start_size = body_start.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1087 | 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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1100 | int body_end_size = body_end.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1101 | 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. |
| 1113 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1127 | int body_size = body.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1128 | 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. |
| 1138 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1151 | int body_size = body.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1152 | 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. |
| 1160 | TEST(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. |
| 1185 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1200 | for (std::vector<std::string>::size_type i = 0; i < blocks.size(); ++i) |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1201 | 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". |
| 1213 | TEST(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] | 3cb5fc2 | 2013-12-12 19:40:56 | [diff] [blame] | 1233 | int body_size = body.size(); |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1234 | 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] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 1239 | // 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. |
| 1242 | TEST(HttpStreamParser, ReadAfterUnownedObjectsDestroyed) { |
| 1243 | MockWrite writes[] = { |
| 1244 | MockWrite(SYNCHRONOUS, 0, |
| 1245 | "GET /foo.html HTTP/1.1\r\n\r\n"), |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 1246 | }; |
| 1247 | |
| 1248 | const int kBodySize = 1; |
| 1249 | MockRead reads[] = { |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 1250 | MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 OK\r\n"), |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 1251 | MockRead(SYNCHRONOUS, 2, "Content-Length: 1\r\n"), |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 1252 | MockRead(SYNCHRONOUS, 3, "Connection: Keep-Alive\r\n\r\n"), |
| 1253 | MockRead(SYNCHRONOUS, 4, "1"), |
| 1254 | MockRead(SYNCHRONOUS, 0, 5), // EOF |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 1255 | }; |
| 1256 | |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 1257 | SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 1258 | scoped_ptr<ClientSocketHandle> socket_handle = |
| 1259 | CreateConnectedSocketHandle(&data); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 1260 | |
| 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()); |
mmenke | f22be0ee | 2015-06-05 15:39:02 | [diff] [blame] | 1271 | TestCompletionCallback callback; |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 1272 | 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())); |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 1285 | |
| 1286 | EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 1287 | EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 1288 | } |
| 1289 | |
[email protected] | 390489b | 2013-12-09 10:49:01 | [diff] [blame] | 1290 | } // namespace |
[email protected] | df8e638 | 2013-11-07 00:19:06 | [diff] [blame] | 1291 | |
[email protected] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 1292 | } // namespace net |