WebResponse.cpp revision 21baf41667f5ac764512f0168a702390daf45bf3
1/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebResponse.h"
28
29#include "ResourceResponse.h"
30#include "ResourceError.h"
31
32namespace android {
33
34WebResponse::WebResponse(URLRequest* request)
35    : m_httpStatusCode(0)
36{
37    m_url = request->url().spec();
38    m_host = request->url().HostNoBrackets();
39    request->GetMimeType(&m_mime);
40    request->GetCharset(&m_encoding);
41    m_length = request->GetExpectedContentSize();
42
43    net::HttpResponseHeaders* responseHeaders = request->response_headers();
44    if (!responseHeaders)
45        return;
46
47    m_httpStatusCode = responseHeaders->response_code();
48    m_httpStatusText = responseHeaders->GetStatusText();
49
50    std::string value;
51    std::string name;
52    void* iter = 0;
53    while (responseHeaders->EnumerateHeaderLines(&iter, &name, &value))
54        m_headerFields[name] = value;
55}
56
57WebResponse::WebResponse(const std::string &url, const std::string &mimeType, const long long length, const std::string &encoding, const int httpStatusCode)
58    : m_encoding(encoding)
59    , m_httpStatusCode(httpStatusCode)
60    , m_length(length)
61    , m_mime(mimeType)
62    , m_url(url)
63{
64}
65
66WebCore::ResourceResponse WebResponse::createResourceResponse()
67{
68    WebCore::ResourceResponse resourceResponse(url(), m_mime.c_str(), m_length, m_encoding.c_str(), "");
69    resourceResponse.setHTTPStatusCode(m_httpStatusCode);
70    resourceResponse.setHTTPStatusText(m_httpStatusText.c_str());
71
72    std::map<std::string, std::string>::const_iterator it;
73    for (it = m_headerFields.begin(); it != m_headerFields.end(); ++it)
74        resourceResponse.setHTTPHeaderField(it->first.c_str(), it->second.c_str());
75
76    return resourceResponse;
77}
78
79WebCore::ResourceError WebResponse::createResourceError()
80{
81    // TODO: Last parameter is a localized string, get the correct one from android
82    WebCore::ResourceError error(m_host.c_str(), m_httpStatusCode, m_url.c_str(), m_httpStatusText.c_str());
83    return error;
84}
85
86
87WebCore::KURL WebResponse::url()
88{
89    WebCore::KURL kurl(WebCore::ParsedURLString, m_url.c_str());
90    return kurl;
91}
92
93void WebResponse::setUrl(std::string url)
94{
95    m_url = url;
96}
97
98} // namespace android
99