OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/network/NetworkUtils.h" | 5 #include "platform/network/NetworkUtils.h" |
6 | 6 |
7 #include "components/mime_util/mime_util.h" | 7 #include "components/mime_util/mime_util.h" |
8 #include "net/base/data_url.h" | 8 #include "net/base/data_url.h" |
9 #include "net/base/ip_address.h" | 9 #include "net/base/ip_address.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 11 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
12 #include "net/base/url_util.h" | 12 #include "net/base/url_util.h" |
13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
| 14 #include "net/url_request/url_request_data_job.h" |
14 #include "platform/SharedBuffer.h" | 15 #include "platform/SharedBuffer.h" |
| 16 #include "platform/loader/fetch/ResourceResponse.h" |
15 #include "platform/weborigin/KURL.h" | 17 #include "platform/weborigin/KURL.h" |
16 #include "public/platform/URLConversion.h" | 18 #include "public/platform/URLConversion.h" |
17 #include "public/platform/WebString.h" | 19 #include "public/platform/WebString.h" |
18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
19 #include "wtf/text/StringUTF8Adaptor.h" | 21 #include "wtf/text/StringUTF8Adaptor.h" |
20 #include "wtf/text/WTFString.h" | 22 #include "wtf/text/WTFString.h" |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 net::registry_controlled_domains::PrivateRegistryFilter | 26 net::registry_controlled_domains::PrivateRegistryFilter |
(...skipping 30 matching lines...) Expand all Loading... |
55 return net::IsLocalHostname(utf8.asStringPiece(), isLocal6); | 57 return net::IsLocalHostname(utf8.asStringPiece(), isLocal6); |
56 } | 58 } |
57 | 59 |
58 String getDomainAndRegistry(const String& host, PrivateRegistryFilter filter) { | 60 String getDomainAndRegistry(const String& host, PrivateRegistryFilter filter) { |
59 StringUTF8Adaptor hostUtf8(host); | 61 StringUTF8Adaptor hostUtf8(host); |
60 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( | 62 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( |
61 hostUtf8.asStringPiece(), getNetPrivateRegistryFilter(filter)); | 63 hostUtf8.asStringPiece(), getNetPrivateRegistryFilter(filter)); |
62 return String(domain.data(), domain.length()); | 64 return String(domain.data(), domain.length()); |
63 } | 65 } |
64 | 66 |
65 PassRefPtr<SharedBuffer> parseDataURL(const KURL& url, | 67 PassRefPtr<SharedBuffer> parseDataURLAndPopulateResponse( |
66 AtomicString& mimetype, | 68 const KURL& url, |
67 AtomicString& charset) { | 69 ResourceResponse& response) { |
| 70 // The following code contains duplication of GetInfoFromDataURL() and |
| 71 // WebURLLoaderImpl::PopulateURLResponse() in |
| 72 // content/child/web_url_loader_impl.cc. Merge them once content/child is |
| 73 // moved to platform/. |
68 std::string utf8MimeType; | 74 std::string utf8MimeType; |
69 std::string utf8Charset; | 75 std::string utf8Charset; |
70 std::string data; | 76 std::string dataString; |
71 if (net::DataURL::Parse(WebStringToGURL(url.getString()), &utf8MimeType, | 77 scoped_refptr<net::HttpResponseHeaders> headers( |
72 &utf8Charset, &data) && | 78 new net::HttpResponseHeaders(std::string())); |
73 mime_util::IsSupportedMimeType(utf8MimeType)) { | 79 |
74 mimetype = WebString::fromUTF8(utf8MimeType); | 80 int result = net::URLRequestDataJob::BuildResponse( |
75 charset = WebString::fromUTF8(utf8Charset); | 81 WebStringToGURL(url.getString()), &utf8MimeType, &utf8Charset, |
76 return SharedBuffer::create(data.data(), data.size()); | 82 &dataString, headers.get()); |
| 83 if (result != net::OK) |
| 84 return nullptr; |
| 85 |
| 86 if (!mime_util::IsSupportedMimeType(utf8MimeType)) |
| 87 return nullptr; |
| 88 |
| 89 RefPtr<SharedBuffer> data = |
| 90 SharedBuffer::create(dataString.data(), dataString.size()); |
| 91 response.setHTTPStatusCode(200); |
| 92 response.setHTTPStatusText("OK"); |
| 93 response.setURL(url); |
| 94 response.setMimeType(WebString::fromUTF8(utf8MimeType)); |
| 95 response.setExpectedContentLength(data->size()); |
| 96 response.setTextEncodingName(WebString::fromUTF8(utf8Charset)); |
| 97 |
| 98 size_t iter = 0; |
| 99 std::string name; |
| 100 std::string value; |
| 101 while (headers->EnumerateHeaderLines(&iter, &name, &value)) { |
| 102 response.addHTTPHeaderField(WebString::fromLatin1(name), |
| 103 WebString::fromLatin1(value)); |
77 } | 104 } |
78 return nullptr; | 105 return data; |
79 } | 106 } |
80 | 107 |
81 bool isRedirectResponseCode(int responseCode) { | 108 bool isRedirectResponseCode(int responseCode) { |
82 return net::HttpResponseHeaders::IsRedirectResponseCode(responseCode); | 109 return net::HttpResponseHeaders::IsRedirectResponseCode(responseCode); |
83 } | 110 } |
84 | 111 |
85 } // NetworkUtils | 112 } // NetworkUtils |
86 | 113 |
87 } // namespace blink | 114 } // namespace blink |
OLD | NEW |