| Index: components/cronet/android/test/native_test_server.cc
|
| diff --git a/components/cronet/android/test/native_test_server.cc b/components/cronet/android/test/native_test_server.cc
|
| index 97261645d361c60480eb00597df1b5e8b7c562b7..7fca703844f3ef0a7f95d7db9eeee7c6f4bf13ff 100644
|
| --- a/components/cronet/android/test/native_test_server.cc
|
| +++ b/components/cronet/android/test/native_test_server.cc
|
| @@ -4,14 +4,24 @@
|
|
|
| #include "native_test_server.h"
|
|
|
| +#include <string>
|
| +
|
| #include "base/android/jni_android.h"
|
| #include "base/android/jni_string.h"
|
| +#include "base/android/path_utils.h"
|
| #include "base/bind.h"
|
| #include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/macros.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/path_service.h"
|
| #include "base/strings/string_util.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "components/cronet/android/cronet_url_request_context_adapter.h"
|
| +#include "components/cronet/android/url_request_context_adapter.h"
|
| #include "jni/NativeTestServer_jni.h"
|
| +#include "net/dns/host_resolver_impl.h"
|
| +#include "net/dns/mock_host_resolver.h"
|
| #include "net/http/http_status_code.h"
|
| #include "net/test/embedded_test_server/embedded_test_server.h"
|
| #include "net/test/embedded_test_server/http_request.h"
|
| @@ -27,10 +37,50 @@ const char echo_header_path[] = "/echo_header";
|
| const char echo_all_headers_path[] = "/echo_all_headers";
|
| const char echo_method_path[] = "/echo_method";
|
| const char redirect_to_echo_body_path[] = "/redirect_to_echo_body";
|
| +const char fake_sdch_domain[] = "fake.sdch.domain";
|
| +// Path that advertises a dictionary if client supports Sdch encoding.
|
| +const char sdch_path[] = "/sdch";
|
| +// Path that returns encoded response if client has the right dictionary.
|
| +const char sdch_test_path[] = "/sdch/test";
|
|
|
| net::test_server::EmbeddedTestServer* g_test_server = nullptr;
|
|
|
| -scoped_ptr<net::test_server::HttpResponse> UploadServerRequestHandler(
|
| +class CustomHttpResponse : public net::test_server::HttpResponse {
|
| + public:
|
| + CustomHttpResponse(const std::string& headers, const std::string& contents)
|
| + : headers_(headers), contents_(contents) {}
|
| +
|
| + std::string ToResponseString() const override {
|
| + return headers_ + "\r\n" + contents_;
|
| + }
|
| +
|
| + void AddHeader(const std::string& key_value_pair) {
|
| + headers_.append(base::StringPrintf("%s\r\n", key_value_pair.c_str()));
|
| + }
|
| +
|
| + private:
|
| + std::string headers_;
|
| + std::string contents_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(CustomHttpResponse);
|
| +};
|
| +
|
| +scoped_ptr<CustomHttpResponse> ConstructResponseBasedOnFile(
|
| + const base::FilePath& file_path) {
|
| + std::string file_contents;
|
| + bool read_file = base::ReadFileToString(file_path, &file_contents);
|
| + DCHECK(read_file);
|
| + base::FilePath headers_path(
|
| + file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers")));
|
| + std::string headers_contents;
|
| + bool read_headers = base::ReadFileToString(headers_path, &headers_contents);
|
| + DCHECK(read_headers);
|
| + scoped_ptr<CustomHttpResponse> http_response(
|
| + new CustomHttpResponse(headers_contents, file_contents));
|
| + return http_response.Pass();
|
| +}
|
| +
|
| +scoped_ptr<net::test_server::HttpResponse> NativeTestServerRequestHandler(
|
| const net::test_server::HttpRequest& request) {
|
| DCHECK(g_test_server);
|
| scoped_ptr<net::test_server::BasicHttpResponse> response(
|
| @@ -77,6 +127,74 @@ scoped_ptr<net::test_server::HttpResponse> UploadServerRequestHandler(
|
| return scoped_ptr<net::test_server::BasicHttpResponse>();
|
| }
|
|
|
| +scoped_ptr<net::test_server::HttpResponse> SdchRequestHandler(
|
| + const net::test_server::HttpRequest& request) {
|
| + DCHECK(g_test_server);
|
| + base::FilePath dir_path;
|
| + bool get_data_dir = base::android::GetDataDirectory(&dir_path);
|
| + DCHECK(get_data_dir);
|
| + dir_path = dir_path.Append(FILE_PATH_LITERAL("test"));
|
| +
|
| + if (request.relative_url == sdch_path) {
|
| + base::FilePath file_path = dir_path.Append("sdch/index");
|
| + scoped_ptr<CustomHttpResponse> response =
|
| + ConstructResponseBasedOnFile(file_path).Pass();
|
| + auto it = request.headers.find("Accept-Encoding");
|
| + if (it != request.headers.end()) {
|
| + if (it->second.find("sdch") != std::string::npos)
|
| + response->AddHeader("Get-Dictionary: /sdch/dict/LeQxM80O");
|
| + }
|
| + return response.Pass();
|
| + }
|
| +
|
| + if (StartsWithASCII(request.relative_url, sdch_test_path, true)) {
|
| + auto it = request.headers.find("Avail-Dictionary");
|
| + if (it != request.headers.end()) {
|
| + if (it->second == "LeQxM80O") {
|
| + base::FilePath file_path = dir_path.Append("sdch/LeQxM80O_encoded");
|
| + return ConstructResponseBasedOnFile(file_path).Pass();
|
| + }
|
| + }
|
| + scoped_ptr<net::test_server::BasicHttpResponse> response(
|
| + new net::test_server::BasicHttpResponse());
|
| + response->set_content_type("text/plain");
|
| + response->set_content("Sdch is not used.\n");
|
| + return response.Pass();
|
| + }
|
| +
|
| + // Unhandled requests result in the Embedded test server sending a 404.
|
| + return scoped_ptr<net::test_server::BasicHttpResponse>();
|
| +}
|
| +
|
| +void RegisterHostResolverProcHelper(
|
| + net::URLRequestContext* url_request_context,
|
| + base::android::ScopedJavaGlobalRef<jobject>* callback) {
|
| + net::HostResolverImpl* resolver =
|
| + static_cast<net::HostResolverImpl*>(url_request_context->host_resolver());
|
| + scoped_refptr<net::RuleBasedHostResolverProc> proc =
|
| + new net::RuleBasedHostResolverProc(NULL);
|
| + proc->AddRule(fake_sdch_domain, "127.0.0.1");
|
| + resolver->set_proc_params_for_test(
|
| + net::HostResolverImpl::ProcTaskParams(proc.get(), 1u));
|
| + JNIEnv* env = base::android::AttachCurrentThread();
|
| + Java_NativeTestServer_onHostResolverProcRegistered(env, callback->obj());
|
| +}
|
| +
|
| +void RegisterHostResolverProcOnNetworkThread(
|
| + CronetURLRequestContextAdapter* context_adapter,
|
| + base::android::ScopedJavaGlobalRef<jobject>* callback) {
|
| + net::URLRequestContext* context = context_adapter->GetURLRequestContext();
|
| + RegisterHostResolverProcHelper(context, callback);
|
| +}
|
| +
|
| +// TODO(xunjieli): Delete this once legacy API is removed.
|
| +void RegisterHostResolverProcOnNetworkThreadLegacyAPI(
|
| + URLRequestContextAdapter* context_adapter,
|
| + base::android::ScopedJavaGlobalRef<jobject>* callback) {
|
| + net::URLRequestContext* context = context_adapter->GetURLRequestContext();
|
| + RegisterHostResolverProcHelper(context, callback);
|
| +}
|
| +
|
| } // namespace
|
|
|
| jboolean StartNativeTestServer(JNIEnv* env,
|
| @@ -87,15 +205,41 @@ jboolean StartNativeTestServer(JNIEnv* env,
|
| return false;
|
| g_test_server = new net::test_server::EmbeddedTestServer();
|
| g_test_server->RegisterRequestHandler(
|
| - base::Bind(&UploadServerRequestHandler));
|
| - // Add a second handler for paths that UploadServerRequestHandler does not
|
| - // handle.
|
| + base::Bind(&NativeTestServerRequestHandler));
|
| + g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler));
|
| base::FilePath test_files_root(
|
| base::android::ConvertJavaStringToUTF8(env, jtest_files_root));
|
| +
|
| + // Add a third handler for paths that NativeTestServerRequestHandler does not
|
| + // handle.
|
| g_test_server->ServeFilesFromDirectory(test_files_root);
|
| return g_test_server->InitializeAndWaitUntilReady();
|
| }
|
|
|
| +void RegisterHostResolverProc(JNIEnv* env,
|
| + jclass jcaller,
|
| + jlong jadapter,
|
| + jboolean jlegacy_api) {
|
| + base::android::ScopedJavaGlobalRef<jobject>* callback =
|
| + new base::android::ScopedJavaGlobalRef<jobject>();
|
| + callback->Reset(env, jcaller);
|
| + if (jlegacy_api == JNI_TRUE) {
|
| + URLRequestContextAdapter* context_adapter =
|
| + reinterpret_cast<URLRequestContextAdapter*>(jadapter);
|
| + context_adapter->PostTaskToNetworkThread(
|
| + FROM_HERE,
|
| + base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI,
|
| + base::Unretained(context_adapter), base::Owned(callback)));
|
| + } else {
|
| + CronetURLRequestContextAdapter* context_adapter =
|
| + reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter);
|
| + context_adapter->PostTaskToNetworkThread(
|
| + FROM_HERE,
|
| + base::Bind(&RegisterHostResolverProcOnNetworkThread,
|
| + base::Unretained(context_adapter), base::Owned(callback)));
|
| + }
|
| +}
|
| +
|
| void ShutdownNativeTestServer(JNIEnv* env, jclass jcaller) {
|
| if (!g_test_server)
|
| return;
|
| @@ -144,6 +288,13 @@ jstring GetFileURL(JNIEnv* env, jclass jcaller, jstring jfile_path) {
|
| return base::android::ConvertUTF8ToJavaString(env, url.spec()).Release();
|
| }
|
|
|
| +jstring GetSdchURL(JNIEnv* env, jclass jcaller) {
|
| + DCHECK(g_test_server);
|
| + std::string url(base::StringPrintf("http://%s:%d", fake_sdch_domain,
|
| + g_test_server->port()));
|
| + return base::android::ConvertUTF8ToJavaString(env, url).Release();
|
| +}
|
| +
|
| bool RegisterNativeTestServer(JNIEnv* env) {
|
| return RegisterNativesImpl(env);
|
| }
|
|
|