initial import
[vuplus_webkit] / Source / WebCore / platform / network / ResourceRequestBase.h
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 "HTTPHeaderMap.h"
33 #include "KURL.h"
34 #include "ResourceLoadPriority.h"
35
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     class ResourceRequest;
48     struct CrossThreadResourceRequestData;
49
50     // Do not use this type directly.  Use ResourceRequest instead.
51     class ResourceRequestBase {
52         WTF_MAKE_FAST_ALLOCATED;
53     public:
54         static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
55
56         // Gets a copy of the data suitable for passing to another thread.
57         PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
58
59         bool isNull() const;
60         bool isEmpty() const;
61
62         const KURL& url() const;
63         void setURL(const KURL& url);
64
65         void removeCredentials();
66
67         ResourceRequestCachePolicy cachePolicy() const;
68         void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
69         
70         double timeoutInterval() const; // May return 0 when using platform default.
71         void setTimeoutInterval(double timeoutInterval);
72         
73         const KURL& firstPartyForCookies() const;
74         void setFirstPartyForCookies(const KURL& firstPartyForCookies);
75         
76         const String& httpMethod() const;
77         void setHTTPMethod(const String& httpMethod);
78         
79         const HTTPHeaderMap& httpHeaderFields() const;
80         String httpHeaderField(const AtomicString& name) const;
81         String httpHeaderField(const char* name) const;
82         void setHTTPHeaderField(const AtomicString& name, const String& value);
83         void setHTTPHeaderField(const char* name, const String& value);
84         void addHTTPHeaderField(const AtomicString& name, const String& value);
85         void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
86         
87         void clearHTTPAuthorization();
88
89         String httpContentType() const { return httpHeaderField("Content-Type");  }
90         void setHTTPContentType(const String& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
91         
92         String httpReferrer() const { return httpHeaderField("Referer"); }
93         void setHTTPReferrer(const String& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); }
94         void clearHTTPReferrer();
95         
96         String httpOrigin() const { return httpHeaderField("Origin"); }
97         void setHTTPOrigin(const String& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
98         void clearHTTPOrigin();
99
100         String httpUserAgent() const { return httpHeaderField("User-Agent"); }
101         void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
102
103         String httpAccept() const { return httpHeaderField("Accept"); }
104         void setHTTPAccept(const String& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
105
106 #if PLATFORM(MAC) || PLATFORM(WIN)
107         // FIXME: This state should either be moved to a CFNetwork-specific
108         // ResourceRequest or should be removed.
109         void setResponseContentDispositionEncodingFallbackArray(const String& encoding1, const String& encoding2 = String(), const String& encoding3 = String());
110 #endif
111
112         FormData* httpBody() const;
113         void setHTTPBody(PassRefPtr<FormData> httpBody);
114         
115         bool allowCookies() const;
116         void setAllowCookies(bool allowCookies);
117
118         ResourceLoadPriority priority() const;
119         void setPriority(ResourceLoadPriority);
120
121         bool isConditional() const;
122
123         // Whether the associated ResourceHandleClient needs to be notified of
124         // upload progress made for that resource.
125         bool reportUploadProgress() const { return m_reportUploadProgress; }
126         void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
127
128         // Whether the timing information should be collected for the request.
129         bool reportLoadTiming() const { return m_reportLoadTiming; }
130         void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
131
132         // Whether actual headers being sent/received should be collected and reported for the request.
133         bool reportRawHeaders() const { return m_reportRawHeaders; }
134         void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
135
136         static double defaultTimeoutInterval(); // May return 0 when using platform default.
137         static void setDefaultTimeoutInterval(double);
138
139         static bool compare(const ResourceRequest&, const ResourceRequest&);
140
141     protected:
142         // Used when ResourceRequest is initialized from a platform representation of the request
143         ResourceRequestBase()
144             : m_resourceRequestUpdated(false)
145             , m_platformRequestUpdated(true)
146             , m_reportUploadProgress(false)
147             , m_reportLoadTiming(false)
148             , m_reportRawHeaders(false)
149             , m_priority(ResourceLoadPriorityLow)
150         {
151         }
152
153         ResourceRequestBase(const KURL& url, ResourceRequestCachePolicy policy)
154             : m_url(url)
155             , m_cachePolicy(policy)
156             , m_timeoutInterval(s_defaultTimeoutInterval)
157             , m_httpMethod("GET")
158             , m_allowCookies(true)
159             , m_resourceRequestUpdated(true)
160             , m_platformRequestUpdated(false)
161             , m_reportUploadProgress(false)
162             , m_reportLoadTiming(false)
163             , m_reportRawHeaders(false)
164             , m_priority(ResourceLoadPriorityLow)
165         {
166         }
167
168         void updatePlatformRequest() const; 
169         void updateResourceRequest() const; 
170
171         // The ResourceRequest subclass may "shadow" this method to compare platform specific fields
172         static bool platformCompare(const ResourceRequest&, const ResourceRequest&) { return true; }
173
174         KURL m_url;
175
176         ResourceRequestCachePolicy m_cachePolicy;
177         double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
178         KURL m_firstPartyForCookies;
179         String m_httpMethod;
180         HTTPHeaderMap m_httpHeaderFields;
181 #if PLATFORM(MAC) || PLATFORM(WIN)
182         // FIXME: This state should either be moved to a CFNetwork-specific
183         // ResourceRequest or should be removed.
184         Vector<String> m_responseContentDispositionEncodingFallbackArray;
185 #endif
186         RefPtr<FormData> m_httpBody;
187         bool m_allowCookies;
188         mutable bool m_resourceRequestUpdated;
189         mutable bool m_platformRequestUpdated;
190         bool m_reportUploadProgress;
191         bool m_reportLoadTiming;
192         bool m_reportRawHeaders;
193         ResourceLoadPriority m_priority;
194
195     private:
196         const ResourceRequest& asResourceRequest() const;
197
198         static double s_defaultTimeoutInterval;
199     };
200
201     bool equalIgnoringHeaderFields(const ResourceRequestBase&, const ResourceRequestBase&);
202
203     inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequestBase::compare(a, b); }
204     inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
205
206     struct CrossThreadResourceRequestDataBase {
207         WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestDataBase); WTF_MAKE_FAST_ALLOCATED;
208     public:
209         CrossThreadResourceRequestDataBase() { }
210         KURL m_url;
211
212         ResourceRequestCachePolicy m_cachePolicy;
213         double m_timeoutInterval;
214         KURL m_firstPartyForCookies;
215
216         String m_httpMethod;
217         OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
218 #if PLATFORM(MAC) || PLATFORM(WIN)
219         Vector<String> m_responseContentDispositionEncodingFallbackArray;
220 #endif
221         RefPtr<FormData> m_httpBody;
222         bool m_allowCookies;
223         ResourceLoadPriority m_priority;
224     };
225     
226     unsigned initializeMaximumHTTPConnectionCountPerHost();
227
228 } // namespace WebCore
229
230 #endif // ResourceRequestBase_h