Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1634)

Unified Diff: android_webview/embeddedtestserver/custom_handlers.cc

Issue 2687573002: [Android Webview] Refactor LoadUrlTest and work on embedded_test_server custom handler (Closed)
Patch Set: discussing patch Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/embeddedtestserver/custom_handlers.cc
diff --git a/android_webview/embeddedtestserver/custom_handlers.cc b/android_webview/embeddedtestserver/custom_handlers.cc
new file mode 100644
index 0000000000000000000000000000000000000000..48ec639fc7e0b8c68c01f252d0970ab085fe0d57
--- /dev/null
+++ b/android_webview/embeddedtestserver/custom_handlers.cc
@@ -0,0 +1,88 @@
+// 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/embeddedtestserver/custom_handlers.h"
+
+#include <stdlib.h>
+#include <sstream>
+#include <string>
+#include <utility>
+
+#include "base/base64.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/format_macros.h"
+#include "base/macros.h"
+#include "base/md5.h"
+#include "base/memory/ptr_util.h"
+#include "base/path_service.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "base/time/time.h"
+#include "net/base/escape.h"
+#include "net/base/url_util.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"
+
+namespace net {
+namespace test_server {
+
+// /click-redirect?url=URL&&echoheaders=HEADERS
+// 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();
+
+ std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
+ http_response->set_content_type("text/html");
+ http_response->AddCustomHeader("Cache-Control", "no-cache, no-store");
+
+ if (request_url.has_query()) {
+ std::vector<std::string> query_list = base::SplitString(
+ request_url.query(), "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
+ std::string pre_url("url=");
+ std::string url;
+ std::string pre_header("echoheaders=");
+ std::string header_query;
+ for (const auto& query : query_list) {
+ if (query.compare(0, pre_url.length(), pre_url) == 0) {
+ url = query.substr(pre_url.length());
+ }
+ if (query.compare(0, pre_header.length(), pre_header) == 0) {
+ header_query = query.substr(pre_header.length());
+ }
+ }
+
+ std::string content;
+ std::vector<std::string> headers = base::SplitString(
+ header_query, "&", 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);
+}
+
+std::vector<jlong> handlers = {reinterpret_cast<jlong>(HandleClickRedirect)};
+
+std::vector<jlong> getHandlers() {
+ return handlers;
+}
+
+} // namespace test_server
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698