| 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 "components/cronet/android/test/native_test_server.h" | 5 #include "components/cronet/android/test/native_test_server.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/android/path_utils.h" | 12 #include "base/android/path_utils.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "components/cronet/android/cronet_url_request_context_adapter.h" | 21 #include "components/cronet/android/test/cronet_test_util.h" |
| 22 #include "components/cronet/android/url_request_context_adapter.h" | |
| 23 #include "jni/NativeTestServer_jni.h" | 22 #include "jni/NativeTestServer_jni.h" |
| 24 #include "net/base/host_port_pair.h" | 23 #include "net/base/host_port_pair.h" |
| 25 #include "net/base/url_util.h" | 24 #include "net/base/url_util.h" |
| 26 #include "net/dns/host_resolver_impl.h" | |
| 27 #include "net/dns/mock_host_resolver.h" | |
| 28 #include "net/http/http_status_code.h" | 25 #include "net/http/http_status_code.h" |
| 29 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 30 #include "net/test/embedded_test_server/http_request.h" | 27 #include "net/test/embedded_test_server/http_request.h" |
| 31 #include "net/test/embedded_test_server/http_response.h" | 28 #include "net/test/embedded_test_server/http_response.h" |
| 32 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 33 | 30 |
| 34 namespace cronet { | 31 namespace cronet { |
| 35 | 32 |
| 36 namespace { | 33 namespace { |
| 37 | 34 |
| 38 const char kEchoBodyPath[] = "/echo_body"; | 35 const char kEchoBodyPath[] = "/echo_body"; |
| 39 const char kEchoHeaderPath[] = "/echo_header"; | 36 const char kEchoHeaderPath[] = "/echo_header"; |
| 40 const char kEchoAllHeadersPath[] = "/echo_all_headers"; | 37 const char kEchoAllHeadersPath[] = "/echo_all_headers"; |
| 41 const char kEchoMethodPath[] = "/echo_method"; | 38 const char kEchoMethodPath[] = "/echo_method"; |
| 42 const char kRedirectToEchoBodyPath[] = "/redirect_to_echo_body"; | 39 const char kRedirectToEchoBodyPath[] = "/redirect_to_echo_body"; |
| 43 const char kFakeSdchDomain[] = "fake.sdch.domain"; | |
| 44 // Host used in QuicTestServer. This must match the certificate used | |
| 45 // (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and | |
| 46 // the file served ( | |
| 47 // components/cronet/android/test/assets/test/quic_data/simple.txt). | |
| 48 const char kFakeQuicDomain[] = "test.example.com"; | |
| 49 // Path that advertises the dictionary passed in query params if client | 40 // Path that advertises the dictionary passed in query params if client |
| 50 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server | 41 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server |
| 51 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". | 42 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". |
| 52 const char kSdchPath[] = "/sdch/index"; | 43 const char kSdchPath[] = "/sdch/index"; |
| 53 // Path that returns encoded response if client has the right dictionary. | 44 // Path that returns encoded response if client has the right dictionary. |
| 54 const char kSdchTestPath[] = "/sdch/test"; | 45 const char kSdchTestPath[] = "/sdch/test"; |
| 55 // Path where dictionaries are stored. | 46 // Path where dictionaries are stored. |
| 56 const char kSdchDictPath[] = "/sdch/dict/"; | 47 const char kSdchDictPath[] = "/sdch/dict/"; |
| 57 | 48 |
| 58 net::EmbeddedTestServer* g_test_server = nullptr; | 49 net::EmbeddedTestServer* g_test_server = nullptr; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 new net::test_server::BasicHttpResponse()); | 153 new net::test_server::BasicHttpResponse()); |
| 163 response->set_content_type("text/plain"); | 154 response->set_content_type("text/plain"); |
| 164 response->set_content("Sdch is not used.\n"); | 155 response->set_content("Sdch is not used.\n"); |
| 165 return std::move(response); | 156 return std::move(response); |
| 166 } | 157 } |
| 167 | 158 |
| 168 // Unhandled requests result in the Embedded test server sending a 404. | 159 // Unhandled requests result in the Embedded test server sending a 404. |
| 169 return scoped_ptr<net::test_server::BasicHttpResponse>(); | 160 return scoped_ptr<net::test_server::BasicHttpResponse>(); |
| 170 } | 161 } |
| 171 | 162 |
| 172 void RegisterHostResolverProcHelper( | |
| 173 net::URLRequestContext* url_request_context) { | |
| 174 net::HostResolverImpl* resolver = | |
| 175 static_cast<net::HostResolverImpl*>(url_request_context->host_resolver()); | |
| 176 scoped_refptr<net::RuleBasedHostResolverProc> proc = | |
| 177 new net::RuleBasedHostResolverProc(NULL); | |
| 178 proc->AddRule(kFakeSdchDomain, "127.0.0.1"); | |
| 179 proc->AddRule(kFakeQuicDomain, "127.0.0.1"); | |
| 180 resolver->set_proc_params_for_test( | |
| 181 net::HostResolverImpl::ProcTaskParams(proc.get(), 1u)); | |
| 182 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 183 Java_NativeTestServer_onHostResolverProcRegistered(env); | |
| 184 } | |
| 185 | |
| 186 void RegisterHostResolverProcOnNetworkThread( | |
| 187 CronetURLRequestContextAdapter* context_adapter) { | |
| 188 RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext()); | |
| 189 } | |
| 190 | |
| 191 // TODO(xunjieli): Delete this once legacy API is removed. | |
| 192 void RegisterHostResolverProcOnNetworkThreadLegacyAPI( | |
| 193 URLRequestContextAdapter* context_adapter) { | |
| 194 RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext()); | |
| 195 } | |
| 196 | |
| 197 } // namespace | 163 } // namespace |
| 198 | 164 |
| 199 jboolean StartNativeTestServer(JNIEnv* env, | 165 jboolean StartNativeTestServer(JNIEnv* env, |
| 200 const JavaParamRef<jclass>& jcaller, | 166 const JavaParamRef<jclass>& jcaller, |
| 201 const JavaParamRef<jstring>& jtest_files_root) { | 167 const JavaParamRef<jstring>& jtest_files_root) { |
| 202 // Shouldn't happen. | 168 // Shouldn't happen. |
| 203 if (g_test_server) | 169 if (g_test_server) |
| 204 return false; | 170 return false; |
| 205 g_test_server = new net::EmbeddedTestServer(); | 171 g_test_server = new net::EmbeddedTestServer(); |
| 206 g_test_server->RegisterRequestHandler( | 172 g_test_server->RegisterRequestHandler( |
| 207 base::Bind(&NativeTestServerRequestHandler)); | 173 base::Bind(&NativeTestServerRequestHandler)); |
| 208 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); | 174 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); |
| 209 base::FilePath test_files_root( | 175 base::FilePath test_files_root( |
| 210 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 176 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
| 211 | 177 |
| 212 // Add a third handler for paths that NativeTestServerRequestHandler does not | 178 // Add a third handler for paths that NativeTestServerRequestHandler does not |
| 213 // handle. | 179 // handle. |
| 214 g_test_server->ServeFilesFromDirectory(test_files_root); | 180 g_test_server->ServeFilesFromDirectory(test_files_root); |
| 215 return g_test_server->Start(); | 181 return g_test_server->Start(); |
| 216 } | 182 } |
| 217 | 183 |
| 218 void RegisterHostResolverProc(JNIEnv* env, | |
| 219 const JavaParamRef<jclass>& jcaller, | |
| 220 jlong jadapter, | |
| 221 jboolean jlegacy_api) { | |
| 222 if (jlegacy_api == JNI_TRUE) { | |
| 223 URLRequestContextAdapter* context_adapter = | |
| 224 reinterpret_cast<URLRequestContextAdapter*>(jadapter); | |
| 225 context_adapter->PostTaskToNetworkThread( | |
| 226 FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI, | |
| 227 base::Unretained(context_adapter))); | |
| 228 } else { | |
| 229 CronetURLRequestContextAdapter* context_adapter = | |
| 230 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | |
| 231 context_adapter->PostTaskToNetworkThread( | |
| 232 FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThread, | |
| 233 base::Unretained(context_adapter))); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 void ShutdownNativeTestServer(JNIEnv* env, | 184 void ShutdownNativeTestServer(JNIEnv* env, |
| 238 const JavaParamRef<jclass>& jcaller) { | 185 const JavaParamRef<jclass>& jcaller) { |
| 239 if (!g_test_server) | 186 if (!g_test_server) |
| 240 return; | 187 return; |
| 241 delete g_test_server; | 188 delete g_test_server; |
| 242 g_test_server = NULL; | 189 g_test_server = NULL; |
| 243 } | 190 } |
| 244 | 191 |
| 245 ScopedJavaLocalRef<jstring> GetEchoBodyURL( | 192 ScopedJavaLocalRef<jstring> GetEchoBodyURL( |
| 246 JNIEnv* env, | 193 JNIEnv* env, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 #else | 267 #else |
| 321 return JNI_FALSE; | 268 return JNI_FALSE; |
| 322 #endif | 269 #endif |
| 323 } | 270 } |
| 324 | 271 |
| 325 bool RegisterNativeTestServer(JNIEnv* env) { | 272 bool RegisterNativeTestServer(JNIEnv* env) { |
| 326 return RegisterNativesImpl(env); | 273 return RegisterNativesImpl(env); |
| 327 } | 274 } |
| 328 | 275 |
| 329 } // namespace cronet | 276 } // namespace cronet |
| OLD | NEW |