1 // -*- mode: c++; c-basic-offset: 4 -*-
3 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
4 * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef ResourceRequest_H_
29 #define ResourceRequest_H_
33 #include "HTTPHeaderMap.h"
37 enum ResourceRequestCachePolicy {
38 UseProtocolCachePolicy, // normal load
39 ReloadIgnoringCacheData, // reload
40 ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
41 ReturnCacheDataDontLoad, // results of a post - allow stale data and only use cache
44 struct ResourceRequest {
46 ResourceRequest(const String& url)
47 : m_url(url.deprecatedString())
48 , m_cachePolicy(UseProtocolCachePolicy)
49 , m_timeoutInterval(defaultTimeoutInterval)
51 , m_allowHTTPCookies(true)
55 ResourceRequest(const KURL& url)
57 , m_cachePolicy(UseProtocolCachePolicy)
58 , m_timeoutInterval(defaultTimeoutInterval)
60 , m_allowHTTPCookies(true)
64 ResourceRequest(const KURL& url, const String& referrer, ResourceRequestCachePolicy policy = UseProtocolCachePolicy)
66 , m_cachePolicy(policy)
67 , m_timeoutInterval(defaultTimeoutInterval)
69 , m_allowHTTPCookies(true)
71 setHTTPReferrer(referrer);
75 : m_cachePolicy(UseProtocolCachePolicy)
76 , m_timeoutInterval(defaultTimeoutInterval)
78 , m_allowHTTPCookies(true)
82 bool isEmpty() const { return m_url.isEmpty(); }
84 const KURL& url() const { return m_url; }
85 void setURL(const KURL& url) { m_url = url; }
87 const ResourceRequestCachePolicy cachePolicy() const { return m_cachePolicy; }
88 void setCachePolicy(ResourceRequestCachePolicy cachePolicy) { m_cachePolicy = cachePolicy; }
90 double timeoutInterval() const { return m_timeoutInterval; }
91 void setTimeoutInterval(double timeoutInterval) { m_timeoutInterval = timeoutInterval; }
93 const KURL& mainDocumentURL() const { return m_mainDocumentURL; }
94 void setMainDocumentURL(const KURL& mainDocumentURL) { m_mainDocumentURL = mainDocumentURL; }
96 const String& httpMethod() const { return m_httpMethod; }
97 void setHTTPMethod(const String& httpMethod) { m_httpMethod = httpMethod; }
99 const HTTPHeaderMap& httpHeaderFields() const { return m_httpHeaderFields; }
100 String httpHeaderField(const String& name) const { return m_httpHeaderFields.get(name); }
101 void setHTTPHeaderField(const String& name, const String& value) { m_httpHeaderFields.set(name, value); }
102 void addHTTPHeaderField(const String& name, const String& value);
103 void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
105 String httpContentType() const { return httpHeaderField("Content-Type"); }
106 void setHTTPContentType(const String& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
108 String httpReferrer() const { return httpHeaderField("Referer"); }
109 void setHTTPReferrer(const String& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); }
111 String httpUserAgent() const { return httpHeaderField("User-Agent"); }
112 void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
114 String httpAccept() const { return httpHeaderField("Accept"); }
115 void setHTTPAccept(const String& httpUserAgent) { setHTTPHeaderField("Accept", httpUserAgent); }
117 const PassRefPtr<FormData> httpBody() const { return m_httpBody; }
118 PassRefPtr<FormData> httpBody() { return m_httpBody; }
119 void setHTTPBody(PassRefPtr<FormData> httpBody) { m_httpBody = httpBody; }
121 bool allowHTTPCookies() const { return m_allowHTTPCookies; }
122 void setAllowHTTPCookies(bool allowHTTPCookies) { m_allowHTTPCookies = allowHTTPCookies; }
125 static const int defaultTimeoutInterval = 60;
129 ResourceRequestCachePolicy m_cachePolicy;
130 double m_timeoutInterval;
131 KURL m_mainDocumentURL;
133 HTTPHeaderMap m_httpHeaderFields;
134 RefPtr<FormData> m_httpBody;
135 bool m_allowHTTPCookies;
138 inline void ResourceRequest::addHTTPHeaderField(const String& name, const String& value)
140 pair<HTTPHeaderMap::iterator, bool> result = m_httpHeaderFields.add(name, value);
142 result.first->second += "," + value;
145 // FIXME: probably shouldn't inline this
146 inline void ResourceRequest::addHTTPHeaderFields(const HTTPHeaderMap& headerFields)
148 HTTPHeaderMap::const_iterator end = headerFields.end();
149 for (HTTPHeaderMap::const_iterator it = headerFields.begin(); it != end; ++it)
150 addHTTPHeaderField(it->first, it->second);
153 } // namespace WebCore
155 #endif // ResourceRequest_H_