Chromium Code Reviews| Index: android_webview/test/embeddedtestserver/custom_handlers.cc |
| diff --git a/android_webview/test/embeddedtestserver/custom_handlers.cc b/android_webview/test/embeddedtestserver/custom_handlers.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab48a1916c929cc91271c927fbff175fbdab9526 |
| --- /dev/null |
| +++ b/android_webview/test/embeddedtestserver/custom_handlers.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "android_webview/test/embeddedtestserver/custom_handlers.h" |
| + |
| +#include <jni.h> |
| + |
| +#include "android_webview/test/jni/AwEmbeddedTestServerImpl_jni.h" |
| +#include "base/android/jni_array.h" |
| +#include "base/base64url.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "net/test/embedded_test_server/http_request.h" |
| +#include "net/test/embedded_test_server/http_response.h" |
| +#include "net/test/embedded_test_server/request_handler_util.h" |
| + |
| +using base::android::JavaParamRef; |
| +using base::android::JavaRef; |
| +using base::android::ScopedJavaLocalRef; |
| +using net::test_server::BasicHttpResponse; |
| +using net::test_server::HttpRequest; |
| +using net::test_server::HttpResponse; |
| +using net::test_server::ParseQuery; |
| +using net::test_server::RequestQuery; |
| + |
| +namespace android_webview { |
| +namespace test { |
| + |
| +// /click-redirect?url=URL&echoheaders=HEADERS |
|
jbudorick
2017/03/13 23:32:54
Right now, you've got HEADERS as a URL-encoded lis
shenghuazhang
2017/03/17 01:07:15
Good suggestion! Also, now user need to encode the
|
| +// Returns a href redirect to URL. |
| +// Responds in the message body with the headers echoed on the current request. |
| +std::unique_ptr<HttpResponse> HandleClickRedirect(const HttpRequest& request) { |
| + if (!ShouldHandle(request, "/click-redirect")) |
| + return nullptr; |
| + GURL request_url = request.GetURL(); |
| + RequestQuery query = ParseQuery(request_url); |
| + |
| + std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
| + http_response->set_content_type("text/html"); |
| + http_response->AddCustomHeader("Cache-Control", "no-cache, no-store"); |
| + |
| + std::string url; |
| + if (query.find("url") != query.end()) { |
| + url = query.at("url").front(); |
| + } |
| + |
| + std::string content; |
| + if (query.find("echoheaders") != query.end()) { |
| + std::string header_list = query.at("echoheaders").front(); |
|
jbudorick
2017/03/13 23:32:54
w/ the above change, this code would just be itera
|
| + std::vector<std::string> headers = base::SplitString( |
| + header_list, "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| + for (const auto& header : headers) { |
| + std::string header_name = header; |
| + std::string header_value = "None"; |
| + if (request.headers.find(header_name) != request.headers.end()) |
| + header_value = request.headers.at(header_name); |
| + content += header_value + "\n"; |
| + } |
| + } |
| + |
| + http_response->set_content(base::StringPrintf( |
| + "<html><body><b>%s</b>" |
| + "<a id=\"click\" href=\"%s\">Click to redirect to %s</a></body></html>", |
| + content.c_str(), url.c_str(), url.c_str())); |
| + |
| + return std::move(http_response); |
| +} |
| + |
| +// static |
| +ScopedJavaLocalRef<jlongArray> GetHandlers(JNIEnv* env, |
| + const JavaParamRef<jobject>& jobj) { |
| + std::vector<jlong> handlers = {reinterpret_cast<jlong>(&HandleClickRedirect)}; |
| + return base::android::ToJavaLongArray(env, handlers); |
| +} |
| + |
| +// static |
| +bool RegisterCustomHandlers(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace test |
| +} // namespace android_webview |