| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/default_handlers.h" | 5 #include "net/test/embedded_test_server/default_handlers.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <ctime> | 8 #include <ctime> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <sstream> | 10 #include <sstream> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <utility> |
| 12 | 13 |
| 13 #include "base/base64.h" | 14 #include "base/base64.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 16 #include "base/format_macros.h" | 17 #include "base/format_macros.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/md5.h" | 19 #include "base/md5.h" |
| 19 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 20 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 46 // Responses with a BAD_REQUEST to any CONNECT requests. | 47 // Responses with a BAD_REQUEST to any CONNECT requests. |
| 47 scoped_ptr<HttpResponse> HandleDefaultConnect(const HttpRequest& request) { | 48 scoped_ptr<HttpResponse> HandleDefaultConnect(const HttpRequest& request) { |
| 48 if (request.method != METHOD_CONNECT) | 49 if (request.method != METHOD_CONNECT) |
| 49 return nullptr; | 50 return nullptr; |
| 50 | 51 |
| 51 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 52 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 52 http_response->set_code(HTTP_BAD_REQUEST); | 53 http_response->set_code(HTTP_BAD_REQUEST); |
| 53 http_response->set_content( | 54 http_response->set_content( |
| 54 "Your client has issued a malformed or illegal request."); | 55 "Your client has issued a malformed or illegal request."); |
| 55 http_response->set_content_type("text/html"); | 56 http_response->set_content_type("text/html"); |
| 56 return http_response.Pass(); | 57 return std::move(http_response); |
| 57 } | 58 } |
| 58 | 59 |
| 59 // /cachetime | 60 // /cachetime |
| 60 // Returns a cacheable response. | 61 // Returns a cacheable response. |
| 61 scoped_ptr<HttpResponse> HandleCacheTime(const HttpRequest& request) { | 62 scoped_ptr<HttpResponse> HandleCacheTime(const HttpRequest& request) { |
| 62 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 63 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 63 http_response->set_content( | 64 http_response->set_content( |
| 64 "<html><head><title>Cache: max-age=60</title></head></html>"); | 65 "<html><head><title>Cache: max-age=60</title></head></html>"); |
| 65 http_response->set_content_type("text/html"); | 66 http_response->set_content_type("text/html"); |
| 66 http_response->AddCustomHeader("Cache-Control", "max-age=60"); | 67 http_response->AddCustomHeader("Cache-Control", "max-age=60"); |
| 67 return http_response.Pass(); | 68 return std::move(http_response); |
| 68 } | 69 } |
| 69 | 70 |
| 70 // /echoheader | /echoheadercache | 71 // /echoheader | /echoheadercache |
| 71 // Responds with the headers echoed in the message body. | 72 // Responds with the headers echoed in the message body. |
| 72 // echoheader does not cache the results, while echoheadercache does. | 73 // echoheader does not cache the results, while echoheadercache does. |
| 73 scoped_ptr<HttpResponse> HandleEchoHeader(const std::string& url, | 74 scoped_ptr<HttpResponse> HandleEchoHeader(const std::string& url, |
| 74 const std::string& cache_control, | 75 const std::string& cache_control, |
| 75 const HttpRequest& request) { | 76 const HttpRequest& request) { |
| 76 if (!ShouldHandle(request, url)) | 77 if (!ShouldHandle(request, url)) |
| 77 return nullptr; | 78 return nullptr; |
| 78 | 79 |
| 79 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 80 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 80 | 81 |
| 81 GURL request_url = request.GetURL(); | 82 GURL request_url = request.GetURL(); |
| 82 if (request_url.has_query()) { | 83 if (request_url.has_query()) { |
| 83 std::string header_name = request_url.query(); | 84 std::string header_name = request_url.query(); |
| 84 http_response->AddCustomHeader("Vary", header_name); | 85 http_response->AddCustomHeader("Vary", header_name); |
| 85 if (request.headers.find(header_name) != request.headers.end()) | 86 if (request.headers.find(header_name) != request.headers.end()) |
| 86 http_response->set_content(request.headers.at(header_name)); | 87 http_response->set_content(request.headers.at(header_name)); |
| 87 else | 88 else |
| 88 http_response->set_content("None"); | 89 http_response->set_content("None"); |
| 89 } | 90 } |
| 90 | 91 |
| 91 http_response->set_content_type("text/plain"); | 92 http_response->set_content_type("text/plain"); |
| 92 http_response->AddCustomHeader("Cache-Control", cache_control); | 93 http_response->AddCustomHeader("Cache-Control", cache_control); |
| 93 return http_response.Pass(); | 94 return std::move(http_response); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // /echo?status=STATUS | 97 // /echo?status=STATUS |
| 97 // Responds with the request body as the response body and | 98 // Responds with the request body as the response body and |
| 98 // a status code of STATUS. | 99 // a status code of STATUS. |
| 99 scoped_ptr<HttpResponse> HandleEcho(const HttpRequest& request) { | 100 scoped_ptr<HttpResponse> HandleEcho(const HttpRequest& request) { |
| 100 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 101 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 101 | 102 |
| 102 GURL request_url = request.GetURL(); | 103 GURL request_url = request.GetURL(); |
| 103 if (request_url.has_query()) { | 104 if (request_url.has_query()) { |
| 104 RequestQuery query = ParseQuery(request_url); | 105 RequestQuery query = ParseQuery(request_url); |
| 105 if (query.find("status") != query.end()) | 106 if (query.find("status") != query.end()) |
| 106 http_response->set_code(static_cast<HttpStatusCode>( | 107 http_response->set_code(static_cast<HttpStatusCode>( |
| 107 std::atoi(query["status"].front().c_str()))); | 108 std::atoi(query["status"].front().c_str()))); |
| 108 } | 109 } |
| 109 | 110 |
| 110 http_response->set_content_type("text/html"); | 111 http_response->set_content_type("text/html"); |
| 111 if (request.method != METHOD_POST && request.method != METHOD_PUT) | 112 if (request.method != METHOD_POST && request.method != METHOD_PUT) |
| 112 http_response->set_content("Echo"); | 113 http_response->set_content("Echo"); |
| 113 else | 114 else |
| 114 http_response->set_content(request.content); | 115 http_response->set_content(request.content); |
| 115 return http_response.Pass(); | 116 return std::move(http_response); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // /echotitle | 119 // /echotitle |
| 119 // Responds with the request body as the title. | 120 // Responds with the request body as the title. |
| 120 scoped_ptr<HttpResponse> HandleEchoTitle(const HttpRequest& request) { | 121 scoped_ptr<HttpResponse> HandleEchoTitle(const HttpRequest& request) { |
| 121 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 122 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 122 http_response->set_content_type("text/html"); | 123 http_response->set_content_type("text/html"); |
| 123 http_response->set_content("<html><head><title>" + request.content + | 124 http_response->set_content("<html><head><title>" + request.content + |
| 124 "</title></head></html>"); | 125 "</title></head></html>"); |
| 125 return http_response.Pass(); | 126 return std::move(http_response); |
| 126 } | 127 } |
| 127 | 128 |
| 128 // /echoall?QUERY | 129 // /echoall?QUERY |
| 129 // Responds with the list of QUERY and the request headers. | 130 // Responds with the list of QUERY and the request headers. |
| 130 scoped_ptr<HttpResponse> HandleEchoAll(const HttpRequest& request) { | 131 scoped_ptr<HttpResponse> HandleEchoAll(const HttpRequest& request) { |
| 131 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 132 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 132 | 133 |
| 133 std::string body = | 134 std::string body = |
| 134 "<html><head><style>" | 135 "<html><head><style>" |
| 135 "pre { border: 1px solid black; margin: 5px; padding: 5px }" | 136 "pre { border: 1px solid black; margin: 5px; padding: 5px }" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 147 | 148 |
| 148 body += | 149 body += |
| 149 "</pre>" | 150 "</pre>" |
| 150 "<h1>Request Headers:</h1><pre>" + | 151 "<h1>Request Headers:</h1><pre>" + |
| 151 request.all_headers + | 152 request.all_headers + |
| 152 "</pre>" | 153 "</pre>" |
| 153 "</body></html>"; | 154 "</body></html>"; |
| 154 | 155 |
| 155 http_response->set_content_type("text/html"); | 156 http_response->set_content_type("text/html"); |
| 156 http_response->set_content(body); | 157 http_response->set_content(body); |
| 157 return http_response.Pass(); | 158 return std::move(http_response); |
| 158 } | 159 } |
| 159 | 160 |
| 160 // /set-cookie?COOKIES | 161 // /set-cookie?COOKIES |
| 161 // Sets response cookies to be COOKIES. | 162 // Sets response cookies to be COOKIES. |
| 162 scoped_ptr<HttpResponse> HandleSetCookie(const HttpRequest& request) { | 163 scoped_ptr<HttpResponse> HandleSetCookie(const HttpRequest& request) { |
| 163 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 164 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 164 http_response->set_content_type("text/html"); | 165 http_response->set_content_type("text/html"); |
| 165 std::string content; | 166 std::string content; |
| 166 GURL request_url = request.GetURL(); | 167 GURL request_url = request.GetURL(); |
| 167 if (request_url.has_query()) { | 168 if (request_url.has_query()) { |
| 168 std::vector<std::string> cookies = base::SplitString( | 169 std::vector<std::string> cookies = base::SplitString( |
| 169 request_url.query(), "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | 170 request_url.query(), "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 170 for (const auto& cookie : cookies) { | 171 for (const auto& cookie : cookies) { |
| 171 http_response->AddCustomHeader("Set-Cookie", cookie); | 172 http_response->AddCustomHeader("Set-Cookie", cookie); |
| 172 content += cookie; | 173 content += cookie; |
| 173 } | 174 } |
| 174 } | 175 } |
| 175 | 176 |
| 176 http_response->set_content(content); | 177 http_response->set_content(content); |
| 177 return http_response.Pass(); | 178 return std::move(http_response); |
| 178 } | 179 } |
| 179 | 180 |
| 180 // /set-many-cookies?N | 181 // /set-many-cookies?N |
| 181 // Sets N cookies in the response. | 182 // Sets N cookies in the response. |
| 182 scoped_ptr<HttpResponse> HandleSetManyCookies(const HttpRequest& request) { | 183 scoped_ptr<HttpResponse> HandleSetManyCookies(const HttpRequest& request) { |
| 183 std::string content; | 184 std::string content; |
| 184 | 185 |
| 185 GURL request_url = request.GetURL(); | 186 GURL request_url = request.GetURL(); |
| 186 size_t num = 0; | 187 size_t num = 0; |
| 187 if (request_url.has_query()) | 188 if (request_url.has_query()) |
| 188 num = std::atoi(request_url.query().c_str()); | 189 num = std::atoi(request_url.query().c_str()); |
| 189 | 190 |
| 190 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 191 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 191 http_response->set_content_type("text/html"); | 192 http_response->set_content_type("text/html"); |
| 192 for (size_t i = 0; i < num; ++i) { | 193 for (size_t i = 0; i < num; ++i) { |
| 193 http_response->AddCustomHeader("Set-Cookie", "a="); | 194 http_response->AddCustomHeader("Set-Cookie", "a="); |
| 194 } | 195 } |
| 195 | 196 |
| 196 http_response->set_content( | 197 http_response->set_content( |
| 197 base::StringPrintf("%" PRIuS " cookies were sent", num)); | 198 base::StringPrintf("%" PRIuS " cookies were sent", num)); |
| 198 return http_response.Pass(); | 199 return std::move(http_response); |
| 199 } | 200 } |
| 200 | 201 |
| 201 // /expect-and-set-cookie?expect=EXPECTED&set=SET&data=DATA | 202 // /expect-and-set-cookie?expect=EXPECTED&set=SET&data=DATA |
| 202 // Verifies that the request cookies match EXPECTED and then returns cookies | 203 // Verifies that the request cookies match EXPECTED and then returns cookies |
| 203 // that match SET and a content that matches DATA. | 204 // that match SET and a content that matches DATA. |
| 204 scoped_ptr<HttpResponse> HandleExpectAndSetCookie(const HttpRequest& request) { | 205 scoped_ptr<HttpResponse> HandleExpectAndSetCookie(const HttpRequest& request) { |
| 205 std::vector<std::string> received_cookies; | 206 std::vector<std::string> received_cookies; |
| 206 if (request.headers.find("Cookie") != request.headers.end()) { | 207 if (request.headers.find("Cookie") != request.headers.end()) { |
| 207 received_cookies = | 208 received_cookies = |
| 208 base::SplitString(request.headers.at("Cookie"), ";", | 209 base::SplitString(request.headers.at("Cookie"), ";", |
| (...skipping 23 matching lines...) Expand all Loading... |
| 232 } | 233 } |
| 233 } | 234 } |
| 234 | 235 |
| 235 std::string content; | 236 std::string content; |
| 236 if (query_list.find("data") != query_list.end()) { | 237 if (query_list.find("data") != query_list.end()) { |
| 237 for (const auto& item : query_list.at("data")) | 238 for (const auto& item : query_list.at("data")) |
| 238 content += item; | 239 content += item; |
| 239 } | 240 } |
| 240 | 241 |
| 241 http_response->set_content(content); | 242 http_response->set_content(content); |
| 242 return http_response.Pass(); | 243 return std::move(http_response); |
| 243 } | 244 } |
| 244 | 245 |
| 245 // /set-header?HEADERS | 246 // /set-header?HEADERS |
| 246 // Returns a response with HEADERS set as the response headers. | 247 // Returns a response with HEADERS set as the response headers. |
| 247 scoped_ptr<HttpResponse> HandleSetHeader(const HttpRequest& request) { | 248 scoped_ptr<HttpResponse> HandleSetHeader(const HttpRequest& request) { |
| 248 std::string content; | 249 std::string content; |
| 249 | 250 |
| 250 GURL request_url = request.GetURL(); | 251 GURL request_url = request.GetURL(); |
| 251 | 252 |
| 252 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 253 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 253 http_response->set_content_type("text/html"); | 254 http_response->set_content_type("text/html"); |
| 254 if (request_url.has_query()) { | 255 if (request_url.has_query()) { |
| 255 RequestQuery headers = ParseQuery(request_url); | 256 RequestQuery headers = ParseQuery(request_url); |
| 256 for (const auto& header : headers) { | 257 for (const auto& header : headers) { |
| 257 size_t delimiter = header.first.find(": "); | 258 size_t delimiter = header.first.find(": "); |
| 258 if (delimiter == std::string::npos) | 259 if (delimiter == std::string::npos) |
| 259 continue; | 260 continue; |
| 260 std::string key = header.first.substr(0, delimiter); | 261 std::string key = header.first.substr(0, delimiter); |
| 261 std::string value = header.first.substr(delimiter + 2); | 262 std::string value = header.first.substr(delimiter + 2); |
| 262 http_response->AddCustomHeader(key, value); | 263 http_response->AddCustomHeader(key, value); |
| 263 content += header.first; | 264 content += header.first; |
| 264 } | 265 } |
| 265 } | 266 } |
| 266 | 267 |
| 267 http_response->set_content(content); | 268 http_response->set_content(content); |
| 268 return http_response.Pass(); | 269 return std::move(http_response); |
| 269 } | 270 } |
| 270 | 271 |
| 271 // /nocontent | 272 // /nocontent |
| 272 // Returns a NO_CONTENT response. | 273 // Returns a NO_CONTENT response. |
| 273 scoped_ptr<HttpResponse> HandleNoContent(const HttpRequest& request) { | 274 scoped_ptr<HttpResponse> HandleNoContent(const HttpRequest& request) { |
| 274 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 275 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 275 http_response->set_code(HTTP_NO_CONTENT); | 276 http_response->set_code(HTTP_NO_CONTENT); |
| 276 return http_response.Pass(); | 277 return std::move(http_response); |
| 277 } | 278 } |
| 278 | 279 |
| 279 // /close-socket | 280 // /close-socket |
| 280 // Immediately closes the connection. | 281 // Immediately closes the connection. |
| 281 scoped_ptr<HttpResponse> HandleCloseSocket(const HttpRequest& request) { | 282 scoped_ptr<HttpResponse> HandleCloseSocket(const HttpRequest& request) { |
| 282 scoped_ptr<RawHttpResponse> http_response(new RawHttpResponse("", "")); | 283 scoped_ptr<RawHttpResponse> http_response(new RawHttpResponse("", "")); |
| 283 return http_response.Pass(); | 284 return std::move(http_response); |
| 284 } | 285 } |
| 285 | 286 |
| 286 // /auth-basic?password=PASS&realm=REALM | 287 // /auth-basic?password=PASS&realm=REALM |
| 287 // Performs "Basic" HTTP authentication using expected password PASS and | 288 // Performs "Basic" HTTP authentication using expected password PASS and |
| 288 // realm REALM. | 289 // realm REALM. |
| 289 scoped_ptr<HttpResponse> HandleAuthBasic(const HttpRequest& request) { | 290 scoped_ptr<HttpResponse> HandleAuthBasic(const HttpRequest& request) { |
| 290 GURL request_url = request.GetURL(); | 291 GURL request_url = request.GetURL(); |
| 291 RequestQuery query = ParseQuery(request_url); | 292 RequestQuery query = ParseQuery(request_url); |
| 292 | 293 |
| 293 std::string expected_password = kDefaultPassword; | 294 std::string expected_password = kDefaultPassword; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 http_response->AddCustomHeader("WWW-Authenticate", | 335 http_response->AddCustomHeader("WWW-Authenticate", |
| 335 "Basic realm=\"" + realm + "\""); | 336 "Basic realm=\"" + realm + "\""); |
| 336 if (query.find("set-cookie-if-challenged") != query.end()) | 337 if (query.find("set-cookie-if-challenged") != query.end()) |
| 337 http_response->AddCustomHeader("Set-Cookie", "got_challenged=true"); | 338 http_response->AddCustomHeader("Set-Cookie", "got_challenged=true"); |
| 338 http_response->set_content(base::StringPrintf( | 339 http_response->set_content(base::StringPrintf( |
| 339 "<html><head><title>Denied: %s</title></head>" | 340 "<html><head><title>Denied: %s</title></head>" |
| 340 "<body>auth=%s<p>b64str=%s<p>username: %s<p>userpass: %s<p>" | 341 "<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 "password: %s<p>You sent:<br>%s<p></body></html>", |
| 342 error.c_str(), auth.c_str(), b64str.c_str(), username.c_str(), | 343 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 userpass.c_str(), password.c_str(), request.all_headers.c_str())); |
| 344 return http_response.Pass(); | 345 return std::move(http_response); |
| 345 } | 346 } |
| 346 | 347 |
| 347 if (request.headers.find("If-None-Match") != request.headers.end() && | 348 if (request.headers.find("If-None-Match") != request.headers.end() && |
| 348 request.headers.at("If-None-Match") == kEtag) { | 349 request.headers.at("If-None-Match") == kEtag) { |
| 349 http_response->set_code(HTTP_NOT_MODIFIED); | 350 http_response->set_code(HTTP_NOT_MODIFIED); |
| 350 return http_response.Pass(); | 351 return std::move(http_response); |
| 351 } | 352 } |
| 352 | 353 |
| 353 base::FilePath file_path = | 354 base::FilePath file_path = |
| 354 base::FilePath().AppendASCII(request.relative_url.substr(1)); | 355 base::FilePath().AppendASCII(request.relative_url.substr(1)); |
| 355 if (file_path.FinalExtension() == FILE_PATH_LITERAL("gif")) { | 356 if (file_path.FinalExtension() == FILE_PATH_LITERAL("gif")) { |
| 356 base::FilePath server_root; | 357 base::FilePath server_root; |
| 357 PathService::Get(base::DIR_SOURCE_ROOT, &server_root); | 358 PathService::Get(base::DIR_SOURCE_ROOT, &server_root); |
| 358 base::FilePath gif_path = server_root.AppendASCII(kLogoPath); | 359 base::FilePath gif_path = server_root.AppendASCII(kLogoPath); |
| 359 std::string gif_data; | 360 std::string gif_data; |
| 360 base::ReadFileToString(gif_path, &gif_data); | 361 base::ReadFileToString(gif_path, &gif_data); |
| 361 http_response->set_content_type("image/gif"); | 362 http_response->set_content_type("image/gif"); |
| 362 http_response->set_content(gif_data); | 363 http_response->set_content(gif_data); |
| 363 } else { | 364 } else { |
| 364 http_response->set_content_type("text/html"); | 365 http_response->set_content_type("text/html"); |
| 365 http_response->set_content( | 366 http_response->set_content( |
| 366 base::StringPrintf("<html><head><title>%s/%s</title></head>" | 367 base::StringPrintf("<html><head><title>%s/%s</title></head>" |
| 367 "<body>auth=%s<p>You sent:<br>%s<p></body></html>", | 368 "<body>auth=%s<p>You sent:<br>%s<p></body></html>", |
| 368 username.c_str(), password.c_str(), auth.c_str(), | 369 username.c_str(), password.c_str(), auth.c_str(), |
| 369 request.all_headers.c_str())); | 370 request.all_headers.c_str())); |
| 370 } | 371 } |
| 371 | 372 |
| 372 http_response->AddCustomHeader("Cache-Control", "max-age=60000"); | 373 http_response->AddCustomHeader("Cache-Control", "max-age=60000"); |
| 373 http_response->AddCustomHeader("Etag", kEtag); | 374 http_response->AddCustomHeader("Etag", kEtag); |
| 374 return http_response.Pass(); | 375 return std::move(http_response); |
| 375 } | 376 } |
| 376 | 377 |
| 377 // /auth-digest | 378 // /auth-digest |
| 378 // Performs "Digest" HTTP authentication. | 379 // Performs "Digest" HTTP authentication. |
| 379 scoped_ptr<HttpResponse> HandleAuthDigest(const HttpRequest& request) { | 380 scoped_ptr<HttpResponse> HandleAuthDigest(const HttpRequest& request) { |
| 380 std::string nonce = base::MD5String( | 381 std::string nonce = base::MD5String( |
| 381 base::StringPrintf("privatekey%s", request.relative_url.c_str())); | 382 base::StringPrintf("privatekey%s", request.relative_url.c_str())); |
| 382 std::string opaque = base::MD5String("opaque"); | 383 std::string opaque = base::MD5String("opaque"); |
| 383 std::string password = kDefaultPassword; | 384 std::string password = kDefaultPassword; |
| 384 std::string realm = kDefaultRealm; | 385 std::string realm = kDefaultRealm; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 "domain=\"/\", qop=\"auth\", algorithm=MD5, nonce=\"%s\", " | 454 "domain=\"/\", qop=\"auth\", algorithm=MD5, nonce=\"%s\", " |
| 454 "opaque=\"%s\"", | 455 "opaque=\"%s\"", |
| 455 realm.c_str(), nonce.c_str(), opaque.c_str()); | 456 realm.c_str(), nonce.c_str(), opaque.c_str()); |
| 456 http_response->AddCustomHeader("WWW-Authenticate", auth_header); | 457 http_response->AddCustomHeader("WWW-Authenticate", auth_header); |
| 457 http_response->set_content(base::StringPrintf( | 458 http_response->set_content(base::StringPrintf( |
| 458 "<html><head><title>Denied: %s</title></head>" | 459 "<html><head><title>Denied: %s</title></head>" |
| 459 "<body>auth=%s<p>" | 460 "<body>auth=%s<p>" |
| 460 "You sent:<br>%s<p>We are replying:<br>%s<p></body></html>", | 461 "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 error.c_str(), auth.c_str(), request.all_headers.c_str(), |
| 462 auth_header.c_str())); | 463 auth_header.c_str())); |
| 463 return http_response.Pass(); | 464 return std::move(http_response); |
| 464 } | 465 } |
| 465 | 466 |
| 466 http_response->set_content_type("text/html"); | 467 http_response->set_content_type("text/html"); |
| 467 http_response->set_content( | 468 http_response->set_content( |
| 468 base::StringPrintf("<html><head><title>%s/%s</title></head>" | 469 base::StringPrintf("<html><head><title>%s/%s</title></head>" |
| 469 "<body>auth=%s<p></body></html>", | 470 "<body>auth=%s<p></body></html>", |
| 470 username.c_str(), password.c_str(), auth.c_str())); | 471 username.c_str(), password.c_str(), auth.c_str())); |
| 471 | 472 |
| 472 return http_response.Pass(); | 473 return std::move(http_response); |
| 473 } | 474 } |
| 474 | 475 |
| 475 // /server-redirect?URL | 476 // /server-redirect?URL |
| 476 // Returns a server-redirect (301) to URL. | 477 // Returns a server-redirect (301) to URL. |
| 477 scoped_ptr<HttpResponse> HandleServerRedirect(const HttpRequest& request) { | 478 scoped_ptr<HttpResponse> HandleServerRedirect(const HttpRequest& request) { |
| 478 GURL request_url = request.GetURL(); | 479 GURL request_url = request.GetURL(); |
| 479 std::string dest = | 480 std::string dest = |
| 480 net::UnescapeURLComponent(request_url.query(), kUnescapeAll); | 481 net::UnescapeURLComponent(request_url.query(), kUnescapeAll); |
| 481 | 482 |
| 482 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 483 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 483 http_response->set_code(HTTP_MOVED_PERMANENTLY); | 484 http_response->set_code(HTTP_MOVED_PERMANENTLY); |
| 484 http_response->AddCustomHeader("Location", dest); | 485 http_response->AddCustomHeader("Location", dest); |
| 485 http_response->set_content_type("text/html"); | 486 http_response->set_content_type("text/html"); |
| 486 http_response->set_content(base::StringPrintf( | 487 http_response->set_content(base::StringPrintf( |
| 487 "<html><head></head><body>Redirecting to %s</body></html>", | 488 "<html><head></head><body>Redirecting to %s</body></html>", |
| 488 dest.c_str())); | 489 dest.c_str())); |
| 489 return http_response.Pass(); | 490 return std::move(http_response); |
| 490 } | 491 } |
| 491 | 492 |
| 492 // /cross-site?URL | 493 // /cross-site?URL |
| 493 // Returns a cross-site redirect to URL. | 494 // Returns a cross-site redirect to URL. |
| 494 scoped_ptr<HttpResponse> HandleCrossSiteRedirect(EmbeddedTestServer* server, | 495 scoped_ptr<HttpResponse> HandleCrossSiteRedirect(EmbeddedTestServer* server, |
| 495 const HttpRequest& request) { | 496 const HttpRequest& request) { |
| 496 if (!ShouldHandle(request, "/cross-site")) | 497 if (!ShouldHandle(request, "/cross-site")) |
| 497 return nullptr; | 498 return nullptr; |
| 498 | 499 |
| 499 std::string dest_all = net::UnescapeURLComponent( | 500 std::string dest_all = net::UnescapeURLComponent( |
| 500 request.relative_url.substr(std::string("/cross-site").size() + 1), | 501 request.relative_url.substr(std::string("/cross-site").size() + 1), |
| 501 kUnescapeAll); | 502 kUnescapeAll); |
| 502 | 503 |
| 503 std::string dest; | 504 std::string dest; |
| 504 size_t delimiter = dest_all.find("/"); | 505 size_t delimiter = dest_all.find("/"); |
| 505 if (delimiter != std::string::npos) { | 506 if (delimiter != std::string::npos) { |
| 506 dest = base::StringPrintf( | 507 dest = base::StringPrintf( |
| 507 "//%s:%hu/%s", dest_all.substr(0, delimiter).c_str(), server->port(), | 508 "//%s:%hu/%s", dest_all.substr(0, delimiter).c_str(), server->port(), |
| 508 dest_all.substr(delimiter + 1).c_str()); | 509 dest_all.substr(delimiter + 1).c_str()); |
| 509 } | 510 } |
| 510 | 511 |
| 511 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 512 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 512 http_response->set_code(HTTP_MOVED_PERMANENTLY); | 513 http_response->set_code(HTTP_MOVED_PERMANENTLY); |
| 513 http_response->AddCustomHeader("Location", dest); | 514 http_response->AddCustomHeader("Location", dest); |
| 514 http_response->set_content_type("text/html"); | 515 http_response->set_content_type("text/html"); |
| 515 http_response->set_content(base::StringPrintf( | 516 http_response->set_content(base::StringPrintf( |
| 516 "<html><head></head><body>Redirecting to %s</body></html>", | 517 "<html><head></head><body>Redirecting to %s</body></html>", |
| 517 dest.c_str())); | 518 dest.c_str())); |
| 518 return http_response.Pass(); | 519 return std::move(http_response); |
| 519 } | 520 } |
| 520 | 521 |
| 521 // /client-redirect?URL | 522 // /client-redirect?URL |
| 522 // Returns a meta redirect to URL. | 523 // Returns a meta redirect to URL. |
| 523 scoped_ptr<HttpResponse> HandleClientRedirect(const HttpRequest& request) { | 524 scoped_ptr<HttpResponse> HandleClientRedirect(const HttpRequest& request) { |
| 524 GURL request_url = request.GetURL(); | 525 GURL request_url = request.GetURL(); |
| 525 std::string dest = | 526 std::string dest = |
| 526 net::UnescapeURLComponent(request_url.query(), kUnescapeAll); | 527 net::UnescapeURLComponent(request_url.query(), kUnescapeAll); |
| 527 | 528 |
| 528 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 529 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 529 http_response->set_content_type("text/html"); | 530 http_response->set_content_type("text/html"); |
| 530 http_response->set_content(base::StringPrintf( | 531 http_response->set_content(base::StringPrintf( |
| 531 "<html><head><meta http-equiv=\"refresh\" content=\"0;url=%s\"></head>" | 532 "<html><head><meta http-equiv=\"refresh\" content=\"0;url=%s\"></head>" |
| 532 "<body>Redirecting to %s</body></html>", | 533 "<body>Redirecting to %s</body></html>", |
| 533 dest.c_str(), dest.c_str())); | 534 dest.c_str(), dest.c_str())); |
| 534 return http_response.Pass(); | 535 return std::move(http_response); |
| 535 } | 536 } |
| 536 | 537 |
| 537 // /defaultresponse | 538 // /defaultresponse |
| 538 // Returns a valid 200 response. | 539 // Returns a valid 200 response. |
| 539 scoped_ptr<HttpResponse> HandleDefaultResponse(const HttpRequest& request) { | 540 scoped_ptr<HttpResponse> HandleDefaultResponse(const HttpRequest& request) { |
| 540 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 541 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 541 http_response->set_content_type("text/html"); | 542 http_response->set_content_type("text/html"); |
| 542 http_response->set_content("Default response given for path: " + | 543 http_response->set_content("Default response given for path: " + |
| 543 request.relative_url); | 544 request.relative_url); |
| 544 return http_response.Pass(); | 545 return std::move(http_response); |
| 545 } | 546 } |
| 546 | 547 |
| 547 // Delays |delay| seconds before sending a response to the client. | 548 // Delays |delay| seconds before sending a response to the client. |
| 548 class DelayedHttpResponse : public BasicHttpResponse { | 549 class DelayedHttpResponse : public BasicHttpResponse { |
| 549 public: | 550 public: |
| 550 explicit DelayedHttpResponse(double delay) : delay_(delay) {} | 551 explicit DelayedHttpResponse(double delay) : delay_(delay) {} |
| 551 | 552 |
| 552 void SendResponse(const SendBytesCallback& send, | 553 void SendResponse(const SendBytesCallback& send, |
| 553 const SendCompleteCallback& done) override { | 554 const SendCompleteCallback& done) override { |
| 554 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 555 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 567 scoped_ptr<HttpResponse> HandleSlowServer(const HttpRequest& request) { | 568 scoped_ptr<HttpResponse> HandleSlowServer(const HttpRequest& request) { |
| 568 double delay = 1.0f; | 569 double delay = 1.0f; |
| 569 | 570 |
| 570 GURL request_url = request.GetURL(); | 571 GURL request_url = request.GetURL(); |
| 571 if (request_url.has_query()) | 572 if (request_url.has_query()) |
| 572 delay = std::atof(request_url.query().c_str()); | 573 delay = std::atof(request_url.query().c_str()); |
| 573 | 574 |
| 574 scoped_ptr<BasicHttpResponse> http_response(new DelayedHttpResponse(delay)); | 575 scoped_ptr<BasicHttpResponse> http_response(new DelayedHttpResponse(delay)); |
| 575 http_response->set_content_type("text/plain"); | 576 http_response->set_content_type("text/plain"); |
| 576 http_response->set_content(base::StringPrintf("waited %.1f seconds", delay)); | 577 http_response->set_content(base::StringPrintf("waited %.1f seconds", delay)); |
| 577 return http_response.Pass(); | 578 return std::move(http_response); |
| 578 } | 579 } |
| 579 | 580 |
| 580 } // namespace anonymous | 581 } // namespace anonymous |
| 581 | 582 |
| 582 #define PREFIXED_HANDLER(prefix, handler) \ | 583 #define PREFIXED_HANDLER(prefix, handler) \ |
| 583 base::Bind(&HandlePrefixedRequest, prefix, base::Bind(handler)) | 584 base::Bind(&HandlePrefixedRequest, prefix, base::Bind(handler)) |
| 584 | 585 |
| 585 void RegisterDefaultHandlers(EmbeddedTestServer* server) { | 586 void RegisterDefaultHandlers(EmbeddedTestServer* server) { |
| 586 server->RegisterDefaultHandler(base::Bind(&HandleDefaultConnect)); | 587 server->RegisterDefaultHandler(base::Bind(&HandleDefaultConnect)); |
| 587 | 588 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 // TODO(svaldez): HandleGetChannelID | 630 // TODO(svaldez): HandleGetChannelID |
| 630 // TODO(svaldez): HandleGetClientCert | 631 // TODO(svaldez): HandleGetClientCert |
| 631 // TODO(svaldez): HandleClientCipherList | 632 // TODO(svaldez): HandleClientCipherList |
| 632 // TODO(svaldez): HandleEchoMultipartPost | 633 // TODO(svaldez): HandleEchoMultipartPost |
| 633 } | 634 } |
| 634 | 635 |
| 635 #undef PREFIXED_HANDLER | 636 #undef PREFIXED_HANDLER |
| 636 | 637 |
| 637 } // namespace test_server | 638 } // namespace test_server |
| 638 } // namespace net | 639 } // namespace net |
| OLD | NEW |