• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google Inc. All Rights Reserved.
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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. 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 APPLE AND ITS CONTRIBUTORS ``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 APPLE INC. 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 "core/fetch/RawResource.h"
28 
29 #include "core/fetch/ResourceClientWalker.h"
30 #include "core/fetch/ResourceFetcher.h"
31 #include "core/fetch/ResourceLoader.h"
32 #include "platform/SharedBuffer.h"
33 
34 namespace WebCore {
35 
RawResource(const ResourceRequest & resourceRequest,Type type)36 RawResource::RawResource(const ResourceRequest& resourceRequest, Type type)
37     : Resource(resourceRequest, type)
38 {
39 }
40 
appendData(const char * data,int length)41 void RawResource::appendData(const char* data, int length)
42 {
43     Resource::appendData(data, length);
44 
45     ResourcePtr<RawResource> protect(this);
46     ResourceClientWalker<RawResourceClient> w(m_clients);
47     while (RawResourceClient* c = w.next())
48         c->dataReceived(this, data, length);
49 }
50 
didAddClient(ResourceClient * c)51 void RawResource::didAddClient(ResourceClient* c)
52 {
53     if (!hasClient(c))
54         return;
55     // The calls to the client can result in events running, potentially causing
56     // this resource to be evicted from the cache and all clients to be removed,
57     // so a protector is necessary.
58     ResourcePtr<RawResource> protect(this);
59     RawResourceClient* client = static_cast<RawResourceClient*>(c);
60     size_t redirectCount = m_redirectChain.size();
61     for (size_t i = 0; i < redirectCount; i++) {
62         RedirectPair redirect = m_redirectChain[i];
63         ResourceRequest request(redirect.m_request);
64         client->redirectReceived(this, request, redirect.m_redirectResponse);
65         if (!hasClient(c))
66             return;
67     }
68     ASSERT(redirectCount == m_redirectChain.size());
69 
70     if (!m_response.isNull())
71         client->responseReceived(this, m_response);
72     if (!hasClient(c))
73         return;
74     if (m_data)
75         client->dataReceived(this, m_data->data(), m_data->size());
76     if (!hasClient(c))
77         return;
78     Resource::didAddClient(client);
79 }
80 
willSendRequest(ResourceRequest & request,const ResourceResponse & response)81 void RawResource::willSendRequest(ResourceRequest& request, const ResourceResponse& response)
82 {
83     ResourcePtr<RawResource> protect(this);
84     if (!response.isNull()) {
85         ResourceClientWalker<RawResourceClient> w(m_clients);
86         while (RawResourceClient* c = w.next())
87             c->redirectReceived(this, request, response);
88         m_redirectChain.append(RedirectPair(request, response));
89     }
90     Resource::willSendRequest(request, response);
91 }
92 
responseReceived(const ResourceResponse & response)93 void RawResource::responseReceived(const ResourceResponse& response)
94 {
95     InternalResourcePtr protect(this);
96     Resource::responseReceived(response);
97     ResourceClientWalker<RawResourceClient> w(m_clients);
98     while (RawResourceClient* c = w.next())
99         c->responseReceived(this, m_response);
100 }
101 
didSendData(unsigned long long bytesSent,unsigned long long totalBytesToBeSent)102 void RawResource::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
103 {
104     ResourceClientWalker<RawResourceClient> w(m_clients);
105     while (RawResourceClient* c = w.next())
106         c->dataSent(this, bytesSent, totalBytesToBeSent);
107 }
108 
didDownloadData(int dataLength)109 void RawResource::didDownloadData(int dataLength)
110 {
111     ResourceClientWalker<RawResourceClient> w(m_clients);
112     while (RawResourceClient* c = w.next())
113         c->dataDownloaded(this, dataLength);
114 }
115 
setDefersLoading(bool defers)116 void RawResource::setDefersLoading(bool defers)
117 {
118     if (m_loader)
119         m_loader->setDefersLoading(defers);
120 }
121 
setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)122 void RawResource::setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)
123 {
124     m_options.dataBufferingPolicy = dataBufferingPolicy;
125     clear();
126 }
127 
shouldIgnoreHeaderForCacheReuse(AtomicString headerName)128 static bool shouldIgnoreHeaderForCacheReuse(AtomicString headerName)
129 {
130     // FIXME: This list of headers that don't affect cache policy almost certainly isn't complete.
131     DEFINE_STATIC_LOCAL(HashSet<AtomicString>, m_headers, ());
132     if (m_headers.isEmpty()) {
133         m_headers.add("Cache-Control");
134         m_headers.add("If-Modified-Since");
135         m_headers.add("If-None-Match");
136         m_headers.add("Origin");
137         m_headers.add("Pragma");
138         m_headers.add("Purpose");
139         m_headers.add("Referer");
140         m_headers.add("User-Agent");
141     }
142     return m_headers.contains(headerName);
143 }
144 
canReuse(const ResourceRequest & newRequest) const145 bool RawResource::canReuse(const ResourceRequest& newRequest) const
146 {
147     if (m_options.dataBufferingPolicy == DoNotBufferData)
148         return false;
149 
150     if (m_resourceRequest.httpMethod() != newRequest.httpMethod())
151         return false;
152 
153     if (m_resourceRequest.httpBody() != newRequest.httpBody())
154         return false;
155 
156     if (m_resourceRequest.allowCookies() != newRequest.allowCookies())
157         return false;
158 
159     // Ensure most headers match the existing headers before continuing.
160     // Note that the list of ignored headers includes some headers explicitly related to caching.
161     // A more detailed check of caching policy will be performed later, this is simply a list of
162     // headers that we might permit to be different and still reuse the existing Resource.
163     const HTTPHeaderMap& newHeaders = newRequest.httpHeaderFields();
164     const HTTPHeaderMap& oldHeaders = m_resourceRequest.httpHeaderFields();
165 
166     HTTPHeaderMap::const_iterator end = newHeaders.end();
167     for (HTTPHeaderMap::const_iterator i = newHeaders.begin(); i != end; ++i) {
168         AtomicString headerName = i->key;
169         if (!shouldIgnoreHeaderForCacheReuse(headerName) && i->value != oldHeaders.get(headerName))
170             return false;
171     }
172 
173     end = oldHeaders.end();
174     for (HTTPHeaderMap::const_iterator i = oldHeaders.begin(); i != end; ++i) {
175         AtomicString headerName = i->key;
176         if (!shouldIgnoreHeaderForCacheReuse(headerName) && i->value != newHeaders.get(headerName))
177             return false;
178     }
179 
180     for (size_t i = 0; i < m_redirectChain.size(); i++) {
181         if (m_redirectChain[i].m_redirectResponse.cacheControlContainsNoStore())
182             return false;
183     }
184 
185     return true;
186 }
187 
clear()188 void RawResource::clear()
189 {
190     m_data.clear();
191     setEncodedSize(0);
192 }
193 
194 } // namespace WebCore
195