souphttpsrc: Remove cookie_jar patch
[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 ! mpgaudioparse
47  *     ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink
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 #define GST_SOUP_SESSION_CONTEXT "gst.soup.session"
90
91 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
92     GST_PAD_SRC,
93     GST_PAD_ALWAYS,
94     GST_STATIC_CAPS_ANY);
95
96 enum
97 {
98   PROP_0,
99   PROP_LOCATION,
100   PROP_IS_LIVE,
101   PROP_USER_AGENT,
102   PROP_AUTOMATIC_REDIRECT,
103   PROP_PROXY,
104   PROP_USER_ID,
105   PROP_USER_PW,
106   PROP_PROXY_ID,
107   PROP_PROXY_PW,
108   PROP_COOKIES,
109   PROP_IRADIO_MODE,
110   PROP_TIMEOUT,
111   PROP_EXTRA_HEADERS,
112   PROP_SOUP_LOG_LEVEL,
113   PROP_COMPRESS,
114   PROP_KEEP_ALIVE,
115   PROP_SSL_STRICT,
116   PROP_SSL_CA_FILE,
117   PROP_SSL_USE_SYSTEM_CA_FILE,
118   PROP_TLS_DATABASE,
119   PROP_RETRIES,
120   PROP_METHOD,
121   PROP_TLS_INTERACTION,
122 };
123
124 #define DEFAULT_USER_AGENT           "GStreamer souphttpsrc " PACKAGE_VERSION " "
125 #define DEFAULT_IRADIO_MODE          TRUE
126 #define DEFAULT_SOUP_LOG_LEVEL       SOUP_LOGGER_LOG_HEADERS
127 #define DEFAULT_COMPRESS             FALSE
128 #define DEFAULT_KEEP_ALIVE           TRUE
129 #define DEFAULT_SSL_STRICT           TRUE
130 #define DEFAULT_SSL_CA_FILE          NULL
131 #define DEFAULT_SSL_USE_SYSTEM_CA_FILE TRUE
132 #define DEFAULT_TLS_DATABASE         NULL
133 #define DEFAULT_TLS_INTERACTION      NULL
134 #define DEFAULT_TIMEOUT              15
135 #define DEFAULT_RETRIES              3
136 #define DEFAULT_SOUP_METHOD          NULL
137
138 #define GROW_BLOCKSIZE_LIMIT 1
139 #define GROW_BLOCKSIZE_COUNT 1
140 #define GROW_BLOCKSIZE_FACTOR 2
141 #define REDUCE_BLOCKSIZE_LIMIT 0.20
142 #define REDUCE_BLOCKSIZE_COUNT 2
143 #define REDUCE_BLOCKSIZE_FACTOR 0.5
144 #define GROW_TIME_LIMIT (1 * GST_SECOND)
145
146 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
147 #define DLNA_OP_TIMED_SEEK  0x02
148 #define DLNA_OP_BYTE_SEEK   0x01
149 #endif
150
151 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
152     gpointer iface_data);
153 static void gst_soup_http_src_finalize (GObject * gobject);
154 static void gst_soup_http_src_dispose (GObject * gobject);
155
156 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
157     const GValue * value, GParamSpec * pspec);
158 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
159     GValue * value, GParamSpec * pspec);
160
161 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
162     element, GstStateChange transition);
163 static void gst_soup_http_src_set_context (GstElement * element,
164     GstContext * context);
165 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
166     GstBuffer ** outbuf);
167 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
168 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
169 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
170 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
171 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
172     GstSegment * segment);
173 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
174 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
175 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
176 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
177     const gchar * uri, GError ** error);
178 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
179     const gchar * uri);
180 static char *gst_soup_http_src_unicodify (const char *str);
181 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
182     const gchar * method);
183 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
184 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
185     guint64 offset, guint64 stop_offset);
186 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
187 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
188 static GstFlowReturn gst_soup_http_src_parse_status (SoupMessage * msg,
189     GstSoupHTTPSrc * src);
190 static GstFlowReturn gst_soup_http_src_got_headers (GstSoupHTTPSrc * src,
191     SoupMessage * msg);
192 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
193     SoupMessage * msg, SoupAuth * auth, gboolean retrying,
194     GstSoupHTTPSrc * src);
195
196 #define gst_soup_http_src_parent_class parent_class
197 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
198     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
199         gst_soup_http_src_uri_handler_init));
200
201 static void
202 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
203 {
204   GObjectClass *gobject_class;
205   GstElementClass *gstelement_class;
206   GstBaseSrcClass *gstbasesrc_class;
207   GstPushSrcClass *gstpushsrc_class;
208
209   gobject_class = (GObjectClass *) klass;
210   gstelement_class = (GstElementClass *) klass;
211   gstbasesrc_class = (GstBaseSrcClass *) klass;
212   gstpushsrc_class = (GstPushSrcClass *) klass;
213
214   gobject_class->set_property = gst_soup_http_src_set_property;
215   gobject_class->get_property = gst_soup_http_src_get_property;
216   gobject_class->finalize = gst_soup_http_src_finalize;
217   gobject_class->dispose = gst_soup_http_src_dispose;
218
219   g_object_class_install_property (gobject_class,
220       PROP_LOCATION,
221       g_param_spec_string ("location", "Location",
222           "Location to read from", "",
223           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224   g_object_class_install_property (gobject_class,
225       PROP_USER_AGENT,
226       g_param_spec_string ("user-agent", "User-Agent",
227           "Value of the User-Agent HTTP request header field",
228           DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229   g_object_class_install_property (gobject_class,
230       PROP_AUTOMATIC_REDIRECT,
231       g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
232           "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
233           TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234   g_object_class_install_property (gobject_class,
235       PROP_PROXY,
236       g_param_spec_string ("proxy", "Proxy",
237           "HTTP proxy server URI", "",
238           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239   g_object_class_install_property (gobject_class,
240       PROP_USER_ID,
241       g_param_spec_string ("user-id", "user-id",
242           "HTTP location URI user id for authentication", "",
243           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244   g_object_class_install_property (gobject_class, PROP_USER_PW,
245       g_param_spec_string ("user-pw", "user-pw",
246           "HTTP location URI user password for authentication", "",
247           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248   g_object_class_install_property (gobject_class, PROP_PROXY_ID,
249       g_param_spec_string ("proxy-id", "proxy-id",
250           "HTTP proxy URI user id for authentication", "",
251           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252   g_object_class_install_property (gobject_class, PROP_PROXY_PW,
253       g_param_spec_string ("proxy-pw", "proxy-pw",
254           "HTTP proxy URI user password for authentication", "",
255           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256   g_object_class_install_property (gobject_class, PROP_COOKIES,
257       g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
258           G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
260       g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
261           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262   g_object_class_install_property (gobject_class, PROP_TIMEOUT,
263       g_param_spec_uint ("timeout", "timeout",
264           "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
265           3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266   g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
267       g_param_spec_boxed ("extra-headers", "Extra Headers",
268           "Extra headers to append to the HTTP request",
269           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270   g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
271       g_param_spec_boolean ("iradio-mode", "iradio-mode",
272           "Enable internet radio mode (ask server to send shoutcast/icecast "
273           "metadata interleaved with the actual stream data)",
274           DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275
276  /**
277    * GstSoupHTTPSrc::http-log-level:
278    *
279    * If set and > 0, captures and dumps HTTP session data as
280    * log messages if log level >= GST_LEVEL_TRACE
281    *
282    * Since: 1.4
283    */
284   g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
285       g_param_spec_enum ("http-log-level", "HTTP log level",
286           "Set log level for soup's HTTP session log",
287           SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
288           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289
290  /**
291    * GstSoupHTTPSrc::compress:
292    *
293    * If set to %TRUE, souphttpsrc will automatically handle gzip
294    * and deflate Content-Encodings. This does not make much difference
295    * and causes more load for normal media files, but makes a real
296    * difference in size for plaintext files.
297    *
298    * Since: 1.4
299    */
300   g_object_class_install_property (gobject_class, PROP_COMPRESS,
301       g_param_spec_boolean ("compress", "Compress",
302           "Allow compressed content encodings",
303           DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304
305  /**
306    * GstSoupHTTPSrc::keep-alive:
307    *
308    * If set to %TRUE, souphttpsrc will keep alive connections when being
309    * set to READY state and only will close connections when connecting
310    * to a different server or when going to NULL state..
311    *
312    * Since: 1.4
313    */
314   g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
315       g_param_spec_boolean ("keep-alive", "keep-alive",
316           "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
317           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318
319  /**
320    * GstSoupHTTPSrc::ssl-strict:
321    *
322    * If set to %TRUE, souphttpsrc will reject all SSL certificates that
323    * are considered invalid.
324    *
325    * Since: 1.4
326    */
327   g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
328       g_param_spec_boolean ("ssl-strict", "SSL Strict",
329           "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
330           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331
332  /**
333    * GstSoupHTTPSrc::ssl-ca-file:
334    *
335    * A SSL anchor CA file that should be used for checking certificates
336    * instead of the system CA file.
337    *
338    * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
339    * value will be ignored.
340    *
341    * Deprecated: Use #GstSoupHTTPSrc::tls-database property instead.
342    * Since: 1.4
343    */
344   g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
345       g_param_spec_string ("ssl-ca-file", "SSL CA File",
346           "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
347           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348
349  /**
350    * GstSoupHTTPSrc::ssl-use-system-ca-file:
351    *
352    * If set to %TRUE, souphttpsrc will use the system's CA file for
353    * checking certificates, unless #GstSoupHTTPSrc::ssl-ca-file or
354    * #GstSoupHTTPSrc::tls-database are non-%NULL.
355    *
356    * Since: 1.4
357    */
358   g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
359       g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
360           "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
361           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362
363   /**
364    * GstSoupHTTPSrc::tls-database:
365    *
366    * TLS database with anchor certificate authorities used to validate
367    * the server certificate.
368    *
369    * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
370    * and #GstSoupHTTPSrc::ssl-ca-file values will be ignored.
371    *
372    * Since: 1.6
373    */
374   g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
375       g_param_spec_object ("tls-database", "TLS database",
376           "TLS database with anchor certificate authorities used to validate the server certificate",
377           G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378
379   /**
380    * GstSoupHTTPSrc::tls-interaction:
381    *
382    * A #GTlsInteraction object to be used when the connection or certificate
383    * database need to interact with the user. This will be used to prompt the
384    * user for passwords or certificate where necessary.
385    *
386    * Since: 1.8
387    */
388   g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
389       g_param_spec_object ("tls-interaction", "TLS interaction",
390           "A GTlsInteraction object to be used when the connection or certificate database need to interact with the user.",
391           G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392
393  /**
394    * GstSoupHTTPSrc::retries:
395    *
396    * Maximum number of retries until giving up.
397    *
398    * Since: 1.4
399    */
400   g_object_class_install_property (gobject_class, PROP_RETRIES,
401       g_param_spec_int ("retries", "Retries",
402           "Maximum number of retries until giving up (-1=infinite)", -1,
403           G_MAXINT, DEFAULT_RETRIES,
404           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405
406  /**
407    * GstSoupHTTPSrc::method
408    *
409    * The HTTP method to use when making a request
410    *
411    * Since: 1.6
412    */
413   g_object_class_install_property (gobject_class, PROP_METHOD,
414       g_param_spec_string ("method", "HTTP method",
415           "The HTTP method to use (GET, HEAD, OPTIONS, etc)",
416           DEFAULT_SOUP_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417
418   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
419
420   gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
421       "Source/Network",
422       "Receive data as a client over the network via HTTP using SOUP",
423       "Wouter Cloetens <wouter@mind.be>");
424   gstelement_class->change_state =
425       GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
426   gstelement_class->set_context =
427       GST_DEBUG_FUNCPTR (gst_soup_http_src_set_context);
428
429   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
430   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
431   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
432   gstbasesrc_class->unlock_stop =
433       GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
434   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
435   gstbasesrc_class->is_seekable =
436       GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
437   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
438   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
439
440   gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
441
442   GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
443       "SOUP HTTP src");
444 }
445
446 static void
447 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
448 {
449   src->retry_count = 0;
450   src->have_size = FALSE;
451   src->got_headers = FALSE;
452   src->seekable = FALSE;
453   src->read_position = 0;
454   src->request_position = 0;
455   src->stop_position = -1;
456   src->content_size = 0;
457   src->have_body = FALSE;
458
459   src->reduce_blocksize_count = 0;
460   src->increase_blocksize_count = 0;
461   src->last_socket_read_time = 0;
462
463 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
464   if (src->dash_oldest_segment) {
465     g_free (src->dash_oldest_segment);
466     src->dash_oldest_segment = NULL;
467   }
468   if (src->dash_newest_segment) {
469     g_free (src->dash_newest_segment);
470     src->dash_newest_segment = NULL;
471   }
472   src->dlna_opt = 0;
473 #endif
474
475   g_cancellable_reset (src->cancellable);
476   g_mutex_lock (&src->mutex);
477   if (src->input_stream) {
478     g_object_unref (src->input_stream);
479     src->input_stream = NULL;
480   }
481   g_mutex_unlock (&src->mutex);
482
483   gst_caps_replace (&src->src_caps, NULL);
484   g_free (src->iradio_name);
485   src->iradio_name = NULL;
486   g_free (src->iradio_genre);
487   src->iradio_genre = NULL;
488   g_free (src->iradio_url);
489   src->iradio_url = NULL;
490 }
491
492 static void
493 gst_soup_http_src_init (GstSoupHTTPSrc * src)
494 {
495   const gchar *proxy;
496
497   g_mutex_init (&src->mutex);
498   g_cond_init (&src->have_headers_cond);
499   src->cancellable = g_cancellable_new ();
500   src->location = NULL;
501   src->redirection_uri = NULL;
502   src->automatic_redirect = TRUE;
503   src->user_agent = g_strdup (DEFAULT_USER_AGENT);
504   src->user_id = NULL;
505   src->user_pw = NULL;
506   src->proxy_id = NULL;
507   src->proxy_pw = NULL;
508   src->cookies = NULL;
509   src->iradio_mode = DEFAULT_IRADIO_MODE;
510   src->session = NULL;
511   src->external_session = NULL;
512   src->forced_external_session = FALSE;
513   src->msg = NULL;
514   src->timeout = DEFAULT_TIMEOUT;
515   src->log_level = DEFAULT_SOUP_LOG_LEVEL;
516   src->compress = DEFAULT_COMPRESS;
517   src->keep_alive = DEFAULT_KEEP_ALIVE;
518   src->ssl_strict = DEFAULT_SSL_STRICT;
519   src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
520   src->tls_database = DEFAULT_TLS_DATABASE;
521   src->tls_interaction = DEFAULT_TLS_INTERACTION;
522   src->max_retries = DEFAULT_RETRIES;
523   src->method = DEFAULT_SOUP_METHOD;
524   src->minimum_blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
525 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
526   src->dash_oldest_segment = NULL;
527   src->dash_newest_segment = NULL;
528   src->received_total = 0;
529   src->dlna_opt = 0;
530 #endif
531   proxy = g_getenv ("http_proxy");
532   if (!gst_soup_http_src_set_proxy (src, proxy)) {
533     GST_WARNING_OBJECT (src,
534         "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
535         proxy);
536   }
537
538   gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
539
540   gst_soup_http_src_reset (src);
541 }
542
543 static void
544 gst_soup_http_src_dispose (GObject * gobject)
545 {
546   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
547
548   GST_DEBUG_OBJECT (src, "dispose");
549
550   gst_soup_http_src_session_close (src);
551
552   if (src->external_session) {
553     g_object_unref (src->external_session);
554     src->external_session = NULL;
555   }
556
557   G_OBJECT_CLASS (parent_class)->dispose (gobject);
558 }
559
560 static void
561 gst_soup_http_src_finalize (GObject * gobject)
562 {
563   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
564
565   GST_DEBUG_OBJECT (src, "finalize");
566
567   g_mutex_clear (&src->mutex);
568   g_cond_clear (&src->have_headers_cond);
569   g_object_unref (src->cancellable);
570   g_free (src->location);
571   g_free (src->redirection_uri);
572   g_free (src->user_agent);
573   if (src->proxy != NULL) {
574     soup_uri_free (src->proxy);
575   }
576   g_free (src->user_id);
577   g_free (src->user_pw);
578   g_free (src->proxy_id);
579   g_free (src->proxy_pw);
580   g_strfreev (src->cookies);
581
582   if (src->extra_headers) {
583     gst_structure_free (src->extra_headers);
584     src->extra_headers = NULL;
585   }
586
587   g_free (src->ssl_ca_file);
588
589   if (src->tls_database)
590     g_object_unref (src->tls_database);
591   g_free (src->method);
592
593   if (src->tls_interaction)
594     g_object_unref (src->tls_interaction);
595
596   G_OBJECT_CLASS (parent_class)->finalize (gobject);
597 }
598
599 static void
600 gst_soup_http_src_set_property (GObject * object, guint prop_id,
601     const GValue * value, GParamSpec * pspec)
602 {
603   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
604
605   switch (prop_id) {
606     case PROP_LOCATION:
607     {
608       const gchar *location;
609
610       location = g_value_get_string (value);
611
612       if (location == NULL) {
613         GST_WARNING ("location property cannot be NULL");
614         goto done;
615       }
616       if (!gst_soup_http_src_set_location (src, location, NULL)) {
617         GST_WARNING ("badly formatted location");
618         goto done;
619       }
620       break;
621     }
622     case PROP_USER_AGENT:
623       g_free (src->user_agent);
624       src->user_agent = g_value_dup_string (value);
625       break;
626     case PROP_IRADIO_MODE:
627       src->iradio_mode = g_value_get_boolean (value);
628       break;
629     case PROP_AUTOMATIC_REDIRECT:
630       src->automatic_redirect = g_value_get_boolean (value);
631       break;
632     case PROP_PROXY:
633     {
634       const gchar *proxy;
635
636       proxy = g_value_get_string (value);
637       if (!gst_soup_http_src_set_proxy (src, proxy)) {
638         GST_WARNING ("badly formatted proxy URI");
639         goto done;
640       }
641       break;
642     }
643     case PROP_COOKIES:
644       g_strfreev (src->cookies);
645       src->cookies = g_strdupv (g_value_get_boxed (value));
646       break;
647     case PROP_IS_LIVE:
648       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
649       break;
650     case PROP_USER_ID:
651       g_free (src->user_id);
652       src->user_id = g_value_dup_string (value);
653       break;
654     case PROP_USER_PW:
655       g_free (src->user_pw);
656       src->user_pw = g_value_dup_string (value);
657       break;
658     case PROP_PROXY_ID:
659       g_free (src->proxy_id);
660       src->proxy_id = g_value_dup_string (value);
661       break;
662     case PROP_PROXY_PW:
663       g_free (src->proxy_pw);
664       src->proxy_pw = g_value_dup_string (value);
665       break;
666     case PROP_TIMEOUT:
667       src->timeout = g_value_get_uint (value);
668       break;
669     case PROP_EXTRA_HEADERS:{
670       const GstStructure *s = gst_value_get_structure (value);
671
672       if (src->extra_headers)
673         gst_structure_free (src->extra_headers);
674
675       src->extra_headers = s ? gst_structure_copy (s) : NULL;
676       break;
677     }
678     case PROP_SOUP_LOG_LEVEL:
679       src->log_level = g_value_get_enum (value);
680       break;
681     case PROP_COMPRESS:
682       src->compress = g_value_get_boolean (value);
683       break;
684     case PROP_KEEP_ALIVE:
685       src->keep_alive = g_value_get_boolean (value);
686       break;
687     case PROP_SSL_STRICT:
688       src->ssl_strict = g_value_get_boolean (value);
689       break;
690     case PROP_SSL_CA_FILE:
691       g_free (src->ssl_ca_file);
692       src->ssl_ca_file = g_value_dup_string (value);
693       break;
694     case PROP_SSL_USE_SYSTEM_CA_FILE:
695       src->ssl_use_system_ca_file = g_value_get_boolean (value);
696       break;
697     case PROP_TLS_DATABASE:
698       g_clear_object (&src->tls_database);
699       src->tls_database = g_value_dup_object (value);
700       break;
701     case PROP_TLS_INTERACTION:
702       g_clear_object (&src->tls_interaction);
703       src->tls_interaction = g_value_dup_object (value);
704       break;
705     case PROP_RETRIES:
706       src->max_retries = g_value_get_int (value);
707       break;
708     case PROP_METHOD:
709       g_free (src->method);
710       src->method = g_value_dup_string (value);
711       break;
712     default:
713       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
714       break;
715   }
716 done:
717   return;
718 }
719
720 static void
721 gst_soup_http_src_get_property (GObject * object, guint prop_id,
722     GValue * value, GParamSpec * pspec)
723 {
724   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
725
726   switch (prop_id) {
727     case PROP_LOCATION:
728       g_value_set_string (value, src->location);
729       break;
730     case PROP_USER_AGENT:
731       g_value_set_string (value, src->user_agent);
732       break;
733     case PROP_AUTOMATIC_REDIRECT:
734       g_value_set_boolean (value, src->automatic_redirect);
735       break;
736     case PROP_PROXY:
737       if (src->proxy == NULL)
738         g_value_set_static_string (value, "");
739       else {
740         char *proxy = soup_uri_to_string (src->proxy, FALSE);
741         g_value_set_string (value, proxy);
742         g_free (proxy);
743       }
744       break;
745     case PROP_COOKIES:
746       g_value_set_boxed (value, g_strdupv (src->cookies));
747       break;
748     case PROP_IS_LIVE:
749       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
750       break;
751     case PROP_IRADIO_MODE:
752       g_value_set_boolean (value, src->iradio_mode);
753       break;
754     case PROP_USER_ID:
755       g_value_set_string (value, src->user_id);
756       break;
757     case PROP_USER_PW:
758       g_value_set_string (value, src->user_pw);
759       break;
760     case PROP_PROXY_ID:
761       g_value_set_string (value, src->proxy_id);
762       break;
763     case PROP_PROXY_PW:
764       g_value_set_string (value, src->proxy_pw);
765       break;
766     case PROP_TIMEOUT:
767       g_value_set_uint (value, src->timeout);
768       break;
769     case PROP_EXTRA_HEADERS:
770       gst_value_set_structure (value, src->extra_headers);
771       break;
772     case PROP_SOUP_LOG_LEVEL:
773       g_value_set_enum (value, src->log_level);
774       break;
775     case PROP_COMPRESS:
776       g_value_set_boolean (value, src->compress);
777       break;
778     case PROP_KEEP_ALIVE:
779       g_value_set_boolean (value, src->keep_alive);
780       break;
781     case PROP_SSL_STRICT:
782       g_value_set_boolean (value, src->ssl_strict);
783       break;
784     case PROP_SSL_CA_FILE:
785       g_value_set_string (value, src->ssl_ca_file);
786       break;
787     case PROP_SSL_USE_SYSTEM_CA_FILE:
788       g_value_set_boolean (value, src->ssl_use_system_ca_file);
789       break;
790     case PROP_TLS_DATABASE:
791       g_value_set_object (value, src->tls_database);
792       break;
793     case PROP_TLS_INTERACTION:
794       g_value_set_object (value, src->tls_interaction);
795       break;
796     case PROP_RETRIES:
797       g_value_set_int (value, src->max_retries);
798       break;
799     case PROP_METHOD:
800       g_value_set_string (value, src->method);
801       break;
802     default:
803       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
804       break;
805   }
806 }
807
808 static gchar *
809 gst_soup_http_src_unicodify (const gchar * str)
810 {
811   const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
812     "GST_TAG_ENCODING", NULL
813   };
814
815   return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
816 }
817
818 static void
819 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
820 {
821   g_cancellable_cancel (src->cancellable);
822   g_cond_signal (&src->have_headers_cond);
823 }
824
825 static gboolean
826 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
827     guint64 stop_offset)
828 {
829   gchar buf[64];
830   gint rc;
831
832   soup_message_headers_remove (src->msg->request_headers, "Range");
833
834 /* This changes are needed to enable Seekable Contents from server.
835    We have observed that , for few specific networks ( VODAFONE ) , without theabove headers ,
836    Youtube is sending non-seekable contents to the Client. */
837 #ifndef TIZEN_FEATURE_SOUP_MODIFICATION
838   if (offset || stop_offset != -1)
839 #endif
840   {
841     if (stop_offset != -1) {
842       g_assert (offset != stop_offset);
843
844       rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
845           G_GUINT64_FORMAT, offset, (stop_offset > 0) ? stop_offset - 1 :
846           stop_offset);
847     } else {
848       rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
849           offset);
850     }
851     if (rc > sizeof (buf) || rc < 0)
852       return FALSE;
853     soup_message_headers_append (src->msg->request_headers, "Range", buf);
854   }
855   src->read_position = offset;
856   return TRUE;
857 }
858
859 static gboolean
860 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
861 {
862   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
863   const gchar *field_name = g_quark_to_string (field_id);
864   gchar *field_content = NULL;
865
866   if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
867     field_content = g_value_dup_string (value);
868   } else {
869     GValue dest = { 0, };
870
871     g_value_init (&dest, G_TYPE_STRING);
872     if (g_value_transform (value, &dest)) {
873       field_content = g_value_dup_string (&dest);
874     }
875   }
876
877   if (field_content == NULL) {
878     GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
879         "or can't be converted to a string", field_name);
880     return FALSE;
881   }
882
883   GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
884       field_content);
885   soup_message_headers_append (src->msg->request_headers, field_name,
886       field_content);
887
888   g_free (field_content);
889
890   return TRUE;
891 }
892
893 static gboolean
894 _append_extra_headers (GQuark field_id, const GValue * value,
895     gpointer user_data)
896 {
897   if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
898     guint n = gst_value_array_get_size (value);
899     guint i;
900
901     for (i = 0; i < n; i++) {
902       const GValue *v = gst_value_array_get_value (value, i);
903
904       if (!_append_extra_header (field_id, v, user_data))
905         return FALSE;
906     }
907   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
908     guint n = gst_value_list_get_size (value);
909     guint i;
910
911     for (i = 0; i < n; i++) {
912       const GValue *v = gst_value_list_get_value (value, i);
913
914       if (!_append_extra_header (field_id, v, user_data))
915         return FALSE;
916     }
917   } else {
918     return _append_extra_header (field_id, value, user_data);
919   }
920
921   return TRUE;
922 }
923
924
925 static gboolean
926 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
927 {
928   if (!src->extra_headers)
929     return TRUE;
930
931   return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
932 }
933
934 static gboolean
935 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
936 {
937   if (src->session) {
938     GST_DEBUG_OBJECT (src, "Session is already open");
939     return TRUE;
940   }
941
942   if (!src->location) {
943     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
944         ("Missing location property"));
945     return FALSE;
946   }
947
948   if (!src->session) {
949     GstQuery *query;
950     gboolean can_share = (src->timeout == DEFAULT_TIMEOUT)
951         && (src->ssl_strict == DEFAULT_SSL_STRICT)
952         && (src->tls_interaction == NULL) && (src->proxy == NULL)
953         && (src->tls_database == DEFAULT_TLS_DATABASE)
954         && (src->ssl_ca_file == DEFAULT_SSL_CA_FILE)
955         && (src->ssl_use_system_ca_file == DEFAULT_SSL_USE_SYSTEM_CA_FILE);
956
957     query = gst_query_new_context (GST_SOUP_SESSION_CONTEXT);
958     if (gst_pad_peer_query (GST_BASE_SRC_PAD (src), query)) {
959       GstContext *context;
960
961       gst_query_parse_context (query, &context);
962       gst_element_set_context (GST_ELEMENT_CAST (src), context);
963     } else {
964       GstMessage *message;
965
966       message =
967           gst_message_new_need_context (GST_OBJECT_CAST (src),
968           GST_SOUP_SESSION_CONTEXT);
969       gst_element_post_message (GST_ELEMENT_CAST (src), message);
970     }
971     gst_query_unref (query);
972
973     GST_OBJECT_LOCK (src);
974     if (src->external_session && (can_share || src->forced_external_session)) {
975       GST_DEBUG_OBJECT (src, "Using external session %p",
976           src->external_session);
977       src->session = g_object_ref (src->external_session);
978       src->session_is_shared = TRUE;
979     } else {
980       GST_DEBUG_OBJECT (src, "Creating session (can share %d)", can_share);
981
982       /* We explicitly set User-Agent to NULL here and overwrite it per message
983        * to be able to have the same session with different User-Agents per
984        * source */
985       if (src->proxy == NULL) {
986         src->session =
987             soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
988             NULL, SOUP_SESSION_TIMEOUT, src->timeout,
989             SOUP_SESSION_SSL_STRICT, src->ssl_strict,
990             SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
991       } else {
992         src->session =
993             soup_session_new_with_options (SOUP_SESSION_PROXY_URI, src->proxy,
994             SOUP_SESSION_TIMEOUT, src->timeout,
995             SOUP_SESSION_SSL_STRICT, src->ssl_strict,
996             SOUP_SESSION_USER_AGENT, NULL,
997             SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
998       }
999
1000       if (src->session) {
1001         gst_soup_util_log_setup (src->session, src->log_level,
1002             GST_ELEMENT (src));
1003         soup_session_add_feature_by_type (src->session,
1004             SOUP_TYPE_CONTENT_DECODER);
1005         soup_session_add_feature_by_type (src->session, SOUP_TYPE_COOKIE_JAR);
1006
1007         if (can_share) {
1008           GstContext *context;
1009           GstMessage *message;
1010           GstStructure *s;
1011
1012           GST_DEBUG_OBJECT (src, "Sharing session %p", src->session);
1013           src->session_is_shared = TRUE;
1014
1015           /* Unset the limit the number of maximum allowed connection */
1016           g_object_set (src->session, SOUP_SESSION_MAX_CONNS, G_MAXINT,
1017               SOUP_SESSION_MAX_CONNS_PER_HOST, G_MAXINT, NULL);
1018
1019           context = gst_context_new (GST_SOUP_SESSION_CONTEXT, TRUE);
1020           s = gst_context_writable_structure (context);
1021           gst_structure_set (s, "session", SOUP_TYPE_SESSION, src->session,
1022               "force", G_TYPE_BOOLEAN, FALSE, NULL);
1023
1024           gst_object_ref (src->session);
1025           GST_OBJECT_UNLOCK (src);
1026           gst_element_set_context (GST_ELEMENT_CAST (src), context);
1027           message =
1028               gst_message_new_have_context (GST_OBJECT_CAST (src), context);
1029           gst_element_post_message (GST_ELEMENT_CAST (src), message);
1030           GST_OBJECT_LOCK (src);
1031           gst_object_unref (src->session);
1032         } else {
1033           src->session_is_shared = FALSE;
1034         }
1035       }
1036     }
1037
1038     if (!src->session) {
1039       GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1040           (NULL), ("Failed to create session"));
1041       GST_OBJECT_UNLOCK (src);
1042       return FALSE;
1043     }
1044
1045     g_signal_connect (src->session, "authenticate",
1046         G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1047
1048     if (!src->session_is_shared) {
1049       if (src->tls_database)
1050         g_object_set (src->session, "tls-database", src->tls_database, NULL);
1051       else if (src->ssl_ca_file)
1052         g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
1053       else
1054         g_object_set (src->session, "ssl-use-system-ca-file",
1055             src->ssl_use_system_ca_file, NULL);
1056     }
1057     GST_OBJECT_UNLOCK (src);
1058   } else {
1059     GST_DEBUG_OBJECT (src, "Re-using session");
1060   }
1061
1062   return TRUE;
1063 }
1064
1065 static void
1066 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
1067 {
1068   GST_DEBUG_OBJECT (src, "Closing session");
1069
1070   g_mutex_lock (&src->mutex);
1071   if (src->msg) {
1072     soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
1073     g_object_unref (src->msg);
1074     src->msg = NULL;
1075   }
1076
1077   if (src->session) {
1078     if (!src->session_is_shared)
1079       soup_session_abort (src->session);
1080     g_signal_handlers_disconnect_by_func (src->session,
1081         G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1082     g_object_unref (src->session);
1083     src->session = NULL;
1084   }
1085
1086   g_mutex_unlock (&src->mutex);
1087 }
1088
1089 static void
1090 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
1091     SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
1092 {
1093   /* Might be from another user of the shared session */
1094   if (!GST_IS_SOUP_HTTP_SRC (src) || msg != src->msg)
1095     return;
1096
1097   if (!retrying) {
1098     /* First time authentication only, if we fail and are called again with retry true fall through */
1099     if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1100       if (src->user_id && src->user_pw)
1101         soup_auth_authenticate (auth, src->user_id, src->user_pw);
1102     } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1103       if (src->proxy_id && src->proxy_pw)
1104         soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
1105     }
1106   }
1107 }
1108
1109 static void
1110 insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
1111 {
1112   GstStructure *headers = user_data;
1113   const GValue *gv;
1114
1115   if (!g_utf8_validate (name, -1, NULL) || !g_utf8_validate (value, -1, NULL))
1116     return;
1117
1118   gv = gst_structure_get_value (headers, name);
1119   if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
1120     GValue v = G_VALUE_INIT;
1121
1122     g_value_init (&v, G_TYPE_STRING);
1123     g_value_set_string (&v, value);
1124     gst_value_array_append_value ((GValue *) gv, &v);
1125     g_value_unset (&v);
1126   } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
1127     GValue arr = G_VALUE_INIT;
1128     GValue v = G_VALUE_INIT;
1129     const gchar *old_value = g_value_get_string (gv);
1130
1131     g_value_init (&arr, GST_TYPE_ARRAY);
1132     g_value_init (&v, G_TYPE_STRING);
1133     g_value_set_string (&v, old_value);
1134     gst_value_array_append_value (&arr, &v);
1135     g_value_set_string (&v, value);
1136     gst_value_array_append_value (&arr, &v);
1137
1138     gst_structure_set_value (headers, name, &arr);
1139     g_value_unset (&v);
1140     g_value_unset (&arr);
1141   } else {
1142     gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
1143   }
1144 }
1145
1146 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1147 static void
1148 gst_soup_http_src_headers_foreach (const gchar * name, const gchar * val,
1149     gpointer src)
1150 {
1151   GST_INFO_OBJECT (src, " %s: %s", name, val);
1152
1153   if (g_ascii_strcasecmp (name, "Dash-Oldest-Segment") == 0) {
1154     if (val) {
1155       GstSoupHTTPSrc * tmp = src;
1156       tmp->dash_oldest_segment = g_strdup (val);
1157       GST_INFO_OBJECT (src, "Dash-Oldest-Segment set as %s ", tmp->dash_oldest_segment);
1158     }
1159   } else if (g_ascii_strcasecmp (name, "Dash-Newest-Segment") == 0) {
1160     if (val) {
1161       GstSoupHTTPSrc * tmp = src;
1162       tmp->dash_newest_segment = g_strdup (val);
1163       GST_INFO_OBJECT (src, "Dash-Newest-Segment set as %s ", tmp->dash_newest_segment);
1164     }
1165   }
1166 }
1167 #endif
1168
1169 static GstFlowReturn
1170 gst_soup_http_src_got_headers (GstSoupHTTPSrc * src, SoupMessage * msg)
1171 {
1172   const char *value;
1173   GstTagList *tag_list;
1174   GstBaseSrc *basesrc;
1175   guint64 newsize;
1176   GHashTable *params = NULL;
1177   GstEvent *http_headers_event;
1178   GstStructure *http_headers, *headers;
1179   const gchar *accept_ranges;
1180 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1181   gint64 start = 0, stop = 0, total = 0;
1182 #endif
1183
1184   GST_INFO_OBJECT (src, "got headers : %d", msg->status_code);
1185 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1186   soup_message_headers_foreach (msg->response_headers,
1187       gst_soup_http_src_headers_foreach, src);
1188 #endif
1189   if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1190       src->proxy_id && src->proxy_pw) {
1191     /* wait for authenticate callback */
1192     return GST_FLOW_OK;
1193   }
1194
1195   http_headers = gst_structure_new_empty ("http-headers");
1196   gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location,
1197       "http-status-code", G_TYPE_UINT, msg->status_code, NULL);
1198   if (src->redirection_uri)
1199     gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
1200         src->redirection_uri, NULL);
1201   headers = gst_structure_new_empty ("request-headers");
1202   soup_message_headers_foreach (msg->request_headers, insert_http_header,
1203       headers);
1204   gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
1205       headers, NULL);
1206   gst_structure_free (headers);
1207   headers = gst_structure_new_empty ("response-headers");
1208   soup_message_headers_foreach (msg->response_headers, insert_http_header,
1209       headers);
1210   gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
1211       headers, NULL);
1212   gst_structure_free (headers);
1213
1214   gst_element_post_message (GST_ELEMENT_CAST (src),
1215       gst_message_new_element (GST_OBJECT_CAST (src),
1216           gst_structure_copy (http_headers)));
1217
1218   if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1219     /* force an error */
1220     gst_structure_free (http_headers);
1221     return gst_soup_http_src_parse_status (msg, src);
1222   }
1223
1224   src->got_headers = TRUE;
1225   g_cond_broadcast (&src->have_headers_cond);
1226
1227   http_headers_event =
1228       gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
1229   gst_event_replace (&src->http_headers_event, http_headers_event);
1230   gst_event_unref (http_headers_event);
1231
1232 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1233   /* Parse DLNA OP CODE */
1234   if ((value = soup_message_headers_get_one
1235         (msg->response_headers, "contentFeatures.dlna.org")) != NULL) {
1236     gchar **token = NULL;
1237     gchar **ptr = NULL;
1238
1239     GST_DEBUG_OBJECT (src, "DLNA server response");
1240
1241     token = g_strsplit (value, ";", 0);
1242     for (ptr = token ; *ptr ; ptr++) {
1243       gchar *tmp = NULL;
1244       gchar *op_code = NULL;
1245
1246       if (!strlen (*ptr))
1247         continue;
1248
1249       tmp = g_ascii_strup (*ptr, strlen (*ptr));
1250       if (!strstr (tmp, "DLNA.ORG_OP")) {
1251         g_free (tmp);
1252         continue;
1253       }
1254
1255       g_free (tmp);
1256
1257       op_code = strchr (*ptr, '=');
1258       if (op_code) {
1259         op_code++;
1260
1261         src->dlna_opt = (atoi (op_code) / 10 << 1) | (atoi (op_code) % 10);
1262         GST_DEBUG_OBJECT (src, "dlna op code: %s (0x%X)", op_code, src->dlna_opt);
1263         break;
1264       }
1265     }
1266     g_strfreev (token);
1267   }
1268 #endif
1269
1270   /* Parse Content-Length. */
1271   if (soup_message_headers_get_encoding (msg->response_headers) ==
1272       SOUP_ENCODING_CONTENT_LENGTH) {
1273 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1274       if (msg->status_code == SOUP_STATUS_PARTIAL_CONTENT) {
1275         newsize = src->request_position +
1276             soup_message_headers_get_content_length (msg->response_headers);
1277       } else {
1278         if (soup_message_headers_get_content_range(msg->response_headers, &start, &stop, &total) && (total > 0)) {
1279           GST_DEBUG_OBJECT (src, "get range header : %" G_GINT64_FORMAT
1280                                   "~%" G_GINT64_FORMAT"/%"G_GINT64_FORMAT, start, stop, total);
1281           newsize = (guint64)total;
1282         } else {
1283           if ((src->have_size) && (src->content_size <= src->request_position)) {
1284             newsize = src->content_size;
1285           } else {
1286             newsize = soup_message_headers_get_content_length (msg->response_headers);
1287           }
1288         }
1289       }
1290 #else
1291       newsize = src->request_position +
1292           soup_message_headers_get_content_length (msg->response_headers);
1293 #endif
1294     if (!src->have_size || (src->content_size != newsize)) {
1295       src->content_size = newsize;
1296       src->have_size = TRUE;
1297       src->seekable = TRUE;
1298       GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1299
1300       basesrc = GST_BASE_SRC_CAST (src);
1301       basesrc->segment.duration = src->content_size;
1302       gst_element_post_message (GST_ELEMENT (src),
1303           gst_message_new_duration_changed (GST_OBJECT (src)));
1304     }
1305   }
1306
1307   /* If the server reports Accept-Ranges: none we don't have to try
1308    * doing range requests at all
1309    */
1310   if ((accept_ranges =
1311           soup_message_headers_get_one (msg->response_headers,
1312               "Accept-Ranges"))) {
1313     if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
1314       src->seekable = FALSE;
1315   }
1316 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1317   else if (src->dlna_opt & DLNA_OP_BYTE_SEEK) {
1318     if (src->have_size) {
1319       GST_DEBUG_OBJECT (src, "DLNA server is seekable");
1320       src->seekable = TRUE;
1321     }
1322   }
1323   /* The Range request header is always included.
1324    * @ref gst_soup_http_src_add_range_header() */
1325   else if ((msg->status_code == SOUP_STATUS_OK) &&
1326     (soup_message_headers_get_content_range (msg->response_headers, &start, &stop, &total) == FALSE)) {
1327     GST_DEBUG_OBJECT (src, "there is no accept range header");
1328     src->seekable = FALSE;
1329   }
1330 #endif
1331
1332   /* Icecast stuff */
1333   tag_list = gst_tag_list_new_empty ();
1334
1335   if ((value =
1336           soup_message_headers_get_one (msg->response_headers,
1337               "icy-metaint")) != NULL) {
1338     gint icy_metaint;
1339
1340     if (g_utf8_validate (value, -1, NULL)) {
1341       icy_metaint = atoi (value);
1342
1343       GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value,
1344           icy_metaint);
1345       if (icy_metaint > 0) {
1346         if (src->src_caps)
1347           gst_caps_unref (src->src_caps);
1348
1349         src->src_caps = gst_caps_new_simple ("application/x-icy",
1350             "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
1351
1352         gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1353       }
1354     }
1355   }
1356   if ((value =
1357           soup_message_headers_get_content_type (msg->response_headers,
1358               &params)) != NULL) {
1359     if (!g_utf8_validate (value, -1, NULL)) {
1360       GST_WARNING_OBJECT (src, "Content-Type is invalid UTF-8");
1361     } else if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1362       gint channels = 2;
1363       gint rate = 44100;
1364       char *param;
1365
1366       GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1367
1368       if (src->src_caps) {
1369         gst_caps_unref (src->src_caps);
1370         src->src_caps = NULL;
1371       }
1372
1373       param = g_hash_table_lookup (params, "channels");
1374       if (param != NULL) {
1375         guint64 val = g_ascii_strtoull (param, NULL, 10);
1376         if (val < 64)
1377           channels = val;
1378         else
1379           channels = 0;
1380       }
1381
1382       param = g_hash_table_lookup (params, "rate");
1383       if (param != NULL) {
1384         guint64 val = g_ascii_strtoull (param, NULL, 10);
1385         if (val < G_MAXINT)
1386           rate = val;
1387         else
1388           rate = 0;
1389       }
1390
1391       if (rate > 0 && channels > 0) {
1392         src->src_caps = gst_caps_new_simple ("audio/x-unaligned-raw",
1393             "format", G_TYPE_STRING, "S16BE",
1394             "layout", G_TYPE_STRING, "interleaved",
1395             "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
1396
1397         gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1398       }
1399     } else {
1400       GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1401
1402       /* Set the Content-Type field on the caps */
1403       if (src->src_caps) {
1404         src->src_caps = gst_caps_make_writable (src->src_caps);
1405         gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
1406             value, NULL);
1407         gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1408       }
1409     }
1410   }
1411
1412   if (params != NULL)
1413     g_hash_table_destroy (params);
1414
1415   if ((value =
1416           soup_message_headers_get_one (msg->response_headers,
1417               "icy-name")) != NULL) {
1418     if (g_utf8_validate (value, -1, NULL)) {
1419       g_free (src->iradio_name);
1420       src->iradio_name = gst_soup_http_src_unicodify (value);
1421       if (src->iradio_name) {
1422         gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
1423             src->iradio_name, NULL);
1424       }
1425     }
1426   }
1427   if ((value =
1428           soup_message_headers_get_one (msg->response_headers,
1429               "icy-genre")) != NULL) {
1430     if (g_utf8_validate (value, -1, NULL)) {
1431       g_free (src->iradio_genre);
1432       src->iradio_genre = gst_soup_http_src_unicodify (value);
1433       if (src->iradio_genre) {
1434         gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
1435             src->iradio_genre, NULL);
1436       }
1437     }
1438   }
1439   if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1440       != NULL) {
1441     if (g_utf8_validate (value, -1, NULL)) {
1442       g_free (src->iradio_url);
1443       src->iradio_url = gst_soup_http_src_unicodify (value);
1444       if (src->iradio_url) {
1445         gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
1446             src->iradio_url, NULL);
1447       }
1448     }
1449   }
1450   if (!gst_tag_list_is_empty (tag_list)) {
1451     GST_DEBUG_OBJECT (src,
1452         "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1453     gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1454   } else {
1455     gst_tag_list_unref (tag_list);
1456   }
1457
1458   /* Handle HTTP errors. */
1459   return gst_soup_http_src_parse_status (msg, src);
1460 }
1461
1462 static GstBuffer *
1463 gst_soup_http_src_alloc_buffer (GstSoupHTTPSrc * src)
1464 {
1465   GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1466   GstFlowReturn rc;
1467   GstBuffer *gstbuf;
1468
1469   rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1,
1470       basesrc->blocksize, &gstbuf);
1471   if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1472     return NULL;
1473   }
1474
1475   return gstbuf;
1476 }
1477
1478 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message)     \
1479   do { \
1480     GST_ELEMENT_ERROR_WITH_DETAILS ((src), cat, code, ("%s", error_message), \
1481         ("%s (%d), URL: %s, Redirect to: %s", (soup_msg)->reason_phrase, \
1482             (soup_msg)->status_code, (src)->location, GST_STR_NULL ((src)->redirection_uri)), \
1483             ("http-status-code", G_TYPE_UINT, (soup_msg)->status_code, \
1484              "http-redirect-uri", G_TYPE_STRING, GST_STR_NULL ((src)->redirection_uri), NULL)); \
1485   } while(0)
1486
1487 static GstFlowReturn
1488 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1489 {
1490   if (msg->method == SOUP_METHOD_HEAD) {
1491     if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1492       GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1493           msg->status_code);
1494     return GST_FLOW_OK;
1495   }
1496
1497   if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1498     switch (msg->status_code) {
1499       case SOUP_STATUS_CANT_RESOLVE:
1500       case SOUP_STATUS_CANT_RESOLVE_PROXY:
1501         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1502             _("Could not resolve server name."));
1503         return GST_FLOW_ERROR;
1504       case SOUP_STATUS_CANT_CONNECT:
1505       case SOUP_STATUS_CANT_CONNECT_PROXY:
1506         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1507             _("Could not establish connection to server."));
1508         return GST_FLOW_ERROR;
1509       case SOUP_STATUS_SSL_FAILED:
1510         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1511             _("Secure connection setup failed."));
1512         return GST_FLOW_ERROR;
1513       case SOUP_STATUS_IO_ERROR:
1514         if (src->max_retries == -1 || src->retry_count < src->max_retries)
1515           return GST_FLOW_CUSTOM_ERROR;
1516         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1517             _("A network error occurred, or the server closed the connection "
1518                 "unexpectedly."));
1519         return GST_FLOW_ERROR;
1520       case SOUP_STATUS_MALFORMED:
1521         SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1522             _("Server sent bad data."));
1523         return GST_FLOW_ERROR;
1524       case SOUP_STATUS_CANCELLED:
1525         /* No error message when interrupted by program. */
1526         break;
1527     }
1528     return GST_FLOW_OK;
1529   }
1530
1531   if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1532       SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1533       SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1534     const gchar *reason_phrase;
1535
1536     reason_phrase = msg->reason_phrase;
1537     if (reason_phrase && !g_utf8_validate (reason_phrase, -1, NULL)) {
1538       GST_ERROR_OBJECT (src, "Invalid UTF-8 in reason");
1539       reason_phrase = "(invalid)";
1540     }
1541
1542     /* Report HTTP error. */
1543
1544     /* when content_size is unknown and we have just finished receiving
1545      * a body message, requests that go beyond the content limits will result
1546      * in an error. Here we convert those to EOS */
1547 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1548     if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1549         ((src->have_body && !src->have_size) ||
1550          (src->have_size && src->request_position >= src->content_size))) {
1551       GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1552           "body, returning EOS");
1553       return GST_FLOW_EOS;
1554     }
1555 #else
1556     if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1557         src->have_body && !src->have_size) {
1558       GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1559           "body, returning EOS");
1560       return GST_FLOW_EOS;
1561     }
1562 #endif
1563
1564     /* FIXME: reason_phrase is not translated and not suitable for user
1565      * error dialog according to libsoup documentation.
1566      */
1567     if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1568       SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND, (reason_phrase));
1569     } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED
1570         || msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED
1571         || msg->status_code == SOUP_STATUS_FORBIDDEN
1572         || msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1573       SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_AUTHORIZED, (reason_phrase));
1574     } else {
1575       SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ, (reason_phrase));
1576     }
1577     return GST_FLOW_ERROR;
1578   }
1579
1580   return GST_FLOW_OK;
1581 }
1582
1583 static void
1584 gst_soup_http_src_restarted_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1585 {
1586   if (soup_session_would_redirect (src->session, msg)) {
1587     src->redirection_uri =
1588         soup_uri_to_string (soup_message_get_uri (msg), FALSE);
1589     src->redirection_permanent =
1590         (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY);
1591     GST_DEBUG_OBJECT (src, "%u redirect to \"%s\" (permanent %d)",
1592         msg->status_code, src->redirection_uri, src->redirection_permanent);
1593   }
1594 }
1595
1596 static gboolean
1597 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1598 {
1599   g_return_val_if_fail (src->msg == NULL, FALSE);
1600
1601   src->msg = soup_message_new (method, src->location);
1602   if (!src->msg) {
1603     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1604         ("Error parsing URL."), ("URL: %s", src->location));
1605     return FALSE;
1606   }
1607
1608   /* Duplicating the defaults of libsoup here. We don't want to set a
1609    * User-Agent in the session as each source might have its own User-Agent
1610    * set */
1611   if (!src->user_agent || !*src->user_agent) {
1612     gchar *user_agent =
1613         g_strdup_printf ("libsoup/%u.%u.%u", soup_get_major_version (),
1614         soup_get_minor_version (), soup_get_micro_version ());
1615     soup_message_headers_append (src->msg->request_headers, "User-Agent",
1616         user_agent);
1617     g_free (user_agent);
1618   } else if (g_str_has_suffix (src->user_agent, " ")) {
1619     gchar *user_agent = g_strdup_printf ("%slibsoup/%u.%u.%u", src->user_agent,
1620         soup_get_major_version (),
1621         soup_get_minor_version (), soup_get_micro_version ());
1622     soup_message_headers_append (src->msg->request_headers, "User-Agent",
1623         user_agent);
1624     g_free (user_agent);
1625   } else {
1626     soup_message_headers_append (src->msg->request_headers, "User-Agent",
1627         src->user_agent);
1628   }
1629
1630   if (!src->keep_alive) {
1631     soup_message_headers_append (src->msg->request_headers, "Connection",
1632         "close");
1633   }
1634   if (src->iradio_mode) {
1635     soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1636         "1");
1637   }
1638
1639 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1640   /* This changes are needed to enable Seekable Contents from server.
1641    We have observed that , for few specific networks ( VODAFONE ) , without theabove headers ,
1642    Youtube is sending non-seekable contents to the Client. */
1643   soup_message_headers_append (src->msg->request_headers, "Accept-Ranges","bytes");
1644 #endif
1645
1646   if (src->cookies) {
1647     gchar **cookie;
1648
1649     for (cookie = src->cookies; *cookie != NULL; cookie++) {
1650       soup_message_headers_append (src->msg->request_headers, "Cookie",
1651           *cookie);
1652     }
1653   }
1654
1655   if (!src->compress)
1656     soup_message_disable_feature (src->msg, SOUP_TYPE_CONTENT_DECODER);
1657
1658   soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1659       (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1660
1661   if (src->automatic_redirect) {
1662     g_signal_connect (src->msg, "restarted",
1663         G_CALLBACK (gst_soup_http_src_restarted_cb), src);
1664   }
1665
1666   gst_soup_http_src_add_range_header (src, src->request_position,
1667       src->stop_position);
1668
1669   gst_soup_http_src_add_extra_headers (src);
1670
1671 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1672   soup_message_headers_foreach (src->msg->request_headers,
1673       gst_soup_http_src_headers_foreach, src);
1674 #endif
1675
1676   return TRUE;
1677 }
1678
1679 /* Lock taken */
1680 static GstFlowReturn
1681 gst_soup_http_src_send_message (GstSoupHTTPSrc * src)
1682 {
1683   GstFlowReturn ret;
1684   GError *error = NULL;
1685
1686   g_return_val_if_fail (src->msg != NULL, GST_FLOW_ERROR);
1687   g_assert (src->input_stream == NULL);
1688
1689   src->input_stream =
1690       soup_session_send (src->session, src->msg, src->cancellable, &error);
1691
1692   if (g_cancellable_is_cancelled (src->cancellable)) {
1693     ret = GST_FLOW_FLUSHING;
1694     goto done;
1695   }
1696
1697   ret = gst_soup_http_src_got_headers (src, src->msg);
1698   if (ret != GST_FLOW_OK) {
1699     goto done;
1700   }
1701
1702   if (!src->input_stream) {
1703     GST_DEBUG_OBJECT (src, "Didn't get an input stream: %s", error->message);
1704     ret = GST_FLOW_ERROR;
1705     goto done;
1706   }
1707
1708   if (SOUP_STATUS_IS_SUCCESSFUL (src->msg->status_code)) {
1709     GST_DEBUG_OBJECT (src, "Successfully got a reply");
1710   } else {
1711     /* FIXME - be more helpful to people debugging */
1712     ret = GST_FLOW_ERROR;
1713   }
1714
1715 done:
1716   if (error)
1717     g_error_free (error);
1718   return ret;
1719 }
1720
1721 static GstFlowReturn
1722 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method)
1723 {
1724   GstFlowReturn ret;
1725
1726   if (src->max_retries != -1 && src->retry_count > src->max_retries) {
1727     GST_DEBUG_OBJECT (src, "Max retries reached");
1728     return GST_FLOW_ERROR;
1729   }
1730
1731   src->retry_count++;
1732   /* EOS immediately if we have an empty segment */
1733   if (src->request_position == src->stop_position)
1734     return GST_FLOW_EOS;
1735
1736   GST_LOG_OBJECT (src, "Running request for method: %s", method);
1737
1738   /* Update the position if we are retrying */
1739   if (src->msg && src->request_position > 0) {
1740     gst_soup_http_src_add_range_header (src, src->request_position,
1741         src->stop_position);
1742   } else if (src->msg && src->request_position == 0)
1743     soup_message_headers_remove (src->msg->request_headers, "Range");
1744
1745   /* add_range_header() has the side effect of setting read_position to
1746    * the requested position. This *needs* to be set regardless of having
1747    * a message or not. Failure to do so would result in calculation being
1748    * done with stale/wrong read position */
1749   src->read_position = src->request_position;
1750
1751   if (!src->msg) {
1752     if (!gst_soup_http_src_build_message (src, method)) {
1753       return GST_FLOW_ERROR;
1754     }
1755   }
1756
1757   if (g_cancellable_is_cancelled (src->cancellable)) {
1758     GST_INFO_OBJECT (src, "interrupted");
1759     return GST_FLOW_FLUSHING;
1760   }
1761
1762   ret = gst_soup_http_src_send_message (src);
1763
1764   /* Check if Range header was respected. */
1765 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1766   if (ret == GST_FLOW_OK && src->request_position > 0 &&
1767       (src->msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) &&
1768           (src->request_position < src->content_size)) {
1769 #else
1770   if (ret == GST_FLOW_OK && src->request_position > 0 &&
1771       src->msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
1772 #endif
1773     src->seekable = FALSE;
1774     GST_ELEMENT_ERROR_WITH_DETAILS (src, RESOURCE, SEEK,
1775         (_("Server does not support seeking.")),
1776         ("Server does not accept Range HTTP header, URL: %s, Redirect to: %s",
1777             src->location, GST_STR_NULL (src->redirection_uri)),
1778         ("http-status-code", G_TYPE_UINT, src->msg->status_code,
1779             "http-redirection-uri", G_TYPE_STRING,
1780             GST_STR_NULL (src->redirection_uri), NULL));
1781     ret = GST_FLOW_ERROR;
1782   }
1783
1784   return ret;
1785 }
1786
1787 /*
1788  * Check if the bytes_read is above a certain threshold of the blocksize, if
1789  * that happens a few times in a row, increase the blocksize; Do the same in
1790  * the opposite direction to reduce the blocksize.
1791  */
1792 static void
1793 gst_soup_http_src_check_update_blocksize (GstSoupHTTPSrc * src,
1794     gint64 bytes_read)
1795 {
1796   guint blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
1797
1798   gint64 time_since_last_read =
1799       g_get_monotonic_time () * GST_USECOND - src->last_socket_read_time;
1800
1801   GST_LOG_OBJECT (src, "Checking to update blocksize. Read: %" G_GINT64_FORMAT
1802       " bytes, blocksize: %u bytes, time since last read: %" GST_TIME_FORMAT,
1803       bytes_read, blocksize, GST_TIME_ARGS (time_since_last_read));
1804
1805   if (bytes_read >= blocksize * GROW_BLOCKSIZE_LIMIT
1806       && time_since_last_read <= GROW_TIME_LIMIT) {
1807     src->reduce_blocksize_count = 0;
1808     src->increase_blocksize_count++;
1809
1810     if (src->increase_blocksize_count >= GROW_BLOCKSIZE_COUNT) {
1811       blocksize *= GROW_BLOCKSIZE_FACTOR;
1812       GST_DEBUG_OBJECT (src, "Increased blocksize to %u", blocksize);
1813       gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1814       src->increase_blocksize_count = 0;
1815     }
1816   } else if (bytes_read < blocksize * REDUCE_BLOCKSIZE_LIMIT
1817       || time_since_last_read > GROW_TIME_LIMIT) {
1818     src->reduce_blocksize_count++;
1819     src->increase_blocksize_count = 0;
1820
1821     if (src->reduce_blocksize_count >= REDUCE_BLOCKSIZE_COUNT) {
1822       blocksize *= REDUCE_BLOCKSIZE_FACTOR;
1823       blocksize = MAX (blocksize, src->minimum_blocksize);
1824       GST_DEBUG_OBJECT (src, "Decreased blocksize to %u", blocksize);
1825       gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1826       src->reduce_blocksize_count = 0;
1827     }
1828   } else {
1829     src->reduce_blocksize_count = src->increase_blocksize_count = 0;
1830   }
1831 }
1832
1833 static void
1834 gst_soup_http_src_update_position (GstSoupHTTPSrc * src, gint64 bytes_read)
1835 {
1836   GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1837   guint64 new_position;
1838
1839   new_position = src->read_position + bytes_read;
1840   if (G_LIKELY (src->request_position == src->read_position))
1841     src->request_position = new_position;
1842   src->read_position = new_position;
1843
1844   if (src->have_size) {
1845     if (new_position > src->content_size) {
1846       GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1847           "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1848           src->content_size);
1849       src->content_size = new_position;
1850       basesrc->segment.duration = src->content_size;
1851       gst_element_post_message (GST_ELEMENT (src),
1852           gst_message_new_duration_changed (GST_OBJECT (src)));
1853     } else if (new_position == src->content_size) {
1854       GST_DEBUG_OBJECT (src, "We're EOS now");
1855     }
1856   }
1857 }
1858
1859 static GstFlowReturn
1860 gst_soup_http_src_read_buffer (GstSoupHTTPSrc * src, GstBuffer ** outbuf)
1861 {
1862   gssize read_bytes;
1863   GstMapInfo mapinfo;
1864   GstBaseSrc *bsrc;
1865   GstFlowReturn ret;
1866
1867   bsrc = GST_BASE_SRC_CAST (src);
1868
1869   *outbuf = gst_soup_http_src_alloc_buffer (src);
1870   if (!*outbuf) {
1871     GST_WARNING_OBJECT (src, "Failed to allocate buffer");
1872     return GST_FLOW_ERROR;
1873   }
1874
1875   if (!gst_buffer_map (*outbuf, &mapinfo, GST_MAP_WRITE)) {
1876     GST_WARNING_OBJECT (src, "Failed to map buffer");
1877     return GST_FLOW_ERROR;
1878   }
1879
1880   read_bytes =
1881       g_input_stream_read (src->input_stream, mapinfo.data, mapinfo.size,
1882       src->cancellable, NULL);
1883   GST_DEBUG_OBJECT (src, "Read %" G_GSSIZE_FORMAT " bytes from http input",
1884       read_bytes);
1885
1886   g_mutex_lock (&src->mutex);
1887   if (g_cancellable_is_cancelled (src->cancellable)) {
1888     gst_buffer_unmap (*outbuf, &mapinfo);
1889     gst_buffer_unref (*outbuf);
1890     g_mutex_unlock (&src->mutex);
1891     return GST_FLOW_FLUSHING;
1892   }
1893
1894   gst_buffer_unmap (*outbuf, &mapinfo);
1895   if (read_bytes > 0) {
1896     gst_buffer_set_size (*outbuf, read_bytes);
1897     GST_BUFFER_OFFSET (*outbuf) = bsrc->segment.position;
1898     ret = GST_FLOW_OK;
1899     gst_soup_http_src_update_position (src, read_bytes);
1900 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1901     src->received_total += read_bytes;
1902 #endif
1903
1904     /* Got some data, reset retry counter */
1905     src->retry_count = 0;
1906
1907     gst_soup_http_src_check_update_blocksize (src, read_bytes);
1908
1909     src->last_socket_read_time = g_get_monotonic_time () * GST_USECOND;
1910
1911     /* If we're at the end of a range request, read again to let libsoup
1912      * finalize the request. This allows to reuse the connection again later,
1913      * otherwise we would have to cancel the message and close the connection
1914      */
1915     if (bsrc->segment.stop != -1
1916         && bsrc->segment.position + read_bytes >= bsrc->segment.stop) {
1917       guint8 tmp[128];
1918
1919       g_object_unref (src->msg);
1920       src->msg = NULL;
1921       src->have_body = TRUE;
1922
1923       /* This should return immediately as we're at the end of the range */
1924       read_bytes =
1925           g_input_stream_read (src->input_stream, tmp, sizeof (tmp),
1926           src->cancellable, NULL);
1927       if (read_bytes > 0)
1928         GST_ERROR_OBJECT (src,
1929             "Read %" G_GSIZE_FORMAT " bytes after end of range", read_bytes);
1930     }
1931   } else {
1932     gst_buffer_unref (*outbuf);
1933     if (read_bytes < 0 ||
1934         (src->have_size && src->read_position < src->content_size)) {
1935       /* Maybe the server disconnected, retry */
1936       ret = GST_FLOW_CUSTOM_ERROR;
1937     } else {
1938       g_object_unref (src->msg);
1939       src->msg = NULL;
1940       ret = GST_FLOW_EOS;
1941       src->have_body = TRUE;
1942     }
1943   }
1944   g_mutex_unlock (&src->mutex);
1945
1946   return ret;
1947 }
1948
1949 static GstFlowReturn
1950 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1951 {
1952   GstSoupHTTPSrc *src;
1953   GstFlowReturn ret = GST_FLOW_OK;
1954   GstEvent *http_headers_event = NULL;
1955
1956   src = GST_SOUP_HTTP_SRC (psrc);
1957
1958 retry:
1959   g_mutex_lock (&src->mutex);
1960
1961   /* Check for pending position change */
1962   if (src->request_position != src->read_position) {
1963     if (src->input_stream) {
1964       g_input_stream_close (src->input_stream, src->cancellable, NULL);
1965       g_object_unref (src->input_stream);
1966       src->input_stream = NULL;
1967     }
1968   }
1969
1970   if (g_cancellable_is_cancelled (src->cancellable)) {
1971     ret = GST_FLOW_FLUSHING;
1972     g_mutex_unlock (&src->mutex);
1973     goto done;
1974   }
1975
1976   /* If we have no open connection to the server, start one */
1977   if (!src->input_stream) {
1978     *outbuf = NULL;
1979     ret =
1980         gst_soup_http_src_do_request (src,
1981         src->method ? src->method : SOUP_METHOD_GET);
1982     http_headers_event = src->http_headers_event;
1983     src->http_headers_event = NULL;
1984   }
1985   g_mutex_unlock (&src->mutex);
1986
1987   if (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_ERROR) {
1988     if (http_headers_event) {
1989       gst_pad_push_event (GST_BASE_SRC_PAD (src), http_headers_event);
1990       http_headers_event = NULL;
1991     }
1992   }
1993
1994   if (ret == GST_FLOW_OK)
1995     ret = gst_soup_http_src_read_buffer (src, outbuf);
1996
1997 done:
1998   GST_DEBUG_OBJECT (src, "Returning %d %s", ret, gst_flow_get_name (ret));
1999   if (ret != GST_FLOW_OK) {
2000     if (http_headers_event)
2001       gst_event_unref (http_headers_event);
2002
2003     g_mutex_lock (&src->mutex);
2004     if (src->input_stream) {
2005       g_object_unref (src->input_stream);
2006       src->input_stream = NULL;
2007     }
2008     g_mutex_unlock (&src->mutex);
2009     if (ret == GST_FLOW_CUSTOM_ERROR) {
2010       ret = GST_FLOW_OK;
2011       goto retry;
2012     }
2013   }
2014
2015   if (ret == GST_FLOW_FLUSHING) {
2016     g_mutex_lock (&src->mutex);
2017     src->retry_count = 0;
2018     g_mutex_unlock (&src->mutex);
2019   }
2020
2021   return ret;
2022 }
2023
2024 static gboolean
2025 gst_soup_http_src_start (GstBaseSrc * bsrc)
2026 {
2027   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2028
2029   GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
2030
2031 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2032   if (src->dash_oldest_segment) {
2033     g_free (src->dash_oldest_segment);
2034     src->dash_oldest_segment = NULL;
2035   }
2036   if (src->dash_newest_segment) {
2037     g_free (src->dash_newest_segment);
2038     src->dash_newest_segment = NULL;
2039   }
2040 #endif
2041   return gst_soup_http_src_session_open (src);
2042 }
2043
2044 static gboolean
2045 gst_soup_http_src_stop (GstBaseSrc * bsrc)
2046 {
2047   GstSoupHTTPSrc *src;
2048
2049   src = GST_SOUP_HTTP_SRC (bsrc);
2050   GST_DEBUG_OBJECT (src, "stop()");
2051   if (src->keep_alive && !src->msg && !src->session_is_shared)
2052     gst_soup_http_src_cancel_message (src);
2053   else
2054     gst_soup_http_src_session_close (src);
2055
2056   gst_soup_http_src_reset (src);
2057   return TRUE;
2058 }
2059
2060 static GstStateChangeReturn
2061 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
2062 {
2063   GstStateChangeReturn ret;
2064   GstSoupHTTPSrc *src;
2065
2066   src = GST_SOUP_HTTP_SRC (element);
2067
2068   switch (transition) {
2069 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2070     case GST_STATE_CHANGE_PAUSED_TO_READY:
2071       GST_WARNING_OBJECT (src, "Last read pos"
2072             ": %" G_GINT64_FORMAT ", received total : %" G_GINT64_FORMAT,
2073             src->read_position, src->received_total);
2074       break;
2075 #endif
2076     case GST_STATE_CHANGE_READY_TO_NULL:
2077       gst_soup_http_src_session_close (src);
2078       break;
2079     default:
2080       break;
2081   }
2082
2083   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2084
2085   return ret;
2086 }
2087
2088 static void
2089 gst_soup_http_src_set_context (GstElement * element, GstContext * context)
2090 {
2091   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (element);
2092
2093   if (g_strcmp0 (gst_context_get_context_type (context),
2094           GST_SOUP_SESSION_CONTEXT) == 0) {
2095     const GstStructure *s = gst_context_get_structure (context);
2096
2097     GST_OBJECT_LOCK (src);
2098     if (src->external_session)
2099       g_object_unref (src->external_session);
2100     src->external_session = NULL;
2101     gst_structure_get (s, "session", SOUP_TYPE_SESSION, &src->external_session,
2102         NULL);
2103     src->forced_external_session = FALSE;
2104     gst_structure_get (s, "force", G_TYPE_BOOLEAN,
2105         &src->forced_external_session, NULL);
2106
2107     GST_DEBUG_OBJECT (src, "Setting external session %p (force: %d)",
2108         src->external_session, src->forced_external_session);
2109     GST_OBJECT_UNLOCK (src);
2110   }
2111
2112   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
2113 }
2114
2115 /* Interrupt a blocking request. */
2116 static gboolean
2117 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
2118 {
2119   GstSoupHTTPSrc *src;
2120
2121   src = GST_SOUP_HTTP_SRC (bsrc);
2122   GST_DEBUG_OBJECT (src, "unlock()");
2123
2124   gst_soup_http_src_cancel_message (src);
2125   return TRUE;
2126 }
2127
2128 /* Interrupt interrupt. */
2129 static gboolean
2130 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
2131 {
2132   GstSoupHTTPSrc *src;
2133
2134   src = GST_SOUP_HTTP_SRC (bsrc);
2135   GST_DEBUG_OBJECT (src, "unlock_stop()");
2136
2137   g_cancellable_reset (src->cancellable);
2138   return TRUE;
2139 }
2140
2141 static gboolean
2142 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
2143 {
2144   GstSoupHTTPSrc *src;
2145
2146   src = GST_SOUP_HTTP_SRC (bsrc);
2147
2148   if (src->have_size) {
2149     GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
2150         src->content_size);
2151     *size = src->content_size;
2152     return TRUE;
2153   }
2154   GST_DEBUG_OBJECT (src, "get_size() = FALSE");
2155   return FALSE;
2156 }
2157
2158 static void
2159 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
2160 {
2161   GstFlowReturn ret = GST_FLOW_OK;
2162
2163   /* Special case to check if the server allows range requests
2164    * before really starting to get data in the buffer creation
2165    * loops.
2166    */
2167   if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
2168     g_mutex_lock (&src->mutex);
2169     while (!src->got_headers && !g_cancellable_is_cancelled (src->cancellable)
2170         && ret == GST_FLOW_OK) {
2171       if ((src->msg && src->msg->method != SOUP_METHOD_HEAD)) {
2172         /* wait for the current request to finish */
2173         g_cond_wait (&src->have_headers_cond, &src->mutex);
2174       } else {
2175         if (gst_soup_http_src_session_open (src)) {
2176           ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD);
2177         }
2178       }
2179     }
2180     g_mutex_unlock (&src->mutex);
2181   }
2182 }
2183
2184 static gboolean
2185 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
2186 {
2187   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2188
2189   gst_soup_http_src_check_seekable (src);
2190
2191   return src->seekable;
2192 }
2193
2194 static gboolean
2195 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
2196 {
2197   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2198
2199   GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2200       ")", segment->start, segment->stop);
2201   if (src->read_position == segment->start &&
2202       src->request_position == src->read_position &&
2203       src->stop_position == segment->stop) {
2204     GST_DEBUG_OBJECT (src,
2205         "Seek to current read/end position and no seek pending");
2206     return TRUE;
2207   }
2208
2209   gst_soup_http_src_check_seekable (src);
2210
2211   /* If we have no headers we don't know yet if it is seekable or not.
2212    * Store the start position and error out later if it isn't */
2213   if (src->got_headers && !src->seekable) {
2214     GST_WARNING_OBJECT (src, "Not seekable");
2215     return FALSE;
2216   }
2217
2218   if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
2219     GST_WARNING_OBJECT (src, "Invalid seek segment");
2220     return FALSE;
2221   }
2222
2223   if (src->have_size && segment->start >= src->content_size) {
2224     GST_WARNING_OBJECT (src,
2225         "Potentially seeking behind end of file, might EOS immediately");
2226   }
2227
2228   /* Wait for create() to handle the jump in offset. */
2229   src->request_position = segment->start;
2230   src->stop_position = segment->stop;
2231
2232   return TRUE;
2233 }
2234
2235 static gboolean
2236 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
2237 {
2238   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2239   gboolean ret;
2240   GstSchedulingFlags flags;
2241   gint minsize, maxsize, align;
2242
2243   switch (GST_QUERY_TYPE (query)) {
2244     case GST_QUERY_URI:
2245       gst_query_set_uri (query, src->location);
2246       if (src->redirection_uri != NULL) {
2247         gst_query_set_uri_redirection (query, src->redirection_uri);
2248         gst_query_set_uri_redirection_permanent (query,
2249             src->redirection_permanent);
2250       }
2251       ret = TRUE;
2252       break;
2253     default:
2254       ret = FALSE;
2255       break;
2256   }
2257
2258   if (!ret)
2259     ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
2260
2261   switch (GST_QUERY_TYPE (query)) {
2262     case GST_QUERY_SCHEDULING:
2263       gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
2264       flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
2265
2266 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2267       if (gst_soup_http_src_is_seekable(bsrc)) {
2268           GST_DEBUG_OBJECT (src, "set seekable flag");
2269           flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2270       }
2271 #endif
2272       gst_query_set_scheduling (query, flags, minsize, maxsize, align);
2273       break;
2274     default:
2275       break;
2276   }
2277
2278   return ret;
2279 }
2280
2281 static gboolean
2282 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
2283     GError ** error)
2284 {
2285   const char *alt_schemes[] = { "icy://", "icyx://" };
2286   guint i;
2287
2288   if (src->location) {
2289     g_free (src->location);
2290     src->location = NULL;
2291   }
2292
2293   if (uri == NULL)
2294     return FALSE;
2295
2296   for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
2297     if (g_str_has_prefix (uri, alt_schemes[i])) {
2298       src->location =
2299           g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
2300       return TRUE;
2301     }
2302   }
2303
2304   if (src->redirection_uri) {
2305     g_free (src->redirection_uri);
2306     src->redirection_uri = NULL;
2307   }
2308
2309   src->location = g_strdup (uri);
2310
2311   return TRUE;
2312 }
2313
2314 static gboolean
2315 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
2316 {
2317   if (src->proxy) {
2318     soup_uri_free (src->proxy);
2319     src->proxy = NULL;
2320   }
2321
2322   if (uri == NULL || *uri == '\0')
2323     return TRUE;
2324
2325   if (g_strstr_len (uri, -1, "://")) {
2326     src->proxy = soup_uri_new (uri);
2327   } else {
2328     gchar *new_uri = g_strconcat ("http://", uri, NULL);
2329
2330     src->proxy = soup_uri_new (new_uri);
2331     g_free (new_uri);
2332   }
2333
2334   return (src->proxy != NULL);
2335 }
2336
2337 static GstURIType
2338 gst_soup_http_src_uri_get_type (GType type)
2339 {
2340   return GST_URI_SRC;
2341 }
2342
2343 static const gchar *const *
2344 gst_soup_http_src_uri_get_protocols (GType type)
2345 {
2346   static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
2347
2348   return protocols;
2349 }
2350
2351 static gchar *
2352 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
2353 {
2354   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2355
2356   /* FIXME: make thread-safe */
2357   return g_strdup (src->location);
2358 }
2359
2360 static gboolean
2361 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
2362     GError ** error)
2363 {
2364   GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2365
2366   return gst_soup_http_src_set_location (src, uri, error);
2367 }
2368
2369 static void
2370 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
2371 {
2372   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
2373
2374   iface->get_type = gst_soup_http_src_uri_get_type;
2375   iface->get_protocols = gst_soup_http_src_uri_get_protocols;
2376   iface->get_uri = gst_soup_http_src_uri_get_uri;
2377   iface->set_uri = gst_soup_http_src_uri_set_uri;
2378 }