1/*
2 * Copyright (C) 2011 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library 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 "WebKitURIResponse.h"
22
23#include "WebKitPrivate.h"
24#include "WebKitURIResponsePrivate.h"
25#include <glib/gi18n-lib.h>
26#include <wtf/text/CString.h>
27
28using namespace WebKit;
29using namespace WebCore;
30
31/**
32 * SECTION: WebKitURIResponse
33 * @Short_description: Represents a URI response
34 * @Title: WebKitURIResponse
35 *
36 * A #WebKitURIResponse contains information such as the URI, the
37 * status code, the content length, the mime type, the HTTP status or
38 * the suggested filename.
39 *
40 */
41
42enum {
43    PROP_0,
44
45    PROP_URI,
46    PROP_STATUS_CODE,
47    PROP_CONTENT_LENGTH,
48    PROP_MIME_TYPE,
49    PROP_SUGGESTED_FILENAME
50};
51
52struct _WebKitURIResponsePrivate {
53    ResourceResponse resourceResponse;
54    CString uri;
55    CString mimeType;
56    CString suggestedFilename;
57};
58
59WEBKIT_DEFINE_TYPE(WebKitURIResponse, webkit_uri_response, G_TYPE_OBJECT)
60
61static void webkitURIResponseGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
62{
63    WebKitURIResponse* response = WEBKIT_URI_RESPONSE(object);
64
65    switch (propId) {
66    case PROP_URI:
67        g_value_set_string(value, webkit_uri_response_get_uri(response));
68        break;
69    case PROP_STATUS_CODE:
70        g_value_set_uint(value, webkit_uri_response_get_status_code(response));
71        break;
72    case PROP_CONTENT_LENGTH:
73        g_value_set_uint64(value, webkit_uri_response_get_content_length(response));
74        break;
75    case PROP_MIME_TYPE:
76        g_value_set_string(value, webkit_uri_response_get_mime_type(response));
77        break;
78    case PROP_SUGGESTED_FILENAME:
79        g_value_set_string(value, webkit_uri_response_get_suggested_filename(response));
80        break;
81    default:
82        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
83    }
84}
85
86static void webkit_uri_response_class_init(WebKitURIResponseClass* responseClass)
87{
88    GObjectClass* objectClass = G_OBJECT_CLASS(responseClass);
89    objectClass->get_property = webkitURIResponseGetProperty;
90
91    /**
92     * WebKitURIResponse:uri:
93     *
94     * The URI for which the response was made.
95     */
96    g_object_class_install_property(objectClass,
97                                    PROP_URI,
98                                    g_param_spec_string("uri",
99                                                        _("URI"),
100                                                        _("The URI for which the response was made."),
101                                                        0,
102                                                      WEBKIT_PARAM_READABLE));
103    /**
104     * WebKitURIResponse:status-code:
105     *
106     * The status code of the response as returned by the server.
107     */
108    g_object_class_install_property(objectClass,
109                                    PROP_STATUS_CODE,
110                                    g_param_spec_uint("status-code",
111                                                      _("Status Code"),
112                                                      _("The status code of the response as returned by the server."),
113                                                      0, G_MAXUINT, SOUP_STATUS_NONE,
114                                                      WEBKIT_PARAM_READABLE));
115
116    /**
117     * WebKitURIResponse:content-length:
118     *
119     * The expected content length of the response.
120     */
121    g_object_class_install_property(objectClass,
122                                    PROP_CONTENT_LENGTH,
123                                    g_param_spec_uint64("content-length",
124                                                        _("Content Length"),
125                                                        _("The expected content length of the response."),
126                                                        0, G_MAXUINT64, 0,
127                                                        WEBKIT_PARAM_READABLE));
128
129    /**
130     * WebKitURIResponse:mime-type:
131     *
132     * The MIME type of the response.
133     */
134    g_object_class_install_property(objectClass,
135                                    PROP_MIME_TYPE,
136                                    g_param_spec_string("mime-type",
137                                                        _("MIME Type"),
138                                                        _("The MIME type of the response"),
139                                                        0,
140                                                        WEBKIT_PARAM_READABLE));
141
142    /**
143     * WebKitURIResponse:suggested-filename:
144     *
145     * The suggested filename for the URI response.
146     */
147    g_object_class_install_property(objectClass,
148                                    PROP_SUGGESTED_FILENAME,
149                                    g_param_spec_string("suggested-filename",
150                                                        _("Suggested Filename"),
151                                                        _("The suggested filename for the URI response"),
152                                                        0,
153                                                        WEBKIT_PARAM_READABLE));
154}
155
156/**
157 * webkit_uri_response_get_uri:
158 * @response: a #WebKitURIResponse
159 *
160 * Returns: the uri of the #WebKitURIResponse
161 */
162const gchar* webkit_uri_response_get_uri(WebKitURIResponse* response)
163{
164    g_return_val_if_fail(WEBKIT_IS_URI_RESPONSE(response), 0);
165
166    response->priv->uri = response->priv->resourceResponse.url().string().utf8();
167    return response->priv->uri.data();
168}
169
170/**
171 * webkit_uri_response_get_status_code:
172 * @response: a #WebKitURIResponse
173 *
174 * Get the status code of the #WebKitURIResponse as returned by
175 * the server. It will normally be a #SoupKnownStatusCode, for
176 * example %SOUP_STATUS_OK, though the server can respond with any
177 * unsigned integer.
178 *
179 * Returns: the status code of @response
180 */
181guint webkit_uri_response_get_status_code(WebKitURIResponse* response)
182{
183    g_return_val_if_fail(WEBKIT_IS_URI_RESPONSE(response), SOUP_STATUS_NONE);
184
185    return response->priv->resourceResponse.httpStatusCode();
186}
187
188/**
189 * webkit_uri_response_get_content_length:
190 * @response: a #WebKitURIResponse
191 *
192 * Get the expected content length of the #WebKitURIResponse. It can
193 * be 0 if the server provided an incorrect or missing Content-Length.
194 *
195 * Returns: the expected content length of @response.
196 */
197guint64 webkit_uri_response_get_content_length(WebKitURIResponse* response)
198{
199    g_return_val_if_fail(WEBKIT_IS_URI_RESPONSE(response), 0);
200
201    return response->priv->resourceResponse.expectedContentLength();
202}
203
204/**
205 * webkit_uri_response_get_mime_type:
206 * @response: a #WebKitURIResponse
207 *
208 * Returns: the MIME type of the #WebKitURIResponse
209 */
210const gchar* webkit_uri_response_get_mime_type(WebKitURIResponse* response)
211{
212    g_return_val_if_fail(WEBKIT_IS_URI_RESPONSE(response), 0);
213
214    response->priv->mimeType = response->priv->resourceResponse.mimeType().utf8();
215    return response->priv->mimeType.data();
216}
217
218/**
219 * webkit_uri_response_get_suggested_filename:
220 * @response: a #WebKitURIResponse
221 *
222 * Get the suggested filename for @response, as specified by
223 * the 'Content-Disposition' HTTP header, or %NULL if it's not
224 * present.
225 *
226 * Returns: (transfer none): the suggested filename or %NULL if
227 *    the 'Content-Disposition' HTTP header is not present.
228 */
229const gchar* webkit_uri_response_get_suggested_filename(WebKitURIResponse* response)
230{
231    g_return_val_if_fail(WEBKIT_IS_URI_RESPONSE(response), 0);
232
233    if (response->priv->resourceResponse.suggestedFilename().isEmpty())
234        return 0;
235
236    response->priv->suggestedFilename = response->priv->resourceResponse.suggestedFilename().utf8();
237    return response->priv->suggestedFilename.data();
238}
239
240WebKitURIResponse* webkitURIResponseCreateForResourceResponse(const WebCore::ResourceResponse& resourceResponse)
241{
242    WebKitURIResponse* uriResponse = WEBKIT_URI_RESPONSE(g_object_new(WEBKIT_TYPE_URI_RESPONSE, NULL));
243    uriResponse->priv->resourceResponse = resourceResponse;
244    return uriResponse;
245}
246
247const WebCore::ResourceResponse& webkitURIResponseGetResourceResponse(WebKitURIResponse* uriResponse)
248{
249    return uriResponse->priv->resourceResponse;
250}
251
252