| 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/test/cronet_test_util.h" | 21 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 22 #include "components/cronet/android/url_request_context_adapter.h" |
| 22 #include "jni/NativeTestServer_jni.h" | 23 #include "jni/NativeTestServer_jni.h" |
| 23 #include "net/base/host_port_pair.h" | 24 #include "net/base/host_port_pair.h" |
| 24 #include "net/base/url_util.h" | 25 #include "net/base/url_util.h" |
| 26 #include "net/dns/host_resolver_impl.h" |
| 27 #include "net/dns/mock_host_resolver.h" |
| 25 #include "net/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 29 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 27 #include "net/test/embedded_test_server/http_request.h" | 30 #include "net/test/embedded_test_server/http_request.h" |
| 28 #include "net/test/embedded_test_server/http_response.h" | 31 #include "net/test/embedded_test_server/http_response.h" |
| 29 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 30 | 33 |
| 31 namespace cronet { | 34 namespace cronet { |
| 32 | 35 |
| 33 namespace { | 36 namespace { |
| 34 | 37 |
| 35 const char kEchoBodyPath[] = "/echo_body"; | 38 const char kEchoBodyPath[] = "/echo_body"; |
| 36 const char kEchoHeaderPath[] = "/echo_header"; | 39 const char kEchoHeaderPath[] = "/echo_header"; |
| 37 const char kEchoAllHeadersPath[] = "/echo_all_headers"; | 40 const char kEchoAllHeadersPath[] = "/echo_all_headers"; |
| 38 const char kEchoMethodPath[] = "/echo_method"; | 41 const char kEchoMethodPath[] = "/echo_method"; |
| 39 const char kRedirectToEchoBodyPath[] = "/redirect_to_echo_body"; | 42 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"; |
| 40 // Path that advertises the dictionary passed in query params if client | 49 // Path that advertises the dictionary passed in query params if client |
| 41 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server | 50 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server |
| 42 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". | 51 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". |
| 43 const char kSdchPath[] = "/sdch/index"; | 52 const char kSdchPath[] = "/sdch/index"; |
| 44 // Path that returns encoded response if client has the right dictionary. | 53 // Path that returns encoded response if client has the right dictionary. |
| 45 const char kSdchTestPath[] = "/sdch/test"; | 54 const char kSdchTestPath[] = "/sdch/test"; |
| 46 // Path where dictionaries are stored. | 55 // Path where dictionaries are stored. |
| 47 const char kSdchDictPath[] = "/sdch/dict/"; | 56 const char kSdchDictPath[] = "/sdch/dict/"; |
| 48 | 57 |
| 49 net::EmbeddedTestServer* g_test_server = nullptr; | 58 net::EmbeddedTestServer* g_test_server = nullptr; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 new net::test_server::BasicHttpResponse()); | 162 new net::test_server::BasicHttpResponse()); |
| 154 response->set_content_type("text/plain"); | 163 response->set_content_type("text/plain"); |
| 155 response->set_content("Sdch is not used.\n"); | 164 response->set_content("Sdch is not used.\n"); |
| 156 return std::move(response); | 165 return std::move(response); |
| 157 } | 166 } |
| 158 | 167 |
| 159 // Unhandled requests result in the Embedded test server sending a 404. | 168 // Unhandled requests result in the Embedded test server sending a 404. |
| 160 return scoped_ptr<net::test_server::BasicHttpResponse>(); | 169 return scoped_ptr<net::test_server::BasicHttpResponse>(); |
| 161 } | 170 } |
| 162 | 171 |
| 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 |
| 163 } // namespace | 197 } // namespace |
| 164 | 198 |
| 165 jboolean StartNativeTestServer(JNIEnv* env, | 199 jboolean StartNativeTestServer(JNIEnv* env, |
| 166 const JavaParamRef<jclass>& jcaller, | 200 const JavaParamRef<jclass>& jcaller, |
| 167 const JavaParamRef<jstring>& jtest_files_root) { | 201 const JavaParamRef<jstring>& jtest_files_root) { |
| 168 // Shouldn't happen. | 202 // Shouldn't happen. |
| 169 if (g_test_server) | 203 if (g_test_server) |
| 170 return false; | 204 return false; |
| 171 g_test_server = new net::EmbeddedTestServer(); | 205 g_test_server = new net::EmbeddedTestServer(); |
| 172 g_test_server->RegisterRequestHandler( | 206 g_test_server->RegisterRequestHandler( |
| 173 base::Bind(&NativeTestServerRequestHandler)); | 207 base::Bind(&NativeTestServerRequestHandler)); |
| 174 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); | 208 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); |
| 175 base::FilePath test_files_root( | 209 base::FilePath test_files_root( |
| 176 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 210 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
| 177 | 211 |
| 178 // Add a third handler for paths that NativeTestServerRequestHandler does not | 212 // Add a third handler for paths that NativeTestServerRequestHandler does not |
| 179 // handle. | 213 // handle. |
| 180 g_test_server->ServeFilesFromDirectory(test_files_root); | 214 g_test_server->ServeFilesFromDirectory(test_files_root); |
| 181 return g_test_server->Start(); | 215 return g_test_server->Start(); |
| 182 } | 216 } |
| 183 | 217 |
| 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 |
| 184 void ShutdownNativeTestServer(JNIEnv* env, | 237 void ShutdownNativeTestServer(JNIEnv* env, |
| 185 const JavaParamRef<jclass>& jcaller) { | 238 const JavaParamRef<jclass>& jcaller) { |
| 186 if (!g_test_server) | 239 if (!g_test_server) |
| 187 return; | 240 return; |
| 188 delete g_test_server; | 241 delete g_test_server; |
| 189 g_test_server = NULL; | 242 g_test_server = NULL; |
| 190 } | 243 } |
| 191 | 244 |
| 192 ScopedJavaLocalRef<jstring> GetEchoBodyURL( | 245 ScopedJavaLocalRef<jstring> GetEchoBodyURL( |
| 193 JNIEnv* env, | 246 JNIEnv* env, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 #else | 320 #else |
| 268 return JNI_FALSE; | 321 return JNI_FALSE; |
| 269 #endif | 322 #endif |
| 270 } | 323 } |
| 271 | 324 |
| 272 bool RegisterNativeTestServer(JNIEnv* env) { | 325 bool RegisterNativeTestServer(JNIEnv* env) { |
| 273 return RegisterNativesImpl(env); | 326 return RegisterNativesImpl(env); |
| 274 } | 327 } |
| 275 | 328 |
| 276 } // namespace cronet | 329 } // namespace cronet |
| OLD | NEW |