Chromium Code Reviews| 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..a1e118257576e98f901d6869ace93b757d334f61 100644 |
| --- a/components/cronet/android/test/native_test_server.cc |
| +++ b/components/cronet/android/test/native_test_server.cc |
| @@ -4,14 +4,25 @@ |
| #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_split.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 +38,54 @@ 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 the dictionaries passed in query params if client |
| +// supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server |
| +// responds with "Get-Dictionary: /sdch/dict/LeQxM80O". |
| +const char sdch_path[] = "/sdch/index"; |
| +// Path that returns encoded response if client has the right dictionary. |
| +const char sdch_test_path[] = "/sdch/test"; |
| +// Path where dictionaries are stored. |
| +const char sdch_dict_path[] = "/sdch/dict/"; |
| 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 +132,85 @@ 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 (StartsWithASCII(request.relative_url, sdch_path, true)) { |
| + base::FilePath file_path = dir_path.Append("sdch/index"); |
| + scoped_ptr<CustomHttpResponse> response = |
| + ConstructResponseBasedOnFile(file_path).Pass(); |
| + // Check for query params to see which dictionaries to advertise. |
|
mef
2015/04/28 20:37:11
Need comment on what query params are used to defi
xunjieli
2015/04/29 02:05:09
Done.
|
| + GURL url = g_test_server->GetURL(request.relative_url); |
| + base::StringPairs pairs; |
| + if (!base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &pairs)) { |
|
mef
2015/04/28 20:37:11
Suggest: using GetValueForKeyInQuery()
xunjieli
2015/04/29 02:05:09
Done. Was thinking we should support multiple dict
|
| + DCHECK(false) << "parsing query params failed"; |
| + } |
| + auto it = request.headers.find("Accept-Encoding"); |
|
mef
2015/04/28 20:37:10
'it' is a bit generic, suggest calling it 'accept_
xunjieli
2015/04/29 02:05:09
Done.
|
| + if (it != request.headers.end()) { |
| + if (it->second.find("sdch") != std::string::npos) { |
| + std::string get_dictionary_header = ""; |
| + for (base::StringPairs::const_iterator it = pairs.begin(); |
|
mef
2015/04/28 20:37:11
This 'it' masks upper level 'it', which makes it c
xunjieli
2015/04/29 02:05:09
Done.
|
| + it != pairs.end(); ++it) { |
|
mef
2015/04/28 20:37:10
Could we use range-based for loop here?
Why do we
xunjieli
2015/04/29 02:05:09
Done. Was thinking of supporting multiple dictiona
|
| + get_dictionary_header += sdch_dict_path + it->second + ", "; |
| + } |
| + if (!get_dictionary_header.empty()) { |
| + // Remove trailing comma and space. |
| + get_dictionary_header = |
| + get_dictionary_header.substr(0, get_dictionary_header.size() - 2); |
| + response->AddHeader("Get-Dictionary: " + get_dictionary_header); |
| + } |
| + } |
| + } |
| + return response.Pass(); |
| + } |
| + |
| + if (StartsWithASCII(request.relative_url, sdch_test_path, true)) { |
| + auto it = request.headers.find("Avail-Dictionary"); |
|
mef
2015/04/28 20:37:10
more descriptive name than 'it'?
xunjieli
2015/04/29 02:05:09
Done.
|
| + if (it != request.headers.end()) { |
| + base::FilePath file_path = |
| + dir_path.Append("sdch/" + it->second + "_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"); |
|
mef
2015/04/28 20:37:10
maybe construct response based on another file wit
xunjieli
2015/04/29 02:05:09
Hmm.. but we just want to make sure here that sdch
mef
2015/04/29 15:37:29
sg, thanks!
|
| + 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) { |
| + 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); |
| +} |
| + |
| +void RegisterHostResolverProcOnNetworkThread( |
| + CronetURLRequestContextAdapter* context_adapter) { |
| + RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext()); |
| +} |
| + |
| +// TODO(xunjieli): Delete this once legacy API is removed. |
| +void RegisterHostResolverProcOnNetworkThreadLegacyAPI( |
| + URLRequestContextAdapter* context_adapter) { |
| + RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext()); |
| +} |
| + |
| } // namespace |
| jboolean StartNativeTestServer(JNIEnv* env, |
| @@ -87,15 +221,36 @@ 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) { |
| + if (jlegacy_api == JNI_TRUE) { |
| + URLRequestContextAdapter* context_adapter = |
| + reinterpret_cast<URLRequestContextAdapter*>(jadapter); |
| + context_adapter->PostTaskToNetworkThread( |
| + FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI, |
| + base::Unretained(context_adapter))); |
| + } else { |
| + CronetURLRequestContextAdapter* context_adapter = |
| + reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
| + context_adapter->PostTaskToNetworkThread( |
| + FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThread, |
| + base::Unretained(context_adapter))); |
| + } |
| +} |
| + |
| void ShutdownNativeTestServer(JNIEnv* env, jclass jcaller) { |
| if (!g_test_server) |
| return; |
| @@ -144,6 +299,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); |
| } |