1/*
2 * Copyright (C) 2003, 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
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 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 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 ResourceRequest_h
28#define ResourceRequest_h
29
30#include "PageIdentifier.h"
31#include "ResourceRequestBase.h"
32#include "URLSoup.h"
33
34namespace WebCore {
35
36 class ResourceRequest : public ResourceRequestBase {
37 public:
38 ResourceRequest(const String& url)
39 : ResourceRequestBase(URL({ }, url), ResourceRequestCachePolicy::UseProtocolCachePolicy)
40 , m_acceptEncoding(true)
41 , m_soupFlags(static_cast<SoupMessageFlags>(0))
42 {
43 }
44
45 ResourceRequest(const URL& url)
46 : ResourceRequestBase(url, ResourceRequestCachePolicy::UseProtocolCachePolicy)
47 , m_acceptEncoding(true)
48 , m_soupFlags(static_cast<SoupMessageFlags>(0))
49 {
50 }
51
52 ResourceRequest(const URL& url, const String& referrer, ResourceRequestCachePolicy policy = ResourceRequestCachePolicy::UseProtocolCachePolicy)
53 : ResourceRequestBase(url, policy)
54 , m_acceptEncoding(true)
55 , m_soupFlags(static_cast<SoupMessageFlags>(0))
56 {
57 setHTTPReferrer(referrer);
58 }
59
60 ResourceRequest()
61 : ResourceRequestBase(URL(), ResourceRequestCachePolicy::UseProtocolCachePolicy)
62 , m_acceptEncoding(true)
63 , m_soupFlags(static_cast<SoupMessageFlags>(0))
64 {
65 }
66
67 ResourceRequest(SoupMessage* soupMessage)
68 : ResourceRequestBase(URL(), ResourceRequestCachePolicy::UseProtocolCachePolicy)
69 , m_acceptEncoding(true)
70 , m_soupFlags(static_cast<SoupMessageFlags>(0))
71 {
72 updateFromSoupMessage(soupMessage);
73 }
74
75 ResourceRequest(SoupRequest* soupRequest)
76 : ResourceRequestBase(soupURIToURL(soup_request_get_uri(soupRequest)), ResourceRequestCachePolicy::UseProtocolCachePolicy)
77 , m_acceptEncoding(true)
78 , m_soupFlags(static_cast<SoupMessageFlags>(0))
79 {
80 updateFromSoupRequest(soupRequest);
81 }
82
83 void updateFromDelegatePreservingOldProperties(const ResourceRequest& delegateProvidedRequest) { *this = delegateProvidedRequest; }
84
85 bool acceptEncoding() const { return m_acceptEncoding; }
86 void setAcceptEncoding(bool acceptEncoding) { m_acceptEncoding = acceptEncoding; }
87
88 void updateSoupMessageHeaders(SoupMessageHeaders*) const;
89 void updateFromSoupMessageHeaders(SoupMessageHeaders*);
90 void updateSoupMessage(SoupMessage*) const;
91 void updateFromSoupMessage(SoupMessage*);
92 void updateSoupRequest(SoupRequest*) const;
93 void updateFromSoupRequest(SoupRequest*);
94
95 SoupMessageFlags soupMessageFlags() const { return m_soupFlags; }
96 void setSoupMessageFlags(SoupMessageFlags soupFlags) { m_soupFlags = soupFlags; }
97
98 Optional<PageIdentifier> initiatingPageID() const { return m_initiatingPageID; }
99 void setInitiatingPageID(PageIdentifier pageID) { m_initiatingPageID = pageID; }
100
101 GUniquePtr<SoupURI> createSoupURI() const;
102
103 template<class Encoder> void encodeWithPlatformData(Encoder&) const;
104 template<class Decoder> bool decodeWithPlatformData(Decoder&);
105
106 private:
107 friend class ResourceRequestBase;
108
109 bool m_acceptEncoding : 1;
110 SoupMessageFlags m_soupFlags;
111 Optional<PageIdentifier> m_initiatingPageID;
112
113 void updateSoupMessageMembers(SoupMessage*) const;
114 void updateSoupMessageBody(SoupMessage*) const;
115 void doUpdatePlatformRequest() { }
116 void doUpdateResourceRequest() { }
117 void doUpdatePlatformHTTPBody() { }
118 void doUpdateResourceHTTPBody() { }
119
120 void doPlatformSetAsIsolatedCopy(const ResourceRequest&) { }
121 };
122
123template<class Encoder>
124void ResourceRequest::encodeWithPlatformData(Encoder& encoder) const
125{
126 encodeBase(encoder);
127
128 // FIXME: Do not encode HTTP message body.
129 // 1. It can be large and thus costly to send across.
130 // 2. It is misleading to provide a body with some requests, while others use body streams, which cannot be serialized at all.
131 encoder << static_cast<bool>(m_httpBody);
132 if (m_httpBody)
133 encoder << m_httpBody->flattenToString();
134
135 encoder << static_cast<uint32_t>(m_soupFlags);
136 encoder << m_initiatingPageID;
137}
138
139template<class Decoder>
140bool ResourceRequest::decodeWithPlatformData(Decoder& decoder)
141{
142 if (!decodeBase(decoder))
143 return false;
144
145 bool hasHTTPBody;
146 if (!decoder.decode(hasHTTPBody))
147 return false;
148 if (hasHTTPBody) {
149 String httpBody;
150 if (!decoder.decode(httpBody))
151 return false;
152 setHTTPBody(FormData::create(httpBody.utf8()));
153 }
154
155 uint32_t soupMessageFlags;
156 if (!decoder.decode(soupMessageFlags))
157 return false;
158 m_soupFlags = static_cast<SoupMessageFlags>(soupMessageFlags);
159
160 Optional<Optional<PageIdentifier>> initiatingPageID;
161 decoder >> initiatingPageID;
162 if (!initiatingPageID)
163 return false;
164 m_initiatingPageID = *initiatingPageID;
165
166 return true;
167}
168
169
170#if SOUP_CHECK_VERSION(2, 43, 1)
171inline SoupMessagePriority toSoupMessagePriority(ResourceLoadPriority priority)
172{
173 switch (priority) {
174 case ResourceLoadPriority::VeryLow:
175 return SOUP_MESSAGE_PRIORITY_VERY_LOW;
176 case ResourceLoadPriority::Low:
177 return SOUP_MESSAGE_PRIORITY_LOW;
178 case ResourceLoadPriority::Medium:
179 return SOUP_MESSAGE_PRIORITY_NORMAL;
180 case ResourceLoadPriority::High:
181 return SOUP_MESSAGE_PRIORITY_HIGH;
182 case ResourceLoadPriority::VeryHigh:
183 return SOUP_MESSAGE_PRIORITY_VERY_HIGH;
184 }
185
186 ASSERT_NOT_REACHED();
187 return SOUP_MESSAGE_PRIORITY_VERY_LOW;
188}
189#endif
190
191} // namespace WebCore
192
193#endif // ResourceRequest_h
194