| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "native_test_server.h" | 5 #include "native_test_server.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/android/path_utils.h" |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" |
| 15 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 13 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" |
| 20 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 21 #include "components/cronet/android/url_request_context_adapter.h" |
| 14 #include "jni/NativeTestServer_jni.h" | 22 #include "jni/NativeTestServer_jni.h" |
| 23 #include "net/dns/host_resolver_impl.h" |
| 24 #include "net/dns/mock_host_resolver.h" |
| 15 #include "net/http/http_status_code.h" | 25 #include "net/http/http_status_code.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 17 #include "net/test/embedded_test_server/http_request.h" | 27 #include "net/test/embedded_test_server/http_request.h" |
| 18 #include "net/test/embedded_test_server/http_response.h" | 28 #include "net/test/embedded_test_server/http_response.h" |
| 19 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 20 | 30 |
| 21 namespace cronet { | 31 namespace cronet { |
| 22 | 32 |
| 23 namespace { | 33 namespace { |
| 24 | 34 |
| 25 const char echo_body_path[] = "/echo_body"; | 35 const char echo_body_path[] = "/echo_body"; |
| 26 const char echo_header_path[] = "/echo_header"; | 36 const char echo_header_path[] = "/echo_header"; |
| 27 const char echo_all_headers_path[] = "/echo_all_headers"; | 37 const char echo_all_headers_path[] = "/echo_all_headers"; |
| 28 const char echo_method_path[] = "/echo_method"; | 38 const char echo_method_path[] = "/echo_method"; |
| 29 const char redirect_to_echo_body_path[] = "/redirect_to_echo_body"; | 39 const char redirect_to_echo_body_path[] = "/redirect_to_echo_body"; |
| 40 const char fake_sdch_domain[] = "fake.sdch.domain"; |
| 41 // Path that advertises a dictionary if client supports Sdch encoding. |
| 42 const char sdch_path[] = "/sdch"; |
| 43 // Path that returns encoded response if client has the right dictionary. |
| 44 const char sdch_test_path[] = "/sdch/test"; |
| 30 | 45 |
| 31 net::test_server::EmbeddedTestServer* g_test_server = nullptr; | 46 net::test_server::EmbeddedTestServer* g_test_server = nullptr; |
| 32 | 47 |
| 33 scoped_ptr<net::test_server::HttpResponse> UploadServerRequestHandler( | 48 class CustomHttpResponse : public net::test_server::HttpResponse { |
| 49 public: |
| 50 CustomHttpResponse(const std::string& headers, const std::string& contents) |
| 51 : headers_(headers), contents_(contents) {} |
| 52 |
| 53 std::string ToResponseString() const override { |
| 54 return headers_ + "\r\n" + contents_; |
| 55 } |
| 56 |
| 57 void AddHeader(const std::string& key_value_pair) { |
| 58 headers_.append(base::StringPrintf("%s\r\n", key_value_pair.c_str())); |
| 59 } |
| 60 |
| 61 private: |
| 62 std::string headers_; |
| 63 std::string contents_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(CustomHttpResponse); |
| 66 }; |
| 67 |
| 68 scoped_ptr<CustomHttpResponse> ConstructResponseBasedOnFile( |
| 69 const base::FilePath& file_path) { |
| 70 std::string file_contents; |
| 71 bool read_file = base::ReadFileToString(file_path, &file_contents); |
| 72 DCHECK(read_file); |
| 73 base::FilePath headers_path( |
| 74 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
| 75 std::string headers_contents; |
| 76 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); |
| 77 DCHECK(read_headers); |
| 78 scoped_ptr<CustomHttpResponse> http_response( |
| 79 new CustomHttpResponse(headers_contents, file_contents)); |
| 80 return http_response.Pass(); |
| 81 } |
| 82 |
| 83 scoped_ptr<net::test_server::HttpResponse> NativeTestServerRequestHandler( |
| 34 const net::test_server::HttpRequest& request) { | 84 const net::test_server::HttpRequest& request) { |
| 35 DCHECK(g_test_server); | 85 DCHECK(g_test_server); |
| 36 scoped_ptr<net::test_server::BasicHttpResponse> response( | 86 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 37 new net::test_server::BasicHttpResponse()); | 87 new net::test_server::BasicHttpResponse()); |
| 38 response->set_content_type("text/plain"); | 88 response->set_content_type("text/plain"); |
| 39 | 89 |
| 40 if (request.relative_url == echo_body_path) { | 90 if (request.relative_url == echo_body_path) { |
| 41 if (request.has_content) { | 91 if (request.has_content) { |
| 42 response->set_content(request.content); | 92 response->set_content(request.content); |
| 43 } else { | 93 } else { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 70 if (request.relative_url == redirect_to_echo_body_path) { | 120 if (request.relative_url == redirect_to_echo_body_path) { |
| 71 response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 121 response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
| 72 response->AddCustomHeader("Location", echo_body_path); | 122 response->AddCustomHeader("Location", echo_body_path); |
| 73 return response.Pass(); | 123 return response.Pass(); |
| 74 } | 124 } |
| 75 | 125 |
| 76 // Unhandled requests result in the Embedded test server sending a 404. | 126 // Unhandled requests result in the Embedded test server sending a 404. |
| 77 return scoped_ptr<net::test_server::BasicHttpResponse>(); | 127 return scoped_ptr<net::test_server::BasicHttpResponse>(); |
| 78 } | 128 } |
| 79 | 129 |
| 130 scoped_ptr<net::test_server::HttpResponse> SdchRequestHandler( |
| 131 const net::test_server::HttpRequest& request) { |
| 132 DCHECK(g_test_server); |
| 133 base::FilePath dir_path; |
| 134 bool get_data_dir = base::android::GetDataDirectory(&dir_path); |
| 135 DCHECK(get_data_dir); |
| 136 dir_path = dir_path.Append(FILE_PATH_LITERAL("test")); |
| 137 |
| 138 if (request.relative_url == sdch_path) { |
| 139 base::FilePath file_path = dir_path.Append("sdch/index"); |
| 140 scoped_ptr<CustomHttpResponse> response = |
| 141 ConstructResponseBasedOnFile(file_path).Pass(); |
| 142 auto it = request.headers.find("Accept-Encoding"); |
| 143 if (it != request.headers.end()) { |
| 144 if (it->second.find("sdch") != std::string::npos) |
| 145 response->AddHeader("Get-Dictionary: /sdch/dict/LeQxM80O"); |
| 146 } |
| 147 return response.Pass(); |
| 148 } |
| 149 |
| 150 if (StartsWithASCII(request.relative_url, sdch_test_path, true)) { |
| 151 auto it = request.headers.find("Avail-Dictionary"); |
| 152 if (it != request.headers.end()) { |
| 153 if (it->second == "LeQxM80O") { |
| 154 base::FilePath file_path = dir_path.Append("sdch/LeQxM80O_encoded"); |
| 155 return ConstructResponseBasedOnFile(file_path).Pass(); |
| 156 } |
| 157 } |
| 158 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 159 new net::test_server::BasicHttpResponse()); |
| 160 response->set_content_type("text/plain"); |
| 161 response->set_content("Sdch is not used.\n"); |
| 162 return response.Pass(); |
| 163 } |
| 164 |
| 165 // Unhandled requests result in the Embedded test server sending a 404. |
| 166 return scoped_ptr<net::test_server::BasicHttpResponse>(); |
| 167 } |
| 168 |
| 169 void RegisterHostResolverProcHelper( |
| 170 net::URLRequestContext* url_request_context, |
| 171 base::android::ScopedJavaGlobalRef<jobject>* callback) { |
| 172 net::HostResolverImpl* resolver = |
| 173 static_cast<net::HostResolverImpl*>(url_request_context->host_resolver()); |
| 174 scoped_refptr<net::RuleBasedHostResolverProc> proc = |
| 175 new net::RuleBasedHostResolverProc(NULL); |
| 176 proc->AddRule(fake_sdch_domain, "127.0.0.1"); |
| 177 resolver->set_proc_params_for_test( |
| 178 net::HostResolverImpl::ProcTaskParams(proc.get(), 1u)); |
| 179 JNIEnv* env = base::android::AttachCurrentThread(); |
| 180 Java_NativeTestServer_onHostResolverProcRegistered(env, callback->obj()); |
| 181 } |
| 182 |
| 183 void RegisterHostResolverProcOnNetworkThread( |
| 184 CronetURLRequestContextAdapter* context_adapter, |
| 185 base::android::ScopedJavaGlobalRef<jobject>* callback) { |
| 186 net::URLRequestContext* context = context_adapter->GetURLRequestContext(); |
| 187 RegisterHostResolverProcHelper(context, callback); |
| 188 } |
| 189 |
| 190 // TODO(xunjieli): Delete this once legacy API is removed. |
| 191 void RegisterHostResolverProcOnNetworkThreadLegacyAPI( |
| 192 URLRequestContextAdapter* context_adapter, |
| 193 base::android::ScopedJavaGlobalRef<jobject>* callback) { |
| 194 net::URLRequestContext* context = context_adapter->GetURLRequestContext(); |
| 195 RegisterHostResolverProcHelper(context, callback); |
| 196 } |
| 197 |
| 80 } // namespace | 198 } // namespace |
| 81 | 199 |
| 82 jboolean StartNativeTestServer(JNIEnv* env, | 200 jboolean StartNativeTestServer(JNIEnv* env, |
| 83 jclass jcaller, | 201 jclass jcaller, |
| 84 jstring jtest_files_root) { | 202 jstring jtest_files_root) { |
| 85 // Shouldn't happen. | 203 // Shouldn't happen. |
| 86 if (g_test_server) | 204 if (g_test_server) |
| 87 return false; | 205 return false; |
| 88 g_test_server = new net::test_server::EmbeddedTestServer(); | 206 g_test_server = new net::test_server::EmbeddedTestServer(); |
| 89 g_test_server->RegisterRequestHandler( | 207 g_test_server->RegisterRequestHandler( |
| 90 base::Bind(&UploadServerRequestHandler)); | 208 base::Bind(&NativeTestServerRequestHandler)); |
| 91 // Add a second handler for paths that UploadServerRequestHandler does not | 209 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); |
| 92 // handle. | |
| 93 base::FilePath test_files_root( | 210 base::FilePath test_files_root( |
| 94 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 211 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
| 212 |
| 213 // Add a third handler for paths that NativeTestServerRequestHandler does not |
| 214 // handle. |
| 95 g_test_server->ServeFilesFromDirectory(test_files_root); | 215 g_test_server->ServeFilesFromDirectory(test_files_root); |
| 96 return g_test_server->InitializeAndWaitUntilReady(); | 216 return g_test_server->InitializeAndWaitUntilReady(); |
| 97 } | 217 } |
| 98 | 218 |
| 219 void RegisterHostResolverProc(JNIEnv* env, |
| 220 jclass jcaller, |
| 221 jlong jadapter, |
| 222 jboolean jlegacy_api) { |
| 223 base::android::ScopedJavaGlobalRef<jobject>* callback = |
| 224 new base::android::ScopedJavaGlobalRef<jobject>(); |
| 225 callback->Reset(env, jcaller); |
| 226 if (jlegacy_api == JNI_TRUE) { |
| 227 URLRequestContextAdapter* context_adapter = |
| 228 reinterpret_cast<URLRequestContextAdapter*>(jadapter); |
| 229 context_adapter->PostTaskToNetworkThread( |
| 230 FROM_HERE, |
| 231 base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI, |
| 232 base::Unretained(context_adapter), base::Owned(callback))); |
| 233 } else { |
| 234 CronetURLRequestContextAdapter* context_adapter = |
| 235 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
| 236 context_adapter->PostTaskToNetworkThread( |
| 237 FROM_HERE, |
| 238 base::Bind(&RegisterHostResolverProcOnNetworkThread, |
| 239 base::Unretained(context_adapter), base::Owned(callback))); |
| 240 } |
| 241 } |
| 242 |
| 99 void ShutdownNativeTestServer(JNIEnv* env, jclass jcaller) { | 243 void ShutdownNativeTestServer(JNIEnv* env, jclass jcaller) { |
| 100 if (!g_test_server) | 244 if (!g_test_server) |
| 101 return; | 245 return; |
| 102 delete g_test_server; | 246 delete g_test_server; |
| 103 g_test_server = NULL; | 247 g_test_server = NULL; |
| 104 } | 248 } |
| 105 | 249 |
| 106 jstring GetEchoBodyURL(JNIEnv* env, jclass jcaller) { | 250 jstring GetEchoBodyURL(JNIEnv* env, jclass jcaller) { |
| 107 DCHECK(g_test_server); | 251 DCHECK(g_test_server); |
| 108 GURL url = g_test_server->GetURL(echo_body_path); | 252 GURL url = g_test_server->GetURL(echo_body_path); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 137 return base::android::ConvertUTF8ToJavaString(env, url.spec()).Release(); | 281 return base::android::ConvertUTF8ToJavaString(env, url.spec()).Release(); |
| 138 } | 282 } |
| 139 | 283 |
| 140 jstring GetFileURL(JNIEnv* env, jclass jcaller, jstring jfile_path) { | 284 jstring GetFileURL(JNIEnv* env, jclass jcaller, jstring jfile_path) { |
| 141 DCHECK(g_test_server); | 285 DCHECK(g_test_server); |
| 142 std::string file = base::android::ConvertJavaStringToUTF8(env, jfile_path); | 286 std::string file = base::android::ConvertJavaStringToUTF8(env, jfile_path); |
| 143 GURL url = g_test_server->GetURL(file); | 287 GURL url = g_test_server->GetURL(file); |
| 144 return base::android::ConvertUTF8ToJavaString(env, url.spec()).Release(); | 288 return base::android::ConvertUTF8ToJavaString(env, url.spec()).Release(); |
| 145 } | 289 } |
| 146 | 290 |
| 291 jstring GetSdchURL(JNIEnv* env, jclass jcaller) { |
| 292 DCHECK(g_test_server); |
| 293 std::string url(base::StringPrintf("http://%s:%d", fake_sdch_domain, |
| 294 g_test_server->port())); |
| 295 return base::android::ConvertUTF8ToJavaString(env, url).Release(); |
| 296 } |
| 297 |
| 147 bool RegisterNativeTestServer(JNIEnv* env) { | 298 bool RegisterNativeTestServer(JNIEnv* env) { |
| 148 return RegisterNativesImpl(env); | 299 return RegisterNativesImpl(env); |
| 149 } | 300 } |
| 150 | 301 |
| 151 } // namespace cronet | 302 } // namespace cronet |
| OLD | NEW |