1 /*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebResourceRequest.h"
28
29 #include "ResourceRequest.h"
30
31 #include <wtf/text/CString.h>
32
33 using namespace WebCore;
34
35 namespace android {
36
WebResourceRequest(const WebCore::ResourceRequest & resourceRequest,bool shouldBlockNetworkLoads)37 WebResourceRequest::WebResourceRequest(const WebCore::ResourceRequest& resourceRequest, bool shouldBlockNetworkLoads)
38 {
39 // Set the load flags based on the WebCore request.
40 m_loadFlags = net::LOAD_NORMAL;
41
42 if (shouldBlockNetworkLoads) {
43 // In the case that the embedder has blocked network loads, we only
44 // ever try to serve content out of the cache. If WebCore has set
45 // ReloadIgnoringCacheData, we would normally attempt to validate
46 // the cached data before serving it. In the absence of network
47 // we can't do that, so we will just return whatever we have in the
48 // cache (which may well be nothing).
49 m_loadFlags |= net::LOAD_ONLY_FROM_CACHE;
50 } else {
51 switch (resourceRequest.cachePolicy()) {
52 case ReloadIgnoringCacheData:
53 m_loadFlags |= net::LOAD_VALIDATE_CACHE;
54 break;
55 case ReturnCacheDataElseLoad:
56 m_loadFlags |= net::LOAD_PREFERRING_CACHE;
57 break;
58 case ReturnCacheDataDontLoad:
59 m_loadFlags |= net::LOAD_ONLY_FROM_CACHE;
60 break;
61 case UseProtocolCachePolicy:
62 break;
63 }
64 }
65
66 // TODO: We should consider setting these flags and net::LOAD_DO_NOT_SEND_AUTH_DATA
67 // when FrameLoaderClient::shouldUseCredentialStorage() is false. However,
68 // the required WebKit logic is not yet in place. See Chromium's
69 // FrameLoaderClientImpl::shouldUseCredentialStorage().
70 if (!resourceRequest.allowCookies()) {
71 m_loadFlags |= net::LOAD_DO_NOT_SAVE_COOKIES;
72 m_loadFlags |= net::LOAD_DO_NOT_SEND_COOKIES;
73 }
74
75
76 switch (resourceRequest.targetType()) {
77 case ResourceRequest::TargetIsPrefetch:
78 m_loadFlags |= (net::LOAD_PREFETCH | net::LOAD_DO_NOT_PROMPT_FOR_LOGIN);
79 break;
80 case ResourceRequest::TargetIsFavicon:
81 m_loadFlags |= net::LOAD_DO_NOT_PROMPT_FOR_LOGIN;
82 break;
83 default: break;
84 }
85
86
87 // Set the request headers
88 const HTTPHeaderMap& map = resourceRequest.httpHeaderFields();
89 for (HTTPHeaderMap::const_iterator it = map.begin(); it != map.end(); ++it) {
90 const std::string& nameUtf8 = it->first.string().utf8().data();
91 const std::string& valueUtf8 = it->second.utf8().data();
92
93 // Skip over referrer headers found in the header map because we already
94 // pulled it out as a separate parameter. We likewise prune the UA since
95 // that will be added back by the network layer.
96 if (LowerCaseEqualsASCII(nameUtf8, "referer") || LowerCaseEqualsASCII(nameUtf8, "user-agent"))
97 continue;
98
99 // Skip over "Cache-Control: max-age=0" header if the corresponding
100 // load flag is already specified. FrameLoader sets both the flag and
101 // the extra header -- the extra header is redundant since our network
102 // implementation will add the necessary headers based on load flags.
103 // See http://code.google.com/p/chromium/issues/detail?id=3434.
104 if ((m_loadFlags & net::LOAD_VALIDATE_CACHE) &&
105 LowerCaseEqualsASCII(nameUtf8, "cache-control") && LowerCaseEqualsASCII(valueUtf8, "max-age=0"))
106 continue;
107
108 m_requestHeaders.SetHeader(nameUtf8, valueUtf8);
109 }
110
111 m_method = resourceRequest.httpMethod().utf8().data();
112 m_referrer = resourceRequest.httpReferrer().utf8().data();
113 m_userAgent = resourceRequest.httpUserAgent().utf8().data();
114
115 m_url = resourceRequest.url().string().utf8().data();
116 }
117
118 } // namespace android
119