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