| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "content/browser/net/url_request_mock_http_job.h" | 5 #include "content/browser/net/url_request_mock_http_job.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/url_request_filter.h" | 15 #include "net/url_request/url_request_filter.h" |
| 16 | 16 |
| 17 static const char kMockHostname[] = "mock.http"; | 17 const char kMockHostname[] = "mock.http"; |
| 18 static const FilePath::CharType kMockHeaderFileSuffix[] = | 18 const FilePath::CharType kMockHeaderFileSuffix[] = |
| 19 FILE_PATH_LITERAL(".mock-http-headers"); | 19 FILE_PATH_LITERAL(".mock-http-headers"); |
| 20 | 20 |
| 21 FilePath URLRequestMockHTTPJob::base_path_; | 21 namespace { |
| 22 |
| 23 // This is the file path leading to the root of the directory to use as the |
| 24 // root of the http server. This returns a reference that can be assigned to. |
| 25 FilePath& BasePath() { |
| 26 CR_DEFINE_STATIC_LOCAL(FilePath, base_path, ()); |
| 27 return base_path; |
| 28 } |
| 29 |
| 30 } // namespace |
| 22 | 31 |
| 23 // static | 32 // static |
| 24 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, | 33 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, |
| 25 const std::string& scheme) { | 34 const std::string& scheme) { |
| 26 return new URLRequestMockHTTPJob(request, | 35 return new URLRequestMockHTTPJob(request, |
| 27 GetOnDiskPath(base_path_, request, scheme)); | 36 GetOnDiskPath(BasePath(), request, scheme)); |
| 28 } | 37 } |
| 29 | 38 |
| 30 // static | 39 // static |
| 31 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { | 40 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { |
| 32 base_path_ = base_path; | 41 BasePath() = base_path; |
| 33 | 42 |
| 34 // Add kMockHostname to net::URLRequestFilter. | 43 // Add kMockHostname to net::URLRequestFilter. |
| 35 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 44 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 36 filter->AddHostnameHandler("http", kMockHostname, | 45 filter->AddHostnameHandler("http", kMockHostname, |
| 37 URLRequestMockHTTPJob::Factory); | 46 URLRequestMockHTTPJob::Factory); |
| 38 } | 47 } |
| 39 | 48 |
| 40 /* static */ | 49 // static |
| 41 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { | 50 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { |
| 42 std::string url = "http://"; | 51 std::string url = "http://"; |
| 43 url.append(kMockHostname); | 52 url.append(kMockHostname); |
| 44 url.append("/"); | 53 url.append("/"); |
| 45 std::string path_str = path.MaybeAsASCII(); | 54 std::string path_str = path.MaybeAsASCII(); |
| 46 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 55 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 47 url.append(path_str); | 56 url.append(path_str); |
| 48 return GURL(url); | 57 return GURL(url); |
| 49 } | 58 } |
| 50 | 59 |
| 51 /* static */ | 60 // static |
| 52 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { | 61 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { |
| 53 std::string url = chrome::kViewSourceScheme; | 62 std::string url = chrome::kViewSourceScheme; |
| 54 url.append(":"); | 63 url.append(":"); |
| 55 url.append(GetMockUrl(path).spec()); | 64 url.append(GetMockUrl(path).spec()); |
| 56 return GURL(url); | 65 return GURL(url); |
| 57 } | 66 } |
| 58 | 67 |
| 59 /* static */ | 68 // static |
| 60 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, | 69 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, |
| 61 net::URLRequest* request, | 70 net::URLRequest* request, |
| 62 const std::string& scheme) { | 71 const std::string& scheme) { |
| 63 // Conceptually we just want to "return base_path + request->url().path()". | 72 // Conceptually we just want to "return base_path + request->url().path()". |
| 64 // But path in the request URL is in URL space (i.e. %-encoded spaces). | 73 // But path in the request URL is in URL space (i.e. %-encoded spaces). |
| 65 // So first we convert base FilePath to a URL, then append the URL | 74 // So first we convert base FilePath to a URL, then append the URL |
| 66 // path to that, and convert the final URL back to a FilePath. | 75 // path to that, and convert the final URL back to a FilePath. |
| 67 GURL file_url(net::FilePathToFileURL(base_path)); | 76 GURL file_url(net::FilePathToFileURL(base_path)); |
| 68 std::string url = file_url.spec() + request->url().path(); | 77 std::string url = file_url.spec() + request->url().path(); |
| 69 FilePath file_path; | 78 FilePath file_path; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 net::HttpResponseInfo info; | 118 net::HttpResponseInfo info; |
| 110 GetResponseInfoConst(&info); | 119 GetResponseInfoConst(&info); |
| 111 return info.headers && info.headers->GetMimeType(mime_type); | 120 return info.headers && info.headers->GetMimeType(mime_type); |
| 112 } | 121 } |
| 113 | 122 |
| 114 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 123 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 115 net::HttpResponseInfo info; | 124 net::HttpResponseInfo info; |
| 116 GetResponseInfo(&info); | 125 GetResponseInfo(&info); |
| 117 return info.headers && info.headers->GetCharset(charset); | 126 return info.headers && info.headers->GetCharset(charset); |
| 118 } | 127 } |
| OLD | NEW |