1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* Copyright (C) 2009 Gustavo Noronha Silva
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if USE(SOUP)
#include "ResourceRequest.h"
#include "BlobData.h"
#include "BlobRegistryImpl.h"
#include "GUniquePtrSoup.h"
#include "HTTPParsers.h"
#include "MIMETypeRegistry.h"
#include "RegistrableDomain.h"
#include "SharedBuffer.h"
#include "URLSoup.h"
#include "WebKitFormDataInputStream.h"
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
static inline SoupMessagePriority toSoupMessagePriority(ResourceLoadPriority priority)
{
switch (priority) {
case ResourceLoadPriority::VeryLow:
return SOUP_MESSAGE_PRIORITY_VERY_LOW;
case ResourceLoadPriority::Low:
return SOUP_MESSAGE_PRIORITY_LOW;
case ResourceLoadPriority::Medium:
return SOUP_MESSAGE_PRIORITY_NORMAL;
case ResourceLoadPriority::High:
return SOUP_MESSAGE_PRIORITY_HIGH;
case ResourceLoadPriority::VeryHigh:
return SOUP_MESSAGE_PRIORITY_VERY_HIGH;
}
RELEASE_ASSERT_NOT_REACHED();
}
GRefPtr<SoupMessage> ResourceRequest::createSoupMessage(BlobRegistryImpl& blobRegistry) const
{
auto uri = createSoupURI();
if (!uri)
return nullptr;
auto soupMessage = adoptGRef(soup_message_new_from_uri(httpMethod().ascii().data(), uri.get()));
soup_message_set_priority(soupMessage.get(), toSoupMessagePriority(priority()));
updateSoupMessageHeaders(soup_message_get_request_headers(soupMessage.get()));
if (firstPartyForCookies().protocolIsInHTTPFamily()) {
if (auto firstParty = urlToSoupURI(firstPartyForCookies()))
soup_message_set_first_party(soupMessage.get(), firstParty.get());
}
if (!isSameSiteUnspecified()) {
if (isSameSite()) {
auto siteForCookies = urlToSoupURI(m_requestData.m_url);
soup_message_set_site_for_cookies(soupMessage.get(), siteForCookies.get());
}
soup_message_set_is_top_level_navigation(soupMessage.get(), isTopSite());
}
if (!acceptEncoding())
soup_message_disable_feature(soupMessage.get(), SOUP_TYPE_CONTENT_DECODER);
if (!allowCookies())
soup_message_disable_feature(soupMessage.get(), SOUP_TYPE_COOKIE_JAR);
updateSoupMessageBody(soupMessage.get(), blobRegistry);
return soupMessage;
}
void ResourceRequest::updateSoupMessageBody(SoupMessage* soupMessage, BlobRegistryImpl& blobRegistry) const
{
auto formData = httpBody();
if (!formData || formData->isEmpty())
return;
// Handle the common special case of one piece of form data, with no files.
auto& elements = formData->elements();
if (elements.size() == 1 && !formData->alwaysStream()) {
if (auto* vector = std::get_if<Vector<uint8_t>>(&elements[0].data)) {
GRefPtr<GBytes> bytes = adoptGRef(g_bytes_new_static(vector->span().data(), vector->size()));
soup_message_set_request_body_from_bytes(soupMessage, nullptr, bytes.get());
return;
}
}
// Precompute the content length.
auto resolvedFormData = formData->resolveBlobReferences();
uint64_t length = 0;
for (auto& element : resolvedFormData->elements()) {
length += element.lengthInBytes([&](auto& url) {
return blobRegistry.blobSize(url);
});
}
if (!length)
return;
GRefPtr<GInputStream> stream = webkitFormDataInputStreamNew(WTFMove(resolvedFormData));
soup_message_set_request_body(soupMessage, nullptr, stream.get(), length);
}
GRefPtr<GInputStream> ResourceRequest::createBodyStream() const
{
auto formData = httpBody();
if (!formData || formData->isEmpty())
return nullptr;
auto resolvedFormData = formData->resolveBlobReferences();
for (auto& element : resolvedFormData->elements()) {
if (element.lengthInBytes() > 0)
return webkitFormDataInputStreamNew(WTFMove(resolvedFormData));
}
return nullptr;
}
void ResourceRequest::updateSoupMessageHeaders(SoupMessageHeaders* soupHeaders) const
{
const HTTPHeaderMap& headers = httpHeaderFields();
if (!headers.isEmpty()) {
HTTPHeaderMap::const_iterator end = headers.end();
for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
soup_message_headers_append(soupHeaders, it->key.utf8().data(), it->value.utf8().data());
}
}
void ResourceRequest::updateFromSoupMessageHeaders(SoupMessageHeaders* soupHeaders)
{
m_requestData.m_httpHeaderFields.clear();
SoupMessageHeadersIter headersIter;
soup_message_headers_iter_init(&headersIter, soupHeaders);
const char* headerName;
const char* headerValue;
while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
m_requestData.m_httpHeaderFields.set(String::fromLatin1(headerName), String::fromLatin1(headerValue));
}
unsigned initializeMaximumHTTPConnectionCountPerHost()
{
// Soup has its own queue control; it wants to have all requests
// given to it, so that it is able to look ahead, and schedule
// them in a good way.
return 10000;
}
GRefPtr<GUri> ResourceRequest::createSoupURI() const
{
return m_requestData.m_url.createGUri();
}
void ResourceRequest::updateFromDelegatePreservingOldProperties(const ResourceRequest& delegateProvidedRequest)
{
// These are things we don't want willSendRequest delegate to mutate or reset.
ResourceLoadPriority oldPriority = priority();
RefPtr<FormData> oldHTTPBody = httpBody();
bool isHiddenFromInspector = hiddenFromInspector();
auto oldRequester = requester();
auto oldInitiatorIdentifier = initiatorIdentifier();
auto oldInspectorInitiatorNodeIdentifier = inspectorInitiatorNodeIdentifier();
*this = delegateProvidedRequest;
setPriority(oldPriority);
setHTTPBody(WTFMove(oldHTTPBody));
setHiddenFromInspector(isHiddenFromInspector);
setRequester(oldRequester);
setInitiatorIdentifier(oldInitiatorIdentifier);
if (oldInspectorInitiatorNodeIdentifier)
setInspectorInitiatorNodeIdentifier(*oldInspectorInitiatorNodeIdentifier);
}
ResourceRequest ResourceRequest::fromResourceRequestData(ResourceRequestData&& requestData)
{
if (std::holds_alternative<ResourceRequestBase::RequestData>(requestData))
return ResourceRequest(WTFMove(std::get<ResourceRequestBase::RequestData>(requestData)));
return ResourceRequest(WTFMove(std::get<ResourceRequestPlatformData>(requestData)));
}
ResourceRequestData ResourceRequest::getRequestDataToSerialize() const
{
if (encodingRequiresPlatformData())
return ResourceRequestPlatformData { m_requestData, m_acceptEncoding, m_redirectCount };
return m_requestData;
}
} // namespace WebCore
#endif // USE(SOUP)
|