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
38namespace 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 : public FastAllocBase {
54    public:
55        // The type of this ResourceRequest, based on how the resource will be used.
56        enum TargetType {
57            TargetIsMainFrame,
58            TargetIsSubframe,
59            TargetIsSubresource,  // Resource is a generic subresource.  (Generally a specific type should be specified)
60            TargetIsStyleSheet,
61            TargetIsScript,
62            TargetIsFontResource,
63            TargetIsImage,
64            TargetIsObject,
65            TargetIsMedia
66        };
67
68        static std::auto_ptr<ResourceRequest> adopt(std::auto_ptr<CrossThreadResourceRequestData>);
69
70        // Gets a copy of the data suitable for passing to another thread.
71        std::auto_ptr<CrossThreadResourceRequestData> copyData() const;
72
73        bool isNull() const;
74        bool isEmpty() const;
75
76        const KURL& url() const;
77        void setURL(const KURL& url);
78
79        void removeCredentials();
80
81        ResourceRequestCachePolicy cachePolicy() const;
82        void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
83
84        double timeoutInterval() const;
85        void setTimeoutInterval(double timeoutInterval);
86
87        const KURL& firstPartyForCookies() const;
88        void setFirstPartyForCookies(const KURL& firstPartyForCookies);
89
90        const String& httpMethod() const;
91        void setHTTPMethod(const String& httpMethod);
92
93        const HTTPHeaderMap& httpHeaderFields() const;
94        String httpHeaderField(const AtomicString& name) const;
95        String httpHeaderField(const char* name) const;
96        void setHTTPHeaderField(const AtomicString& name, const String& value);
97        void setHTTPHeaderField(const char* name, const String& value);
98        void addHTTPHeaderField(const AtomicString& name, const String& value);
99        void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
100
101        String httpContentType() const { return httpHeaderField("Content-Type");  }
102        void setHTTPContentType(const String& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
103
104        String httpReferrer() const { return httpHeaderField("Referer"); }
105        void setHTTPReferrer(const String& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); }
106        void clearHTTPReferrer();
107
108        String httpOrigin() const { return httpHeaderField("Origin"); }
109        void setHTTPOrigin(const String& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
110        void clearHTTPOrigin();
111
112        String httpUserAgent() const { return httpHeaderField("User-Agent"); }
113        void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
114
115        String httpAccept() const { return httpHeaderField("Accept"); }
116        void setHTTPAccept(const String& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
117
118        void setResponseContentDispositionEncodingFallbackArray(const String& encoding1, const String& encoding2 = String(), const String& encoding3 = String());
119
120        FormData* httpBody() const;
121        void setHTTPBody(PassRefPtr<FormData> httpBody);
122
123        bool allowCookies() const;
124        void setAllowCookies(bool allowCookies);
125
126        bool isConditional() const;
127
128        // Whether the associated ResourceHandleClient needs to be notified of
129        // upload progress made for that resource.
130        bool reportUploadProgress() const { return m_reportUploadProgress; }
131        void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
132
133        // What this request is for.
134        TargetType targetType() const { return m_targetType; }
135        void setTargetType(TargetType type) { m_targetType = type; }
136
137    protected:
138        // Used when ResourceRequest is initialized from a platform representation of the request
139        ResourceRequestBase()
140            : m_resourceRequestUpdated(false)
141            , m_platformRequestUpdated(true)
142            , m_reportUploadProgress(false)
143            , m_targetType(TargetIsSubresource)
144        {
145        }
146
147        ResourceRequestBase(const KURL& url, ResourceRequestCachePolicy policy)
148            : m_url(url)
149            , m_cachePolicy(policy)
150            , m_timeoutInterval(unspecifiedTimeoutInterval)
151            , m_httpMethod("GET")
152            , m_allowCookies(true)
153            , m_resourceRequestUpdated(true)
154            , m_platformRequestUpdated(false)
155            , m_reportUploadProgress(false)
156            , m_targetType(TargetIsSubresource)
157        {
158        }
159
160        void updatePlatformRequest() const;
161        void updateResourceRequest() const;
162
163        KURL m_url;
164
165        ResourceRequestCachePolicy m_cachePolicy;
166        double m_timeoutInterval;
167        KURL m_firstPartyForCookies;
168        String m_httpMethod;
169        HTTPHeaderMap m_httpHeaderFields;
170        Vector<String> m_responseContentDispositionEncodingFallbackArray;
171        RefPtr<FormData> m_httpBody;
172        bool m_allowCookies;
173        mutable bool m_resourceRequestUpdated;
174        mutable bool m_platformRequestUpdated;
175        bool m_reportUploadProgress;
176        TargetType m_targetType;
177
178    private:
179        const ResourceRequest& asResourceRequest() const;
180    };
181
182    bool equalIgnoringHeaderFields(const ResourceRequestBase&, const ResourceRequestBase&);
183
184    bool operator==(const ResourceRequestBase&, const ResourceRequestBase&);
185    inline bool operator!=(ResourceRequestBase& a, const ResourceRequestBase& b) { return !(a == b); }
186
187    struct CrossThreadResourceRequestData : Noncopyable {
188        KURL m_url;
189
190        ResourceRequestCachePolicy m_cachePolicy;
191        double m_timeoutInterval;
192        KURL m_firstPartyForCookies;
193
194        String m_httpMethod;
195        OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
196        Vector<String> m_responseContentDispositionEncodingFallbackArray;
197        RefPtr<FormData> m_httpBody;
198        bool m_allowCookies;
199    };
200
201    unsigned initializeMaximumHTTPConnectionCountPerHost();
202
203} // namespace WebCore
204
205#endif // ResourceRequestBase_h
206