1/*
2 * Copyright (C) 2009 Gustavo Noronha Silva
3 * Copyright (C) 2009 Collabora Ltd.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "ResourceResponse.h"
23
24#include "GOwnPtr.h"
25#include "HTTPParsers.h"
26#include "MIMETypeRegistry.h"
27#include "PlatformString.h"
28#include "SoupURIUtils.h"
29#include <wtf/text/CString.h>
30
31using namespace std;
32
33namespace WebCore {
34
35SoupMessage* ResourceResponse::toSoupMessage() const
36{
37    // This GET here is just because SoupMessage wants it, we dn't really know.
38    SoupMessage* soupMessage = soup_message_new("GET", url().string().utf8().data());
39    if (!soupMessage)
40        return 0;
41
42    soupMessage->status_code = httpStatusCode();
43
44    const HTTPHeaderMap& headers = httpHeaderFields();
45    SoupMessageHeaders* soupHeaders = soupMessage->response_headers;
46    if (!headers.isEmpty()) {
47        HTTPHeaderMap::const_iterator end = headers.end();
48        for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
49            soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
50    }
51
52    soup_message_set_flags(soupMessage, m_soupFlags);
53
54    // Body data is not in the message.
55    return soupMessage;
56}
57
58void ResourceResponse::updateFromSoupMessage(SoupMessage* soupMessage)
59{
60    m_url = soupURIToKURL(soup_message_get_uri(soupMessage));
61
62    m_httpStatusCode = soupMessage->status_code;
63
64    SoupMessageHeadersIter headersIter;
65    const char* headerName;
66    const char* headerValue;
67
68    soup_message_headers_iter_init(&headersIter, soupMessage->response_headers);
69    while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
70        m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
71
72    m_soupFlags = soup_message_get_flags(soupMessage);
73
74    String contentType = soup_message_headers_get_one(soupMessage->response_headers, "Content-Type");
75    setMimeType(extractMIMETypeFromMediaType(contentType));
76
77    setTextEncodingName(extractCharsetFromMediaType(contentType));
78    setExpectedContentLength(soup_message_headers_get_content_length(soupMessage->response_headers));
79    setHTTPStatusText(soupMessage->reason_phrase);
80    setSuggestedFilename(filenameFromHTTPContentDisposition(httpHeaderField("Content-Disposition")));
81}
82
83}
84