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<HttpResponseInterface> HandleFileRequest( |
43 const HttpRequest& request) { | 32 const base::FilePath& server_root, |
33 const HttpRequest& request) { | |
44 // This is a test-only server. Ignore I/O thread restrictions. | 34 // This is a test-only server. Ignore I/O thread restrictions. |
45 base::ThreadRestrictions::ScopedAllowIO allow_io; | 35 base::ThreadRestrictions::ScopedAllowIO allow_io; |
46 | 36 |
47 // Trim the first byte ('/'). | 37 // Trim the first byte ('/'). |
48 std::string request_path(request.relative_url.substr(1)); | 38 std::string request_path(request.relative_url.substr(1)); |
49 | 39 |
40 // Remove the query string if present. | |
41 size_t query_pos = request_path.find('?'); | |
42 if (query_pos != std::string::npos) | |
43 request_path = request_path.substr(0, query_pos); | |
44 | |
50 std::string file_contents; | 45 std::string file_contents; |
51 if (!file_util::ReadFileToString( | 46 if (!file_util::ReadFileToString( |
52 server_root.AppendASCII(request_path), &file_contents)) { | 47 server_root.AppendASCII(request_path), &file_contents)) { |
53 return scoped_ptr<HttpResponse>(NULL); | 48 return scoped_ptr<HttpResponseInterface>(NULL); |
54 } | 49 } |
55 | 50 |
56 scoped_ptr<HttpResponse> http_response(new HttpResponse); | 51 scoped_ptr<HttpResponse> http_response(new HttpResponse); |
57 http_response->set_code(net::test_server::SUCCESS); | 52 http_response->set_code(net::test_server::SUCCESS); |
58 http_response->set_content(file_contents); | 53 http_response->set_content(file_contents); |
59 return http_response.Pass(); | 54 return http_response.PassAs<HttpResponseInterface>(); |
60 } | 55 } |
61 | 56 |
62 } // namespace | 57 } // namespace |
63 | 58 |
64 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, | 59 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
65 StreamListenSocket::Delegate* delegate) | 60 StreamListenSocket::Delegate* delegate) |
66 : TCPListenSocket(socket_descriptor, delegate) { | 61 : TCPListenSocket(socket_descriptor, delegate) { |
67 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
68 } | 63 } |
69 | 64 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 listen_socket_ = NULL; // Release the listen socket. | 149 listen_socket_ = NULL; // Release the listen socket. |
155 STLDeleteContainerPairSecondPointers(connections_.begin(), | 150 STLDeleteContainerPairSecondPointers(connections_.begin(), |
156 connections_.end()); | 151 connections_.end()); |
157 connections_.clear(); | 152 connections_.clear(); |
158 } | 153 } |
159 | 154 |
160 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, | 155 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, |
161 scoped_ptr<HttpRequest> request) { | 156 scoped_ptr<HttpRequest> request) { |
162 DCHECK(io_thread_->BelongsToCurrentThread()); | 157 DCHECK(io_thread_->BelongsToCurrentThread()); |
163 | 158 |
159 bool request_handled = false; | |
160 | |
164 for (size_t i = 0; i < request_handlers_.size(); ++i) { | 161 for (size_t i = 0; i < request_handlers_.size(); ++i) { |
165 scoped_ptr<HttpResponse> response = | 162 scoped_ptr<HttpResponseInterface> response = |
166 request_handlers_[i].Run(*request.get()); | 163 request_handlers_[i].Run(*request.get()); |
167 if (response.get()) { | 164 if (response.get()) { |
168 connection->SendResponse(response.Pass()); | 165 connection->SendResponse(response.Pass()); |
169 return; | 166 request_handled = true; |
167 break; | |
170 } | 168 } |
171 } | 169 } |
172 | 170 |
173 LOG(WARNING) << "Request not handled. Returning 404: " | 171 if (!request_handled) { |
satorux1
2013/05/22 01:54:56
thank you for fixing the bug.
| |
174 << request->relative_url; | 172 LOG(WARNING) << "Request not handled. Returning 404: " |
175 scoped_ptr<HttpResponse> not_found_response(new HttpResponse()); | 173 << request->relative_url; |
176 not_found_response->set_code(NOT_FOUND); | 174 scoped_ptr<HttpResponse> not_found_response(new HttpResponse); |
177 connection->SendResponse(not_found_response.Pass()); | 175 not_found_response->set_code(NOT_FOUND); |
176 connection->SendResponse( | |
177 not_found_response.PassAs<HttpResponseInterface>()); | |
178 } | |
178 | 179 |
179 // Drop the connection, since we do not support multiple requests per | 180 // Drop the connection, since we do not support multiple requests per |
180 // connection. | 181 // connection. |
181 connections_.erase(connection->socket_.get()); | 182 connections_.erase(connection->socket_.get()); |
182 delete connection; | 183 delete connection; |
183 } | 184 } |
184 | 185 |
185 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { | 186 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { |
186 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) | 187 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) |
187 << relative_url; | 188 << relative_url; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 std::map<StreamListenSocket*, HttpConnection*>::iterator it = | 242 std::map<StreamListenSocket*, HttpConnection*>::iterator it = |
242 connections_.find(socket); | 243 connections_.find(socket); |
243 if (it == connections_.end()) { | 244 if (it == connections_.end()) { |
244 return NULL; | 245 return NULL; |
245 } | 246 } |
246 return it->second; | 247 return it->second; |
247 } | 248 } |
248 | 249 |
249 } // namespace test_server | 250 } // namespace test_server |
250 } // namespace net | 251 } // namespace net |
OLD | NEW |