Chromium Code Reviews| 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 <jni.h> | |
| 8 | |
| 9 #include "base/android/jni_array.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "net/test/embedded_test_server/http_request.h" | |
| 12 #include "net/test/embedded_test_server/http_response.h" | |
| 13 #include "net/test/embedded_test_server/request_handler_util.h" | |
| 14 #include "net/test/jni/CustomHandlers_jni.h" | |
| 15 | |
| 16 using base::android::JavaParamRef; | |
| 17 using base::android::JavaRef; | |
| 18 using base::android::ScopedJavaLocalRef; | |
| 19 using net::test_server::BasicHttpResponse; | |
| 20 using net::test_server::HttpRequest; | |
| 21 using net::test_server::HttpResponse; | |
| 22 | |
| 23 namespace net { | |
| 24 namespace test { | |
| 25 | |
| 26 // /click-redirect?url=URL&&echoheaders=HEADERS | |
|
shenghuazhang
2017/03/13 18:48:15
Should be '/click-redirect?url=URL&echoheaders=HEA
| |
| 27 // Returns a href redirect to URL. | |
| 28 // Responds in the message body with the headers echoed on the current request. | |
| 29 std::unique_ptr<HttpResponse> HandleClickRedirect(const HttpRequest& request) { | |
| 30 if (!ShouldHandle(request, "/click-redirect")) | |
| 31 return nullptr; | |
| 32 GURL request_url = request.GetURL(); | |
| 33 | |
| 34 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 35 http_response->set_content_type("text/html"); | |
| 36 http_response->AddCustomHeader("Cache-Control", "no-cache, no-store"); | |
| 37 | |
| 38 if (request_url.has_query()) { | |
| 39 std::vector<std::string> query_list = base::SplitString( | |
| 40 request_url.query(), "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 41 std::string pre_url("url="); | |
| 42 std::string url; | |
| 43 std::string pre_header("echoheaders="); | |
| 44 std::string header_query; | |
| 45 for (const auto& query : query_list) { | |
| 46 if (query.compare(0, pre_url.length(), pre_url) == 0) { | |
| 47 url = query.substr(pre_url.length()); | |
| 48 } | |
| 49 if (query.compare(0, pre_header.length(), pre_header) == 0) { | |
| 50 header_query = query.substr(pre_header.length()); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 std::string content; | |
| 55 std::vector<std::string> headers = base::SplitString( | |
| 56 header_query, "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 57 for (const auto& header : headers) { | |
| 58 std::string header_name = header; | |
| 59 std::string header_value = "None"; | |
| 60 if (request.headers.find(header_name) != request.headers.end()) | |
| 61 header_value = request.headers.at(header_name); | |
| 62 content += header_value + "\n"; | |
| 63 } | |
| 64 | |
| 65 http_response->set_content(base::StringPrintf( | |
| 66 "<html><body><b>%s</b>" | |
| 67 "<a id=\"click\" href=\"%s\">Click to redirect to %s</a></body></html>", | |
| 68 content.c_str(), url.c_str(), url.c_str())); | |
| 69 } | |
| 70 return std::move(http_response); | |
| 71 } | |
| 72 | |
| 73 static ScopedJavaLocalRef<jlongArray> GetHandlers( | |
| 74 JNIEnv* env, | |
| 75 const JavaParamRef<jobject>& jobj) { | |
| 76 std::vector<jlong> handlers = {reinterpret_cast<jlong>(&HandleClickRedirect)}; | |
| 77 return base::android::ToJavaLongArray(env, handlers); | |
| 78 } | |
| 79 | |
| 80 // static | |
| 81 bool RegisterCustomHandlers(JNIEnv* env) { | |
| 82 return RegisterNativesImpl(env); | |
| 83 } | |
| 84 | |
| 85 } // namespace test | |
| 86 } // namespace net | |
| OLD | NEW |