OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/test/embedded_test_server/default_handlers.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 #include <ctime> |
| 9 #include <map> |
| 10 #include <sstream> |
| 11 #include <string> |
| 12 |
| 13 #include "base/base64.h" |
| 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" |
| 16 #include "base/format_macros.h" |
| 17 #include "base/macros.h" |
| 18 #include "base/md5.h" |
| 19 #include "base/path_service.h" |
| 20 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" |
| 23 #include "base/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" |
| 25 #include "net/base/escape.h" |
| 26 #include "net/base/url_util.h" |
| 27 #include "net/test/embedded_test_server/http_request.h" |
| 28 #include "net/test/embedded_test_server/http_response.h" |
| 29 #include "net/test/embedded_test_server/request_handler_util.h" |
| 30 |
| 31 namespace net { |
| 32 namespace test_server { |
| 33 namespace { |
| 34 |
| 35 const UnescapeRule::Type kUnescapeAll = |
| 36 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS | |
| 37 UnescapeRule::SPOOFING_AND_CONTROL_CHARS | |
| 38 UnescapeRule::REPLACE_PLUS_WITH_SPACE; |
| 39 |
| 40 const char kDefaultRealm[] = "testrealm"; |
| 41 const char kDefaultPassword[] = "secret"; |
| 42 const char kEtag[] = "abc"; |
| 43 const char kLogoPath[] = "chrome/test/data/google/logo.gif"; |
| 44 |
| 45 // method: CONNECT |
| 46 // Responses with a BAD_REQUEST to any CONNECT requests. |
| 47 scoped_ptr<HttpResponse> HandleDefaultConnect(const HttpRequest& request) { |
| 48 if (request.method != METHOD_CONNECT) |
| 49 return nullptr; |
| 50 |
| 51 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 52 http_response->set_code(HTTP_BAD_REQUEST); |
| 53 http_response->set_content( |
| 54 "Your client has issued a malformed or illegal request."); |
| 55 http_response->set_content_type("text/html"); |
| 56 return http_response.Pass(); |
| 57 } |
| 58 |
| 59 // /cachetime |
| 60 // Returns a cacheable response. |
| 61 scoped_ptr<HttpResponse> HandleCacheTime(const HttpRequest& request) { |
| 62 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 63 http_response->set_content( |
| 64 "<html><head><title>Cache: max-age=60</title></head></html>"); |
| 65 http_response->set_content_type("text/html"); |
| 66 http_response->AddCustomHeader("Cache-Control", "max-age=60"); |
| 67 return http_response.Pass(); |
| 68 } |
| 69 |
| 70 // /echoheader | /echoheadercache |
| 71 // Responds with the headers echoed in the message body. |
| 72 // echoheader does not cache the results, while echoheadercache does. |
| 73 scoped_ptr<HttpResponse> HandleEchoHeader(const std::string& url, |
| 74 const std::string& cache_control, |
| 75 const HttpRequest& request) { |
| 76 if (!ShouldHandle(request, url)) |
| 77 return nullptr; |
| 78 |
| 79 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 80 |
| 81 GURL request_url = request.GetURL(); |
| 82 if (request_url.has_query()) { |
| 83 std::string header_name = request_url.query(); |
| 84 http_response->AddCustomHeader("Vary", header_name); |
| 85 if (request.headers.find(header_name) != request.headers.end()) |
| 86 http_response->set_content(request.headers.at(header_name)); |
| 87 else |
| 88 http_response->set_content("None"); |
| 89 } |
| 90 |
| 91 http_response->set_content_type("text/plain"); |
| 92 http_response->AddCustomHeader("Cache-Control", cache_control); |
| 93 return http_response.Pass(); |
| 94 } |
| 95 |
| 96 // /echo?status=STATUS |
| 97 // Responds with the request body as the response body and |
| 98 // a status code of STATUS. |
| 99 scoped_ptr<HttpResponse> HandleEcho(const HttpRequest& request) { |
| 100 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 101 |
| 102 GURL request_url = request.GetURL(); |
| 103 if (request_url.has_query()) { |
| 104 RequestQuery query = ParseQuery(request_url); |
| 105 if (query.find("status") != query.end()) |
| 106 http_response->set_code(static_cast<HttpStatusCode>( |
| 107 std::atoi(query["status"].front().c_str()))); |
| 108 } |
| 109 |
| 110 http_response->set_content_type("text/html"); |
| 111 if (request.method != METHOD_POST && request.method != METHOD_PUT) |
| 112 http_response->set_content("Echo"); |
| 113 else |
| 114 http_response->set_content(request.content); |
| 115 return http_response.Pass(); |
| 116 } |
| 117 |
| 118 // /echotitle |
| 119 // Responds with the request body as the title. |
| 120 scoped_ptr<HttpResponse> HandleEchoTitle(const HttpRequest& request) { |
| 121 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 122 http_response->set_content_type("text/html"); |
| 123 http_response->set_content("<html><head><title>" + request.content + |
| 124 "</title></head></html>"); |
| 125 return http_response.Pass(); |
| 126 } |
| 127 |
| 128 // /echoall?QUERY |
| 129 // Responds with the list of QUERY and the request headers. |
| 130 scoped_ptr<HttpResponse> HandleEchoAll(const HttpRequest& request) { |
| 131 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 132 |
| 133 std::string body = |
| 134 "<html><head><style>" |
| 135 "pre { border: 1px solid black; margin: 5px; padding: 5px }" |
| 136 "</style></head><body>" |
| 137 "<div style=\"float: right\">" |
| 138 "<a href=\"/echo\">back to referring page</a></div>" |
| 139 "<h1>Request Body:</h1><pre>"; |
| 140 |
| 141 if (request.has_content) { |
| 142 std::vector<std::string> query_list = base::SplitString( |
| 143 request.content, "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 144 for (const auto& query : query_list) |
| 145 body += query + "\n"; |
| 146 } |
| 147 |
| 148 body += |
| 149 "</pre>" |
| 150 "<h1>Request Headers:</h1><pre>" + |
| 151 request.all_headers + |
| 152 "</pre>" |
| 153 "</body></html>"; |
| 154 |
| 155 http_response->set_content_type("text/html"); |
| 156 http_response->set_content(body); |
| 157 return http_response.Pass(); |
| 158 } |
| 159 |
| 160 // /set-cookie?COOKIES |
| 161 // Sets response cookies to be COOKIES. |
| 162 scoped_ptr<HttpResponse> HandleSetCookie(const HttpRequest& request) { |
| 163 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 164 http_response->set_content_type("text/html"); |
| 165 std::string content; |
| 166 GURL request_url = request.GetURL(); |
| 167 if (request_url.has_query()) { |
| 168 std::vector<std::string> cookies = base::SplitString( |
| 169 request_url.query(), "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 170 for (const auto& cookie : cookies) { |
| 171 http_response->AddCustomHeader("Set-Cookie", cookie); |
| 172 content += cookie; |
| 173 } |
| 174 } |
| 175 |
| 176 http_response->set_content(content); |
| 177 return http_response.Pass(); |
| 178 } |
| 179 |
| 180 // /set-many-cookies?N |
| 181 // Sets N cookies in the response. |
| 182 scoped_ptr<HttpResponse> HandleSetManyCookies(const HttpRequest& request) { |
| 183 std::string content; |
| 184 |
| 185 GURL request_url = request.GetURL(); |
| 186 size_t num = 0; |
| 187 if (request_url.has_query()) |
| 188 num = std::atoi(request_url.query().c_str()); |
| 189 |
| 190 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 191 http_response->set_content_type("text/html"); |
| 192 for (size_t i = 0; i < num; ++i) { |
| 193 http_response->AddCustomHeader("Set-Cookie", "a="); |
| 194 } |
| 195 |
| 196 http_response->set_content( |
| 197 base::StringPrintf("%" PRIuS " cookies were sent", num)); |
| 198 return http_response.Pass(); |
| 199 } |
| 200 |
| 201 // /expect-and-set-cookie?expect=EXPECTED&set=SET&data=DATA |
| 202 // Verifies that the request cookies match EXPECTED and then returns cookies |
| 203 // that match SET and a content that matches DATA. |
| 204 scoped_ptr<HttpResponse> HandleExpectAndSetCookie(const HttpRequest& request) { |
| 205 std::vector<std::string> received_cookies; |
| 206 if (request.headers.find("Cookie") != request.headers.end()) { |
| 207 received_cookies = |
| 208 base::SplitString(request.headers.at("Cookie"), ";", |
| 209 base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 210 } |
| 211 |
| 212 bool got_all_expected = true; |
| 213 GURL request_url = request.GetURL(); |
| 214 RequestQuery query_list = ParseQuery(request_url); |
| 215 if (query_list.find("expect") != query_list.end()) { |
| 216 for (const auto& expected_cookie : query_list.at("expect")) { |
| 217 bool found = false; |
| 218 for (const auto& received_cookie : received_cookies) { |
| 219 if (expected_cookie == received_cookie) |
| 220 found = true; |
| 221 } |
| 222 got_all_expected &= found; |
| 223 } |
| 224 } |
| 225 |
| 226 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 227 http_response->set_content_type("text/html"); |
| 228 if (got_all_expected) { |
| 229 for (const auto& cookie : query_list.at("set")) { |
| 230 http_response->AddCustomHeader( |
| 231 "Set-Cookie", net::UnescapeURLComponent(cookie, kUnescapeAll)); |
| 232 } |
| 233 } |
| 234 |
| 235 std::string content; |
| 236 if (query_list.find("data") != query_list.end()) { |
| 237 for (const auto& item : query_list.at("data")) |
| 238 content += item; |
| 239 } |
| 240 |
| 241 http_response->set_content(content); |
| 242 return http_response.Pass(); |
| 243 } |
| 244 |
| 245 // /set-header?HEADERS |
| 246 // Returns a response with HEADERS set as the response headers. |
| 247 scoped_ptr<HttpResponse> HandleSetHeader(const HttpRequest& request) { |
| 248 std::string content; |
| 249 |
| 250 GURL request_url = request.GetURL(); |
| 251 |
| 252 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 253 http_response->set_content_type("text/html"); |
| 254 if (request_url.has_query()) { |
| 255 RequestQuery headers = ParseQuery(request_url); |
| 256 for (const auto& header : headers) { |
| 257 size_t delimiter = header.first.find(": "); |
| 258 if (delimiter == std::string::npos) |
| 259 continue; |
| 260 std::string key = header.first.substr(0, delimiter); |
| 261 std::string value = header.first.substr(delimiter + 2); |
| 262 http_response->AddCustomHeader(key, value); |
| 263 content += header.first; |
| 264 } |
| 265 } |
| 266 |
| 267 http_response->set_content(content); |
| 268 return http_response.Pass(); |
| 269 } |
| 270 |
| 271 // /nocontent |
| 272 // Returns a NO_CONTENT response. |
| 273 scoped_ptr<HttpResponse> HandleNoContent(const HttpRequest& request) { |
| 274 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 275 http_response->set_code(HTTP_NO_CONTENT); |
| 276 return http_response.Pass(); |
| 277 } |
| 278 |
| 279 // /close-socket |
| 280 // Immediately closes the connection. |
| 281 scoped_ptr<HttpResponse> HandleCloseSocket(const HttpRequest& request) { |
| 282 scoped_ptr<RawHttpResponse> http_response(new RawHttpResponse("", "")); |
| 283 return http_response.Pass(); |
| 284 } |
| 285 |
| 286 // /auth-basic?password=PASS&realm=REALM |
| 287 // Performs "Basic" HTTP authentication using expected password PASS and |
| 288 // realm REALM. |
| 289 scoped_ptr<HttpResponse> HandleAuthBasic(const HttpRequest& request) { |
| 290 GURL request_url = request.GetURL(); |
| 291 RequestQuery query = ParseQuery(request_url); |
| 292 |
| 293 std::string expected_password = kDefaultPassword; |
| 294 if (query.find("password") != query.end()) |
| 295 expected_password = query.at("password").front(); |
| 296 std::string realm = kDefaultRealm; |
| 297 if (query.find("realm") != query.end()) |
| 298 realm = query.at("realm").front(); |
| 299 |
| 300 bool authed = false; |
| 301 std::string error; |
| 302 std::string auth; |
| 303 std::string username; |
| 304 std::string userpass; |
| 305 std::string password; |
| 306 std::string b64str; |
| 307 if (request.headers.find("Authorization") == request.headers.end()) { |
| 308 error = "Missing Authorization Header"; |
| 309 } else { |
| 310 auth = request.headers.at("Authorization"); |
| 311 if (auth.find("Basic ") == std::string::npos) { |
| 312 error = "Invalid Authorization Header"; |
| 313 } else { |
| 314 b64str = auth.substr(std::string("Basic ").size()); |
| 315 base::Base64Decode(b64str, &userpass); |
| 316 size_t delimiter = userpass.find(":"); |
| 317 if (delimiter != std::string::npos) { |
| 318 username = userpass.substr(0, delimiter); |
| 319 password = userpass.substr(delimiter + 1); |
| 320 if (password == expected_password) |
| 321 authed = true; |
| 322 else |
| 323 error = "Invalid Credentials"; |
| 324 } else { |
| 325 error = "Invalid Credentials"; |
| 326 } |
| 327 } |
| 328 } |
| 329 |
| 330 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 331 if (!authed) { |
| 332 http_response->set_code(HTTP_UNAUTHORIZED); |
| 333 http_response->set_content_type("text/html"); |
| 334 http_response->AddCustomHeader("WWW-Authenticate", |
| 335 "Basic realm=\"" + realm + "\""); |
| 336 if (query.find("set-cookie-if-challenged") != query.end()) |
| 337 http_response->AddCustomHeader("Set-Cookie", "got_challenged=true"); |
| 338 http_response->set_content(base::StringPrintf( |
| 339 "<html><head><title>Denied: %s</title></head>" |
| 340 "<body>auth=%s<p>b64str=%s<p>username: %s<p>userpass: %s<p>" |
| 341 "password: %s<p>You sent:<br>%s<p></body></html>", |
| 342 error.c_str(), auth.c_str(), b64str.c_str(), username.c_str(), |
| 343 userpass.c_str(), password.c_str(), request.all_headers.c_str())); |
| 344 return http_response.Pass(); |
| 345 } |
| 346 |
| 347 if (request.headers.find("If-None-Match") != request.headers.end() && |
| 348 request.headers.at("If-None-Match") == kEtag) { |
| 349 http_response->set_code(HTTP_NOT_MODIFIED); |
| 350 return http_response.Pass(); |
| 351 } |
| 352 |
| 353 base::FilePath file_path = |
| 354 base::FilePath().AppendASCII(request.relative_url.substr(1)); |
| 355 if (file_path.FinalExtension() == FILE_PATH_LITERAL("gif")) { |
| 356 base::FilePath server_root; |
| 357 PathService::Get(base::DIR_SOURCE_ROOT, &server_root); |
| 358 base::FilePath gif_path = server_root.AppendASCII(kLogoPath); |
| 359 std::string gif_data; |
| 360 base::ReadFileToString(gif_path, &gif_data); |
| 361 http_response->set_content_type("image/gif"); |
| 362 http_response->set_content(gif_data); |
| 363 } else { |
| 364 http_response->set_content_type("text/html"); |
| 365 http_response->set_content( |
| 366 base::StringPrintf("<html><head><title>%s/%s</title></head>" |
| 367 "<body>auth=%s<p>You sent:<br>%s<p></body></html>", |
| 368 username.c_str(), password.c_str(), auth.c_str(), |
| 369 request.all_headers.c_str())); |
| 370 } |
| 371 |
| 372 http_response->AddCustomHeader("Cache-Control", "max-age=60000"); |
| 373 http_response->AddCustomHeader("Etag", kEtag); |
| 374 return http_response.Pass(); |
| 375 } |
| 376 |
| 377 // /auth-digest |
| 378 // Performs "Digest" HTTP authentication. |
| 379 scoped_ptr<HttpResponse> HandleAuthDigest(const HttpRequest& request) { |
| 380 std::string nonce = base::MD5String( |
| 381 base::StringPrintf("privatekey%s", request.relative_url.c_str())); |
| 382 std::string opaque = base::MD5String("opaque"); |
| 383 std::string password = kDefaultPassword; |
| 384 std::string realm = kDefaultRealm; |
| 385 |
| 386 bool authed = false; |
| 387 std::string error; |
| 388 std::string auth; |
| 389 std::string digest_str = "Digest"; |
| 390 std::string username; |
| 391 if (request.headers.find("Authorization") == request.headers.end()) { |
| 392 error = "no auth"; |
| 393 } else if (request.headers.at("Authorization").substr(0, digest_str.size()) != |
| 394 digest_str) { |
| 395 error = "not digest"; |
| 396 } else { |
| 397 auth = request.headers.at("Authorization").substr(digest_str.size() + 1); |
| 398 |
| 399 std::map<std::string, std::string> auth_pairs; |
| 400 base::StringPairs auth_vector; |
| 401 base::SplitStringIntoKeyValuePairs(auth, '=', ',', &auth_vector); |
| 402 for (const auto& auth_pair : auth_vector) { |
| 403 std::string key; |
| 404 std::string value; |
| 405 base::TrimWhitespaceASCII(auth_pair.first, base::TRIM_ALL, &key); |
| 406 base::TrimWhitespaceASCII(auth_pair.second, base::TRIM_ALL, &value); |
| 407 if (value.size() > 2 && value.at(0) == '"' && |
| 408 value.at(value.size() - 1) == '"') { |
| 409 value = value.substr(1, value.size() - 2); |
| 410 } |
| 411 auth_pairs[key] = value; |
| 412 } |
| 413 |
| 414 if (auth_pairs["nonce"] != nonce) { |
| 415 error = "wrong nonce"; |
| 416 } else if (auth_pairs["opaque"] != opaque) { |
| 417 error = "wrong opaque"; |
| 418 } else { |
| 419 username = auth_pairs["username"]; |
| 420 |
| 421 std::string hash1 = base::MD5String( |
| 422 base::StringPrintf("%s:%s:%s", auth_pairs["username"].c_str(), |
| 423 realm.c_str(), password.c_str())); |
| 424 std::string hash2 = base::MD5String(base::StringPrintf( |
| 425 "%s:%s", request.method_string.c_str(), auth_pairs["uri"].c_str())); |
| 426 |
| 427 std::string response; |
| 428 if (auth_pairs.find("qop") != auth_pairs.end() && |
| 429 auth_pairs.find("nc") != auth_pairs.end() && |
| 430 auth_pairs.find("cnonce") != auth_pairs.end()) { |
| 431 response = base::MD5String(base::StringPrintf( |
| 432 "%s:%s:%s:%s:%s:%s", hash1.c_str(), nonce.c_str(), |
| 433 auth_pairs["nc"].c_str(), auth_pairs["cnonce"].c_str(), |
| 434 auth_pairs["qop"].c_str(), hash2.c_str())); |
| 435 } else { |
| 436 response = base::MD5String(base::StringPrintf( |
| 437 "%s:%s:%s", hash1.c_str(), nonce.c_str(), hash2.c_str())); |
| 438 } |
| 439 |
| 440 if (auth_pairs["response"] == response) |
| 441 authed = true; |
| 442 else |
| 443 error = "wrong password"; |
| 444 } |
| 445 } |
| 446 |
| 447 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 448 if (!authed) { |
| 449 http_response->set_code(HTTP_UNAUTHORIZED); |
| 450 http_response->set_content_type("text/html"); |
| 451 std::string auth_header = base::StringPrintf( |
| 452 "Digest realm=\"%s\", " |
| 453 "domain=\"/\", qop=\"auth\", algorithm=MD5, nonce=\"%s\", " |
| 454 "opaque=\"%s\"", |
| 455 realm.c_str(), nonce.c_str(), opaque.c_str()); |
| 456 http_response->AddCustomHeader("WWW-Authenticate", auth_header); |
| 457 http_response->set_content(base::StringPrintf( |
| 458 "<html><head><title>Denied: %s</title></head>" |
| 459 "<body>auth=%s<p>" |
| 460 "You sent:<br>%s<p>We are replying:<br>%s<p></body></html>", |
| 461 error.c_str(), auth.c_str(), request.all_headers.c_str(), |
| 462 auth_header.c_str())); |
| 463 return http_response.Pass(); |
| 464 } |
| 465 |
| 466 http_response->set_content_type("text/html"); |
| 467 http_response->set_content( |
| 468 base::StringPrintf("<html><head><title>%s/%s</title></head>" |
| 469 "<body>auth=%s<p></body></html>", |
| 470 username.c_str(), password.c_str(), auth.c_str())); |
| 471 |
| 472 return http_response.Pass(); |
| 473 } |
| 474 |
| 475 // /server-redirect?URL |
| 476 // Returns a server-redirect (301) to URL. |
| 477 scoped_ptr<HttpResponse> HandleServerRedirect(const HttpRequest& request) { |
| 478 GURL request_url = request.GetURL(); |
| 479 std::string dest = |
| 480 net::UnescapeURLComponent(request_url.query(), kUnescapeAll); |
| 481 |
| 482 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 483 http_response->set_code(HTTP_MOVED_PERMANENTLY); |
| 484 http_response->AddCustomHeader("Location", dest); |
| 485 http_response->set_content_type("text/html"); |
| 486 http_response->set_content(base::StringPrintf( |
| 487 "<html><head></head><body>Redirecting to %s</body></html>", |
| 488 dest.c_str())); |
| 489 return http_response.Pass(); |
| 490 } |
| 491 |
| 492 // /cross-site?URL |
| 493 // Returns a cross-site redirect to URL. |
| 494 scoped_ptr<HttpResponse> HandleCrossSiteRedirect(EmbeddedTestServer* server, |
| 495 const HttpRequest& request) { |
| 496 if (!ShouldHandle(request, "/cross-site")) |
| 497 return nullptr; |
| 498 |
| 499 std::string dest_all = net::UnescapeURLComponent( |
| 500 request.relative_url.substr(std::string("/cross-site").size() + 1), |
| 501 kUnescapeAll); |
| 502 |
| 503 std::string dest; |
| 504 size_t delimiter = dest_all.find("/"); |
| 505 if (delimiter != std::string::npos) { |
| 506 dest = base::StringPrintf( |
| 507 "//%s:%hu/%s", dest_all.substr(0, delimiter).c_str(), server->port(), |
| 508 dest_all.substr(delimiter + 1).c_str()); |
| 509 } |
| 510 |
| 511 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 512 http_response->set_code(HTTP_MOVED_PERMANENTLY); |
| 513 http_response->AddCustomHeader("Location", dest); |
| 514 http_response->set_content_type("text/html"); |
| 515 http_response->set_content(base::StringPrintf( |
| 516 "<html><head></head><body>Redirecting to %s</body></html>", |
| 517 dest.c_str())); |
| 518 return http_response.Pass(); |
| 519 } |
| 520 |
| 521 // /client-redirect?URL |
| 522 // Returns a meta redirect to URL. |
| 523 scoped_ptr<HttpResponse> HandleClientRedirect(const HttpRequest& request) { |
| 524 GURL request_url = request.GetURL(); |
| 525 std::string dest = |
| 526 net::UnescapeURLComponent(request_url.query(), kUnescapeAll); |
| 527 |
| 528 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 529 http_response->set_content_type("text/html"); |
| 530 http_response->set_content(base::StringPrintf( |
| 531 "<html><head><meta http-equiv=\"refresh\" content=\"0;url=%s\"></head>" |
| 532 "<body>Redirecting to %s</body></html>", |
| 533 dest.c_str(), dest.c_str())); |
| 534 return http_response.Pass(); |
| 535 } |
| 536 |
| 537 // /defaultresponse |
| 538 // Returns a valid 200 response. |
| 539 scoped_ptr<HttpResponse> HandleDefaultResponse(const HttpRequest& request) { |
| 540 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 541 http_response->set_content_type("text/html"); |
| 542 http_response->set_content("Default response given for path: " + |
| 543 request.relative_url); |
| 544 return http_response.Pass(); |
| 545 } |
| 546 |
| 547 // Delays |delay| seconds before sending a response to the client. |
| 548 class DelayedHttpResponse : public BasicHttpResponse { |
| 549 public: |
| 550 explicit DelayedHttpResponse(double delay) : delay_(delay) {} |
| 551 |
| 552 void SendResponse(const SendBytesCallback& send, |
| 553 const SendCompleteCallback& done) override { |
| 554 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 555 FROM_HERE, base::Bind(send, ToResponseString(), done), |
| 556 base::TimeDelta::FromSecondsD(delay_)); |
| 557 } |
| 558 |
| 559 private: |
| 560 const double delay_; |
| 561 |
| 562 DISALLOW_COPY_AND_ASSIGN(DelayedHttpResponse); |
| 563 }; |
| 564 |
| 565 // /slow?N |
| 566 // Returns a response to the server delayed by N seconds. |
| 567 scoped_ptr<HttpResponse> HandleSlowServer(const HttpRequest& request) { |
| 568 double delay = 1.0f; |
| 569 |
| 570 GURL request_url = request.GetURL(); |
| 571 if (request_url.has_query()) |
| 572 delay = std::atof(request_url.query().c_str()); |
| 573 |
| 574 scoped_ptr<BasicHttpResponse> http_response(new DelayedHttpResponse(delay)); |
| 575 http_response->set_content_type("text/plain"); |
| 576 http_response->set_content(base::StringPrintf("waited %.1f seconds", delay)); |
| 577 return http_response.Pass(); |
| 578 } |
| 579 |
| 580 } // namespace anonymous |
| 581 |
| 582 #define PREFIXED_HANDLER(prefix, handler) \ |
| 583 base::Bind(&HandlePrefixedRequest, prefix, base::Bind(handler)) |
| 584 |
| 585 void RegisterDefaultHandlers(EmbeddedTestServer* server) { |
| 586 server->RegisterDefaultHandler(base::Bind(&HandleDefaultConnect)); |
| 587 |
| 588 server->RegisterDefaultHandler( |
| 589 PREFIXED_HANDLER("/cachetime", &HandleCacheTime)); |
| 590 server->RegisterDefaultHandler( |
| 591 base::Bind(&HandleEchoHeader, "/echoheader", "no-cache")); |
| 592 server->RegisterDefaultHandler( |
| 593 base::Bind(&HandleEchoHeader, "/echoheadercache", "max-age=60000")); |
| 594 server->RegisterDefaultHandler(PREFIXED_HANDLER("/echo", &HandleEcho)); |
| 595 server->RegisterDefaultHandler( |
| 596 PREFIXED_HANDLER("/echotitle", &HandleEchoTitle)); |
| 597 server->RegisterDefaultHandler(PREFIXED_HANDLER("/echoall", &HandleEchoAll)); |
| 598 server->RegisterDefaultHandler( |
| 599 PREFIXED_HANDLER("/set-cookie", &HandleSetCookie)); |
| 600 server->RegisterDefaultHandler( |
| 601 PREFIXED_HANDLER("/set-many-cookies", &HandleSetManyCookies)); |
| 602 server->RegisterDefaultHandler( |
| 603 PREFIXED_HANDLER("/expect-and-set-cookie", &HandleExpectAndSetCookie)); |
| 604 server->RegisterDefaultHandler( |
| 605 PREFIXED_HANDLER("/set-header", &HandleSetHeader)); |
| 606 server->RegisterDefaultHandler( |
| 607 PREFIXED_HANDLER("/nocontent", &HandleNoContent)); |
| 608 server->RegisterDefaultHandler( |
| 609 PREFIXED_HANDLER("/close-socket", &HandleCloseSocket)); |
| 610 server->RegisterDefaultHandler( |
| 611 PREFIXED_HANDLER("/auth-basic", &HandleAuthBasic)); |
| 612 server->RegisterDefaultHandler( |
| 613 PREFIXED_HANDLER("/auth-digest", &HandleAuthDigest)); |
| 614 server->RegisterDefaultHandler( |
| 615 PREFIXED_HANDLER("/server-redirect", &HandleServerRedirect)); |
| 616 server->RegisterDefaultHandler(base::Bind(&HandleCrossSiteRedirect, server)); |
| 617 server->RegisterDefaultHandler( |
| 618 PREFIXED_HANDLER("/client-redirect", &HandleClientRedirect)); |
| 619 server->RegisterDefaultHandler( |
| 620 PREFIXED_HANDLER("/defaultresponse", &HandleDefaultResponse)); |
| 621 server->RegisterDefaultHandler(PREFIXED_HANDLER("/slow", &HandleSlowServer)); |
| 622 |
| 623 // TODO(svaldez): HandleDownload |
| 624 // TODO(svaldez): HandleDownloadFinish |
| 625 // TODO(svaldez): HandleZipFile |
| 626 // TODO(svaldez): HandleRangeReset |
| 627 // TODO(svaldez): HandleSSLManySmallRecords |
| 628 // TODO(svaldez): HandleChunkedServer |
| 629 // TODO(svaldez): HandleGetSSLSessionCache |
| 630 // TODO(svaldez): HandleGetChannelID |
| 631 // TODO(svaldez): HandleGetClientCert |
| 632 // TODO(svaldez): HandleClientCipherList |
| 633 // TODO(svaldez): HandleEchoMultipartPost |
| 634 } |
| 635 |
| 636 #undef PREFIXED_HANDLER |
| 637 |
| 638 } // namespace test_server |
| 639 } // namespace net |
OLD | NEW |