| 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" |
| 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" |
| 8 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 9 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 10 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/threading/thread_restrictions.h" |
| 12 #include "net/test/embedded_test_server/http_connection.h" | 16 #include "net/test/embedded_test_server/http_connection.h" |
| 13 #include "net/test/embedded_test_server/http_request.h" | 17 #include "net/test/embedded_test_server/http_request.h" |
| 14 #include "net/test/embedded_test_server/http_response.h" | 18 #include "net/test/embedded_test_server/http_response.h" |
| 15 #include "net/tools/fetch/http_listen_socket.h" | 19 #include "net/tools/fetch/http_listen_socket.h" |
| 16 | 20 |
| 17 namespace net { | 21 namespace net { |
| 18 namespace test_server { | 22 namespace test_server { |
| 19 | 23 |
| 20 namespace { | 24 namespace { |
| 21 | 25 |
| 22 const int kPort = 8040; | 26 const int kPort = 8040; |
| 23 const char kIp[] = "127.0.0.1"; | 27 const char kIp[] = "127.0.0.1"; |
| 24 const int kRetries = 10; | 28 const int kRetries = 10; |
| 25 | 29 |
| 26 // Callback to handle requests with default predefined response for requests | 30 // Callback to handle requests with default predefined response for requests |
| 27 // matching the address |url|. | 31 // matching the address |url|. |
| 28 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, | 32 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, |
| 29 const HttpResponse& response, | 33 const HttpResponse& response, |
| 30 const HttpRequest& request) { | 34 const HttpRequest& request) { |
| 31 const GURL request_url = url.Resolve(request.relative_url); | 35 const GURL request_url = url.Resolve(request.relative_url); |
| 32 if (url.path() != request_url.path()) | 36 if (url.path() != request_url.path()) |
| 33 return scoped_ptr<HttpResponse>(NULL); | 37 return scoped_ptr<HttpResponse>(NULL); |
| 34 return scoped_ptr<HttpResponse>(new HttpResponse(response)); | 38 return scoped_ptr<HttpResponse>(new HttpResponse(response)); |
| 35 } | 39 } |
| 36 | 40 |
| 41 // Handles |request| by serving a file from under |server_root|. |
| 42 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root, |
| 43 const HttpRequest& request) { |
| 44 // This is a test-only server. Ignore I/O thread restrictions. |
| 45 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 46 |
| 47 // Trim the first byte ('/'). |
| 48 std::string request_path(request.relative_url.substr(1)); |
| 49 |
| 50 std::string file_contents; |
| 51 if (!file_util::ReadFileToString( |
| 52 server_root.AppendASCII(request_path), &file_contents)) { |
| 53 return scoped_ptr<HttpResponse>(NULL); |
| 54 } |
| 55 |
| 56 scoped_ptr<HttpResponse> http_response(new HttpResponse); |
| 57 http_response->set_code(net::test_server::SUCCESS); |
| 58 http_response->set_content(file_contents); |
| 59 return http_response.Pass(); |
| 60 } |
| 61 |
| 37 } // namespace | 62 } // namespace |
| 38 | 63 |
| 39 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, | 64 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
| 40 StreamListenSocket::Delegate* delegate) | 65 StreamListenSocket::Delegate* delegate) |
| 41 : TCPListenSocket(socket_descriptor, delegate) { | 66 : TCPListenSocket(socket_descriptor, delegate) { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 } | 68 } |
| 44 | 69 |
| 45 void HttpListenSocket::Listen() { | 70 void HttpListenSocket::Listen() { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 TCPListenSocket::Listen(); | 72 TCPListenSocket::Listen(); |
| 48 } | 73 } |
| 49 | 74 |
| 50 HttpListenSocket::~HttpListenSocket() { | 75 HttpListenSocket::~HttpListenSocket() { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 } | 77 } |
| 53 | 78 |
| 54 EmbeddedTestServer::EmbeddedTestServer( | 79 EmbeddedTestServer::EmbeddedTestServer( |
| 55 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread) | 80 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread) |
| 56 : io_thread_(io_thread), | 81 : io_thread_(io_thread), |
| 57 port_(-1), | 82 port_(-1), |
| 58 weak_factory_(this) { | 83 weak_factory_(this) { |
| 59 DCHECK(io_thread_); | 84 DCHECK(io_thread_); |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 } | 86 } |
| 62 | 87 |
| 63 EmbeddedTestServer::~EmbeddedTestServer() { | 88 EmbeddedTestServer::~EmbeddedTestServer() { |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 |
| 91 if (Started() && !ShutdownAndWaitUntilComplete()) { |
| 92 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; |
| 93 } |
| 65 } | 94 } |
| 66 | 95 |
| 67 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { | 96 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 | 98 |
| 70 base::RunLoop run_loop; | 99 base::RunLoop run_loop; |
| 71 if (!io_thread_->PostTaskAndReply( | 100 if (!io_thread_->PostTaskAndReply( |
| 72 FROM_HERE, | 101 FROM_HERE, |
| 73 base::Bind(&EmbeddedTestServer::InitializeOnIOThread, | 102 base::Bind(&EmbeddedTestServer::InitializeOnIOThread, |
| 74 base::Unretained(this)), | 103 base::Unretained(this)), |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 connections_.erase(connection->socket_.get()); | 181 connections_.erase(connection->socket_.get()); |
| 153 delete connection; | 182 delete connection; |
| 154 } | 183 } |
| 155 | 184 |
| 156 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { | 185 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { |
| 157 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) | 186 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) |
| 158 << relative_url; | 187 << relative_url; |
| 159 return base_url_.Resolve(relative_url); | 188 return base_url_.Resolve(relative_url); |
| 160 } | 189 } |
| 161 | 190 |
| 191 void EmbeddedTestServer::ServeFilesFromDirectory( |
| 192 const base::FilePath& directory) { |
| 193 RegisterRequestHandler(base::Bind(&HandleFileRequest, directory)); |
| 194 } |
| 195 |
| 162 void EmbeddedTestServer::RegisterRequestHandler( | 196 void EmbeddedTestServer::RegisterRequestHandler( |
| 163 const HandleRequestCallback& callback) { | 197 const HandleRequestCallback& callback) { |
| 164 request_handlers_.push_back(callback); | 198 request_handlers_.push_back(callback); |
| 165 } | 199 } |
| 166 | 200 |
| 167 void EmbeddedTestServer::DidAccept(StreamListenSocket* server, | 201 void EmbeddedTestServer::DidAccept(StreamListenSocket* server, |
| 168 StreamListenSocket* connection) { | 202 StreamListenSocket* connection) { |
| 169 DCHECK(io_thread_->BelongsToCurrentThread()); | 203 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 170 | 204 |
| 171 HttpConnection* http_connection = new HttpConnection( | 205 HttpConnection* http_connection = new HttpConnection( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 std::map<StreamListenSocket*, HttpConnection*>::iterator it = | 241 std::map<StreamListenSocket*, HttpConnection*>::iterator it = |
| 208 connections_.find(socket); | 242 connections_.find(socket); |
| 209 if (it == connections_.end()) { | 243 if (it == connections_.end()) { |
| 210 return NULL; | 244 return NULL; |
| 211 } | 245 } |
| 212 return it->second; | 246 return it->second; |
| 213 } | 247 } |
| 214 | 248 |
| 215 } // namespace test_server | 249 } // namespace test_server |
| 216 } // namespace net | 250 } // namespace net |
| OLD | NEW |