1/*
2 * Copyright (C) 2009 Gustavo Noronha Silva
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "ResourceRequest.h"
22
23#include "GOwnPtr.h"
24#include "GOwnPtrSoup.h"
25#include "HTTPParsers.h"
26#include "MIMETypeRegistry.h"
27#include "PlatformString.h"
28#include "SoupURIUtils.h"
29#include <wtf/text/CString.h>
30
31#include <libsoup/soup.h>
32
33using namespace std;
34
35namespace WebCore {
36
37void ResourceRequest::updateSoupMessage(SoupMessage* soupMessage) const
38{
39    g_object_set(soupMessage, SOUP_MESSAGE_METHOD, httpMethod().utf8().data(), NULL);
40
41    const HTTPHeaderMap& headers = httpHeaderFields();
42    SoupMessageHeaders* soupHeaders = soupMessage->request_headers;
43    if (!headers.isEmpty()) {
44        HTTPHeaderMap::const_iterator end = headers.end();
45        for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
46            soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
47    }
48
49    String firstPartyString = firstPartyForCookies().string();
50    if (!firstPartyString.isEmpty()) {
51        GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
52        soup_message_set_first_party(soupMessage, firstParty.get());
53    }
54
55    soup_message_set_flags(soupMessage, m_soupFlags);
56}
57
58SoupMessage* ResourceRequest::toSoupMessage() const
59{
60    SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
61    if (!soupMessage)
62        return 0;
63
64    const HTTPHeaderMap& headers = httpHeaderFields();
65    SoupMessageHeaders* soupHeaders = soupMessage->request_headers;
66    if (!headers.isEmpty()) {
67        HTTPHeaderMap::const_iterator end = headers.end();
68        for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
69            soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
70    }
71
72    String firstPartyString = firstPartyForCookies().string();
73    if (!firstPartyString.isEmpty()) {
74        GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
75        soup_message_set_first_party(soupMessage, firstParty.get());
76    }
77
78    soup_message_set_flags(soupMessage, m_soupFlags);
79
80    // Body data is only handled at ResourceHandleSoup::startHttp for
81    // now; this is because this may not be a good place to go
82    // openning and mmapping files. We should maybe revisit this.
83    return soupMessage;
84}
85
86void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
87{
88    m_url = soupURIToKURL(soup_message_get_uri(soupMessage));
89
90    m_httpMethod = String::fromUTF8(soupMessage->method);
91
92    m_httpHeaderFields.clear();
93    SoupMessageHeadersIter headersIter;
94    const char* headerName;
95    const char* headerValue;
96    soup_message_headers_iter_init(&headersIter, soupMessage->request_headers);
97    while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue)) {
98        m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
99    }
100
101    if (soupMessage->request_body->data)
102        m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
103
104    SoupURI* firstParty = soup_message_get_first_party(soupMessage);
105    if (firstParty)
106        m_firstPartyForCookies = soupURIToKURL(firstParty);
107
108    m_soupFlags = soup_message_get_flags(soupMessage);
109
110    // FIXME: m_allowCookies should probably be handled here and on
111    // doUpdatePlatformRequest somehow.
112}
113
114unsigned initializeMaximumHTTPConnectionCountPerHost()
115{
116    // Soup has its own queue control; it wants to have all requests
117    // given to it, so that it is able to look ahead, and schedule
118    // them in a good way.
119    return 10000;
120}
121
122}
123