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
.reportRawHeaders())
209 load_flags
|= net::LOAD_REPORT_RAW_HEADERS
;
211 if (!request
.allowStoredCredentials()) {
212 load_flags
|= net::LOAD_DO_NOT_SAVE_COOKIES
;
213 load_flags
|= net::LOAD_DO_NOT_SEND_COOKIES
;
216 if (!request
.allowStoredCredentials())
217 load_flags
|= net::LOAD_DO_NOT_SEND_AUTH_DATA
;
222 scoped_refptr
<ResourceRequestBody
> GetRequestBodyForWebURLRequest(
223 const blink::WebURLRequest
& request
) {
224 scoped_refptr
<ResourceRequestBody
> request_body
;
226 if (request
.httpBody().isNull()) {
230 const std::string
& method
= request
.httpMethod().latin1();
231 // GET and HEAD requests shouldn't have http bodies.
232 DCHECK(method
!= "GET" && method
!= "HEAD");
234 const WebHTTPBody
& httpBody
= request
.httpBody();
235 request_body
= new ResourceRequestBody();
237 WebHTTPBody::Element element
;
238 while (httpBody
.elementAt(i
++, element
)) {
239 switch (element
.type
) {
240 case WebHTTPBody::Element::TypeData
:
241 if (!element
.data
.isEmpty()) {
242 // Blink sometimes gives empty data to append. These aren't
243 // necessary so they are just optimized out here.
244 request_body
->AppendBytes(
245 element
.data
.data(), static_cast<int>(element
.data
.size()));
248 case WebHTTPBody::Element::TypeFile
:
249 if (element
.fileLength
== -1) {
250 request_body
->AppendFileRange(
251 base::FilePath::FromUTF16Unsafe(element
.filePath
),
252 0, kuint64max
, base::Time());
254 request_body
->AppendFileRange(
255 base::FilePath::FromUTF16Unsafe(element
.filePath
),
256 static_cast<uint64
>(element
.fileStart
),
257 static_cast<uint64
>(element
.fileLength
),
258 base::Time::FromDoubleT(element
.modificationTime
));
261 case WebHTTPBody::Element::TypeFileSystemURL
: {
262 GURL file_system_url
= element
.fileSystemURL
;
263 DCHECK(file_system_url
.SchemeIsFileSystem());
264 request_body
->AppendFileSystemFileRange(
266 static_cast<uint64
>(element
.fileStart
),
267 static_cast<uint64
>(element
.fileLength
),
268 base::Time::FromDoubleT(element
.modificationTime
));
271 case WebHTTPBody::Element::TypeBlob
:
272 request_body
->AppendBlob(element
.blobUUID
.utf8());
278 request_body
->set_identifier(request
.httpBody().identifier());
282 blink::WebURLError
CreateWebURLError(const blink::WebURL
& unreachable_url
,
283 bool stale_copy_in_cache
,
285 blink::WebURLError error
;
286 error
.domain
= WebString::fromUTF8(net::kErrorDomain
);
287 error
.reason
= reason
;
288 error
.unreachableURL
= unreachable_url
;
289 error
.staleCopyInCache
= stale_copy_in_cache
;
290 if (reason
== net::ERR_ABORTED
) {
291 error
.isCancellation
= true;
292 } else if (reason
== net::ERR_TEMPORARILY_THROTTLED
) {
293 error
.localizedDescription
=
294 WebString::fromUTF8(kThrottledErrorDescription
);
296 error
.localizedDescription
=
297 WebString::fromUTF8(net::ErrorToString(reason
));
302 blink::WebURLError
CreateWebURLError(const blink::WebURL
& unreachable_url
,
303 bool stale_copy_in_cache
,
305 bool was_ignored_by_handler
) {
306 blink::WebURLError error
=
307 CreateWebURLError(unreachable_url
, stale_copy_in_cache
, reason
);
308 error
.wasIgnoredByHandler
= was_ignored_by_handler
;
312 } // namespace content