| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "android_webview/embeddedtestserver/custom_handlers.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 #include <sstream> |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/base64.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" |
| 15 #include "base/format_macros.h" |
| 16 #include "base/macros.h" |
| 17 #include "base/md5.h" |
| 18 #include "base/memory/ptr_util.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/threading/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 |
| 34 // /click-redirect?url=URL&&echoheaders=HEADERS |
| 35 // Returns a href redirect to URL. |
| 36 // Responds in the message body with the headers echoed on the current request. |
| 37 std::unique_ptr<HttpResponse> HandleClickRedirect(const HttpRequest& request) { |
| 38 if (!ShouldHandle(request, "/click-redirect")) |
| 39 return nullptr; |
| 40 GURL request_url = request.GetURL(); |
| 41 |
| 42 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| 43 http_response->set_content_type("text/html"); |
| 44 http_response->AddCustomHeader("Cache-Control", "no-cache, no-store"); |
| 45 |
| 46 if (request_url.has_query()) { |
| 47 std::vector<std::string> query_list = base::SplitString( |
| 48 request_url.query(), "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 49 std::string pre_url("url="); |
| 50 std::string url; |
| 51 std::string pre_header("echoheaders="); |
| 52 std::string header_query; |
| 53 for (const auto& query : query_list) { |
| 54 if (query.compare(0, pre_url.length(), pre_url) == 0) { |
| 55 url = query.substr(pre_url.length()); |
| 56 } |
| 57 if (query.compare(0, pre_header.length(), pre_header) == 0) { |
| 58 header_query = query.substr(pre_header.length()); |
| 59 } |
| 60 } |
| 61 |
| 62 std::string content; |
| 63 std::vector<std::string> headers = base::SplitString( |
| 64 header_query, "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 65 for (const auto& header : headers) { |
| 66 std::string header_name = header; |
| 67 std::string header_value = "None"; |
| 68 if (request.headers.find(header_name) != request.headers.end()) |
| 69 header_value = request.headers.at(header_name); |
| 70 content += header_value + "\n"; |
| 71 } |
| 72 |
| 73 http_response->set_content(base::StringPrintf( |
| 74 "<html><body><b>%s</b>" |
| 75 "<a id=\"click\" href=\"%s\">Click to redirect to %s</a></body></html>", |
| 76 content.c_str(), url.c_str(), url.c_str())); |
| 77 } |
| 78 return std::move(http_response); |
| 79 } |
| 80 |
| 81 std::vector<jlong> handlers = {reinterpret_cast<jlong>(HandleClickRedirect)}; |
| 82 |
| 83 std::vector<jlong> getHandlers() { |
| 84 return handlers; |
| 85 } |
| 86 |
| 87 } // namespace test_server |
| 88 } // namespace net |
| OLD | NEW |