| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/server/http_server.h" |
| 6 |
| 5 #include <stdint.h> | 7 #include <stdint.h> |
| 6 | 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> |
| 8 #include <utility> | 11 #include <utility> |
| 9 #include <vector> | 12 #include <vector> |
| 10 | 13 |
| 11 #include "base/bind.h" | 14 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
| 13 #include "base/callback_helpers.h" | 16 #include "base/callback_helpers.h" |
| 14 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 15 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 16 #include "base/location.h" | 19 #include "base/location.h" |
| 17 #include "base/logging.h" | 20 #include "base/logging.h" |
| 18 #include "base/macros.h" | 21 #include "base/macros.h" |
| 22 #include "base/memory/ptr_util.h" |
| 19 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" | |
| 21 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
| 22 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
| 23 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
| 24 #include "base/strings/string_split.h" | 27 #include "base/strings/string_split.h" |
| 25 #include "base/strings/string_util.h" | 28 #include "base/strings/string_util.h" |
| 26 #include "base/strings/stringprintf.h" | 29 #include "base/strings/stringprintf.h" |
| 27 #include "base/thread_task_runner_handle.h" | 30 #include "base/thread_task_runner_handle.h" |
| 28 #include "base/time/time.h" | 31 #include "base/time/time.h" |
| 29 #include "net/base/address_list.h" | 32 #include "net/base/address_list.h" |
| 30 #include "net/base/io_buffer.h" | 33 #include "net/base/io_buffer.h" |
| 31 #include "net/base/ip_endpoint.h" | 34 #include "net/base/ip_endpoint.h" |
| 32 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 33 #include "net/base/test_completion_callback.h" | 36 #include "net/base/test_completion_callback.h" |
| 34 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
| 35 #include "net/http/http_util.h" | 38 #include "net/http/http_util.h" |
| 36 #include "net/log/net_log.h" | 39 #include "net/log/net_log.h" |
| 37 #include "net/server/http_server.h" | |
| 38 #include "net/server/http_server_request_info.h" | 40 #include "net/server/http_server_request_info.h" |
| 39 #include "net/socket/tcp_client_socket.h" | 41 #include "net/socket/tcp_client_socket.h" |
| 40 #include "net/socket/tcp_server_socket.h" | 42 #include "net/socket/tcp_server_socket.h" |
| 41 #include "net/url_request/url_fetcher.h" | 43 #include "net/url_request/url_fetcher.h" |
| 42 #include "net/url_request/url_fetcher_delegate.h" | 44 #include "net/url_request/url_fetcher_delegate.h" |
| 43 #include "net/url_request/url_request_context.h" | 45 #include "net/url_request/url_request_context.h" |
| 44 #include "net/url_request/url_request_context_getter.h" | 46 #include "net/url_request/url_request_context_getter.h" |
| 45 #include "net/url_request/url_request_test_util.h" | 47 #include "net/url_request/url_request_test_util.h" |
| 46 #include "testing/gtest/include/gtest/gtest.h" | 48 #include "testing/gtest/include/gtest/gtest.h" |
| 47 | 49 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Return true if response has data equal to or more than content length. | 168 // Return true if response has data equal to or more than content length. |
| 167 int64_t body_size = static_cast<int64_t>(response.size()) - end_of_headers; | 169 int64_t body_size = static_cast<int64_t>(response.size()) - end_of_headers; |
| 168 DCHECK_LE(0, body_size); | 170 DCHECK_LE(0, body_size); |
| 169 scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders( | 171 scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders( |
| 170 HttpUtil::AssembleRawHeaders(response.data(), end_of_headers))); | 172 HttpUtil::AssembleRawHeaders(response.data(), end_of_headers))); |
| 171 return body_size >= headers->GetContentLength(); | 173 return body_size >= headers->GetContentLength(); |
| 172 } | 174 } |
| 173 | 175 |
| 174 scoped_refptr<IOBufferWithSize> read_buffer_; | 176 scoped_refptr<IOBufferWithSize> read_buffer_; |
| 175 scoped_refptr<DrainableIOBuffer> write_buffer_; | 177 scoped_refptr<DrainableIOBuffer> write_buffer_; |
| 176 scoped_ptr<TCPClientSocket> socket_; | 178 std::unique_ptr<TCPClientSocket> socket_; |
| 177 int connect_result_; | 179 int connect_result_; |
| 178 }; | 180 }; |
| 179 | 181 |
| 180 } // namespace | 182 } // namespace |
| 181 | 183 |
| 182 class HttpServerTest : public testing::Test, | 184 class HttpServerTest : public testing::Test, |
| 183 public HttpServer::Delegate { | 185 public HttpServer::Delegate { |
| 184 public: | 186 public: |
| 185 HttpServerTest() : quit_after_request_count_(0) {} | 187 HttpServerTest() : quit_after_request_count_(0) {} |
| 186 | 188 |
| 187 void SetUp() override { | 189 void SetUp() override { |
| 188 scoped_ptr<ServerSocket> server_socket( | 190 std::unique_ptr<ServerSocket> server_socket( |
| 189 new TCPServerSocket(NULL, NetLog::Source())); | 191 new TCPServerSocket(NULL, NetLog::Source())); |
| 190 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | 192 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
| 191 server_.reset(new HttpServer(std::move(server_socket), this)); | 193 server_.reset(new HttpServer(std::move(server_socket), this)); |
| 192 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 194 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
| 193 } | 195 } |
| 194 | 196 |
| 195 void OnConnect(int connection_id) override {} | 197 void OnConnect(int connection_id) override {} |
| 196 | 198 |
| 197 void OnHttpRequest(int connection_id, | 199 void OnHttpRequest(int connection_id, |
| 198 const HttpServerRequestInfo& info) override { | 200 const HttpServerRequestInfo& info) override { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 225 } | 227 } |
| 226 | 228 |
| 227 HttpServerRequestInfo GetRequest(size_t request_index) { | 229 HttpServerRequestInfo GetRequest(size_t request_index) { |
| 228 return requests_[request_index].first; | 230 return requests_[request_index].first; |
| 229 } | 231 } |
| 230 | 232 |
| 231 int GetConnectionId(size_t request_index) { | 233 int GetConnectionId(size_t request_index) { |
| 232 return requests_[request_index].second; | 234 return requests_[request_index].second; |
| 233 } | 235 } |
| 234 | 236 |
| 235 void HandleAcceptResult(scoped_ptr<StreamSocket> socket) { | 237 void HandleAcceptResult(std::unique_ptr<StreamSocket> socket) { |
| 236 server_->accepted_socket_.reset(socket.release()); | 238 server_->accepted_socket_.reset(socket.release()); |
| 237 server_->HandleAcceptResult(OK); | 239 server_->HandleAcceptResult(OK); |
| 238 } | 240 } |
| 239 | 241 |
| 240 protected: | 242 protected: |
| 241 scoped_ptr<HttpServer> server_; | 243 std::unique_ptr<HttpServer> server_; |
| 242 IPEndPoint server_address_; | 244 IPEndPoint server_address_; |
| 243 base::Closure run_loop_quit_func_; | 245 base::Closure run_loop_quit_func_; |
| 244 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 246 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
| 245 | 247 |
| 246 private: | 248 private: |
| 247 size_t quit_after_request_count_; | 249 size_t quit_after_request_count_; |
| 248 }; | 250 }; |
| 249 | 251 |
| 250 namespace { | 252 namespace { |
| 251 | 253 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 | 418 |
| 417 private: | 419 private: |
| 418 base::Closure quit_loop_func_; | 420 base::Closure quit_loop_func_; |
| 419 }; | 421 }; |
| 420 | 422 |
| 421 base::RunLoop run_loop; | 423 base::RunLoop run_loop; |
| 422 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); | 424 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); |
| 423 | 425 |
| 424 scoped_refptr<URLRequestContextGetter> request_context_getter( | 426 scoped_refptr<URLRequestContextGetter> request_context_getter( |
| 425 new TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get())); | 427 new TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get())); |
| 426 scoped_ptr<URLFetcher> fetcher = | 428 std::unique_ptr<URLFetcher> fetcher = |
| 427 URLFetcher::Create(GURL(base::StringPrintf("http://127.0.0.1:%d/test", | 429 URLFetcher::Create(GURL(base::StringPrintf("http://127.0.0.1:%d/test", |
| 428 server_address_.port())), | 430 server_address_.port())), |
| 429 URLFetcher::GET, &delegate); | 431 URLFetcher::GET, &delegate); |
| 430 fetcher->SetRequestContext(request_context_getter.get()); | 432 fetcher->SetRequestContext(request_context_getter.get()); |
| 431 fetcher->AddExtraRequestHeader( | 433 fetcher->AddExtraRequestHeader( |
| 432 base::StringPrintf("content-length:%d", 1 << 30)); | 434 base::StringPrintf("content-length:%d", 1 << 30)); |
| 433 fetcher->Start(); | 435 fetcher->Start(); |
| 434 | 436 |
| 435 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); | 437 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); |
| 436 ASSERT_EQ(0u, requests_.size()); | 438 ASSERT_EQ(0u, requests_.size()); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 int read_buf_len_; | 563 int read_buf_len_; |
| 562 CompletionCallback read_callback_; | 564 CompletionCallback read_callback_; |
| 563 std::string pending_read_data_; | 565 std::string pending_read_data_; |
| 564 BoundNetLog net_log_; | 566 BoundNetLog net_log_; |
| 565 | 567 |
| 566 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); | 568 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); |
| 567 }; | 569 }; |
| 568 | 570 |
| 569 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { | 571 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { |
| 570 MockStreamSocket* socket = new MockStreamSocket(); | 572 MockStreamSocket* socket = new MockStreamSocket(); |
| 571 HandleAcceptResult(make_scoped_ptr<StreamSocket>(socket)); | 573 HandleAcceptResult(base::WrapUnique<StreamSocket>(socket)); |
| 572 std::string body("body"); | 574 std::string body("body"); |
| 573 std::string request_text = base::StringPrintf( | 575 std::string request_text = base::StringPrintf( |
| 574 "GET /test HTTP/1.1\r\n" | 576 "GET /test HTTP/1.1\r\n" |
| 575 "SomeHeader: 1\r\n" | 577 "SomeHeader: 1\r\n" |
| 576 "Content-Length: %" PRIuS "\r\n\r\n%s", | 578 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 577 body.length(), | 579 body.length(), |
| 578 body.c_str()); | 580 body.c_str()); |
| 579 socket->DidRead(request_text.c_str(), request_text.length() - 2); | 581 socket->DidRead(request_text.c_str(), request_text.length() - 2); |
| 580 ASSERT_EQ(0u, requests_.size()); | 582 ASSERT_EQ(0u, requests_.size()); |
| 581 socket->DidRead(request_text.c_str() + request_text.length() - 2, 2); | 583 socket->DidRead(request_text.c_str() + request_text.length() - 2, 2); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 649 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 648 client.Send("GET / HTTP/1.1\r\n\r\n"); | 650 client.Send("GET / HTTP/1.1\r\n\r\n"); |
| 649 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 651 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
| 650 ASSERT_EQ(1ul, connection_ids_.size()); | 652 ASSERT_EQ(1ul, connection_ids_.size()); |
| 651 ASSERT_EQ(0ul, requests_.size()); | 653 ASSERT_EQ(0ul, requests_.size()); |
| 652 } | 654 } |
| 653 | 655 |
| 654 } // namespace | 656 } // namespace |
| 655 | 657 |
| 656 } // namespace net | 658 } // namespace net |
| OLD | NEW |