2 * Copyright (C) 2006, 2008, 2016 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
27 #ifndef ResourceResponseBase_h
28 #define ResourceResponseBase_h
30 #include "CacheValidation.h"
31 #include "CertificateInfo.h"
32 #include "HTTPHeaderMap.h"
33 #include "ResourceLoadTiming.h"
37 #include <sys/time.h> // For time_t structure.
42 class ResourceResponse;
43 struct CrossThreadResourceResponseData;
45 // Do not use this class directly, use the class ResponseResponse instead
46 class ResourceResponseBase {
47 WTF_MAKE_FAST_ALLOCATED;
49 static std::unique_ptr<ResourceResponse> adopt(std::unique_ptr<CrossThreadResourceResponseData>);
51 // Gets a copy of the data suitable for passing to another thread.
52 std::unique_ptr<CrossThreadResourceResponseData> copyData() const;
54 bool isNull() const { return m_isNull; }
55 WEBCORE_EXPORT bool isHTTP() const;
57 WEBCORE_EXPORT const URL& url() const;
58 WEBCORE_EXPORT void setURL(const URL&);
60 WEBCORE_EXPORT const String& mimeType() const;
61 WEBCORE_EXPORT void setMimeType(const String& mimeType);
63 WEBCORE_EXPORT long long expectedContentLength() const;
64 WEBCORE_EXPORT void setExpectedContentLength(long long expectedContentLength);
66 WEBCORE_EXPORT const String& textEncodingName() const;
67 WEBCORE_EXPORT void setTextEncodingName(const String& name);
69 WEBCORE_EXPORT int httpStatusCode() const;
70 WEBCORE_EXPORT void setHTTPStatusCode(int);
72 WEBCORE_EXPORT const String& httpStatusText() const;
73 WEBCORE_EXPORT void setHTTPStatusText(const String&);
75 WEBCORE_EXPORT const String& httpVersion() const;
76 WEBCORE_EXPORT void setHTTPVersion(const String&);
77 bool isHttpVersion0_9() const;
79 WEBCORE_EXPORT const HTTPHeaderMap& httpHeaderFields() const;
81 String httpHeaderField(const String& name) const;
82 WEBCORE_EXPORT String httpHeaderField(HTTPHeaderName) const;
83 WEBCORE_EXPORT void setHTTPHeaderField(const String& name, const String& value);
84 void setHTTPHeaderField(HTTPHeaderName, const String& value);
86 void addHTTPHeaderField(const String& name, const String& value);
88 // Instead of passing a string literal to any of these functions, just use a HTTPHeaderName instead.
89 template<size_t length> String httpHeaderField(const char (&)[length]) const = delete;
90 template<size_t length> void setHTTPHeaderField(const char (&)[length], const String&) = delete;
91 template<size_t length> void addHTTPHeaderField(const char (&)[length], const String&) = delete;
93 bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
95 WEBCORE_EXPORT bool isAttachment() const;
96 WEBCORE_EXPORT String suggestedFilename() const;
98 WEBCORE_EXPORT void includeCertificateInfo() const;
99 bool containsCertificateInfo() const { return m_includesCertificateInfo; }
100 WEBCORE_EXPORT CertificateInfo certificateInfo() const;
102 // These functions return parsed values of the corresponding response headers.
103 // NaN means that the header was not present or had invalid value.
104 WEBCORE_EXPORT bool cacheControlContainsNoCache() const;
105 WEBCORE_EXPORT bool cacheControlContainsNoStore() const;
106 WEBCORE_EXPORT bool cacheControlContainsMustRevalidate() const;
107 WEBCORE_EXPORT bool hasCacheValidatorFields() const;
108 WEBCORE_EXPORT Optional<std::chrono::microseconds> cacheControlMaxAge() const;
109 WEBCORE_EXPORT Optional<std::chrono::system_clock::time_point> date() const;
110 WEBCORE_EXPORT Optional<std::chrono::microseconds> age() const;
111 WEBCORE_EXPORT Optional<std::chrono::system_clock::time_point> expires() const;
112 WEBCORE_EXPORT Optional<std::chrono::system_clock::time_point> lastModified() const;
114 // This is primarily for testing support. It is not necessarily accurate in all scenarios.
115 enum class Source { Unknown, Network, DiskCache, DiskCacheAfterValidation, MemoryCache, MemoryCacheAfterValidation };
116 WEBCORE_EXPORT Source source() const;
117 WEBCORE_EXPORT void setSource(Source);
119 ResourceLoadTiming& resourceLoadTiming() const { return m_resourceLoadTiming; }
121 // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
122 unsigned memoryUsage() const
124 // average size, mostly due to URL and Header Map strings
128 static bool compare(const ResourceResponse&, const ResourceResponse&);
130 template<class Encoder> void encode(Encoder&) const;
131 template<class Decoder> static bool decode(Decoder&, ResourceResponseBase&);
140 WEBCORE_EXPORT ResourceResponseBase();
141 WEBCORE_EXPORT ResourceResponseBase(const URL&, const String& mimeType, long long expectedLength, const String& textEncodingName);
143 WEBCORE_EXPORT void lazyInit(InitLevel) const;
145 // The ResourceResponse subclass should shadow these functions to lazily initialize platform specific fields
146 void platformLazyInit(InitLevel) { }
147 CertificateInfo platformCertificateInfo() const { return CertificateInfo(); };
148 String platformSuggestedFileName() const { return String(); }
150 static bool platformCompare(const ResourceResponse&, const ResourceResponse&) { return true; }
153 const ResourceResponse& asResourceResponse() const;
154 void parseCacheControlDirectives() const;
155 void updateHeaderParsedState(HTTPHeaderName);
160 AtomicString m_mimeType;
161 long long m_expectedContentLength;
162 AtomicString m_textEncodingName;
163 AtomicString m_httpStatusText;
164 AtomicString m_httpVersion;
165 HTTPHeaderMap m_httpHeaderFields;
166 mutable ResourceLoadTiming m_resourceLoadTiming;
168 mutable bool m_includesCertificateInfo;
169 mutable CertificateInfo m_certificateInfo;
171 int m_httpStatusCode;
174 mutable Optional<std::chrono::microseconds> m_age;
175 mutable Optional<std::chrono::system_clock::time_point> m_date;
176 mutable Optional<std::chrono::system_clock::time_point> m_expires;
177 mutable Optional<std::chrono::system_clock::time_point> m_lastModified;
178 mutable CacheControlDirectives m_cacheControlDirectives;
180 mutable bool m_haveParsedCacheControlHeader { false };
181 mutable bool m_haveParsedAgeHeader { false };
182 mutable bool m_haveParsedDateHeader { false };
183 mutable bool m_haveParsedExpiresHeader { false };
184 mutable bool m_haveParsedLastModifiedHeader { false };
186 Source m_source { Source::Unknown };
189 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponseBase::compare(a, b); }
190 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
192 template<class Encoder>
193 void ResourceResponseBase::encode(Encoder& encoder) const
200 encoder << m_url.string();
201 encoder << m_mimeType;
202 encoder << static_cast<int64_t>(m_expectedContentLength);
203 encoder << m_textEncodingName;
204 encoder << m_httpStatusText;
205 encoder << m_httpVersion;
206 encoder << m_httpHeaderFields;
207 encoder << m_resourceLoadTiming;
208 encoder << m_httpStatusCode;
209 encoder << m_includesCertificateInfo;
210 if (m_includesCertificateInfo)
211 encoder << m_certificateInfo;
212 encoder.encodeEnum(m_source);
215 template<class Decoder>
216 bool ResourceResponseBase::decode(Decoder& decoder, ResourceResponseBase& response)
218 ASSERT(response.m_isNull);
220 if (!decoder.decode(responseIsNull))
226 if (!decoder.decode(url))
228 response.m_url = URL(URL(), url);
229 if (!decoder.decode(response.m_mimeType))
231 int64_t expectedContentLength;
232 if (!decoder.decode(expectedContentLength))
234 response.m_expectedContentLength = expectedContentLength;
235 if (!decoder.decode(response.m_textEncodingName))
237 if (!decoder.decode(response.m_httpStatusText))
239 if (!decoder.decode(response.m_httpVersion))
241 if (!decoder.decode(response.m_httpHeaderFields))
243 if (!decoder.decode(response.m_resourceLoadTiming))
245 if (!decoder.decode(response.m_httpStatusCode))
247 if (!decoder.decode(response.m_includesCertificateInfo))
249 if (response.m_includesCertificateInfo) {
250 if (!decoder.decode(response.m_certificateInfo))
253 if (!decoder.decodeEnum(response.m_source))
255 response.m_isNull = false;
260 struct CrossThreadResourceResponseDataBase {
261 WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseDataBase); WTF_MAKE_FAST_ALLOCATED;
263 CrossThreadResourceResponseDataBase() { }
266 long long m_expectedContentLength;
267 String m_textEncodingName;
268 int m_httpStatusCode;
269 String m_httpStatusText;
270 String m_httpVersion;
271 std::unique_ptr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
272 ResourceLoadTiming m_resourceLoadTiming;
275 } // namespace WebCore
277 #endif // ResourceResponseBase_h