souphttpsrc: Make sure to not return EOS immediately if we finished a range request
[platform/upstream/gst-plugins-good.git] / ext / soup / gstsouphttpsrc.c
1 /* GStreamer
2  * Copyright (C) 2007-2008 Wouter Cloetens <wouter@mind.be>
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
13  */
14
15 /**
16  * SECTION:element-souphttpsrc
17  *
18  * This plugin reads data from a remote location specified by a URI.
19  * Supported protocols are 'http', 'https'.
20  * 
21  * An HTTP proxy must be specified by its URL.
22  * If the "http_proxy" environment variable is set, its value is used.
23  * If built with libsoup's GNOME integration features, the GNOME proxy
24  * configuration will be used, or failing that, proxy autodetection.
25  * The #GstSoupHTTPSrc:proxy property can be used to override the default.
26  *
27  * In case the #GstSoupHTTPSrc:iradio-mode property is set and the location is
28  * an HTTP resource, souphttpsrc will send special Icecast HTTP headers to the
29  * server to request additional Icecast meta-information.
30  * If the server is not an Icecast server, it will behave as if the
31  * #GstSoupHTTPSrc:iradio-mode property were not set. If it is, souphttpsrc will
32  * output data with a media type of application/x-icy, in which case you will
33  * need to use the #ICYDemux element as follow-up element to extract the Icecast
34  * metadata and to determine the underlying media type.
35  *
36  * <refsect2>
37  * <title>Example launch line</title>
38  * |[
39  * gst-launch-1.0 -v souphttpsrc location=https://some.server.org/index.html
40  *     ! filesink location=/home/joe/server.html
41  * ]| The above pipeline reads a web page from a server using the HTTPS protocol
42  * and writes it to a local file.
43  * |[
44  * gst-launch-1.0 -v souphttpsrc user-agent="FooPlayer 0.99 beta"
45  *     automatic-redirect=false proxy=http://proxy.intranet.local:8080
46  *     location=http://music.foobar.com/demo.mp3 ! mad ! audioconvert
47  *     ! audioresample ! alsasink
48  * ]| The above pipeline will read and decode and play an mp3 file from a
49  * web server using the HTTP protocol. If the server sends redirects,
50  * the request fails instead of following the redirect. The specified
51  * HTTP proxy server is used. The User-Agent HTTP request header
52  * is set to a custom string instead of "GStreamer souphttpsrc."
53  * |[
54  * gst-launch-1.0 -v souphttpsrc location=http://10.11.12.13/mjpeg
55  *     do-timestamp=true ! multipartdemux
56  *     ! image/jpeg,width=640,height=480 ! matroskamux
57  *     ! filesink location=mjpeg.mkv
58  * ]| The above pipeline reads a motion JPEG stream from an IP camera
59  * using the HTTP protocol, encoded as mime/multipart image/jpeg
60  * parts, and writes a Matroska motion JPEG file. The width and
61  * height properties are set in the caps to provide the Matroska
62  * multiplexer with the information to set this in the header.
63  * Timestamps are set on the buffers as they arrive from the camera.
64  * These are used by the mime/multipart demultiplexer to emit timestamps
65  * on the JPEG-encoded video frame buffers. This allows the Matroska
66  * multiplexer to timestamp the frames in the resulting file.
67  * </refsect2>
68  */
69
70 #ifdef HAVE_CONFIG_H
71 #include "config.h"
72 #endif
73
74 #include <string.h>
75 #ifdef HAVE_STDLIB_H
76 #include <stdlib.h>             /* atoi() */
77 #endif
78 #include <gst/gstelement.h>
79 #include <gst/gst-i18n-plugin.h>
80 #include <libsoup/soup.h>
81 #include "gstsouphttpsrc.h"
82 #include "gstsouputils.h"
83
84 #include <gst/tag/tag.h>
85
86 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
87 #define GST_CAT_DEFAULT souphttpsrc_debug
88
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
90     GST_PAD_SRC,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS_ANY);
93
94 enum
95 {
96   PROP_0,
97   PROP_LOCATION,
98   PROP_IS_LIVE,
99   PROP_USER_AGENT,
100   PROP_AUTOMATIC_REDIRECT,
101   PROP_PROXY,
102   PROP_USER_ID,
103   PROP_USER_PW,
104   PROP_PROXY_ID,
105   PROP_PROXY_PW,
106   PROP_COOKIES,
107   PROP_IRADIO_MODE,
108   PROP_TIMEOUT,
109   PROP_EXTRA_HEADERS,
110   PROP_SOUP_LOG_LEVEL,
111   PROP_COMPRESS,
112   PROP_KEEP_ALIVE
113 };
114
115 #define DEFAULT_USER_AGENT           "GStreamer souphttpsrc "
116 #define DEFAULT_IRADIO_MODE          TRUE
117 #define DEFAULT_SOUP_LOG_LEVEL       SOUP_LOGGER_LOG_HEADERS
118 #define DEFAULT_COMPRESS             FALSE
119 #define DEFAULT_KEEP_ALIVE           FALSE
120
121 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
122     gpointer iface_data);
123 static void gst_soup_http_src_finalize (GObject * gobject);
124 static void gst_soup_http_src_dispose (GObject * gobject);
125
126 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
127     const GValue * value, GParamSpec * pspec);
128 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
129     GValue * value, GParamSpec * pspec);
130
131 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
132     element, GstStateChange transition);
133 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
134     GstBuffer ** outbuf);
135 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
136 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
137 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
138 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
139 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
140     GstSegment * segment);
141 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
142 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
143 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
144 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
145     const gchar * uri, GError ** error);
146 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
147     const gchar * uri);
148 static char *gst_soup_http_src_unicodify (const char *str);
149 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
150     const gchar * method);
151 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
152 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
153 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
154     guint64 offset, guint64 stop_offset);
155 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
156 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
157 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
158 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
159 static void gst_soup_http_src_parse_status (SoupMessage * msg,
160     GstSoupHTTPSrc * src);
161 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
162 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
163     gsize max_len, gpointer user_data);
164 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
165     SoupBuffer * chunk, GstSoupHTTPSrc * src);
166 static void gst_soup_http_src_response_cb (SoupSession * session,
167     SoupMessage * msg, GstSoupHTTPSrc * src);
168 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
169     GstSoupHTTPSrc * src);
170 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
171     GstSoupHTTPSrc * src);
172 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
173     GstSoupHTTPSrc * src);
174 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
175     SoupMessage * msg, SoupAuth * auth, gboolean retrying,
176     GstSoupHTTPSrc * src);
177
178 #define gst_soup_http_src_parent_class parent_class
179 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
180     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
181         gst_soup_http_src_uri_handler_init));
182
183 static void
184 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
185 {
186   GObjectClass *gobject_class;
187   GstElementClass *gstelement_class;
188   GstBaseSrcClass *gstbasesrc_class;
189   GstPushSrcClass *gstpushsrc_class;
190
191   gobject_class = (GObjectClass *) klass;
192   gstelement_class = (GstElementClass *) klass;
193   gstbasesrc_class = (GstBaseSrcClass *) klass;
194   gstpushsrc_class = (GstPushSrcClass *) klass;
195
196   gobject_class->set_property = gst_soup_http_src_set_property;
197   gobject_class->get_property = gst_soup_http_src_get_property;
198   gobject_class->finalize = gst_soup_http_src_finalize;
199   gobject_class->dispose = gst_soup_http_src_dispose;
200
201   g_object_class_install_property (gobject_class,
202       PROP_LOCATION,
203       g_param_spec_string ("location", "Location",
204           "Location to read from", "",
205           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206   g_object_class_install_property (gobject_class,
207       PROP_USER_AGENT,
208       g_param_spec_string ("user-agent", "User-Agent",
209           "Value of the User-Agent HTTP request header field",
210           DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211   g_object_class_install_property (gobject_class,
212       PROP_AUTOMATIC_REDIRECT,
213       g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
214           "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
215           TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216   g_object_class_install_property (gobject_class,
217       PROP_PROXY,
218       g_param_spec_string ("proxy", "Proxy",
219           "HTTP proxy server URI", "",
220           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221   g_object_class_install_property (gobject_class,
222       PROP_USER_ID,
223       g_param_spec_string ("user-id", "user-id",
224           "HTTP location URI user id for authentication", "",
225           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226   g_object_class_install_property (gobject_class, PROP_USER_PW,
227       g_param_spec_string ("user-pw", "user-pw",
228           "HTTP location URI user password for authentication", "",
229           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230   g_object_class_install_property (gobject_class, PROP_PROXY_ID,
231       g_param_spec_string ("proxy-id", "proxy-id",
232           "HTTP proxy URI user id for authentication", "",
233           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234   g_object_class_install_property (gobject_class, PROP_PROXY_PW,
235       g_param_spec_string ("proxy-pw", "proxy-pw",
236           "HTTP proxy URI user password for authentication", "",
237           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238   g_object_class_install_property (gobject_class, PROP_COOKIES,
239       g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
240           G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
242       g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
243           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244   g_object_class_install_property (gobject_class, PROP_TIMEOUT,
245       g_param_spec_uint ("timeout", "timeout",
246           "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
247           3600, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248   g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
249       g_param_spec_boxed ("extra-headers", "Extra Headers",
250           "Extra headers to append to the HTTP request",
251           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252   g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
253       g_param_spec_boolean ("iradio-mode", "iradio-mode",
254           "Enable internet radio mode (ask server to send shoutcast/icecast "
255           "metadata interleaved with the actual stream data)",
256           DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257
258  /**
259    * GstSoupHTTPSrc::http-log-level:
260    *
261    * If set and > 0, captures and dumps HTTP session data as
262    * log messages if log level >= GST_LEVEL_TRACE
263    *
264    * Since: 1.4
265    */
266   g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
267       g_param_spec_enum ("http-log-level", "HTTP log level",
268           "Set log level for soup's HTTP session log",
269           SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
270           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271
272  /**
273    * GstSoupHTTPSrc::compress:
274    *
275    * If set to %TRUE, souphttpsrc will automatically handle gzip
276    * and deflate Content-Encodings. This does not make much difference
277    * and causes more load for normal media files, but makes a real
278    * difference in size for plaintext files.
279    *
280    * Since: 1.4
281    */
282   g_object_class_install_property (gobject_class, PROP_COMPRESS,
283       g_param_spec_boolean ("compress", "Compress",
284           "Allow compressed content encodings",
285           DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286
287  /**
288    * GstSoupHTTPSrc::keep-alive:
289    *
290    * If set to %TRUE, souphttpsrc will keep alive connections when being
291    * set to READY state and only will close connections when connecting
292    * to a different server or when going to NULL state..
293    *
294    * Since: 1.4
295    */
296   g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
297       g_param_spec_boolean ("keep-alive", "keep-alive",
298           "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
299           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300
301   gst_element_class_add_pad_template (gstelement_class,
302       gst_static_pad_template_get (&srctemplate));
303
304   gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
305       "Source/Network",
306       "Receive data as a client over the network via HTTP using SOUP",
307       "Wouter Cloetens <wouter@mind.be>");
308   gstelement_class->change_state =
309       GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
310
311   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
312   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
313   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
314   gstbasesrc_class->unlock_stop =
315       GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
316   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
317   gstbasesrc_class->is_seekable =
318       GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
319   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
320   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
321
322   gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
323
324   GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
325       "SOUP HTTP src");
326 }
327
328 static void
329 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
330 {
331   src->interrupted = FALSE;
332   src->retry = FALSE;
333   src->have_size = FALSE;
334   src->got_headers = FALSE;
335   src->seekable = FALSE;
336   src->read_position = 0;
337   src->request_position = 0;
338   src->stop_position = -1;
339   src->content_size = 0;
340   src->have_body = FALSE;
341
342   src->ret = GST_FLOW_OK;
343
344   gst_caps_replace (&src->src_caps, NULL);
345   g_free (src->iradio_name);
346   src->iradio_name = NULL;
347   g_free (src->iradio_genre);
348   src->iradio_genre = NULL;
349   g_free (src->iradio_url);
350   src->iradio_url = NULL;
351 }
352
353 static void
354 gst_soup_http_src_init (GstSoupHTTPSrc * src)
355 {
356   const gchar *proxy;
357
358   g_mutex_init (&src->mutex);
359   g_cond_init (&src->request_finished_cond);
360   src->location = NULL;
361   src->redirection_uri = NULL;
362   src->automatic_redirect = TRUE;
363   src->user_agent = g_strdup (DEFAULT_USER_AGENT);
364   src->user_id = NULL;
365   src->user_pw = NULL;
366   src->proxy_id = NULL;
367   src->proxy_pw = NULL;
368   src->cookies = NULL;
369   src->iradio_mode = DEFAULT_IRADIO_MODE;
370   src->loop = NULL;
371   src->context = NULL;
372   src->session = NULL;
373   src->msg = NULL;
374   src->log_level = DEFAULT_SOUP_LOG_LEVEL;
375   proxy = g_getenv ("http_proxy");
376   if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
377     GST_WARNING_OBJECT (src,
378         "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
379         proxy);
380   }
381
382   gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
383
384   gst_soup_http_src_reset (src);
385 }
386
387 static void
388 gst_soup_http_src_dispose (GObject * gobject)
389 {
390   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
391
392   GST_DEBUG_OBJECT (src, "dispose");
393
394   gst_soup_http_src_session_close (src);
395
396   G_OBJECT_CLASS (parent_class)->dispose (gobject);
397 }
398
399 static void
400 gst_soup_http_src_finalize (GObject * gobject)
401 {
402   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
403
404   GST_DEBUG_OBJECT (src, "finalize");
405
406   g_mutex_clear (&src->mutex);
407   g_cond_clear (&src->request_finished_cond);
408   g_free (src->location);
409   if (src->redirection_uri) {
410     g_free (src->redirection_uri);
411   }
412   g_free (src->user_agent);
413   if (src->proxy != NULL) {
414     soup_uri_free (src->proxy);
415   }
416   g_free (src->user_id);
417   g_free (src->user_pw);
418   g_free (src->proxy_id);
419   g_free (src->proxy_pw);
420   g_strfreev (src->cookies);
421
422   if (src->extra_headers) {
423     gst_structure_free (src->extra_headers);
424     src->extra_headers = NULL;
425   }
426
427   G_OBJECT_CLASS (parent_class)->finalize (gobject);
428 }
429
430 static void
431 gst_soup_http_src_set_property (GObject * object, guint prop_id,
432     const GValue * value, GParamSpec * pspec)
433 {
434   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
435
436   switch (prop_id) {
437     case PROP_LOCATION:
438     {
439       const gchar *location;
440
441       location = g_value_get_string (value);
442
443       if (location == NULL) {
444         GST_WARNING ("location property cannot be NULL");
445         goto done;
446       }
447       if (!gst_soup_http_src_set_location (src, location, NULL)) {
448         GST_WARNING ("badly formatted location");
449         goto done;
450       }
451       break;
452     }
453     case PROP_USER_AGENT:
454       if (src->user_agent)
455         g_free (src->user_agent);
456       src->user_agent = g_value_dup_string (value);
457       break;
458     case PROP_IRADIO_MODE:
459       src->iradio_mode = g_value_get_boolean (value);
460       break;
461     case PROP_AUTOMATIC_REDIRECT:
462       src->automatic_redirect = g_value_get_boolean (value);
463       break;
464     case PROP_PROXY:
465     {
466       const gchar *proxy;
467
468       proxy = g_value_get_string (value);
469
470       if (proxy == NULL) {
471         GST_WARNING ("proxy property cannot be NULL");
472         goto done;
473       }
474       if (!gst_soup_http_src_set_proxy (src, proxy)) {
475         GST_WARNING ("badly formatted proxy URI");
476         goto done;
477       }
478       break;
479     }
480     case PROP_COOKIES:
481       g_strfreev (src->cookies);
482       src->cookies = g_strdupv (g_value_get_boxed (value));
483       break;
484     case PROP_IS_LIVE:
485       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
486       break;
487     case PROP_USER_ID:
488       if (src->user_id)
489         g_free (src->user_id);
490       src->user_id = g_value_dup_string (value);
491       break;
492     case PROP_USER_PW:
493       if (src->user_pw)
494         g_free (src->user_pw);
495       src->user_pw = g_value_dup_string (value);
496       break;
497     case PROP_PROXY_ID:
498       if (src->proxy_id)
499         g_free (src->proxy_id);
500       src->proxy_id = g_value_dup_string (value);
501       break;
502     case PROP_PROXY_PW:
503       if (src->proxy_pw)
504         g_free (src->proxy_pw);
505       src->proxy_pw = g_value_dup_string (value);
506       break;
507     case PROP_TIMEOUT:
508       src->timeout = g_value_get_uint (value);
509       break;
510     case PROP_EXTRA_HEADERS:{
511       const GstStructure *s = gst_value_get_structure (value);
512
513       if (src->extra_headers)
514         gst_structure_free (src->extra_headers);
515
516       src->extra_headers = s ? gst_structure_copy (s) : NULL;
517       break;
518     }
519     case PROP_SOUP_LOG_LEVEL:
520       src->log_level = g_value_get_enum (value);
521       break;
522     case PROP_COMPRESS:
523       src->compress = g_value_get_boolean (value);
524       break;
525     case PROP_KEEP_ALIVE:
526       src->keep_alive = g_value_get_boolean (value);
527       break;
528     default:
529       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530       break;
531   }
532 done:
533   return;
534 }
535
536 static void
537 gst_soup_http_src_get_property (GObject * object, guint prop_id,
538     GValue * value, GParamSpec * pspec)
539 {
540   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
541
542   switch (prop_id) {
543     case PROP_LOCATION:
544       g_value_set_string (value, src->location);
545       break;
546     case PROP_USER_AGENT:
547       g_value_set_string (value, src->user_agent);
548       break;
549     case PROP_AUTOMATIC_REDIRECT:
550       g_value_set_boolean (value, src->automatic_redirect);
551       break;
552     case PROP_PROXY:
553       if (src->proxy == NULL)
554         g_value_set_static_string (value, "");
555       else {
556         char *proxy = soup_uri_to_string (src->proxy, FALSE);
557
558         g_value_set_string (value, proxy);
559         g_free (proxy);
560       }
561       break;
562     case PROP_COOKIES:
563       g_value_set_boxed (value, g_strdupv (src->cookies));
564       break;
565     case PROP_IS_LIVE:
566       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
567       break;
568     case PROP_IRADIO_MODE:
569       g_value_set_boolean (value, src->iradio_mode);
570       break;
571     case PROP_USER_ID:
572       g_value_set_string (value, src->user_id);
573       break;
574     case PROP_USER_PW:
575       g_value_set_string (value, src->user_pw);
576       break;
577     case PROP_PROXY_ID:
578       g_value_set_string (value, src->proxy_id);
579       break;
580     case PROP_PROXY_PW:
581       g_value_set_string (value, src->proxy_pw);
582       break;
583     case PROP_TIMEOUT:
584       g_value_set_uint (value, src->timeout);
585       break;
586     case PROP_EXTRA_HEADERS:
587       gst_value_set_structure (value, src->extra_headers);
588       break;
589     case PROP_SOUP_LOG_LEVEL:
590       g_value_set_enum (value, src->log_level);
591       break;
592     case PROP_COMPRESS:
593       g_value_set_boolean (value, src->compress);
594       break;
595     case PROP_KEEP_ALIVE:
596       g_value_set_boolean (value, src->keep_alive);
597       break;
598     default:
599       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
600       break;
601   }
602 }
603
604 static gchar *
605 gst_soup_http_src_unicodify (const gchar * str)
606 {
607   const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
608     "GST_TAG_ENCODING", NULL
609   };
610
611   return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
612 }
613
614 static void
615 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
616 {
617   if (src->msg != NULL) {
618     src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
619     soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
620   }
621   src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
622   src->msg = NULL;
623 }
624
625 static void
626 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
627 {
628   soup_session_queue_message (src->session, src->msg,
629       (SoupSessionCallback) gst_soup_http_src_response_cb, src);
630   src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
631 }
632
633 static gboolean
634 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
635     guint64 stop_offset)
636 {
637   gchar buf[64];
638
639   gint rc;
640
641   soup_message_headers_remove (src->msg->request_headers, "Range");
642   if (offset || stop_offset != -1) {
643     if (stop_offset != -1) {
644       rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
645           G_GUINT64_FORMAT, offset, stop_offset);
646     } else {
647       rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
648           offset);
649     }
650     if (rc > sizeof (buf) || rc < 0)
651       return FALSE;
652     soup_message_headers_append (src->msg->request_headers, "Range", buf);
653   }
654   src->read_position = offset;
655   return TRUE;
656 }
657
658 static gboolean
659 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
660 {
661   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
662   const gchar *field_name = g_quark_to_string (field_id);
663   gchar *field_content = NULL;
664
665   if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
666     field_content = g_value_dup_string (value);
667   } else {
668     GValue dest = { 0, };
669
670     g_value_init (&dest, G_TYPE_STRING);
671     if (g_value_transform (value, &dest)) {
672       field_content = g_value_dup_string (&dest);
673     }
674   }
675
676   if (field_content == NULL) {
677     GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
678         "or can't be converted to a string", field_name);
679     return FALSE;
680   }
681
682   GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
683       field_content);
684   soup_message_headers_append (src->msg->request_headers, field_name,
685       field_content);
686
687   g_free (field_content);
688
689   return TRUE;
690 }
691
692 static gboolean
693 _append_extra_headers (GQuark field_id, const GValue * value,
694     gpointer user_data)
695 {
696   if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
697     guint n = gst_value_array_get_size (value);
698     guint i;
699
700     for (i = 0; i < n; i++) {
701       const GValue *v = gst_value_array_get_value (value, i);
702
703       if (!_append_extra_header (field_id, v, user_data))
704         return FALSE;
705     }
706   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
707     guint n = gst_value_list_get_size (value);
708     guint i;
709
710     for (i = 0; i < n; i++) {
711       const GValue *v = gst_value_list_get_value (value, i);
712
713       if (!_append_extra_header (field_id, v, user_data))
714         return FALSE;
715     }
716   } else {
717     return _append_extra_header (field_id, value, user_data);
718   }
719
720   return TRUE;
721 }
722
723
724 static gboolean
725 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
726 {
727   if (!src->extra_headers)
728     return TRUE;
729
730   return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
731 }
732
733
734 static void
735 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
736 {
737   soup_session_unpause_message (src->session, src->msg);
738 }
739
740 static void
741 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
742 {
743   soup_session_pause_message (src->session, src->msg);
744 }
745
746 static gboolean
747 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
748 {
749   if (src->session) {
750     GST_DEBUG_OBJECT (src, "Session is already open");
751     return TRUE;
752   }
753
754   if (!src->location) {
755     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
756         ("Missing location property"));
757     return FALSE;
758   }
759
760   if (!src->context)
761     src->context = g_main_context_new ();
762
763   if (!src->loop)
764     src->loop = g_main_loop_new (src->context, TRUE);
765   if (!src->loop) {
766     GST_ELEMENT_ERROR (src, LIBRARY, INIT,
767         (NULL), ("Failed to start GMainLoop"));
768     g_main_context_unref (src->context);
769     return FALSE;
770   }
771
772   if (!src->session) {
773     GST_DEBUG_OBJECT (src, "Creating session");
774     if (src->proxy == NULL) {
775       src->session =
776           soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
777           src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
778           SOUP_SESSION_TIMEOUT, src->timeout,
779           SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
780           NULL);
781     } else {
782       src->session =
783           soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
784           src->context, SOUP_SESSION_PROXY_URI, src->proxy,
785           SOUP_SESSION_TIMEOUT, src->timeout,
786           SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
787     }
788
789     if (!src->session) {
790       GST_ELEMENT_ERROR (src, LIBRARY, INIT,
791           (NULL), ("Failed to create async session"));
792       return FALSE;
793     }
794
795     g_signal_connect (src->session, "authenticate",
796         G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
797
798     /* Set up logging */
799     gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
800   } else {
801     GST_DEBUG_OBJECT (src, "Re-using session");
802   }
803
804   if (src->compress)
805     soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
806   else
807     soup_session_remove_feature_by_type (src->session,
808         SOUP_TYPE_CONTENT_DECODER);
809
810   return TRUE;
811 }
812
813 static void
814 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
815 {
816   if (src->session) {
817     soup_session_abort (src->session);  /* This unrefs the message. */
818     g_object_unref (src->session);
819     src->session = NULL;
820     src->msg = NULL;
821   }
822   if (src->loop) {
823     g_main_loop_unref (src->loop);
824     g_main_context_unref (src->context);
825     src->loop = NULL;
826     src->context = NULL;
827   }
828 }
829
830 static void
831 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
832     SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
833 {
834   if (!retrying) {
835     /* First time authentication only, if we fail and are called again with retry true fall through */
836     if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
837       if (src->user_id && src->user_pw)
838         soup_auth_authenticate (auth, src->user_id, src->user_pw);
839     } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
840       if (src->proxy_id && src->proxy_pw)
841         soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
842     }
843   }
844 }
845
846 static void
847 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
848 {
849   const char *value;
850   GstTagList *tag_list;
851   GstBaseSrc *basesrc;
852   guint64 newsize;
853   GHashTable *params = NULL;
854
855   GST_INFO_OBJECT (src, "got headers");
856
857   if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
858       src->proxy_id && src->proxy_pw)
859     return;
860
861   if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
862     src->redirection_uri = g_strdup (soup_message_headers_get_one
863         (msg->response_headers, "Location"));
864     GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
865         src->redirection_uri);
866     return;
867   }
868
869   if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
870     return;
871
872   src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
873   src->got_headers = TRUE;
874
875   /* Parse Content-Length. */
876   if (soup_message_headers_get_encoding (msg->response_headers) ==
877       SOUP_ENCODING_CONTENT_LENGTH) {
878     newsize = src->request_position +
879         soup_message_headers_get_content_length (msg->response_headers);
880     if (!src->have_size || (src->content_size != newsize)) {
881       src->content_size = newsize;
882       src->have_size = TRUE;
883       src->seekable = TRUE;
884       GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
885
886       basesrc = GST_BASE_SRC_CAST (src);
887       basesrc->segment.duration = src->content_size;
888       gst_element_post_message (GST_ELEMENT (src),
889           gst_message_new_duration_changed (GST_OBJECT (src)));
890     }
891   }
892
893   /* Icecast stuff */
894   tag_list = gst_tag_list_new_empty ();
895
896   if ((value =
897           soup_message_headers_get_one (msg->response_headers,
898               "icy-metaint")) != NULL) {
899     gint icy_metaint = atoi (value);
900
901     GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
902     if (icy_metaint > 0) {
903       if (src->src_caps)
904         gst_caps_unref (src->src_caps);
905
906       src->src_caps = gst_caps_new_simple ("application/x-icy",
907           "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
908
909       gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
910     }
911   }
912   if ((value =
913           soup_message_headers_get_content_type (msg->response_headers,
914               &params)) != NULL) {
915     GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
916     if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
917       gint channels = 2;
918       gint rate = 44100;
919       char *param;
920
921       if (src->src_caps)
922         gst_caps_unref (src->src_caps);
923
924       param = g_hash_table_lookup (params, "channels");
925       if (param != NULL)
926         channels = atol (param);
927
928       param = g_hash_table_lookup (params, "rate");
929       if (param != NULL)
930         rate = atol (param);
931
932       src->src_caps = gst_caps_new_simple ("audio/x-raw",
933           "format", G_TYPE_STRING, "S16BE",
934           "layout", G_TYPE_STRING, "interleaved",
935           "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
936
937       gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
938     } else {
939       /* Set the Content-Type field on the caps */
940       if (src->src_caps) {
941         src->src_caps = gst_caps_make_writable (src->src_caps);
942         gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
943             value, NULL);
944         gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
945       }
946     }
947   }
948
949   if (params != NULL)
950     g_hash_table_destroy (params);
951
952   if ((value =
953           soup_message_headers_get_one (msg->response_headers,
954               "icy-name")) != NULL) {
955     g_free (src->iradio_name);
956     src->iradio_name = gst_soup_http_src_unicodify (value);
957     if (src->iradio_name) {
958       gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
959           src->iradio_name, NULL);
960     }
961   }
962   if ((value =
963           soup_message_headers_get_one (msg->response_headers,
964               "icy-genre")) != NULL) {
965     g_free (src->iradio_genre);
966     src->iradio_genre = gst_soup_http_src_unicodify (value);
967     if (src->iradio_genre) {
968       gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
969           src->iradio_genre, NULL);
970     }
971   }
972   if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
973       != NULL) {
974     g_free (src->iradio_url);
975     src->iradio_url = gst_soup_http_src_unicodify (value);
976     if (src->iradio_url) {
977       gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
978           src->iradio_url, NULL);
979     }
980   }
981   if (!gst_tag_list_is_empty (tag_list)) {
982     GST_DEBUG_OBJECT (src,
983         "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
984     gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
985   } else {
986     gst_tag_list_unref (tag_list);
987   }
988
989   /* Handle HTTP errors. */
990   gst_soup_http_src_parse_status (msg, src);
991
992   /* Check if Range header was respected. */
993   if (src->ret == GST_FLOW_CUSTOM_ERROR &&
994       src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
995     src->seekable = FALSE;
996     GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
997         (_("Server does not support seeking.")),
998         ("Server does not accept Range HTTP header, URL: %s", src->location));
999     src->ret = GST_FLOW_ERROR;
1000   }
1001
1002   /* If we are going to error out, stop all processing right here, so we
1003    * don't output any data (such as an error html page), and return
1004    * GST_FLOW_ERROR from the create function instead of having
1005    * got_chunk_cb overwrite src->ret with FLOW_OK again. */
1006   if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
1007     gst_soup_http_src_session_pause_message (src);
1008
1009     if (src->loop)
1010       g_main_loop_quit (src->loop);
1011   }
1012   g_cond_signal (&src->request_finished_cond);
1013 }
1014
1015 /* Have body. Signal EOS. */
1016 static void
1017 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1018 {
1019   if (G_UNLIKELY (msg != src->msg)) {
1020     GST_DEBUG_OBJECT (src, "got body, but not for current message");
1021     return;
1022   }
1023   if (G_UNLIKELY (src->session_io_status !=
1024           GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1025     /* Probably a redirect. */
1026     return;
1027   }
1028   GST_DEBUG_OBJECT (src, "got body");
1029   src->ret = GST_FLOW_EOS;
1030   src->have_body = TRUE;
1031
1032   /* no need to interrupt the message here, we do it on the
1033    * finished_cb anyway if needed. And getting the body might mean
1034    * that the connection was hang up before finished. This happens when
1035    * the pipeline is stalled for too long (long pauses during playback).
1036    * Best to let it continue from here and pause because it reached the
1037    * final bytes based on content_size or received an out of range error */
1038 }
1039
1040 /* Finished. Signal EOS. */
1041 static void
1042 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1043 {
1044   if (G_UNLIKELY (msg != src->msg)) {
1045     GST_DEBUG_OBJECT (src, "finished, but not for current message");
1046     return;
1047   }
1048   GST_DEBUG_OBJECT (src, "finished");
1049   src->ret = GST_FLOW_EOS;
1050   if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1051     /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1052      * that occurred in the QUEUEING state; i.e. before the connection setup
1053      * was complete. Do nothing */
1054   } else if (src->session_io_status ==
1055       GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0 &&
1056       (src->have_size && src->read_position < src->content_size)) {
1057     /* The server disconnected while streaming. Reconnect and seeking to the
1058      * last location. */
1059     src->retry = TRUE;
1060     src->ret = GST_FLOW_CUSTOM_ERROR;
1061   } else if (G_UNLIKELY (src->session_io_status !=
1062           GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1063     if (msg->method == SOUP_METHOD_HEAD) {
1064       GST_DEBUG_OBJECT (src, "Ignoring error %d:%s during HEAD request",
1065           msg->status_code, msg->reason_phrase);
1066     } else {
1067       gst_soup_http_src_parse_status (msg, src);
1068     }
1069   }
1070   if (src->loop)
1071     g_main_loop_quit (src->loop);
1072   g_cond_signal (&src->request_finished_cond);
1073 }
1074
1075 /* Buffer lifecycle management.
1076  *
1077  * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1078  * Soup take control.
1079  * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1080  * associated with a SoupBuffer.
1081  * Soup reads HTTP data in the GstBuffer's data buffer.
1082  * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1083  * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1084  * increments its refcount (to 2), pauses the flow of data from the HTTP
1085  * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1086  * again and breaks out of the GMainLoop.
1087  * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1088  * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1089  * refcount (to 1).
1090  * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1091  * downstream element.
1092  * If Soup fails to read HTTP data, it does not call
1093  * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1094  * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1095  * refcount to 0, freeing it.
1096  */
1097
1098 typedef struct
1099 {
1100   GstBuffer *buffer;
1101   GstMapInfo map;
1102 } SoupGstChunk;
1103
1104 static void
1105 gst_soup_http_src_chunk_free (gpointer user_data)
1106 {
1107   SoupGstChunk *chunk = (SoupGstChunk *) user_data;
1108
1109   gst_buffer_unmap (chunk->buffer, &chunk->map);
1110   gst_buffer_unref (chunk->buffer);
1111   g_slice_free (SoupGstChunk, chunk);
1112 }
1113
1114 static SoupBuffer *
1115 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1116     gpointer user_data)
1117 {
1118   GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1119   GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1120   GstBuffer *gstbuf;
1121   SoupBuffer *soupbuf;
1122   gsize length;
1123   GstFlowReturn rc;
1124   SoupGstChunk *chunk;
1125
1126   if (max_len)
1127     length = MIN (basesrc->blocksize, max_len);
1128   else
1129     length = basesrc->blocksize;
1130   GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1131       length, max_len);
1132
1133   rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1134   if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1135     /* Failed to allocate buffer. Stall SoupSession and return error code
1136      * to create(). */
1137     src->ret = rc;
1138     g_main_loop_quit (src->loop);
1139     return NULL;
1140   }
1141
1142   chunk = g_slice_new0 (SoupGstChunk);
1143   chunk->buffer = gstbuf;
1144   gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1145
1146   soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1147       chunk, gst_soup_http_src_chunk_free);
1148
1149   return soupbuf;
1150 }
1151
1152 static void
1153 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1154     GstSoupHTTPSrc * src)
1155 {
1156   GstBaseSrc *basesrc;
1157   guint64 new_position;
1158   SoupGstChunk *gchunk;
1159
1160   if (G_UNLIKELY (msg != src->msg)) {
1161     GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1162     return;
1163   }
1164   if (G_UNLIKELY (!src->outbuf)) {
1165     GST_DEBUG_OBJECT (src, "got chunk but we're not expecting one");
1166     src->ret = GST_FLOW_OK;
1167     gst_soup_http_src_cancel_message (src);
1168     g_main_loop_quit (src->loop);
1169     return;
1170   }
1171
1172   src->have_body = FALSE;
1173   if (G_UNLIKELY (src->session_io_status !=
1174           GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1175     /* Probably a redirect. */
1176     return;
1177   }
1178   basesrc = GST_BASE_SRC_CAST (src);
1179   GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1180       chunk->length);
1181
1182   /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1183   gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1184   *src->outbuf = gchunk->buffer;
1185
1186   gst_buffer_resize (*src->outbuf, 0, chunk->length);
1187   GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1188
1189   gst_buffer_ref (*src->outbuf);
1190
1191   new_position = src->read_position + chunk->length;
1192   if (G_LIKELY (src->request_position == src->read_position))
1193     src->request_position = new_position;
1194   src->read_position = new_position;
1195
1196   if (src->have_size) {
1197     if (new_position > src->content_size) {
1198       GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1199           "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1200           src->content_size);
1201       src->content_size = new_position;
1202       basesrc->segment.duration = src->content_size;
1203       gst_element_post_message (GST_ELEMENT (src),
1204           gst_message_new_duration_changed (GST_OBJECT (src)));
1205     } else if (new_position == src->content_size) {
1206       GST_DEBUG_OBJECT (src, "We're EOS now");
1207     }
1208   }
1209
1210   src->ret = GST_FLOW_OK;
1211   g_main_loop_quit (src->loop);
1212   gst_soup_http_src_session_pause_message (src);
1213 }
1214
1215 static void
1216 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1217     GstSoupHTTPSrc * src)
1218 {
1219   if (G_UNLIKELY (msg != src->msg)) {
1220     GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1221         msg->status_code, msg->reason_phrase);
1222     return;
1223   }
1224   if (G_UNLIKELY (src->session_io_status !=
1225           GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1226       && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1227     /* Ignore redirections. */
1228     return;
1229   }
1230   GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1231       msg->reason_phrase);
1232   if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1233       src->read_position > 0 && (src->have_size
1234           && src->read_position < src->content_size)) {
1235     /* The server disconnected while streaming. Reconnect and seeking to the
1236      * last location. */
1237     src->retry = TRUE;
1238   } else
1239     gst_soup_http_src_parse_status (msg, src);
1240   /* The session's SoupMessage object expires after this callback returns. */
1241   src->msg = NULL;
1242   g_main_loop_quit (src->loop);
1243 }
1244
1245 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message)     \
1246   GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message),        \
1247       ("%s (%d), URL: %s", (soup_msg)->reason_phrase,                \
1248           (soup_msg)->status_code, (src)->location));
1249
1250 static void
1251 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1252 {
1253   if (msg->method == SOUP_METHOD_HEAD) {
1254     if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1255       GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1256           msg->status_code);
1257   } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1258     switch (msg->status_code) {
1259       case SOUP_STATUS_CANT_RESOLVE:
1260       case SOUP_STATUS_CANT_RESOLVE_PROXY:
1261         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1262             _("Could not resolve server name."));
1263         src->ret = GST_FLOW_ERROR;
1264         break;
1265       case SOUP_STATUS_CANT_CONNECT:
1266       case SOUP_STATUS_CANT_CONNECT_PROXY:
1267         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1268             _("Could not establish connection to server."));
1269         src->ret = GST_FLOW_ERROR;
1270         break;
1271       case SOUP_STATUS_SSL_FAILED:
1272         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1273             _("Secure connection setup failed."));
1274         src->ret = GST_FLOW_ERROR;
1275         break;
1276       case SOUP_STATUS_IO_ERROR:
1277         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1278             _("A network error occured, or the server closed the connection "
1279                 "unexpectedly."));
1280         src->ret = GST_FLOW_ERROR;
1281         break;
1282       case SOUP_STATUS_MALFORMED:
1283         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1284             _("Server sent bad data."));
1285         src->ret = GST_FLOW_ERROR;
1286         break;
1287       case SOUP_STATUS_CANCELLED:
1288         /* No error message when interrupted by program. */
1289         break;
1290     }
1291   } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1292       SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1293       SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1294     /* Report HTTP error. */
1295
1296     /* when content_size is unknown and we have just finished receiving
1297      * a body message, requests that go beyond the content limits will result
1298      * in an error. Here we convert those to EOS */
1299     if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1300         src->have_body && !src->have_size) {
1301       GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1302           "body, returning EOS");
1303       src->ret = GST_FLOW_EOS;
1304       return;
1305     }
1306
1307     /* FIXME: reason_phrase is not translated and not suitable for user
1308      * error dialog according to libsoup documentation.
1309      */
1310     if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1311       GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1312           ("%s", msg->reason_phrase),
1313           ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1314               src->location));
1315     } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED ||
1316         msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED ||
1317         msg->status_code == SOUP_STATUS_FORBIDDEN ||
1318         msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1319       GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED,
1320           ("%s", msg->reason_phrase),
1321           ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1322               src->location));
1323     } else {
1324       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1325           ("%s", msg->reason_phrase),
1326           ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1327               src->location));
1328     }
1329     src->ret = GST_FLOW_ERROR;
1330   }
1331 }
1332
1333 static gboolean
1334 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1335 {
1336   src->msg = soup_message_new (method, src->location);
1337   if (!src->msg) {
1338     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1339         ("Error parsing URL."), ("URL: %s", src->location));
1340     return FALSE;
1341   }
1342   src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1343   if (!src->keep_alive) {
1344     soup_message_headers_append (src->msg->request_headers, "Connection",
1345         "close");
1346   }
1347   if (src->iradio_mode) {
1348     soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1349         "1");
1350   }
1351   if (src->cookies) {
1352     gchar **cookie;
1353
1354     for (cookie = src->cookies; *cookie != NULL; cookie++) {
1355       soup_message_headers_append (src->msg->request_headers, "Cookie",
1356           *cookie);
1357     }
1358   }
1359   src->retry = FALSE;
1360
1361   g_signal_connect (src->msg, "got_headers",
1362       G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1363   g_signal_connect (src->msg, "got_body",
1364       G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1365   g_signal_connect (src->msg, "finished",
1366       G_CALLBACK (gst_soup_http_src_finished_cb), src);
1367   g_signal_connect (src->msg, "got_chunk",
1368       G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1369   soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1370       (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1371   soup_message_set_chunk_allocator (src->msg,
1372       gst_soup_http_src_chunk_allocator, src, NULL);
1373   gst_soup_http_src_add_range_header (src, src->request_position,
1374       src->stop_position);
1375
1376   gst_soup_http_src_add_extra_headers (src);
1377
1378   return TRUE;
1379 }
1380
1381 static GstFlowReturn
1382 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1383     GstBuffer ** outbuf)
1384 {
1385   /* If we're not OK, just go out of here */
1386   if (src->ret != GST_FLOW_OK) {
1387     GST_DEBUG_OBJECT (src, "Previous flow return not OK: %s",
1388         gst_flow_get_name (src->ret));
1389     return src->ret;
1390   }
1391
1392   GST_LOG_OBJECT (src, "Running request for method: %s", method);
1393   if (src->msg && (src->request_position != src->read_position)) {
1394     if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1395       gst_soup_http_src_add_range_header (src, src->request_position,
1396           src->stop_position);
1397     } else {
1398       GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1399           " to %" G_GUINT64_FORMAT ": requeueing connection request",
1400           src->read_position, src->request_position);
1401       gst_soup_http_src_cancel_message (src);
1402     }
1403   }
1404   if (!src->msg)
1405     if (!gst_soup_http_src_build_message (src, method)) {
1406       return GST_FLOW_ERROR;
1407     }
1408
1409   src->ret = GST_FLOW_CUSTOM_ERROR;
1410   src->outbuf = outbuf;
1411   do {
1412     if (src->interrupted) {
1413       GST_DEBUG_OBJECT (src, "interrupted");
1414       break;
1415     }
1416     if (src->retry) {
1417       GST_DEBUG_OBJECT (src, "Reconnecting");
1418       if (!gst_soup_http_src_build_message (src, method)) {
1419         return GST_FLOW_ERROR;
1420       }
1421       src->retry = FALSE;
1422       continue;
1423     }
1424     if (!src->msg) {
1425       GST_DEBUG_OBJECT (src, "EOS reached");
1426       break;
1427     }
1428
1429     switch (src->session_io_status) {
1430       case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1431         GST_DEBUG_OBJECT (src, "Queueing connection request");
1432         gst_soup_http_src_queue_message (src);
1433         break;
1434       case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1435         break;
1436       case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1437         gst_soup_http_src_session_unpause_message (src);
1438         break;
1439       case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1440         /* Impossible. */
1441         break;
1442     }
1443
1444     if (src->ret == GST_FLOW_CUSTOM_ERROR)
1445       g_main_loop_run (src->loop);
1446
1447   } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1448
1449   /* Let the request finish if we had a stop position and are there */
1450   if (src->ret == GST_FLOW_OK && src->stop_position != -1
1451       && src->read_position >= src->stop_position) {
1452     src->outbuf = NULL;
1453     gst_soup_http_src_session_unpause_message (src);
1454     g_main_loop_run (src->loop);
1455
1456     g_cond_signal (&src->request_finished_cond);
1457     /* Return OK unconditionally here, src->ret will
1458      * be most likely be EOS now but we want to
1459      * consume the buffer we got above */
1460     return GST_FLOW_OK;
1461   }
1462
1463   if (src->ret == GST_FLOW_CUSTOM_ERROR)
1464     src->ret = GST_FLOW_EOS;
1465   g_cond_signal (&src->request_finished_cond);
1466
1467   return src->ret;
1468 }
1469
1470 static GstFlowReturn
1471 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1472 {
1473   GstSoupHTTPSrc *src;
1474   GstFlowReturn ret;
1475
1476   src = GST_SOUP_HTTP_SRC (psrc);
1477
1478   g_mutex_lock (&src->mutex);
1479   *outbuf = NULL;
1480   ret = gst_soup_http_src_do_request (src, SOUP_METHOD_GET, outbuf);
1481   g_mutex_unlock (&src->mutex);
1482   return ret;
1483 }
1484
1485 static gboolean
1486 gst_soup_http_src_start (GstBaseSrc * bsrc)
1487 {
1488   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1489
1490   GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1491
1492   return gst_soup_http_src_session_open (src);
1493 }
1494
1495 static gboolean
1496 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1497 {
1498   GstSoupHTTPSrc *src;
1499
1500   src = GST_SOUP_HTTP_SRC (bsrc);
1501   GST_DEBUG_OBJECT (src, "stop()");
1502   if (src->keep_alive)
1503     gst_soup_http_src_cancel_message (src);
1504   else
1505     gst_soup_http_src_session_close (src);
1506
1507   gst_soup_http_src_reset (src);
1508   return TRUE;
1509 }
1510
1511 static GstStateChangeReturn
1512 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
1513 {
1514   GstStateChangeReturn ret;
1515   GstSoupHTTPSrc *src;
1516
1517   src = GST_SOUP_HTTP_SRC (element);
1518
1519   switch (transition) {
1520     case GST_STATE_CHANGE_READY_TO_NULL:
1521       gst_soup_http_src_session_close (src);
1522       break;
1523     default:
1524       break;
1525   }
1526
1527   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1528
1529   return ret;
1530 }
1531
1532 /* Interrupt a blocking request. */
1533 static gboolean
1534 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1535 {
1536   GstSoupHTTPSrc *src;
1537
1538   src = GST_SOUP_HTTP_SRC (bsrc);
1539   GST_DEBUG_OBJECT (src, "unlock()");
1540
1541   src->interrupted = TRUE;
1542   if (src->loop)
1543     g_main_loop_quit (src->loop);
1544   g_cond_signal (&src->request_finished_cond);
1545   return TRUE;
1546 }
1547
1548 /* Interrupt interrupt. */
1549 static gboolean
1550 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1551 {
1552   GstSoupHTTPSrc *src;
1553
1554   src = GST_SOUP_HTTP_SRC (bsrc);
1555   GST_DEBUG_OBJECT (src, "unlock_stop()");
1556
1557   src->interrupted = FALSE;
1558   return TRUE;
1559 }
1560
1561 static gboolean
1562 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1563 {
1564   GstSoupHTTPSrc *src;
1565
1566   src = GST_SOUP_HTTP_SRC (bsrc);
1567
1568   if (src->have_size) {
1569     GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1570         src->content_size);
1571     *size = src->content_size;
1572     return TRUE;
1573   }
1574   GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1575   return FALSE;
1576 }
1577
1578 static void
1579 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1580 {
1581   GstFlowReturn ret = GST_FLOW_OK;
1582
1583   /* Special case to check if the server allows range requests
1584    * before really starting to get data in the buffer creation
1585    * loops.
1586    */
1587   if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1588     g_mutex_lock (&src->mutex);
1589     while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1590       if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1591           src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1592         /* wait for the current request to finish */
1593         g_cond_wait (&src->request_finished_cond, &src->mutex);
1594       } else {
1595         if (gst_soup_http_src_session_open (src)) {
1596           ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1597         }
1598       }
1599     }
1600     if (src->ret == GST_FLOW_EOS) {
1601       /* A HEAD request shouldn't lead to EOS */
1602       src->ret = GST_FLOW_OK;
1603     }
1604     /* resets status to idle */
1605     gst_soup_http_src_cancel_message (src);
1606     g_mutex_unlock (&src->mutex);
1607   }
1608
1609 }
1610
1611 static gboolean
1612 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1613 {
1614   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1615
1616   gst_soup_http_src_check_seekable (src);
1617
1618   return src->seekable;
1619 }
1620
1621 static gboolean
1622 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1623 {
1624   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1625
1626   GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1627       ")", segment->start, segment->stop);
1628   if (src->read_position == segment->start &&
1629       src->request_position == src->read_position &&
1630       src->stop_position == segment->stop) {
1631     GST_DEBUG_OBJECT (src,
1632         "Seek to current read/end position and no seek pending");
1633     return TRUE;
1634   }
1635
1636   gst_soup_http_src_check_seekable (src);
1637
1638   /* If we have no headers we don't know yet if it is seekable or not.
1639    * Store the start position and error out later if it isn't */
1640   if (src->got_headers && !src->seekable) {
1641     GST_WARNING_OBJECT (src, "Not seekable");
1642     return FALSE;
1643   }
1644
1645   if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1646     GST_WARNING_OBJECT (src, "Invalid seek segment");
1647     return FALSE;
1648   }
1649
1650   if (src->have_size && segment->start >= src->content_size) {
1651     GST_WARNING_OBJECT (src,
1652         "Potentially seeking behind end of file, might EOS immediately");
1653   }
1654
1655   /* Wait for create() to handle the jump in offset. */
1656   src->request_position = segment->start;
1657   src->stop_position = segment->stop;
1658
1659   return TRUE;
1660 }
1661
1662 static gboolean
1663 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1664 {
1665   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1666   gboolean ret;
1667   GstSchedulingFlags flags;
1668   gint minsize, maxsize, align;
1669
1670   switch (GST_QUERY_TYPE (query)) {
1671     case GST_QUERY_URI:
1672       gst_query_set_uri (query, src->location);
1673       if (src->redirection_uri != NULL)
1674         gst_query_set_uri_redirection (query, src->redirection_uri);
1675       ret = TRUE;
1676       break;
1677     default:
1678       ret = FALSE;
1679       break;
1680   }
1681
1682   if (!ret)
1683     ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1684
1685   switch (GST_QUERY_TYPE (query)) {
1686     case GST_QUERY_SCHEDULING:
1687       gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1688       flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1689       gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1690       break;
1691     default:
1692       break;
1693   }
1694
1695   return ret;
1696 }
1697
1698 static gboolean
1699 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1700     GError ** error)
1701 {
1702   const char *alt_schemes[] = { "icy://", "icyx://" };
1703   guint i;
1704
1705   if (src->location) {
1706     g_free (src->location);
1707     src->location = NULL;
1708   }
1709
1710   if (uri == NULL)
1711     return FALSE;
1712
1713   for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1714     if (g_str_has_prefix (uri, alt_schemes[i])) {
1715       src->location =
1716           g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1717       return TRUE;
1718     }
1719   }
1720
1721   if (src->redirection_uri) {
1722     g_free (src->redirection_uri);
1723     src->redirection_uri = NULL;
1724   }
1725
1726   src->location = g_strdup (uri);
1727
1728   return TRUE;
1729 }
1730
1731 static gboolean
1732 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1733 {
1734   if (src->proxy) {
1735     soup_uri_free (src->proxy);
1736     src->proxy = NULL;
1737   }
1738   if (g_str_has_prefix (uri, "http://")) {
1739     src->proxy = soup_uri_new (uri);
1740   } else {
1741     gchar *new_uri = g_strconcat ("http://", uri, NULL);
1742
1743     src->proxy = soup_uri_new (new_uri);
1744     g_free (new_uri);
1745   }
1746
1747   return TRUE;
1748 }
1749
1750 static guint
1751 gst_soup_http_src_uri_get_type (GType type)
1752 {
1753   return GST_URI_SRC;
1754 }
1755
1756 static const gchar *const *
1757 gst_soup_http_src_uri_get_protocols (GType type)
1758 {
1759   static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
1760
1761   return protocols;
1762 }
1763
1764 static gchar *
1765 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1766 {
1767   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1768
1769   /* FIXME: make thread-safe */
1770   return g_strdup (src->location);
1771 }
1772
1773 static gboolean
1774 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1775     GError ** error)
1776 {
1777   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1778
1779   return gst_soup_http_src_set_location (src, uri, error);
1780 }
1781
1782 static void
1783 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1784 {
1785   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1786
1787   iface->get_type = gst_soup_http_src_uri_get_type;
1788   iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1789   iface->get_uri = gst_soup_http_src_uri_get_uri;
1790   iface->set_uri = gst_soup_http_src_uri_set_uri;
1791 }