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