Home | History | Annotate | Download | only in network
      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 
     33 #include <memory>
     34 
     35 namespace WebCore {
     36 
     37 class ResourceResponse;
     38 struct CrossThreadResourceResponseData;
     39 
     40 // Do not use this class directly, use the class ResponseResponse instead
     41 class ResourceResponseBase : public FastAllocBase {
     42 public:
     43     static std::auto_ptr<ResourceResponse> adopt(std::auto_ptr<CrossThreadResourceResponseData>);
     44 
     45     // Gets a copy of the data suitable for passing to another thread.
     46     std::auto_ptr<CrossThreadResourceResponseData> copyData() const;
     47 
     48     bool isNull() const { return m_isNull; }
     49     bool isHTTP() const;
     50 
     51     const KURL& url() const;
     52     void setURL(const KURL& url);
     53 
     54     const String& mimeType() const;
     55     void setMimeType(const String& mimeType);
     56 
     57     long long expectedContentLength() const;
     58     void setExpectedContentLength(long long expectedContentLength);
     59 
     60     const String& textEncodingName() const;
     61     void setTextEncodingName(const String& name);
     62 
     63     // FIXME should compute this on the fly
     64     const String& suggestedFilename() const;
     65     void setSuggestedFilename(const String&);
     66 
     67     int httpStatusCode() const;
     68     void setHTTPStatusCode(int);
     69 
     70     const String& httpStatusText() const;
     71     void setHTTPStatusText(const String&);
     72 
     73     String httpHeaderField(const AtomicString& name) const;
     74     String httpHeaderField(const char* name) const;
     75     void setHTTPHeaderField(const AtomicString& name, const String& value);
     76     const HTTPHeaderMap& httpHeaderFields() const;
     77 
     78     bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
     79 
     80     bool isAttachment() const;
     81 
     82     // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-odified header.
     83     // Leaving it for now but this should go away in favor of generic solution.
     84     void setLastModifiedDate(time_t);
     85     time_t lastModifiedDate() const;
     86 
     87     // These functions return parsed values of the corresponding response headers.
     88     // NaN means that the header was not present or had invalid value.
     89     bool cacheControlContainsNoCache() const;
     90     bool cacheControlContainsNoStore() const;
     91     bool cacheControlContainsMustRevalidate() const;
     92     double cacheControlMaxAge() const;
     93     double date() const;
     94     double age() const;
     95     double expires() const;
     96     double lastModified() const;
     97 
     98     // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
     99     unsigned memoryUsage() const
    100     {
    101         // average size, mostly due to URL and Header Map strings
    102         return 1280;
    103     }
    104 
    105     static bool compare(const ResourceResponse& a, const ResourceResponse& b);
    106 
    107 protected:
    108     ResourceResponseBase();
    109     ResourceResponseBase(const KURL& url, const String& mimeType, long long expectedLength, const String& textEncodingName, const String& filename);
    110 
    111     void lazyInit() const;
    112 
    113     // The ResourceResponse subclass may "shadow" this method to lazily initialize platform specific fields
    114     void platformLazyInit() { }
    115 
    116     // The ResourceResponse subclass may "shadow" this method to compare platform specific fields
    117     static bool platformCompare(const ResourceResponse&, const ResourceResponse&) { return true; }
    118 
    119     KURL m_url;
    120     String m_mimeType;
    121     long long m_expectedContentLength;
    122     String m_textEncodingName;
    123     String m_suggestedFilename;
    124     int m_httpStatusCode;
    125     String m_httpStatusText;
    126     HTTPHeaderMap m_httpHeaderFields;
    127     time_t m_lastModifiedDate;
    128 
    129     bool m_isNull : 1;
    130 
    131 private:
    132     void parseCacheControlDirectives() const;
    133 
    134     mutable bool m_haveParsedCacheControlHeader : 1;
    135     mutable bool m_haveParsedAgeHeader : 1;
    136     mutable bool m_haveParsedDateHeader : 1;
    137     mutable bool m_haveParsedExpiresHeader : 1;
    138     mutable bool m_haveParsedLastModifiedHeader : 1;
    139 
    140     mutable bool m_cacheControlContainsNoCache : 1;
    141     mutable bool m_cacheControlContainsNoStore : 1;
    142     mutable bool m_cacheControlContainsMustRevalidate : 1;
    143     mutable double m_cacheControlMaxAge;
    144 
    145     mutable double m_age;
    146     mutable double m_date;
    147     mutable double m_expires;
    148     mutable double m_lastModified;
    149 };
    150 
    151 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponseBase::compare(a, b); }
    152 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
    153 
    154 struct CrossThreadResourceResponseData : Noncopyable {
    155     KURL m_url;
    156     String m_mimeType;
    157     long long m_expectedContentLength;
    158     String m_textEncodingName;
    159     String m_suggestedFilename;
    160     int m_httpStatusCode;
    161     String m_httpStatusText;
    162     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
    163     time_t m_lastModifiedDate;
    164 };
    165 
    166 } // namespace WebCore
    167 
    168 #endif // ResourceResponseBase_h
    169