Fix link in German terms of service.
[chromium-blink-merge.git] / content / child / web_url_request_util.cc
blob6737307c744b5af3a2d255c3d3a82234da880c55
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;
21 namespace content {
23 namespace {
25 const char kThrottledErrorDescription[] =
26 "Request throttled. Visit http://dev.chromium.org/throttling for more "
27 "information.";
29 class HeaderFlattener : public blink::WebHTTPHeaderVisitor {
30 public:
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 (LowerCaseEqualsASCII(name_latin1, "referer"))
41 return;
43 if (LowerCaseEqualsASCII(name_latin1, "accept"))
44 has_accept_header_ = true;
46 if (!buffer_.empty())
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_) {
55 if (!buffer_.empty())
56 buffer_.append("\r\n");
57 buffer_.append("Accept: */*");
58 has_accept_header_ = true;
60 return buffer_;
63 private:
64 std::string buffer_;
65 bool has_accept_header_;
68 } // namespace
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;
85 NOTREACHED();
86 return RESOURCE_TYPE_SUB_RESOURCE;
89 switch (requestContext) {
90 // Favicon
91 case WebURLRequest::RequestContextFavicon:
92 return RESOURCE_TYPE_FAVICON;
94 // Font
95 case WebURLRequest::RequestContextFont:
96 return RESOURCE_TYPE_FONT_RESOURCE;
98 // Image
99 case WebURLRequest::RequestContextImage:
100 case WebURLRequest::RequestContextImageSet:
101 return RESOURCE_TYPE_IMAGE;
103 // Media
104 case WebURLRequest::RequestContextAudio:
105 case WebURLRequest::RequestContextVideo:
106 return RESOURCE_TYPE_MEDIA;
108 // Object
109 case WebURLRequest::RequestContextEmbed:
110 case WebURLRequest::RequestContextObject:
111 return RESOURCE_TYPE_OBJECT;
113 // Ping
114 case WebURLRequest::RequestContextBeacon:
115 case WebURLRequest::RequestContextCSPReport:
116 case WebURLRequest::RequestContextPing:
117 return RESOURCE_TYPE_PING;
119 // Prefetch
120 case WebURLRequest::RequestContextPrefetch:
121 return RESOURCE_TYPE_PREFETCH;
123 // Script
124 case WebURLRequest::RequestContextImport:
125 case WebURLRequest::RequestContextScript:
126 return RESOURCE_TYPE_SCRIPT;
128 // Style
129 case WebURLRequest::RequestContextXSLT:
130 case WebURLRequest::RequestContextStyle:
131 return RESOURCE_TYPE_STYLESHEET;
133 // Subresource
134 case WebURLRequest::RequestContextDownload:
135 case WebURLRequest::RequestContextManifest:
136 case WebURLRequest::RequestContextSubresource:
137 case WebURLRequest::RequestContextPlugin:
138 return RESOURCE_TYPE_SUB_RESOURCE;
140 // TextTrack
141 case WebURLRequest::RequestContextTrack:
142 return RESOURCE_TYPE_MEDIA;
144 // Workers
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;
152 // Unspecified
153 case WebURLRequest::RequestContextInternal:
154 case WebURLRequest::RequestContextUnspecified:
155 return RESOURCE_TYPE_SUB_RESOURCE;
157 // XHR
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
164 // function.
165 case WebURLRequest::RequestContextForm:
166 case WebURLRequest::RequestContextHyperlink:
167 case WebURLRequest::RequestContextLocation:
168 case WebURLRequest::RequestContextFrame:
169 case WebURLRequest::RequestContextIframe:
170 NOTREACHED();
171 return RESOURCE_TYPE_SUB_RESOURCE;
173 default:
174 NOTREACHED();
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;
192 break;
193 case WebURLRequest::ReloadBypassingCache:
194 load_flags |= net::LOAD_BYPASS_CACHE;
195 break;
196 case WebURLRequest::ReturnCacheDataElseLoad:
197 load_flags |= net::LOAD_PREFERRING_CACHE;
198 break;
199 case WebURLRequest::ReturnCacheDataDontLoad:
200 load_flags |= net::LOAD_ONLY_FROM_CACHE;
201 break;
202 case WebURLRequest::UseProtocolCachePolicy:
203 break;
204 default:
205 NOTREACHED();
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;
219 return load_flags;
222 scoped_refptr<ResourceRequestBody> GetRequestBodyForWebURLRequest(
223 const blink::WebURLRequest& request) {
224 scoped_refptr<ResourceRequestBody> request_body;
226 if (request.httpBody().isNull()) {
227 return request_body;
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();
236 size_t i = 0;
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()));
247 break;
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());
253 } else {
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));
260 break;
261 case WebHTTPBody::Element::TypeFileSystemURL: {
262 GURL file_system_url = element.fileSystemURL;
263 DCHECK(file_system_url.SchemeIsFileSystem());
264 request_body->AppendFileSystemFileRange(
265 file_system_url,
266 static_cast<uint64>(element.fileStart),
267 static_cast<uint64>(element.fileLength),
268 base::Time::FromDoubleT(element.modificationTime));
269 break;
271 case WebHTTPBody::Element::TypeBlob:
272 request_body->AppendBlob(element.blobUUID.utf8());
273 break;
274 default:
275 NOTREACHED();
278 request_body->set_identifier(request.httpBody().identifier());
279 return request_body;
282 blink::WebURLError CreateWebURLError(const blink::WebURL& unreachable_url,
283 bool stale_copy_in_cache,
284 int reason) {
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);
295 } else {
296 error.localizedDescription =
297 WebString::fromUTF8(net::ErrorToString(reason));
299 return error;
302 } // namespace content