| 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 "components/cronet/android/test/native_test_server.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 11 #include "base/android/path_utils.h" | 12 #include "base/android/path_utils.h" |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 std::string file_contents; | 62 std::string file_contents; |
| 62 bool read_file = base::ReadFileToString(file_path, &file_contents); | 63 bool read_file = base::ReadFileToString(file_path, &file_contents); |
| 63 DCHECK(read_file); | 64 DCHECK(read_file); |
| 64 base::FilePath headers_path( | 65 base::FilePath headers_path( |
| 65 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 66 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
| 66 std::string headers_contents; | 67 std::string headers_contents; |
| 67 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); | 68 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); |
| 68 DCHECK(read_headers); | 69 DCHECK(read_headers); |
| 69 scoped_ptr<net::test_server::RawHttpResponse> http_response( | 70 scoped_ptr<net::test_server::RawHttpResponse> http_response( |
| 70 new net::test_server::RawHttpResponse(headers_contents, file_contents)); | 71 new net::test_server::RawHttpResponse(headers_contents, file_contents)); |
| 71 return http_response.Pass(); | 72 return http_response; |
| 72 } | 73 } |
| 73 | 74 |
| 74 scoped_ptr<net::test_server::HttpResponse> NativeTestServerRequestHandler( | 75 scoped_ptr<net::test_server::HttpResponse> NativeTestServerRequestHandler( |
| 75 const net::test_server::HttpRequest& request) { | 76 const net::test_server::HttpRequest& request) { |
| 76 DCHECK(g_test_server); | 77 DCHECK(g_test_server); |
| 77 scoped_ptr<net::test_server::BasicHttpResponse> response( | 78 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 78 new net::test_server::BasicHttpResponse()); | 79 new net::test_server::BasicHttpResponse()); |
| 79 response->set_content_type("text/plain"); | 80 response->set_content_type("text/plain"); |
| 80 | 81 |
| 81 if (request.relative_url == kEchoBodyPath) { | 82 if (request.relative_url == kEchoBodyPath) { |
| 82 if (request.has_content) { | 83 if (request.has_content) { |
| 83 response->set_content(request.content); | 84 response->set_content(request.content); |
| 84 } else { | 85 } else { |
| 85 response->set_content("Request has no body. :("); | 86 response->set_content("Request has no body. :("); |
| 86 } | 87 } |
| 87 return response.Pass(); | 88 return std::move(response); |
| 88 } | 89 } |
| 89 | 90 |
| 90 if (base::StartsWith(request.relative_url, kEchoHeaderPath, | 91 if (base::StartsWith(request.relative_url, kEchoHeaderPath, |
| 91 base::CompareCase::SENSITIVE)) { | 92 base::CompareCase::SENSITIVE)) { |
| 92 GURL url = g_test_server->GetURL(request.relative_url); | 93 GURL url = g_test_server->GetURL(request.relative_url); |
| 93 auto it = request.headers.find(url.query()); | 94 auto it = request.headers.find(url.query()); |
| 94 if (it != request.headers.end()) { | 95 if (it != request.headers.end()) { |
| 95 response->set_content(it->second); | 96 response->set_content(it->second); |
| 96 } else { | 97 } else { |
| 97 response->set_content("Header not found. :("); | 98 response->set_content("Header not found. :("); |
| 98 } | 99 } |
| 99 return response.Pass(); | 100 return std::move(response); |
| 100 } | 101 } |
| 101 | 102 |
| 102 if (request.relative_url == kEchoAllHeadersPath) { | 103 if (request.relative_url == kEchoAllHeadersPath) { |
| 103 response->set_content(request.all_headers); | 104 response->set_content(request.all_headers); |
| 104 return response.Pass(); | 105 return std::move(response); |
| 105 } | 106 } |
| 106 | 107 |
| 107 if (request.relative_url == kEchoMethodPath) { | 108 if (request.relative_url == kEchoMethodPath) { |
| 108 response->set_content(request.method_string); | 109 response->set_content(request.method_string); |
| 109 return response.Pass(); | 110 return std::move(response); |
| 110 } | 111 } |
| 111 | 112 |
| 112 if (request.relative_url == kRedirectToEchoBodyPath) { | 113 if (request.relative_url == kRedirectToEchoBodyPath) { |
| 113 response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 114 response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
| 114 response->AddCustomHeader("Location", kEchoBodyPath); | 115 response->AddCustomHeader("Location", kEchoBodyPath); |
| 115 return response.Pass(); | 116 return std::move(response); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // Unhandled requests result in the Embedded test server sending a 404. | 119 // Unhandled requests result in the Embedded test server sending a 404. |
| 119 return scoped_ptr<net::test_server::BasicHttpResponse>(); | 120 return scoped_ptr<net::test_server::BasicHttpResponse>(); |
| 120 } | 121 } |
| 121 | 122 |
| 122 scoped_ptr<net::test_server::HttpResponse> SdchRequestHandler( | 123 scoped_ptr<net::test_server::HttpResponse> SdchRequestHandler( |
| 123 const net::test_server::HttpRequest& request) { | 124 const net::test_server::HttpRequest& request) { |
| 124 DCHECK(g_test_server); | 125 DCHECK(g_test_server); |
| 125 base::FilePath dir_path; | 126 base::FilePath dir_path; |
| 126 bool get_data_dir = base::android::GetDataDirectory(&dir_path); | 127 bool get_data_dir = base::android::GetDataDirectory(&dir_path); |
| 127 DCHECK(get_data_dir); | 128 DCHECK(get_data_dir); |
| 128 dir_path = dir_path.Append(FILE_PATH_LITERAL("test")); | 129 dir_path = dir_path.Append(FILE_PATH_LITERAL("test")); |
| 129 | 130 |
| 130 if (base::StartsWith(request.relative_url, kSdchPath, | 131 if (base::StartsWith(request.relative_url, kSdchPath, |
| 131 base::CompareCase::SENSITIVE)) { | 132 base::CompareCase::SENSITIVE)) { |
| 132 base::FilePath file_path = dir_path.Append("sdch/index"); | 133 base::FilePath file_path = dir_path.Append("sdch/index"); |
| 133 scoped_ptr<net::test_server::RawHttpResponse> response = | 134 scoped_ptr<net::test_server::RawHttpResponse> response = |
| 134 ConstructResponseBasedOnFile(file_path).Pass(); | 135 ConstructResponseBasedOnFile(file_path); |
| 135 // Check for query params to see which dictionary to advertise. | 136 // Check for query params to see which dictionary to advertise. |
| 136 // For instance, ?q=dictionaryA will make the server advertise dictionaryA. | 137 // For instance, ?q=dictionaryA will make the server advertise dictionaryA. |
| 137 GURL url = g_test_server->GetURL(request.relative_url); | 138 GURL url = g_test_server->GetURL(request.relative_url); |
| 138 std::string dictionary; | 139 std::string dictionary; |
| 139 if (!net::GetValueForKeyInQuery(url, "q", &dictionary)) { | 140 if (!net::GetValueForKeyInQuery(url, "q", &dictionary)) { |
| 140 CHECK(false) << "dictionary is not found in query params of " | 141 CHECK(false) << "dictionary is not found in query params of " |
| 141 << request.relative_url; | 142 << request.relative_url; |
| 142 } | 143 } |
| 143 auto accept_encoding_header = request.headers.find("Accept-Encoding"); | 144 auto accept_encoding_header = request.headers.find("Accept-Encoding"); |
| 144 if (accept_encoding_header != request.headers.end()) { | 145 if (accept_encoding_header != request.headers.end()) { |
| 145 if (accept_encoding_header->second.find("sdch") != std::string::npos) | 146 if (accept_encoding_header->second.find("sdch") != std::string::npos) |
| 146 response->AddHeader(base::StringPrintf( | 147 response->AddHeader(base::StringPrintf( |
| 147 "Get-Dictionary: %s%s", kSdchDictPath, dictionary.c_str())); | 148 "Get-Dictionary: %s%s", kSdchDictPath, dictionary.c_str())); |
| 148 } | 149 } |
| 149 return response.Pass(); | 150 return std::move(response); |
| 150 } | 151 } |
| 151 | 152 |
| 152 if (base::StartsWith(request.relative_url, kSdchTestPath, | 153 if (base::StartsWith(request.relative_url, kSdchTestPath, |
| 153 base::CompareCase::SENSITIVE)) { | 154 base::CompareCase::SENSITIVE)) { |
| 154 auto avail_dictionary_header = request.headers.find("Avail-Dictionary"); | 155 auto avail_dictionary_header = request.headers.find("Avail-Dictionary"); |
| 155 if (avail_dictionary_header != request.headers.end()) { | 156 if (avail_dictionary_header != request.headers.end()) { |
| 156 base::FilePath file_path = dir_path.Append( | 157 base::FilePath file_path = dir_path.Append( |
| 157 "sdch/" + avail_dictionary_header->second + "_encoded"); | 158 "sdch/" + avail_dictionary_header->second + "_encoded"); |
| 158 return ConstructResponseBasedOnFile(file_path).Pass(); | 159 return ConstructResponseBasedOnFile(file_path); |
| 159 } | 160 } |
| 160 scoped_ptr<net::test_server::BasicHttpResponse> response( | 161 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 161 new net::test_server::BasicHttpResponse()); | 162 new net::test_server::BasicHttpResponse()); |
| 162 response->set_content_type("text/plain"); | 163 response->set_content_type("text/plain"); |
| 163 response->set_content("Sdch is not used.\n"); | 164 response->set_content("Sdch is not used.\n"); |
| 164 return response.Pass(); | 165 return std::move(response); |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Unhandled requests result in the Embedded test server sending a 404. | 168 // Unhandled requests result in the Embedded test server sending a 404. |
| 168 return scoped_ptr<net::test_server::BasicHttpResponse>(); | 169 return scoped_ptr<net::test_server::BasicHttpResponse>(); |
| 169 } | 170 } |
| 170 | 171 |
| 171 void RegisterHostResolverProcHelper( | 172 void RegisterHostResolverProcHelper( |
| 172 net::URLRequestContext* url_request_context) { | 173 net::URLRequestContext* url_request_context) { |
| 173 net::HostResolverImpl* resolver = | 174 net::HostResolverImpl* resolver = |
| 174 static_cast<net::HostResolverImpl*>(url_request_context->host_resolver()); | 175 static_cast<net::HostResolverImpl*>(url_request_context->host_resolver()); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 #else | 320 #else |
| 320 return JNI_FALSE; | 321 return JNI_FALSE; |
| 321 #endif | 322 #endif |
| 322 } | 323 } |
| 323 | 324 |
| 324 bool RegisterNativeTestServer(JNIEnv* env) { | 325 bool RegisterNativeTestServer(JNIEnv* env) { |
| 325 return RegisterNativesImpl(env); | 326 return RegisterNativesImpl(env); |
| 326 } | 327 } |
| 327 | 328 |
| 328 } // namespace cronet | 329 } // namespace cronet |
| OLD | NEW |