1 /*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "platform/network/ResourceResponse.h"
29
30 #include "platform/network/HTTPParsers.h"
31 #include "wtf/CurrentTime.h"
32 #include "wtf/MathExtras.h"
33 #include "wtf/StdLibExtras.h"
34
35 namespace WebCore {
36
37 static void parseCacheHeader(const String& header, Vector<pair<String, String> >& result);
38
ResourceResponse()39 ResourceResponse::ResourceResponse()
40 : m_expectedContentLength(0)
41 , m_httpStatusCode(0)
42 , m_lastModifiedDate(0)
43 , m_wasCached(false)
44 , m_connectionID(0)
45 , m_connectionReused(false)
46 , m_isNull(true)
47 , m_haveParsedCacheControlHeader(false)
48 , m_haveParsedAgeHeader(false)
49 , m_haveParsedDateHeader(false)
50 , m_haveParsedExpiresHeader(false)
51 , m_haveParsedLastModifiedHeader(false)
52 , m_cacheControlContainsNoCache(false)
53 , m_cacheControlContainsNoStore(false)
54 , m_cacheControlContainsMustRevalidate(false)
55 , m_cacheControlMaxAge(0.0)
56 , m_age(0.0)
57 , m_date(0.0)
58 , m_expires(0.0)
59 , m_lastModified(0.0)
60 , m_httpVersion(Unknown)
61 , m_appCacheID(0)
62 , m_isMultipartPayload(false)
63 , m_wasFetchedViaSPDY(false)
64 , m_wasNpnNegotiated(false)
65 , m_wasAlternateProtocolAvailable(false)
66 , m_wasFetchedViaProxy(false)
67 , m_responseTime(0)
68 , m_remotePort(0)
69 {
70 }
71
ResourceResponse(const KURL & url,const AtomicString & mimeType,long long expectedLength,const AtomicString & textEncodingName,const String & filename)72 ResourceResponse::ResourceResponse(const KURL& url, const AtomicString& mimeType, long long expectedLength, const AtomicString& textEncodingName, const String& filename)
73 : m_url(url)
74 , m_mimeType(mimeType)
75 , m_expectedContentLength(expectedLength)
76 , m_textEncodingName(textEncodingName)
77 , m_suggestedFilename(filename)
78 , m_httpStatusCode(0)
79 , m_lastModifiedDate(0)
80 , m_wasCached(false)
81 , m_connectionID(0)
82 , m_connectionReused(false)
83 , m_isNull(false)
84 , m_haveParsedCacheControlHeader(false)
85 , m_haveParsedAgeHeader(false)
86 , m_haveParsedDateHeader(false)
87 , m_haveParsedExpiresHeader(false)
88 , m_haveParsedLastModifiedHeader(false)
89 , m_cacheControlContainsNoCache(false)
90 , m_cacheControlContainsNoStore(false)
91 , m_cacheControlContainsMustRevalidate(false)
92 , m_cacheControlMaxAge(0.0)
93 , m_age(0.0)
94 , m_date(0.0)
95 , m_expires(0.0)
96 , m_lastModified(0.0)
97 , m_httpVersion(Unknown)
98 , m_appCacheID(0)
99 , m_isMultipartPayload(false)
100 , m_wasFetchedViaSPDY(false)
101 , m_wasNpnNegotiated(false)
102 , m_wasAlternateProtocolAvailable(false)
103 , m_wasFetchedViaProxy(false)
104 , m_responseTime(0)
105 , m_remotePort(0)
106 {
107 }
108
adopt(PassOwnPtr<CrossThreadResourceResponseData> data)109 PassOwnPtr<ResourceResponse> ResourceResponse::adopt(PassOwnPtr<CrossThreadResourceResponseData> data)
110 {
111 OwnPtr<ResourceResponse> response = adoptPtr(new ResourceResponse);
112 response->setURL(data->m_url);
113 response->setMimeType(AtomicString(data->m_mimeType));
114 response->setExpectedContentLength(data->m_expectedContentLength);
115 response->setTextEncodingName(AtomicString(data->m_textEncodingName));
116 response->setSuggestedFilename(data->m_suggestedFilename);
117
118 response->setHTTPStatusCode(data->m_httpStatusCode);
119 response->setHTTPStatusText(AtomicString(data->m_httpStatusText));
120
121 response->m_httpHeaderFields.adopt(data->m_httpHeaders.release());
122 response->setLastModifiedDate(data->m_lastModifiedDate);
123 response->setResourceLoadTiming(data->m_resourceLoadTiming.release());
124 response->m_securityInfo = data->m_securityInfo;
125 response->m_httpVersion = data->m_httpVersion;
126 response->m_appCacheID = data->m_appCacheID;
127 response->m_appCacheManifestURL = data->m_appCacheManifestURL.copy();
128 response->m_isMultipartPayload = data->m_isMultipartPayload;
129 response->m_wasFetchedViaSPDY = data->m_wasFetchedViaSPDY;
130 response->m_wasNpnNegotiated = data->m_wasNpnNegotiated;
131 response->m_wasAlternateProtocolAvailable = data->m_wasAlternateProtocolAvailable;
132 response->m_wasFetchedViaProxy = data->m_wasFetchedViaProxy;
133 response->m_responseTime = data->m_responseTime;
134 response->m_remoteIPAddress = AtomicString(data->m_remoteIPAddress);
135 response->m_remotePort = data->m_remotePort;
136 response->m_downloadedFilePath = data->m_downloadedFilePath;
137 response->m_downloadedFileHandle = data->m_downloadedFileHandle;
138
139 // Bug https://bugs.webkit.org/show_bug.cgi?id=60397 this doesn't support
140 // whatever values may be present in the opaque m_extraData structure.
141
142 return response.release();
143 }
144
copyData() const145 PassOwnPtr<CrossThreadResourceResponseData> ResourceResponse::copyData() const
146 {
147 OwnPtr<CrossThreadResourceResponseData> data = adoptPtr(new CrossThreadResourceResponseData);
148 data->m_url = url().copy();
149 data->m_mimeType = mimeType().string().isolatedCopy();
150 data->m_expectedContentLength = expectedContentLength();
151 data->m_textEncodingName = textEncodingName().string().isolatedCopy();
152 data->m_suggestedFilename = suggestedFilename().isolatedCopy();
153 data->m_httpStatusCode = httpStatusCode();
154 data->m_httpStatusText = httpStatusText().string().isolatedCopy();
155 data->m_httpHeaders = httpHeaderFields().copyData();
156 data->m_lastModifiedDate = lastModifiedDate();
157 if (m_resourceLoadTiming)
158 data->m_resourceLoadTiming = m_resourceLoadTiming->deepCopy();
159 data->m_securityInfo = CString(m_securityInfo.data(), m_securityInfo.length());
160 data->m_httpVersion = m_httpVersion;
161 data->m_appCacheID = m_appCacheID;
162 data->m_appCacheManifestURL = m_appCacheManifestURL.copy();
163 data->m_isMultipartPayload = m_isMultipartPayload;
164 data->m_wasFetchedViaSPDY = m_wasFetchedViaSPDY;
165 data->m_wasNpnNegotiated = m_wasNpnNegotiated;
166 data->m_wasAlternateProtocolAvailable = m_wasAlternateProtocolAvailable;
167 data->m_wasFetchedViaProxy = m_wasFetchedViaProxy;
168 data->m_responseTime = m_responseTime;
169 data->m_remoteIPAddress = m_remoteIPAddress.string().isolatedCopy();
170 data->m_remotePort = m_remotePort;
171 data->m_downloadedFilePath = m_downloadedFilePath.isolatedCopy();
172 data->m_downloadedFileHandle = m_downloadedFileHandle;
173
174 // Bug https://bugs.webkit.org/show_bug.cgi?id=60397 this doesn't support
175 // whatever values may be present in the opaque m_extraData structure.
176
177 return data.release();
178 }
179
isHTTP() const180 bool ResourceResponse::isHTTP() const
181 {
182 return m_url.protocolIsInHTTPFamily();
183 }
184
url() const185 const KURL& ResourceResponse::url() const
186 {
187 return m_url;
188 }
189
setURL(const KURL & url)190 void ResourceResponse::setURL(const KURL& url)
191 {
192 m_isNull = false;
193
194 m_url = url;
195 }
196
mimeType() const197 const AtomicString& ResourceResponse::mimeType() const
198 {
199 return m_mimeType;
200 }
201
setMimeType(const AtomicString & mimeType)202 void ResourceResponse::setMimeType(const AtomicString& mimeType)
203 {
204 m_isNull = false;
205
206 // FIXME: MIME type is determined by HTTP Content-Type header. We should update the header, so that it doesn't disagree with m_mimeType.
207 m_mimeType = mimeType;
208 }
209
expectedContentLength() const210 long long ResourceResponse::expectedContentLength() const
211 {
212 return m_expectedContentLength;
213 }
214
setExpectedContentLength(long long expectedContentLength)215 void ResourceResponse::setExpectedContentLength(long long expectedContentLength)
216 {
217 m_isNull = false;
218
219 // FIXME: Content length is determined by HTTP Content-Length header. We should update the header, so that it doesn't disagree with m_expectedContentLength.
220 m_expectedContentLength = expectedContentLength;
221 }
222
textEncodingName() const223 const AtomicString& ResourceResponse::textEncodingName() const
224 {
225 return m_textEncodingName;
226 }
227
setTextEncodingName(const AtomicString & encodingName)228 void ResourceResponse::setTextEncodingName(const AtomicString& encodingName)
229 {
230 m_isNull = false;
231
232 // FIXME: Text encoding is determined by HTTP Content-Type header. We should update the header, so that it doesn't disagree with m_textEncodingName.
233 m_textEncodingName = encodingName;
234 }
235
236 // FIXME should compute this on the fly
suggestedFilename() const237 const String& ResourceResponse::suggestedFilename() const
238 {
239 return m_suggestedFilename;
240 }
241
setSuggestedFilename(const String & suggestedName)242 void ResourceResponse::setSuggestedFilename(const String& suggestedName)
243 {
244 m_isNull = false;
245
246 // FIXME: Suggested file name is calculated based on other headers. There should not be a setter for it.
247 m_suggestedFilename = suggestedName;
248 }
249
httpStatusCode() const250 int ResourceResponse::httpStatusCode() const
251 {
252 return m_httpStatusCode;
253 }
254
setHTTPStatusCode(int statusCode)255 void ResourceResponse::setHTTPStatusCode(int statusCode)
256 {
257 m_httpStatusCode = statusCode;
258 }
259
httpStatusText() const260 const AtomicString& ResourceResponse::httpStatusText() const
261 {
262 return m_httpStatusText;
263 }
264
setHTTPStatusText(const AtomicString & statusText)265 void ResourceResponse::setHTTPStatusText(const AtomicString& statusText)
266 {
267 m_httpStatusText = statusText;
268 }
269
httpHeaderField(const AtomicString & name) const270 const AtomicString& ResourceResponse::httpHeaderField(const AtomicString& name) const
271 {
272 return m_httpHeaderFields.get(name);
273 }
274
httpHeaderField(const char * name) const275 const AtomicString& ResourceResponse::httpHeaderField(const char* name) const
276 {
277 return m_httpHeaderFields.get(name);
278 }
279
updateHeaderParsedState(const AtomicString & name)280 void ResourceResponse::updateHeaderParsedState(const AtomicString& name)
281 {
282 DEFINE_STATIC_LOCAL(const AtomicString, ageHeader, ("age", AtomicString::ConstructFromLiteral));
283 DEFINE_STATIC_LOCAL(const AtomicString, cacheControlHeader, ("cache-control", AtomicString::ConstructFromLiteral));
284 DEFINE_STATIC_LOCAL(const AtomicString, dateHeader, ("date", AtomicString::ConstructFromLiteral));
285 DEFINE_STATIC_LOCAL(const AtomicString, expiresHeader, ("expires", AtomicString::ConstructFromLiteral));
286 DEFINE_STATIC_LOCAL(const AtomicString, lastModifiedHeader, ("last-modified", AtomicString::ConstructFromLiteral));
287 DEFINE_STATIC_LOCAL(const AtomicString, pragmaHeader, ("pragma", AtomicString::ConstructFromLiteral));
288
289 if (equalIgnoringCase(name, ageHeader))
290 m_haveParsedAgeHeader = false;
291 else if (equalIgnoringCase(name, cacheControlHeader) || equalIgnoringCase(name, pragmaHeader))
292 m_haveParsedCacheControlHeader = false;
293 else if (equalIgnoringCase(name, dateHeader))
294 m_haveParsedDateHeader = false;
295 else if (equalIgnoringCase(name, expiresHeader))
296 m_haveParsedExpiresHeader = false;
297 else if (equalIgnoringCase(name, lastModifiedHeader))
298 m_haveParsedLastModifiedHeader = false;
299 }
300
setHTTPHeaderField(const AtomicString & name,const AtomicString & value)301 void ResourceResponse::setHTTPHeaderField(const AtomicString& name, const AtomicString& value)
302 {
303 updateHeaderParsedState(name);
304
305 m_httpHeaderFields.set(name, value);
306 }
307
addHTTPHeaderField(const AtomicString & name,const AtomicString & value)308 void ResourceResponse::addHTTPHeaderField(const AtomicString& name, const AtomicString& value)
309 {
310 updateHeaderParsedState(name);
311
312 HTTPHeaderMap::AddResult result = m_httpHeaderFields.add(name, value);
313 if (!result.isNewEntry)
314 result.iterator->value = result.iterator->value + ", " + value;
315 }
316
clearHTTPHeaderField(const AtomicString & name)317 void ResourceResponse::clearHTTPHeaderField(const AtomicString& name)
318 {
319 m_httpHeaderFields.remove(name);
320 }
321
httpHeaderFields() const322 const HTTPHeaderMap& ResourceResponse::httpHeaderFields() const
323 {
324 return m_httpHeaderFields;
325 }
326
parseCacheControlDirectives() const327 void ResourceResponse::parseCacheControlDirectives() const
328 {
329 ASSERT(!m_haveParsedCacheControlHeader);
330
331 m_haveParsedCacheControlHeader = true;
332
333 m_cacheControlContainsMustRevalidate = false;
334 m_cacheControlContainsNoCache = false;
335 m_cacheControlMaxAge = std::numeric_limits<double>::quiet_NaN();
336
337 DEFINE_STATIC_LOCAL(const AtomicString, cacheControlString, ("cache-control", AtomicString::ConstructFromLiteral));
338 DEFINE_STATIC_LOCAL(const AtomicString, noCacheDirective, ("no-cache", AtomicString::ConstructFromLiteral));
339 DEFINE_STATIC_LOCAL(const AtomicString, noStoreDirective, ("no-store", AtomicString::ConstructFromLiteral));
340 DEFINE_STATIC_LOCAL(const AtomicString, mustRevalidateDirective, ("must-revalidate", AtomicString::ConstructFromLiteral));
341 DEFINE_STATIC_LOCAL(const AtomicString, maxAgeDirective, ("max-age", AtomicString::ConstructFromLiteral));
342
343 const AtomicString& cacheControlValue = m_httpHeaderFields.get(cacheControlString);
344 if (!cacheControlValue.isEmpty()) {
345 Vector<pair<String, String> > directives;
346 parseCacheHeader(cacheControlValue, directives);
347
348 size_t directivesSize = directives.size();
349 for (size_t i = 0; i < directivesSize; ++i) {
350 // RFC2616 14.9.1: A no-cache directive with a value is only meaningful for proxy caches.
351 // It should be ignored by a browser level cache.
352 if (equalIgnoringCase(directives[i].first, noCacheDirective) && directives[i].second.isEmpty())
353 m_cacheControlContainsNoCache = true;
354 else if (equalIgnoringCase(directives[i].first, noStoreDirective))
355 m_cacheControlContainsNoStore = true;
356 else if (equalIgnoringCase(directives[i].first, mustRevalidateDirective))
357 m_cacheControlContainsMustRevalidate = true;
358 else if (equalIgnoringCase(directives[i].first, maxAgeDirective)) {
359 if (!std::isnan(m_cacheControlMaxAge)) {
360 // First max-age directive wins if there are multiple ones.
361 continue;
362 }
363 bool ok;
364 double maxAge = directives[i].second.toDouble(&ok);
365 if (ok)
366 m_cacheControlMaxAge = maxAge;
367 }
368 }
369 }
370
371 if (!m_cacheControlContainsNoCache) {
372 // Handle Pragma: no-cache
373 // This is deprecated and equivalent to Cache-control: no-cache
374 // Don't bother tokenizing the value, it is not important
375 DEFINE_STATIC_LOCAL(const AtomicString, pragmaHeader, ("pragma", AtomicString::ConstructFromLiteral));
376 const AtomicString& pragmaValue = m_httpHeaderFields.get(pragmaHeader);
377
378 m_cacheControlContainsNoCache = pragmaValue.lower().contains(noCacheDirective);
379 }
380 }
381
cacheControlContainsNoCache() const382 bool ResourceResponse::cacheControlContainsNoCache() const
383 {
384 if (!m_haveParsedCacheControlHeader)
385 parseCacheControlDirectives();
386 return m_cacheControlContainsNoCache;
387 }
388
cacheControlContainsNoStore() const389 bool ResourceResponse::cacheControlContainsNoStore() const
390 {
391 if (!m_haveParsedCacheControlHeader)
392 parseCacheControlDirectives();
393 return m_cacheControlContainsNoStore;
394 }
395
cacheControlContainsMustRevalidate() const396 bool ResourceResponse::cacheControlContainsMustRevalidate() const
397 {
398 if (!m_haveParsedCacheControlHeader)
399 parseCacheControlDirectives();
400 return m_cacheControlContainsMustRevalidate;
401 }
402
hasCacheValidatorFields() const403 bool ResourceResponse::hasCacheValidatorFields() const
404 {
405 DEFINE_STATIC_LOCAL(const AtomicString, lastModifiedHeader, ("last-modified", AtomicString::ConstructFromLiteral));
406 DEFINE_STATIC_LOCAL(const AtomicString, eTagHeader, ("etag", AtomicString::ConstructFromLiteral));
407 return !m_httpHeaderFields.get(lastModifiedHeader).isEmpty() || !m_httpHeaderFields.get(eTagHeader).isEmpty();
408 }
409
cacheControlMaxAge() const410 double ResourceResponse::cacheControlMaxAge() const
411 {
412 if (!m_haveParsedCacheControlHeader)
413 parseCacheControlDirectives();
414 return m_cacheControlMaxAge;
415 }
416
parseDateValueInHeader(const HTTPHeaderMap & headers,const AtomicString & headerName)417 static double parseDateValueInHeader(const HTTPHeaderMap& headers, const AtomicString& headerName)
418 {
419 const AtomicString& headerValue = headers.get(headerName);
420 if (headerValue.isEmpty())
421 return std::numeric_limits<double>::quiet_NaN();
422 // This handles all date formats required by RFC2616:
423 // Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
424 // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
425 // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
426 double dateInMilliseconds = parseDate(headerValue);
427 if (!std::isfinite(dateInMilliseconds))
428 return std::numeric_limits<double>::quiet_NaN();
429 return dateInMilliseconds / 1000;
430 }
431
date() const432 double ResourceResponse::date() const
433 {
434 if (!m_haveParsedDateHeader) {
435 DEFINE_STATIC_LOCAL(const AtomicString, headerName, ("date", AtomicString::ConstructFromLiteral));
436 m_date = parseDateValueInHeader(m_httpHeaderFields, headerName);
437 m_haveParsedDateHeader = true;
438 }
439 return m_date;
440 }
441
age() const442 double ResourceResponse::age() const
443 {
444 if (!m_haveParsedAgeHeader) {
445 DEFINE_STATIC_LOCAL(const AtomicString, headerName, ("age", AtomicString::ConstructFromLiteral));
446 const AtomicString& headerValue = m_httpHeaderFields.get(headerName);
447 bool ok;
448 m_age = headerValue.toDouble(&ok);
449 if (!ok)
450 m_age = std::numeric_limits<double>::quiet_NaN();
451 m_haveParsedAgeHeader = true;
452 }
453 return m_age;
454 }
455
expires() const456 double ResourceResponse::expires() const
457 {
458 if (!m_haveParsedExpiresHeader) {
459 DEFINE_STATIC_LOCAL(const AtomicString, headerName, ("expires", AtomicString::ConstructFromLiteral));
460 m_expires = parseDateValueInHeader(m_httpHeaderFields, headerName);
461 m_haveParsedExpiresHeader = true;
462 }
463 return m_expires;
464 }
465
lastModified() const466 double ResourceResponse::lastModified() const
467 {
468 if (!m_haveParsedLastModifiedHeader) {
469 DEFINE_STATIC_LOCAL(const AtomicString, headerName, ("last-modified", AtomicString::ConstructFromLiteral));
470 m_lastModified = parseDateValueInHeader(m_httpHeaderFields, headerName);
471 m_haveParsedLastModifiedHeader = true;
472 }
473 return m_lastModified;
474 }
475
isAttachment() const476 bool ResourceResponse::isAttachment() const
477 {
478 DEFINE_STATIC_LOCAL(const AtomicString, headerName, ("content-disposition", AtomicString::ConstructFromLiteral));
479 String value = m_httpHeaderFields.get(headerName);
480 size_t loc = value.find(';');
481 if (loc != kNotFound)
482 value = value.left(loc);
483 value = value.stripWhiteSpace();
484 DEFINE_STATIC_LOCAL(const AtomicString, attachmentString, ("attachment", AtomicString::ConstructFromLiteral));
485 return equalIgnoringCase(value, attachmentString);
486 }
487
setLastModifiedDate(time_t lastModifiedDate)488 void ResourceResponse::setLastModifiedDate(time_t lastModifiedDate)
489 {
490 m_lastModifiedDate = lastModifiedDate;
491 }
492
lastModifiedDate() const493 time_t ResourceResponse::lastModifiedDate() const
494 {
495 return m_lastModifiedDate;
496 }
497
wasCached() const498 bool ResourceResponse::wasCached() const
499 {
500 return m_wasCached;
501 }
502
setWasCached(bool value)503 void ResourceResponse::setWasCached(bool value)
504 {
505 m_wasCached = value;
506 }
507
connectionReused() const508 bool ResourceResponse::connectionReused() const
509 {
510 return m_connectionReused;
511 }
512
setConnectionReused(bool connectionReused)513 void ResourceResponse::setConnectionReused(bool connectionReused)
514 {
515 m_connectionReused = connectionReused;
516 }
517
connectionID() const518 unsigned ResourceResponse::connectionID() const
519 {
520 return m_connectionID;
521 }
522
setConnectionID(unsigned connectionID)523 void ResourceResponse::setConnectionID(unsigned connectionID)
524 {
525 m_connectionID = connectionID;
526 }
527
resourceLoadTiming() const528 ResourceLoadTiming* ResourceResponse::resourceLoadTiming() const
529 {
530 return m_resourceLoadTiming.get();
531 }
532
setResourceLoadTiming(PassRefPtr<ResourceLoadTiming> resourceLoadTiming)533 void ResourceResponse::setResourceLoadTiming(PassRefPtr<ResourceLoadTiming> resourceLoadTiming)
534 {
535 m_resourceLoadTiming = resourceLoadTiming;
536 }
537
resourceLoadInfo() const538 PassRefPtr<ResourceLoadInfo> ResourceResponse::resourceLoadInfo() const
539 {
540 return m_resourceLoadInfo.get();
541 }
542
setResourceLoadInfo(PassRefPtr<ResourceLoadInfo> loadInfo)543 void ResourceResponse::setResourceLoadInfo(PassRefPtr<ResourceLoadInfo> loadInfo)
544 {
545 m_resourceLoadInfo = loadInfo;
546 }
547
setDownloadedFilePath(const String & downloadedFilePath)548 void ResourceResponse::setDownloadedFilePath(const String& downloadedFilePath)
549 {
550 m_downloadedFilePath = downloadedFilePath;
551 if (m_downloadedFilePath.isEmpty()) {
552 m_downloadedFileHandle.clear();
553 return;
554 }
555 OwnPtr<BlobData> blobData = BlobData::create();
556 blobData->appendFile(m_downloadedFilePath);
557 blobData->detachFromCurrentThread();
558 m_downloadedFileHandle = BlobDataHandle::create(blobData.release(), -1);
559 }
560
compare(const ResourceResponse & a,const ResourceResponse & b)561 bool ResourceResponse::compare(const ResourceResponse& a, const ResourceResponse& b)
562 {
563 if (a.isNull() != b.isNull())
564 return false;
565 if (a.url() != b.url())
566 return false;
567 if (a.mimeType() != b.mimeType())
568 return false;
569 if (a.expectedContentLength() != b.expectedContentLength())
570 return false;
571 if (a.textEncodingName() != b.textEncodingName())
572 return false;
573 if (a.suggestedFilename() != b.suggestedFilename())
574 return false;
575 if (a.httpStatusCode() != b.httpStatusCode())
576 return false;
577 if (a.httpStatusText() != b.httpStatusText())
578 return false;
579 if (a.httpHeaderFields() != b.httpHeaderFields())
580 return false;
581 if (a.resourceLoadTiming() && b.resourceLoadTiming() && *a.resourceLoadTiming() == *b.resourceLoadTiming())
582 return true;
583 if (a.resourceLoadTiming() != b.resourceLoadTiming())
584 return false;
585 return true;
586 }
587
isCacheHeaderSeparator(UChar c)588 static bool isCacheHeaderSeparator(UChar c)
589 {
590 // See RFC 2616, Section 2.2
591 switch (c) {
592 case '(':
593 case ')':
594 case '<':
595 case '>':
596 case '@':
597 case ',':
598 case ';':
599 case ':':
600 case '\\':
601 case '"':
602 case '/':
603 case '[':
604 case ']':
605 case '?':
606 case '=':
607 case '{':
608 case '}':
609 case ' ':
610 case '\t':
611 return true;
612 default:
613 return false;
614 }
615 }
616
isControlCharacter(UChar c)617 static bool isControlCharacter(UChar c)
618 {
619 return c < ' ' || c == 127;
620 }
621
trimToNextSeparator(const String & str)622 static inline String trimToNextSeparator(const String& str)
623 {
624 return str.substring(0, str.find(isCacheHeaderSeparator));
625 }
626
parseCacheHeader(const String & header,Vector<pair<String,String>> & result)627 static void parseCacheHeader(const String& header, Vector<pair<String, String> >& result)
628 {
629 const String safeHeader = header.removeCharacters(isControlCharacter);
630 unsigned max = safeHeader.length();
631 for (unsigned pos = 0; pos < max; /* pos incremented in loop */) {
632 size_t nextCommaPosition = safeHeader.find(',', pos);
633 size_t nextEqualSignPosition = safeHeader.find('=', pos);
634 if (nextEqualSignPosition != kNotFound && (nextEqualSignPosition < nextCommaPosition || nextCommaPosition == kNotFound)) {
635 // Get directive name, parse right hand side of equal sign, then add to map
636 String directive = trimToNextSeparator(safeHeader.substring(pos, nextEqualSignPosition - pos).stripWhiteSpace());
637 pos += nextEqualSignPosition - pos + 1;
638
639 String value = safeHeader.substring(pos, max - pos).stripWhiteSpace();
640 if (value[0] == '"') {
641 // The value is a quoted string
642 size_t nextDoubleQuotePosition = value.find('"', 1);
643 if (nextDoubleQuotePosition != kNotFound) {
644 // Store the value as a quoted string without quotes
645 result.append(pair<String, String>(directive, value.substring(1, nextDoubleQuotePosition - 1).stripWhiteSpace()));
646 pos += (safeHeader.find('"', pos) - pos) + nextDoubleQuotePosition + 1;
647 // Move past next comma, if there is one
648 size_t nextCommaPosition2 = safeHeader.find(',', pos);
649 if (nextCommaPosition2 != kNotFound)
650 pos += nextCommaPosition2 - pos + 1;
651 else
652 return; // Parse error if there is anything left with no comma
653 } else {
654 // Parse error; just use the rest as the value
655 result.append(pair<String, String>(directive, trimToNextSeparator(value.substring(1, value.length() - 1).stripWhiteSpace())));
656 return;
657 }
658 } else {
659 // The value is a token until the next comma
660 size_t nextCommaPosition2 = value.find(',');
661 if (nextCommaPosition2 != kNotFound) {
662 // The value is delimited by the next comma
663 result.append(pair<String, String>(directive, trimToNextSeparator(value.substring(0, nextCommaPosition2).stripWhiteSpace())));
664 pos += (safeHeader.find(',', pos) - pos) + 1;
665 } else {
666 // The rest is the value; no change to value needed
667 result.append(pair<String, String>(directive, trimToNextSeparator(value)));
668 return;
669 }
670 }
671 } else if (nextCommaPosition != kNotFound && (nextCommaPosition < nextEqualSignPosition || nextEqualSignPosition == kNotFound)) {
672 // Add directive to map with empty string as value
673 result.append(pair<String, String>(trimToNextSeparator(safeHeader.substring(pos, nextCommaPosition - pos).stripWhiteSpace()), ""));
674 pos += nextCommaPosition - pos + 1;
675 } else {
676 // Add last directive to map with empty string as value
677 result.append(pair<String, String>(trimToNextSeparator(safeHeader.substring(pos, max - pos).stripWhiteSpace()), ""));
678 return;
679 }
680 }
681 }
682
683 }
684