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#ifndef ResourceResponseBase_h
28#define ResourceResponseBase_h
29
30#include "HTTPHeaderMap.h"
31#include "KURL.h"
32#include "ResourceLoadTiming.h"
33
34#include <wtf/PassOwnPtr.h>
35#include <wtf/RefPtr.h>
36
37#if OS(SOLARIS)
38#include <sys/time.h> // For time_t structure.
39#endif
40
41namespace WebCore {
42
43class ResourceResponse;
44struct CrossThreadResourceResponseData;
45
46// Do not use this class directly, use the class ResponseResponse instead
47class ResourceResponseBase {
48    WTF_MAKE_FAST_ALLOCATED;
49public:
50    static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
51
52    // Gets a copy of the data suitable for passing to another thread.
53    PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
54
55    bool isNull() const { return m_isNull; }
56    bool isHTTP() const;
57
58    const KURL& url() const;
59    void setURL(const KURL& url);
60
61    const String& mimeType() const;
62    void setMimeType(const String& mimeType);
63
64    long long expectedContentLength() const;
65    void setExpectedContentLength(long long expectedContentLength);
66
67    const String& textEncodingName() const;
68    void setTextEncodingName(const String& name);
69
70    // FIXME: Should compute this on the fly.
71    // There should not be a setter exposed, as suggested file name is determined based on other headers in a manner that WebCore does not necessarily know about.
72    const String& suggestedFilename() const;
73    void setSuggestedFilename(const String&);
74
75    int httpStatusCode() const;
76    void setHTTPStatusCode(int);
77
78    const String& httpStatusText() const;
79    void setHTTPStatusText(const String&);
80
81    String httpHeaderField(const AtomicString& name) const;
82    String httpHeaderField(const char* name) const;
83    void setHTTPHeaderField(const AtomicString& name, const String& value);
84    void addHTTPHeaderField(const AtomicString& name, const String& value);
85    const HTTPHeaderMap& httpHeaderFields() const;
86
87    bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
88
89    bool isAttachment() const;
90
91    // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-Modified header.
92    // Leaving it for now but this should go away in favor of generic solution.
93    void setLastModifiedDate(time_t);
94    time_t lastModifiedDate() const;
95
96    // These functions return parsed values of the corresponding response headers.
97    // NaN means that the header was not present or had invalid value.
98    bool cacheControlContainsNoCache() const;
99    bool cacheControlContainsNoStore() const;
100    bool cacheControlContainsMustRevalidate() const;
101    bool hasCacheValidatorFields() const;
102    double cacheControlMaxAge() const;
103    double date() const;
104    double age() const;
105    double expires() const;
106    double lastModified() const;
107
108    unsigned connectionID() const;
109    void setConnectionID(unsigned);
110
111    bool connectionReused() const;
112    void setConnectionReused(bool);
113
114    bool wasCached() const;
115    void setWasCached(bool);
116
117    ResourceLoadTiming* resourceLoadTiming() const;
118    void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
119
120    // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
121    unsigned memoryUsage() const
122    {
123        // average size, mostly due to URL and Header Map strings
124        return 1280;
125    }
126
127    static bool compare(const ResourceResponse&, const ResourceResponse&);
128
129protected:
130    enum InitLevel {
131        Uninitialized,
132        CommonFieldsOnly,
133        CommonAndUncommonFields,
134        AllFields
135    };
136
137    ResourceResponseBase();
138    ResourceResponseBase(const KURL& url, const String& mimeType, long long expectedLength, const String& textEncodingName, const String& filename);
139
140    void lazyInit(InitLevel) const;
141
142    // The ResourceResponse subclass may "shadow" this method to lazily initialize platform specific fields
143    void platformLazyInit(InitLevel) { }
144
145    // The ResourceResponse subclass may "shadow" this method to compare platform specific fields
146    static bool platformCompare(const ResourceResponse&, const ResourceResponse&) { return true; }
147
148    KURL m_url;
149    String m_mimeType;
150    long long m_expectedContentLength;
151    String m_textEncodingName;
152    String m_suggestedFilename;
153    int m_httpStatusCode;
154    String m_httpStatusText;
155    HTTPHeaderMap m_httpHeaderFields;
156    time_t m_lastModifiedDate;
157    bool m_wasCached : 1;
158    unsigned m_connectionID;
159    bool m_connectionReused : 1;
160    RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
161
162    bool m_isNull : 1;
163
164private:
165    const ResourceResponse& asResourceResponse() const;
166    void parseCacheControlDirectives() const;
167    void updateHeaderParsedState(const AtomicString& name);
168
169    mutable bool m_haveParsedCacheControlHeader : 1;
170    mutable bool m_haveParsedAgeHeader : 1;
171    mutable bool m_haveParsedDateHeader : 1;
172    mutable bool m_haveParsedExpiresHeader : 1;
173    mutable bool m_haveParsedLastModifiedHeader : 1;
174
175    mutable bool m_cacheControlContainsNoCache : 1;
176    mutable bool m_cacheControlContainsNoStore : 1;
177    mutable bool m_cacheControlContainsMustRevalidate : 1;
178    mutable double m_cacheControlMaxAge;
179
180    mutable double m_age;
181    mutable double m_date;
182    mutable double m_expires;
183    mutable double m_lastModified;
184};
185
186inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponseBase::compare(a, b); }
187inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
188
189struct CrossThreadResourceResponseDataBase {
190    WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseDataBase); WTF_MAKE_FAST_ALLOCATED;
191public:
192    CrossThreadResourceResponseDataBase() { }
193    KURL m_url;
194    String m_mimeType;
195    long long m_expectedContentLength;
196    String m_textEncodingName;
197    String m_suggestedFilename;
198    int m_httpStatusCode;
199    String m_httpStatusText;
200    OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
201    time_t m_lastModifiedDate;
202    RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
203};
204
205} // namespace WebCore
206
207#endif // ResourceResponseBase_h
208