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