• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
4  * Copyright (C) 2009 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ResourceRequestBase_h
29 #define ResourceRequestBase_h
30 
31 #include "FormData.h"
32 #include "KURL.h"
33 #include "HTTPHeaderMap.h"
34 
35 #include <memory>
36 #include <wtf/OwnPtr.h>
37 
38 namespace WebCore {
39 
40     enum ResourceRequestCachePolicy {
41         UseProtocolCachePolicy, // normal load
42         ReloadIgnoringCacheData, // reload
43         ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
44         ReturnCacheDataDontLoad, // results of a post - allow stale data and only use cache
45     };
46 
47     const int unspecifiedTimeoutInterval = INT_MAX;
48 
49     class ResourceRequest;
50     struct CrossThreadResourceRequestData;
51 
52     // Do not use this type directly.  Use ResourceRequest instead.
53     class ResourceRequestBase {
54     public:
55         static std::auto_ptr<ResourceRequest> adopt(std::auto_ptr<CrossThreadResourceRequestData>);
56 
57         // Gets a copy of the data suitable for passing to another thread.
58         std::auto_ptr<CrossThreadResourceRequestData> copyData() const;
59 
60         bool isNull() const;
61         bool isEmpty() const;
62 
63         const KURL& url() const;
64         void setURL(const KURL& url);
65 
66         ResourceRequestCachePolicy cachePolicy() const;
67         void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
68 
69         double timeoutInterval() const;
70         void setTimeoutInterval(double timeoutInterval);
71 
72         const KURL& mainDocumentURL() const;
73         void setMainDocumentURL(const KURL& mainDocumentURL);
74 
75         const String& httpMethod() const;
76         void setHTTPMethod(const String& httpMethod);
77 
78         const HTTPHeaderMap& httpHeaderFields() const;
79         String httpHeaderField(const AtomicString& name) const;
80         void setHTTPHeaderField(const AtomicString& name, const String& value);
81         void addHTTPHeaderField(const AtomicString& name, const String& value);
82         void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
83 
httpContentType()84         String httpContentType() const { return httpHeaderField("Content-Type");  }
setHTTPContentType(const String & httpContentType)85         void setHTTPContentType(const String& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
86 
httpReferrer()87         String httpReferrer() const { return httpHeaderField("Referer"); }
setHTTPReferrer(const String & httpReferrer)88         void setHTTPReferrer(const String& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); }
clearHTTPReferrer()89         void clearHTTPReferrer() { m_httpHeaderFields.remove("Referer"); }
90 
httpOrigin()91         String httpOrigin() const { return httpHeaderField("Origin"); }
setHTTPOrigin(const String & httpOrigin)92         void setHTTPOrigin(const String& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
clearHTTPOrigin()93         void clearHTTPOrigin() { m_httpHeaderFields.remove("Origin"); }
94 
httpUserAgent()95         String httpUserAgent() const { return httpHeaderField("User-Agent"); }
setHTTPUserAgent(const String & httpUserAgent)96         void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
97 
httpAccept()98         String httpAccept() const { return httpHeaderField("Accept"); }
setHTTPAccept(const String & httpAccept)99         void setHTTPAccept(const String& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
100 
101         void setResponseContentDispositionEncodingFallbackArray(const String& encoding1, const String& encoding2 = String(), const String& encoding3 = String());
102 
103         FormData* httpBody() const;
104         void setHTTPBody(PassRefPtr<FormData> httpBody);
105 
106         bool allowHTTPCookies() const;
107         void setAllowHTTPCookies(bool allowHTTPCookies);
108 
109         bool isConditional() const;
110 
111     protected:
112         // Used when ResourceRequest is initialized from a platform representation of the request
ResourceRequestBase()113         ResourceRequestBase()
114             : m_resourceRequestUpdated(false)
115             , m_platformRequestUpdated(true)
116         {
117         }
118 
ResourceRequestBase(const KURL & url,ResourceRequestCachePolicy policy)119         ResourceRequestBase(const KURL& url, ResourceRequestCachePolicy policy)
120             : m_url(url)
121             , m_cachePolicy(policy)
122             , m_timeoutInterval(unspecifiedTimeoutInterval)
123             , m_httpMethod("GET")
124             , m_allowHTTPCookies(true)
125             , m_resourceRequestUpdated(true)
126             , m_platformRequestUpdated(false)
127         {
128         }
129 
130         void updatePlatformRequest() const;
131         void updateResourceRequest() const;
132 
133         KURL m_url;
134 
135         ResourceRequestCachePolicy m_cachePolicy;
136         double m_timeoutInterval;
137         KURL m_mainDocumentURL;
138         String m_httpMethod;
139         HTTPHeaderMap m_httpHeaderFields;
140         Vector<String> m_responseContentDispositionEncodingFallbackArray;
141         RefPtr<FormData> m_httpBody;
142         bool m_allowHTTPCookies;
143         mutable bool m_resourceRequestUpdated;
144         mutable bool m_platformRequestUpdated;
145 
146     private:
147         const ResourceRequest& asResourceRequest() const;
148     };
149 
150     bool equalIgnoringHeaderFields(const ResourceRequestBase&, const ResourceRequestBase&);
151 
152     bool operator==(const ResourceRequestBase&, const ResourceRequestBase&);
153     inline bool operator!=(ResourceRequestBase& a, const ResourceRequestBase& b) { return !(a == b); }
154 
155     struct CrossThreadResourceRequestData {
156         KURL m_url;
157 
158         ResourceRequestCachePolicy m_cachePolicy;
159         double m_timeoutInterval;
160         KURL m_mainDocumentURL;
161 
162         String m_httpMethod;
163         OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
164         Vector<String> m_responseContentDispositionEncodingFallbackArray;
165         RefPtr<FormData> m_httpBody;
166         bool m_allowHTTPCookies;
167     };
168 
169 } // namespace WebCore
170 
171 #endif // ResourceRequestBase_h
172