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
3 // found in the LICENSE file.
5 #include "content/child/web_url_request_util.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/load_flags.h"
10 #include "net/base/net_errors.h"
11 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
14 #include "third_party/WebKit/public/platform/WebURLError.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 using blink::WebHTTPBody
;
18 using blink::WebString
;
19 using blink::WebURLRequest
;
25 const char kThrottledErrorDescription
[] =
26 "Request throttled. Visit http://dev.chromium.org/throttling for more "
29 class HeaderFlattener
: public blink::WebHTTPHeaderVisitor
{
31 HeaderFlattener() : has_accept_header_(false) {}
33 virtual void visitHeader(const WebString
& name
, const WebString
& value
) {
34 // Headers are latin1.
35 const std::string
& name_latin1
= name
.latin1();
36 const std::string
& value_latin1
= value
.latin1();
38 // Skip over referrer headers found in the header map because we already
39 // pulled it out as a separate parameter.
40 if (base::LowerCaseEqualsASCII(name_latin1
, "referer"))
43 if (base::LowerCaseEqualsASCII(name_latin1
, "accept"))
44 has_accept_header_
= true;
47 buffer_
.append("\r\n");
48 buffer_
.append(name_latin1
+ ": " + value_latin1
);
51 const std::string
& GetBuffer() {
52 // In some cases, WebKit doesn't add an Accept header, but not having the
53 // header confuses some web servers. See bug 808613.
54 if (!has_accept_header_
) {
56 buffer_
.append("\r\n");
57 buffer_
.append("Accept: */*");
58 has_accept_header_
= true;
65 bool has_accept_header_
;
70 ResourceType
WebURLRequestToResourceType(const WebURLRequest
& request
) {
71 WebURLRequest::RequestContext requestContext
= request
.requestContext();
72 if (request
.frameType() != WebURLRequest::FrameTypeNone
) {
73 DCHECK(requestContext
== WebURLRequest::RequestContextForm
||
74 requestContext
== WebURLRequest::RequestContextFrame
||
75 requestContext
== WebURLRequest::RequestContextHyperlink
||
76 requestContext
== WebURLRequest::RequestContextIframe
||
77 requestContext
== WebURLRequest::RequestContextInternal
||
78 requestContext
== WebURLRequest::RequestContextLocation
);
79 if (request
.frameType() == WebURLRequest::FrameTypeTopLevel
||
80 request
.frameType() == WebURLRequest::FrameTypeAuxiliary
) {
81 return RESOURCE_TYPE_MAIN_FRAME
;
83 if (request
.frameType() == WebURLRequest::FrameTypeNested
)
84 return RESOURCE_TYPE_SUB_FRAME
;
86 return RESOURCE_TYPE_SUB_RESOURCE
;
89 switch (requestContext
) {
91 case WebURLRequest::RequestContextFavicon
:
92 return RESOURCE_TYPE_FAVICON
;
95 case WebURLRequest::RequestContextFont
:
96 return RESOURCE_TYPE_FONT_RESOURCE
;
99 case WebURLRequest::RequestContextImage
:
100 case WebURLRequest::RequestContextImageSet
:
101 return RESOURCE_TYPE_IMAGE
;
104 case WebURLRequest::RequestContextAudio
:
105 case WebURLRequest::RequestContextVideo
:
106 return RESOURCE_TYPE_MEDIA
;
109 case WebURLRequest::RequestContextEmbed
:
110 case WebURLRequest::RequestContextObject
:
111 return RESOURCE_TYPE_OBJECT
;
114 case WebURLRequest::RequestContextBeacon
:
115 case WebURLRequest::RequestContextCSPReport
:
116 case WebURLRequest::RequestContextPing
:
117 return RESOURCE_TYPE_PING
;
120 case WebURLRequest::RequestContextPrefetch
:
121 return RESOURCE_TYPE_PREFETCH
;
124 case WebURLRequest::RequestContextImport
:
125 case WebURLRequest::RequestContextScript
:
126 return RESOURCE_TYPE_SCRIPT
;
129 case WebURLRequest::RequestContextXSLT
:
130 case WebURLRequest::RequestContextStyle
:
131 return RESOURCE_TYPE_STYLESHEET
;
134 case WebURLRequest::RequestContextDownload
:
135 case WebURLRequest::RequestContextManifest
:
136 case WebURLRequest::RequestContextSubresource
:
137 case WebURLRequest::RequestContextPlugin
:
138 return RESOURCE_TYPE_SUB_RESOURCE
;
141 case WebURLRequest::RequestContextTrack
:
142 return RESOURCE_TYPE_MEDIA
;
145 case WebURLRequest::RequestContextServiceWorker
:
146 return RESOURCE_TYPE_SERVICE_WORKER
;
147 case WebURLRequest::RequestContextSharedWorker
:
148 return RESOURCE_TYPE_SHARED_WORKER
;
149 case WebURLRequest::RequestContextWorker
:
150 return RESOURCE_TYPE_WORKER
;
153 case WebURLRequest::RequestContextInternal
:
154 case WebURLRequest::RequestContextUnspecified
:
155 return RESOURCE_TYPE_SUB_RESOURCE
;
158 case WebURLRequest::RequestContextEventSource
:
159 case WebURLRequest::RequestContextFetch
:
160 case WebURLRequest::RequestContextXMLHttpRequest
:
161 return RESOURCE_TYPE_XHR
;
163 // These should be handled by the FrameType checks at the top of the
165 case WebURLRequest::RequestContextForm
:
166 case WebURLRequest::RequestContextHyperlink
:
167 case WebURLRequest::RequestContextLocation
:
168 case WebURLRequest::RequestContextFrame
:
169 case WebURLRequest::RequestContextIframe
:
171 return RESOURCE_TYPE_SUB_RESOURCE
;
175 return RESOURCE_TYPE_SUB_RESOURCE
;
179 std::string
GetWebURLRequestHeaders(const blink::WebURLRequest
& request
) {
180 HeaderFlattener flattener
;
181 request
.visitHTTPHeaderFields(&flattener
);
182 return flattener
.GetBuffer();
185 int GetLoadFlagsForWebURLRequest(const blink::WebURLRequest
& request
) {
186 int load_flags
= net::LOAD_NORMAL
;
187 GURL url
= request
.url();
188 switch (request
.cachePolicy()) {
189 case WebURLRequest::ReloadIgnoringCacheData
:
190 // Required by LayoutTests/http/tests/misc/refresh-headers.php
191 load_flags
|= net::LOAD_VALIDATE_CACHE
;
193 case WebURLRequest::ReloadBypassingCache
:
194 load_flags
|= net::LOAD_BYPASS_CACHE
;
196 case WebURLRequest::ReturnCacheDataElseLoad
:
197 load_flags
|= net::LOAD_PREFERRING_CACHE
;
199 case WebURLRequest::ReturnCacheDataDontLoad
:
200 load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
202 case WebURLRequest::UseProtocolCachePolicy
:
208 if (!request
.allowStoredCredentials()) {
209 load_flags
|= net::LOAD_DO_NOT_SAVE_COOKIES
;
210 load_flags
|= net::LOAD_DO_NOT_SEND_COOKIES
;
213 if (!request
.allowStoredCredentials())
214 load_flags
|= net::LOAD_DO_NOT_SEND_AUTH_DATA
;
219 scoped_refptr
<ResourceRequestBody
> GetRequestBodyForWebURLRequest(
220 const blink::WebURLRequest
& request
) {
221 scoped_refptr
<ResourceRequestBody
> request_body
;
223 if (request
.httpBody().isNull()) {
227 const std::string
& method
= request
.httpMethod().latin1();
228 // GET and HEAD requests shouldn't have http bodies.
229 DCHECK(method
!= "GET" && method
!= "HEAD");
231 const WebHTTPBody
& httpBody
= request
.httpBody();
232 request_body
= new ResourceRequestBody();
234 WebHTTPBody::Element element
;
235 while (httpBody
.elementAt(i
++, element
)) {
236 switch (element
.type
) {
237 case WebHTTPBody::Element::TypeData
:
238 if (!element
.data
.isEmpty()) {
239 // Blink sometimes gives empty data to append. These aren't
240 // necessary so they are just optimized out here.
241 request_body
->AppendBytes(
242 element
.data
.data(), static_cast<int>(element
.data
.size()));
245 case WebHTTPBody::Element::TypeFile
:
246 if (element
.fileLength
== -1) {
247 request_body
->AppendFileRange(
248 base::FilePath::FromUTF16Unsafe(element
.filePath
),
249 0, kuint64max
, base::Time());
251 request_body
->AppendFileRange(
252 base::FilePath::FromUTF16Unsafe(element
.filePath
),
253 static_cast<uint64
>(element
.fileStart
),
254 static_cast<uint64
>(element
.fileLength
),
255 base::Time::FromDoubleT(element
.modificationTime
));
258 case WebHTTPBody::Element::TypeFileSystemURL
: {
259 GURL file_system_url
= element
.fileSystemURL
;
260 DCHECK(file_system_url
.SchemeIsFileSystem());
261 request_body
->AppendFileSystemFileRange(
263 static_cast<uint64
>(element
.fileStart
),
264 static_cast<uint64
>(element
.fileLength
),
265 base::Time::FromDoubleT(element
.modificationTime
));
268 case WebHTTPBody::Element::TypeBlob
:
269 request_body
->AppendBlob(element
.blobUUID
.utf8());
275 request_body
->set_identifier(request
.httpBody().identifier());
279 blink::WebURLError
CreateWebURLError(const blink::WebURL
& unreachable_url
,
280 bool stale_copy_in_cache
,
282 blink::WebURLError error
;
283 error
.domain
= WebString::fromUTF8(net::kErrorDomain
);
284 error
.reason
= reason
;
285 error
.unreachableURL
= unreachable_url
;
286 error
.staleCopyInCache
= stale_copy_in_cache
;
287 if (reason
== net::ERR_ABORTED
) {
288 error
.isCancellation
= true;
289 } else if (reason
== net::ERR_TEMPORARILY_THROTTLED
) {
290 error
.localizedDescription
=
291 WebString::fromUTF8(kThrottledErrorDescription
);
293 error
.localizedDescription
=
294 WebString::fromUTF8(net::ErrorToString(reason
));
299 blink::WebURLError
CreateWebURLError(const blink::WebURL
& unreachable_url
,
300 bool stale_copy_in_cache
,
302 bool was_ignored_by_handler
) {
303 blink::WebURLError error
=
304 CreateWebURLError(unreachable_url
, stale_copy_in_cache
, reason
);
305 error
.wasIgnoredByHandler
= was_ignored_by_handler
;
309 } // namespace content