Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/net/url_request_mock_http_job.h" | 5 #include "chrome/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 "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| 11 #include "net/http/http_response_headers.h" | 11 #include "net/http/http_response_headers.h" |
| 12 #include "net/url_request/url_request_filter.h" | 12 #include "net/url_request/url_request_filter.h" |
| 13 | 13 |
| 14 static const char kMockHostname[] = "mock.http"; | 14 static const char kMockHostname[] = "mock.http"; |
| 15 static const wchar_t kMockHeaderFileSuffix[] = L".mock-http-headers"; | 15 static const FilePath::CharType kMockHeaderFileSuffix[] = |
| 16 FILE_PATH_LITERAL(".mock-http-headers"); | |
|
tony
2009/10/13 00:31:39
Nit: Maybe make this a char* and use AppendASCII?
Evan Martin
2009/10/13 00:45:42
Unfortunately, AppendASCII is for appending path c
| |
| 16 | 17 |
| 17 std::wstring URLRequestMockHTTPJob::base_path_ = L""; | 18 FilePath URLRequestMockHTTPJob::base_path_; |
| 18 | 19 |
| 19 /* static */ | 20 /* static */ |
| 20 URLRequestJob* URLRequestMockHTTPJob::Factory(URLRequest* request, | 21 URLRequestJob* URLRequestMockHTTPJob::Factory(URLRequest* request, |
| 21 const std::string& scheme) { | 22 const std::string& scheme) { |
| 22 return new URLRequestMockHTTPJob(request, | 23 return new URLRequestMockHTTPJob(request, |
| 23 GetOnDiskPath(base_path_, request, scheme)); | 24 GetOnDiskPath(base_path_, request, scheme)); |
| 24 } | 25 } |
| 25 | 26 |
| 26 /* static */ | 27 /* static */ |
| 27 void URLRequestMockHTTPJob::AddUrlHandler(const std::wstring& base_path) { | 28 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { |
| 28 base_path_ = base_path; | 29 base_path_ = base_path; |
| 29 | 30 |
| 30 // Add kMockHostname to URLRequestFilter. | 31 // Add kMockHostname to URLRequestFilter. |
| 31 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | 32 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
| 32 filter->AddHostnameHandler("http", kMockHostname, | 33 filter->AddHostnameHandler("http", kMockHostname, |
| 33 URLRequestMockHTTPJob::Factory); | 34 URLRequestMockHTTPJob::Factory); |
| 34 } | 35 } |
| 35 | 36 |
| 36 /* static */ | 37 /* static */ |
| 37 GURL URLRequestMockHTTPJob::GetMockUrl(const std::wstring& path) { | 38 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { |
| 38 std::string url = "http://"; | 39 std::string url = "http://"; |
| 39 url.append(kMockHostname); | 40 url.append(kMockHostname); |
| 40 url.append("/"); | 41 url.append("/"); |
| 41 url.append(WideToUTF8(path)); | 42 url.append(WideToUTF8(path.ToWStringHack())); |
| 42 return GURL(url); | 43 return GURL(url); |
| 43 } | 44 } |
| 44 | 45 |
| 45 /* static */ | 46 /* static */ |
| 46 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const std::wstring& base_path, | 47 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, |
| 47 URLRequest* request, | 48 URLRequest* request, |
| 48 const std::string& scheme) { | 49 const std::string& scheme) { |
| 49 std::string file_url("file:///"); | 50 std::string file_url("file:///"); |
| 50 file_url += WideToUTF8(base_path); | 51 file_url += WideToUTF8(base_path.ToWStringHack()); |
| 51 file_url += request->url().path(); | 52 file_url += request->url().path(); |
| 52 | 53 |
| 53 // Convert the file:/// URL to a path on disk. | 54 // Convert the file:/// URL to a path on disk. |
| 54 FilePath file_path; | 55 FilePath file_path; |
| 55 net::FileURLToFilePath(GURL(file_url), &file_path); | 56 net::FileURLToFilePath(GURL(file_url), &file_path); |
| 56 return file_path; | 57 return file_path; |
| 57 } | 58 } |
| 58 | 59 |
| 59 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request, | 60 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request, |
| 60 const FilePath& file_path) | 61 const FilePath& file_path) |
| 61 : URLRequestFileJob(request, file_path) { } | 62 : URLRequestFileJob(request, file_path) { } |
| 62 | 63 |
| 63 // Public virtual version. | 64 // Public virtual version. |
| 64 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 65 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 65 // Forward to private const version. | 66 // Forward to private const version. |
| 66 GetResponseInfoConst(info); | 67 GetResponseInfoConst(info); |
| 67 } | 68 } |
| 68 | 69 |
| 69 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 70 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 70 int* http_status_code) { | 71 int* http_status_code) { |
| 71 // Override the URLRequestFileJob implementation to invoke the default one | 72 // Override the URLRequestFileJob implementation to invoke the default one |
| 72 // based on HttpResponseInfo. | 73 // based on HttpResponseInfo. |
| 73 return URLRequestJob::IsRedirectResponse(location, http_status_code); | 74 return URLRequestJob::IsRedirectResponse(location, http_status_code); |
| 74 } | 75 } |
| 75 | 76 |
| 76 // Private const version. | 77 // Private const version. |
| 77 void URLRequestMockHTTPJob::GetResponseInfoConst( | 78 void URLRequestMockHTTPJob::GetResponseInfoConst( |
| 78 net::HttpResponseInfo* info) const { | 79 net::HttpResponseInfo* info) const { |
| 79 std::wstring header_file = file_path_.ToWStringHack() + kMockHeaderFileSuffix; | 80 FilePath header_file = FilePath(file_path_.value() + kMockHeaderFileSuffix); |
| 80 std::string raw_headers; | 81 std::string raw_headers; |
| 81 if (!file_util::ReadFileToString(header_file, &raw_headers)) | 82 if (!file_util::ReadFileToString(header_file, &raw_headers)) |
| 82 return; | 83 return; |
| 83 | 84 |
| 84 // ParseRawHeaders expects \0 to end each header line. | 85 // ParseRawHeaders expects \0 to end each header line. |
| 85 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 86 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| 86 info->headers = new net::HttpResponseHeaders(raw_headers); | 87 info->headers = new net::HttpResponseHeaders(raw_headers); |
| 87 } | 88 } |
| 88 | 89 |
| 89 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { | 90 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { |
| 90 net::HttpResponseInfo info; | 91 net::HttpResponseInfo info; |
| 91 GetResponseInfoConst(&info); | 92 GetResponseInfoConst(&info); |
| 92 return info.headers && info.headers->GetMimeType(mime_type); | 93 return info.headers && info.headers->GetMimeType(mime_type); |
| 93 } | 94 } |
| 94 | 95 |
| 95 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 96 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 96 net::HttpResponseInfo info; | 97 net::HttpResponseInfo info; |
| 97 GetResponseInfo(&info); | 98 GetResponseInfo(&info); |
| 98 return info.headers && info.headers->GetCharset(charset); | 99 return info.headers && info.headers->GetCharset(charset); |
| 99 } | 100 } |
| OLD | NEW |