• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-request-http.c: http: URI request object
4  *
5  * Copyright (C) 2009, 2010 Red Hat, Inc.
6  * Copyright (C) 2010 Igalia, S.L.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <glib/gi18n-lib.h>
29 
30 #include "soup-request-http.h"
31 #include "soup.h"
32 #include "soup-message-private.h"
33 #include "soup-session-private.h"
34 
35 /**
36  * SECTION:soup-request-http
37  * @short_description: SoupRequest support for "http" and "https" URIs
38  *
39  * #SoupRequestHTTP implements #SoupRequest for "http" and "https"
40  * URIs.
41  *
42  * To do more complicated HTTP operations using the #SoupRequest APIs,
43  * call soup_request_http_get_message() to get the request's
44  * #SoupMessage.
45  */
46 
47 struct _SoupRequestHTTPPrivate {
48 	SoupMessage *msg;
49 	char *content_type;
50 };
51 
52 G_DEFINE_TYPE_WITH_PRIVATE (SoupRequestHTTP, soup_request_http, SOUP_TYPE_REQUEST)
53 
54 static void content_sniffed (SoupMessage *msg,
55 			     const char  *content_type,
56 			     GHashTable  *params,
57 			     gpointer     user_data);
58 
59 static void
soup_request_http_init(SoupRequestHTTP * http)60 soup_request_http_init (SoupRequestHTTP *http)
61 {
62 	http->priv = soup_request_http_get_instance_private (http);
63 }
64 
65 static gboolean
soup_request_http_check_uri(SoupRequest * request,SoupURI * uri,GError ** error)66 soup_request_http_check_uri (SoupRequest  *request,
67 			     SoupURI      *uri,
68 			     GError      **error)
69 {
70 	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
71 
72 	if (!SOUP_URI_VALID_FOR_HTTP (uri))
73 		return FALSE;
74 
75 	http->priv->msg = soup_message_new_from_uri (SOUP_METHOD_GET, uri);
76 	soup_message_set_soup_request (http->priv->msg, request);
77 
78 	g_signal_connect (http->priv->msg, "content-sniffed",
79 			  G_CALLBACK (content_sniffed), http);
80 	return TRUE;
81 }
82 
83 static void
soup_request_http_finalize(GObject * object)84 soup_request_http_finalize (GObject *object)
85 {
86 	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (object);
87 
88 	if (http->priv->msg) {
89 		g_signal_handlers_disconnect_by_func (http->priv->msg,
90 						      G_CALLBACK (content_sniffed),
91 						      http);
92 		g_object_unref (http->priv->msg);
93 	}
94 
95 	g_free (http->priv->content_type);
96 
97 	G_OBJECT_CLASS (soup_request_http_parent_class)->finalize (object);
98 }
99 
100 static GInputStream *
soup_request_http_send(SoupRequest * request,GCancellable * cancellable,GError ** error)101 soup_request_http_send (SoupRequest          *request,
102 			GCancellable         *cancellable,
103 			GError              **error)
104 {
105 	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
106 	SoupSession *session = soup_request_get_session (request);
107 
108 	g_return_val_if_fail (!SOUP_IS_SESSION_ASYNC (session), NULL);
109 
110 	return soup_session_send (session, http->priv->msg,
111 				  cancellable, error);
112 }
113 
114 
115 static void
http_input_stream_ready_cb(GObject * source,GAsyncResult * result,gpointer user_data)116 http_input_stream_ready_cb (GObject *source, GAsyncResult *result, gpointer user_data)
117 {
118 	GTask *task = user_data;
119 	GError *error = NULL;
120 	GInputStream *stream;
121 
122 	stream = soup_session_send_finish (SOUP_SESSION (source), result, &error);
123 	if (stream)
124 		g_task_return_pointer (task, stream, g_object_unref);
125 	else
126 		g_task_return_error (task, error);
127 	g_object_unref (task);
128 }
129 
130 static void
soup_request_http_send_async(SoupRequest * request,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)131 soup_request_http_send_async (SoupRequest          *request,
132 			      GCancellable         *cancellable,
133 			      GAsyncReadyCallback   callback,
134 			      gpointer              user_data)
135 {
136 	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
137 	SoupSession *session = soup_request_get_session (request);
138 	GTask *task;
139 
140 	g_return_if_fail (!SOUP_IS_SESSION_SYNC (session));
141 
142 	task = g_task_new (request, cancellable, callback, user_data);
143 	soup_session_send_async (session, http->priv->msg, cancellable,
144 				 http_input_stream_ready_cb, task);
145 }
146 
147 static GInputStream *
soup_request_http_send_finish(SoupRequest * request,GAsyncResult * result,GError ** error)148 soup_request_http_send_finish (SoupRequest   *request,
149 			       GAsyncResult  *result,
150 			       GError       **error)
151 {
152 	g_return_val_if_fail (g_task_is_valid (result, request), NULL);
153 
154 	return g_task_propagate_pointer (G_TASK (result), error);
155 }
156 
157 static goffset
soup_request_http_get_content_length(SoupRequest * request)158 soup_request_http_get_content_length (SoupRequest *request)
159 {
160 	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
161 
162 	return soup_message_headers_get_content_length (http->priv->msg->response_headers);
163 }
164 
165 static void
content_sniffed(SoupMessage * msg,const char * content_type,GHashTable * params,gpointer user_data)166 content_sniffed (SoupMessage *msg,
167 		 const char  *content_type,
168 		 GHashTable  *params,
169 		 gpointer     user_data)
170 {
171 	SoupRequestHTTP *http = user_data;
172 	GString *sniffed_type;
173 
174 	sniffed_type = g_string_new (content_type);
175 	if (params) {
176 		GHashTableIter iter;
177 		gpointer key, value;
178 
179 		g_hash_table_iter_init (&iter, params);
180 		while (g_hash_table_iter_next (&iter, &key, &value)) {
181 			g_string_append (sniffed_type, "; ");
182 			soup_header_g_string_append_param (sniffed_type, key, value);
183 		}
184 	}
185 	g_free (http->priv->content_type);
186 	http->priv->content_type = g_string_free (sniffed_type, FALSE);
187 }
188 
189 static const char *
soup_request_http_get_content_type(SoupRequest * request)190 soup_request_http_get_content_type (SoupRequest *request)
191 {
192 	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
193 
194 	return http->priv->content_type;
195 }
196 
197 static const char *http_schemes[] = { "http", "https", NULL };
198 
199 static void
soup_request_http_class_init(SoupRequestHTTPClass * request_http_class)200 soup_request_http_class_init (SoupRequestHTTPClass *request_http_class)
201 {
202 	GObjectClass *object_class = G_OBJECT_CLASS (request_http_class);
203 	SoupRequestClass *request_class =
204 		SOUP_REQUEST_CLASS (request_http_class);
205 
206 	request_class->schemes = http_schemes;
207 
208 	object_class->finalize = soup_request_http_finalize;
209 
210 	request_class->check_uri = soup_request_http_check_uri;
211 	request_class->send = soup_request_http_send;
212 	request_class->send_async = soup_request_http_send_async;
213 	request_class->send_finish = soup_request_http_send_finish;
214 	request_class->get_content_length = soup_request_http_get_content_length;
215 	request_class->get_content_type = soup_request_http_get_content_type;
216 }
217 
218 /**
219  * soup_request_http_get_message:
220  * @http: a #SoupRequestHTTP object
221  *
222  * Gets a new reference to the #SoupMessage associated to this SoupRequest
223  *
224  * Returns: (transfer full): a new reference to the #SoupMessage
225  *
226  * Since: 2.40
227  */
228 SoupMessage *
soup_request_http_get_message(SoupRequestHTTP * http)229 soup_request_http_get_message (SoupRequestHTTP *http)
230 {
231 	g_return_val_if_fail (SOUP_IS_REQUEST_HTTP (http), NULL);
232 
233 	return g_object_ref (http->priv->msg);
234 }
235