OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/test/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
16 #include "net/test/embedded_test_server/http_connection.h" | 16 #include "net/test/embedded_test_server/http_connection.h" |
17 #include "net/test/embedded_test_server/http_request.h" | 17 #include "net/test/embedded_test_server/http_request.h" |
18 #include "net/test/embedded_test_server/http_response.h" | 18 #include "net/test/embedded_test_server/http_response.h" |
19 #include "net/tools/fetch/http_listen_socket.h" | 19 #include "net/tools/fetch/http_listen_socket.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 namespace test_server { | 22 namespace test_server { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const int kPort = 8040; | 26 const int kPort = 8040; |
27 const char kIp[] = "127.0.0.1"; | 27 const char kIp[] = "127.0.0.1"; |
28 const int kRetries = 10; | 28 const int kRetries = 10; |
29 | 29 |
30 // Callback to handle requests with default predefined response for requests | |
31 // matching the address |url|. | |
32 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, | |
33 const HttpResponse& response, | |
34 const HttpRequest& request) { | |
35 const GURL request_url = url.Resolve(request.relative_url); | |
36 if (url.path() != request_url.path()) | |
37 return scoped_ptr<HttpResponse>(NULL); | |
38 return scoped_ptr<HttpResponse>(new HttpResponse(response)); | |
39 } | |
40 | |
41 // Handles |request| by serving a file from under |server_root|. | 30 // Handles |request| by serving a file from under |server_root|. |
42 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root, | 31 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root, |
43 const HttpRequest& request) { | 32 const HttpRequest& request) { |
44 // This is a test-only server. Ignore I/O thread restrictions. | 33 // This is a test-only server. Ignore I/O thread restrictions. |
45 base::ThreadRestrictions::ScopedAllowIO allow_io; | 34 base::ThreadRestrictions::ScopedAllowIO allow_io; |
46 | 35 |
47 // Trim the first byte ('/'). | 36 // Trim the first byte ('/'). |
48 std::string request_path(request.relative_url.substr(1)); | 37 std::string request_path(request.relative_url.substr(1)); |
49 | 38 |
| 39 // Remove the query string if present. |
| 40 size_t query_pos = request_path.find('?'); |
| 41 if (query_pos != std::string::npos) |
| 42 request_path = request_path.substr(0, query_pos); |
| 43 |
50 std::string file_contents; | 44 std::string file_contents; |
51 if (!file_util::ReadFileToString( | 45 if (!file_util::ReadFileToString( |
52 server_root.AppendASCII(request_path), &file_contents)) { | 46 server_root.AppendASCII(request_path), &file_contents)) { |
53 return scoped_ptr<HttpResponse>(NULL); | 47 return scoped_ptr<HttpResponse>(NULL); |
54 } | 48 } |
55 | 49 |
56 scoped_ptr<HttpResponse> http_response(new HttpResponse); | 50 scoped_ptr<HttpResponseImpl> http_response(new HttpResponseImpl); |
57 http_response->set_code(net::test_server::SUCCESS); | 51 http_response->set_code(net::test_server::SUCCESS); |
58 http_response->set_content(file_contents); | 52 http_response->set_content(file_contents); |
59 return http_response.Pass(); | 53 return http_response.PassAs<HttpResponse>(); |
60 } | 54 } |
61 | 55 |
62 } // namespace | 56 } // namespace |
63 | 57 |
64 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, | 58 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
65 StreamListenSocket::Delegate* delegate) | 59 StreamListenSocket::Delegate* delegate) |
66 : TCPListenSocket(socket_descriptor, delegate) { | 60 : TCPListenSocket(socket_descriptor, delegate) { |
67 DCHECK(thread_checker_.CalledOnValidThread()); | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
68 } | 62 } |
69 | 63 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 listen_socket_ = NULL; // Release the listen socket. | 148 listen_socket_ = NULL; // Release the listen socket. |
155 STLDeleteContainerPairSecondPointers(connections_.begin(), | 149 STLDeleteContainerPairSecondPointers(connections_.begin(), |
156 connections_.end()); | 150 connections_.end()); |
157 connections_.clear(); | 151 connections_.clear(); |
158 } | 152 } |
159 | 153 |
160 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, | 154 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, |
161 scoped_ptr<HttpRequest> request) { | 155 scoped_ptr<HttpRequest> request) { |
162 DCHECK(io_thread_->BelongsToCurrentThread()); | 156 DCHECK(io_thread_->BelongsToCurrentThread()); |
163 | 157 |
| 158 bool request_handled = false; |
| 159 |
164 for (size_t i = 0; i < request_handlers_.size(); ++i) { | 160 for (size_t i = 0; i < request_handlers_.size(); ++i) { |
165 scoped_ptr<HttpResponse> response = | 161 scoped_ptr<HttpResponse> response = |
166 request_handlers_[i].Run(*request.get()); | 162 request_handlers_[i].Run(*request.get()); |
167 if (response.get()) { | 163 if (response.get()) { |
168 connection->SendResponse(response.Pass()); | 164 connection->SendResponse(response.Pass()); |
169 return; | 165 request_handled = true; |
| 166 break; |
170 } | 167 } |
171 } | 168 } |
172 | 169 |
173 LOG(WARNING) << "Request not handled. Returning 404: " | 170 if (!request_handled) { |
174 << request->relative_url; | 171 LOG(WARNING) << "Request not handled. Returning 404: " |
175 scoped_ptr<HttpResponse> not_found_response(new HttpResponse()); | 172 << request->relative_url; |
176 not_found_response->set_code(NOT_FOUND); | 173 scoped_ptr<HttpResponseImpl> not_found_response(new HttpResponseImpl()); |
177 connection->SendResponse(not_found_response.Pass()); | 174 not_found_response->set_code(NOT_FOUND); |
| 175 connection->SendResponse(not_found_response.PassAs<HttpResponse>()); |
| 176 } |
178 | 177 |
179 // Drop the connection, since we do not support multiple requests per | 178 // Drop the connection, since we do not support multiple requests per |
180 // connection. | 179 // connection. |
181 connections_.erase(connection->socket_.get()); | 180 connections_.erase(connection->socket_.get()); |
182 delete connection; | 181 delete connection; |
183 } | 182 } |
184 | 183 |
185 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { | 184 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { |
186 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) | 185 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) |
187 << relative_url; | 186 << relative_url; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 std::map<StreamListenSocket*, HttpConnection*>::iterator it = | 240 std::map<StreamListenSocket*, HttpConnection*>::iterator it = |
242 connections_.find(socket); | 241 connections_.find(socket); |
243 if (it == connections_.end()) { | 242 if (it == connections_.end()) { |
244 return NULL; | 243 return NULL; |
245 } | 244 } |
246 return it->second; | 245 return it->second; |
247 } | 246 } |
248 | 247 |
249 } // namespace test_server | 248 } // namespace test_server |
250 } // namespace net | 249 } // namespace net |
OLD | NEW |