gstsouphttpsrc.c 68.7 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) 2007-2008 Wouter Cloetens <wouter@mind.be>
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more
 */

15 16 17 18
/**
 * SECTION:element-souphttpsrc
 *
 * This plugin reads data from a remote location specified by a URI.
19
 * Supported protocols are 'http', 'https'.
20
 *
21 22
 * An HTTP proxy must be specified by its URL.
 * If the "http_proxy" environment variable is set, its value is used.
23 24
 * If built with libsoup's GNOME integration features, the GNOME proxy
 * configuration will be used, or failing that, proxy autodetection.
25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * The #GstSoupHTTPSrc:proxy property can be used to override the default.
 *
 * In case the #GstSoupHTTPSrc:iradio-mode property is set and the location is
 * an HTTP resource, souphttpsrc will send special Icecast HTTP headers to the
 * server to request additional Icecast meta-information.
 * If the server is not an Icecast server, it will behave as if the
 * #GstSoupHTTPSrc:iradio-mode property were not set. If it is, souphttpsrc will
 * output data with a media type of application/x-icy, in which case you will
 * need to use the #ICYDemux element as follow-up element to extract the Icecast
 * metadata and to determine the underlying media type.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
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 47
 *     location=http://music.foobar.com/demo.mp3 ! mpgaudioparse
 *     ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink
48
 * ]| The above pipeline will read and decode and play an mp3 file from a
49 50 51 52
 * web server using the HTTP protocol. If the server sends redirects,
 * the request fails instead of following the redirect. The specified
 * HTTP proxy server is used. The User-Agent HTTP request header
 * 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 57
 *     ! image/jpeg,width=640,height=480 ! matroskamux
 *     ! filesink location=mjpeg.mkv
58
 * ]| The above pipeline reads a motion JPEG stream from an IP camera
59 60 61 62 63 64 65 66 67 68 69
 * using the HTTP protocol, encoded as mime/multipart image/jpeg
 * parts, and writes a Matroska motion JPEG file. The width and
 * height properties are set in the caps to provide the Matroska
 * multiplexer with the information to set this in the header.
 * Timestamps are set on the buffers as they arrive from the camera.
 * These are used by the mime/multipart demultiplexer to emit timestamps
 * on the JPEG-encoded video frame buffers. This allows the Matroska
 * multiplexer to timestamp the frames in the resulting file.
 * </refsect2>
 */

70 71 72 73 74
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
75 76 77
#ifdef HAVE_STDLIB_H
#include <stdlib.h>             /* atoi() */
#endif
78 79
#include <gst/gstelement.h>
#include <gst/gst-i18n-plugin.h>
80 81
#include <libsoup/soup.h>
#include "gstsouphttpsrc.h"
82
#include "gstsouputils.h"
83

84 85
#include <gst/tag/tag.h>

86 87 88
GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
#define GST_CAT_DEFAULT souphttpsrc_debug

89 90
#define GST_SOUP_SESSION_CONTEXT "gst.soup.session"

91 92 93 94 95 96 97 98 99
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

enum
{
  PROP_0,
  PROP_LOCATION,
100
  PROP_IS_LIVE,
101
  PROP_USER_AGENT,
102 103
  PROP_AUTOMATIC_REDIRECT,
  PROP_PROXY,
104 105 106 107
  PROP_USER_ID,
  PROP_USER_PW,
  PROP_PROXY_ID,
  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 115 116
  PROP_KEEP_ALIVE,
  PROP_SSL_STRICT,
  PROP_SSL_CA_FILE,
117
  PROP_SSL_USE_SYSTEM_CA_FILE,
118
  PROP_TLS_DATABASE,
119
  PROP_RETRIES,
120 121
  PROP_METHOD,
  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 130 131
#define DEFAULT_SSL_STRICT           TRUE
#define DEFAULT_SSL_CA_FILE          NULL
#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 139 140 141 142 143 144
#define GROW_BLOCKSIZE_LIMIT 1
#define GROW_BLOCKSIZE_COUNT 1
#define GROW_BLOCKSIZE_FACTOR 2
#define REDUCE_BLOCKSIZE_LIMIT 0.20
#define REDUCE_BLOCKSIZE_COUNT 2
#define REDUCE_BLOCKSIZE_FACTOR 0.5

145
static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
146
    gpointer iface_data);
147
static void gst_soup_http_src_finalize (GObject * gobject);
148
static void gst_soup_http_src_dispose (GObject * gobject);
149

150
static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
151
    const GValue * value, GParamSpec * pspec);
152
static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
153 154
    GValue * value, GParamSpec * pspec);

155 156
static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
    element, GstStateChange transition);
157 158
static void gst_soup_http_src_set_context (GstElement * element,
    GstContext * context);
159
static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
160
    GstBuffer ** outbuf);
161 162 163 164 165
static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
166
    GstSegment * segment);
167
static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
168 169 170
static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
171
    const gchar * uri, GError ** error);
172
static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
173
    const gchar * uri);
174
static char *gst_soup_http_src_unicodify (const char *str);
175 176
static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
    const gchar * method);
177 178
static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
179
    guint64 offset, guint64 stop_offset);
180
static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
181
static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
182
static GstFlowReturn gst_soup_http_src_parse_status (SoupMessage * msg,
183
    GstSoupHTTPSrc * src);
184
static GstFlowReturn gst_soup_http_src_got_headers (GstSoupHTTPSrc * src,
185
    SoupMessage * msg);
186 187 188
static void gst_soup_http_src_authenticate_cb (SoupSession * session,
    SoupMessage * msg, SoupAuth * auth, gboolean retrying,
    GstSoupHTTPSrc * src);
189

190 191 192 193
#define gst_soup_http_src_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
    G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
        gst_soup_http_src_uri_handler_init));
194 195

static void
196
gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
197 198
{
  GObjectClass *gobject_class;
199
  GstElementClass *gstelement_class;
200 201 202 203
  GstBaseSrcClass *gstbasesrc_class;
  GstPushSrcClass *gstpushsrc_class;

  gobject_class = (GObjectClass *) klass;
204
  gstelement_class = (GstElementClass *) klass;
205 206 207
  gstbasesrc_class = (GstBaseSrcClass *) klass;
  gstpushsrc_class = (GstPushSrcClass *) klass;

208 209
  gobject_class->set_property = gst_soup_http_src_set_property;
  gobject_class->get_property = gst_soup_http_src_get_property;
210
  gobject_class->finalize = gst_soup_http_src_finalize;
211
  gobject_class->dispose = gst_soup_http_src_dispose;
212

213 214
  g_object_class_install_property (gobject_class,
      PROP_LOCATION,
215
      g_param_spec_string ("location", "Location",
216 217
          "Location to read from", "",
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 219 220 221
  g_object_class_install_property (gobject_class,
      PROP_USER_AGENT,
      g_param_spec_string ("user-agent", "User-Agent",
          "Value of the User-Agent HTTP request header field",
222
          DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 224 225 226
  g_object_class_install_property (gobject_class,
      PROP_AUTOMATIC_REDIRECT,
      g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
          "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
227
          TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 229 230
  g_object_class_install_property (gobject_class,
      PROP_PROXY,
      g_param_spec_string ("proxy", "Proxy",
231 232
          "HTTP proxy server URI", "",
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
  g_object_class_install_property (gobject_class,
234 235 236
      PROP_USER_ID,
      g_param_spec_string ("user-id", "user-id",
          "HTTP location URI user id for authentication", "",
237
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 239 240
  g_object_class_install_property (gobject_class, PROP_USER_PW,
      g_param_spec_string ("user-pw", "user-pw",
          "HTTP location URI user password for authentication", "",
241
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 243
  g_object_class_install_property (gobject_class, PROP_PROXY_ID,
      g_param_spec_string ("proxy-id", "proxy-id",
244 245
          "HTTP proxy URI user id for authentication", "",
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 247 248
  g_object_class_install_property (gobject_class, PROP_PROXY_PW,
      g_param_spec_string ("proxy-pw", "proxy-pw",
          "HTTP proxy URI user password for authentication", "",
249
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 251
  g_object_class_install_property (gobject_class, PROP_COOKIES,
      g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
252
          G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 254
  g_object_class_install_property (gobject_class, PROP_IS_LIVE,
      g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
255
          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 257 258
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
      g_param_spec_uint ("timeout", "timeout",
          "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
259
          3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 261 262
  g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
      g_param_spec_boxed ("extra-headers", "Extra Headers",
          "Extra headers to append to the HTTP request",
263
          GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 265 266 267 268
  g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
      g_param_spec_boolean ("iradio-mode", "iradio-mode",
          "Enable internet radio mode (ask server to send shoutcast/icecast "
          "metadata interleaved with the actual stream data)",
          DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269

270 271 272 273 274 275 276 277 278 279 280 281 282
 /**
   * GstSoupHTTPSrc::http-log-level:
   *
   * If set and > 0, captures and dumps HTTP session data as
   * log messages if log level >= GST_LEVEL_TRACE
   *
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
      g_param_spec_enum ("http-log-level", "HTTP log level",
          "Set log level for soup's HTTP session log",
          SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
 /**
   * GstSoupHTTPSrc::compress:
   *
   * If set to %TRUE, souphttpsrc will automatically handle gzip
   * and deflate Content-Encodings. This does not make much difference
   * and causes more load for normal media files, but makes a real
   * difference in size for plaintext files.
   *
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_COMPRESS,
      g_param_spec_boolean ("compress", "Compress",
          "Allow compressed content encodings",
          DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

299 300 301 302 303 304 305 306 307 308 309 310 311 312
 /**
   * GstSoupHTTPSrc::keep-alive:
   *
   * If set to %TRUE, souphttpsrc will keep alive connections when being
   * set to READY state and only will close connections when connecting
   * to a different server or when going to NULL state..
   *
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
      g_param_spec_boolean ("keep-alive", "keep-alive",
          "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
 /**
   * GstSoupHTTPSrc::ssl-strict:
   *
   * If set to %TRUE, souphttpsrc will reject all SSL certificates that
   * are considered invalid.
   *
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
      g_param_spec_boolean ("ssl-strict", "SSL Strict",
          "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

 /**
   * GstSoupHTTPSrc::ssl-ca-file:
   *
   * A SSL anchor CA file that should be used for checking certificates
   * instead of the system CA file.
   *
332 333 334 335
   * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
   * value will be ignored.
   *
   * Deprecated: Use #GstSoupHTTPSrc::tls-database property instead.
336 337 338 339 340 341 342 343 344 345 346
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
      g_param_spec_string ("ssl-ca-file", "SSL CA File",
          "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

 /**
   * GstSoupHTTPSrc::ssl-use-system-ca-file:
   *
   * If set to %TRUE, souphttpsrc will use the system's CA file for
347 348
   * checking certificates, unless #GstSoupHTTPSrc::ssl-ca-file or
   * #GstSoupHTTPSrc::tls-database are non-%NULL.
349 350 351 352 353 354 355 356
   *
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
      g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
          "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  /**
   * GstSoupHTTPSrc::tls-database:
   *
   * TLS database with anchor certificate authorities used to validate
   * the server certificate.
   *
   * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
   * and #GstSoupHTTPSrc::ssl-ca-file values will be ignored.
   *
   * Since: 1.6
   */
  g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
      g_param_spec_object ("tls-database", "TLS database",
          "TLS database with anchor certificate authorities used to validate the server certificate",
          G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

373 374 375 376 377 378 379 380 381 382 383 384 385 386
  /**
   * GstSoupHTTPSrc::tls-interaction:
   *
   * A #GTlsInteraction object to be used when the connection or certificate
   * database need to interact with the user. This will be used to prompt the
   * user for passwords or certificate where necessary.
   *
   * Since: 1.8
   */
  g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
      g_param_spec_object ("tls-interaction", "TLS interaction",
          "A GTlsInteraction object to be used when the connection or certificate database need to interact with the user.",
          G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

387 388 389 390 391 392 393 394 395 396 397 398 399
 /**
   * GstSoupHTTPSrc::retries:
   *
   * Maximum number of retries until giving up.
   *
   * Since: 1.4
   */
  g_object_class_install_property (gobject_class, PROP_RETRIES,
      g_param_spec_int ("retries", "Retries",
          "Maximum number of retries until giving up (-1=infinite)", -1,
          G_MAXINT, DEFAULT_RETRIES,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

400 401 402 403 404 405 406 407 408 409 410 411
 /**
   * GstSoupHTTPSrc::method
   *
   * The HTTP method to use when making a request
   *
   * Since: 1.6
   */
  g_object_class_install_property (gobject_class, PROP_METHOD,
      g_param_spec_string ("method", "HTTP method",
          "The HTTP method to use (GET, HEAD, OPTIONS, etc)",
          DEFAULT_SOUP_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

412
  gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
413

414
  gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
415 416 417
      "Source/Network",
      "Receive data as a client over the network via HTTP using SOUP",
      "Wouter Cloetens <wouter@mind.be>");
418 419
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
420 421
  gstelement_class->set_context =
      GST_DEBUG_FUNCPTR (gst_soup_http_src_set_context);
422

423 424 425
  gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
  gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
  gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
426
  gstbasesrc_class->unlock_stop =
427 428
      GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
  gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
429
  gstbasesrc_class->is_seekable =
430 431
      GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
  gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
432
  gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
433

434
  gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
435 436 437

  GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
      "SOUP HTTP src");
438 439
}

440 441 442
static void
gst_soup_http_src_reset (GstSoupHTTPSrc * src)
{
443
  src->retry_count = 0;
444
  src->have_size = FALSE;
445
  src->got_headers = FALSE;
446
  src->seekable = FALSE;
447 448
  src->read_position = 0;
  src->request_position = 0;
449
  src->stop_position = -1;
450
  src->content_size = 0;
451
  src->have_body = FALSE;
452

453 454 455
  src->reduce_blocksize_count = 0;
  src->increase_blocksize_count = 0;

456
  g_cancellable_reset (src->cancellable);
457
  g_mutex_lock (&src->mutex);
458 459 460 461
  if (src->input_stream) {
    g_object_unref (src->input_stream);
    src->input_stream = NULL;
  }
462
  g_mutex_unlock (&src->mutex);
463

464 465 466 467 468 469 470 471 472
  gst_caps_replace (&src->src_caps, NULL);
  g_free (src->iradio_name);
  src->iradio_name = NULL;
  g_free (src->iradio_genre);
  src->iradio_genre = NULL;
  g_free (src->iradio_url);
  src->iradio_url = NULL;
}

473
static void
474
gst_soup_http_src_init (GstSoupHTTPSrc * src)
475
{
476 477
  const gchar *proxy;

478
  g_mutex_init (&src->mutex);
479 480
  g_cond_init (&src->have_headers_cond);
  src->cancellable = g_cancellable_new ();
481
  src->location = NULL;
482
  src->redirection_uri = NULL;
483
  src->automatic_redirect = TRUE;
484
  src->user_agent = g_strdup (DEFAULT_USER_AGENT);
485 486 487 488
  src->user_id = NULL;
  src->user_pw = NULL;
  src->proxy_id = NULL;
  src->proxy_pw = NULL;
489
  src->cookies = NULL;
490
  src->iradio_mode = DEFAULT_IRADIO_MODE;
491
  src->session = NULL;
492 493
  src->external_session = NULL;
  src->forced_external_session = FALSE;
494
  src->msg = NULL;
495
  src->timeout = DEFAULT_TIMEOUT;
496
  src->log_level = DEFAULT_SOUP_LOG_LEVEL;
497 498
  src->compress = DEFAULT_COMPRESS;
  src->keep_alive = DEFAULT_KEEP_ALIVE;
499 500
  src->ssl_strict = DEFAULT_SSL_STRICT;
  src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
501
  src->tls_database = DEFAULT_TLS_DATABASE;
502
  src->tls_interaction = DEFAULT_TLS_INTERACTION;
503
  src->max_retries = DEFAULT_RETRIES;
504
  src->method = DEFAULT_SOUP_METHOD;
505
  src->minimum_blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
506
  proxy = g_getenv ("http_proxy");
507
  if (!gst_soup_http_src_set_proxy (src, proxy)) {
508 509 510 511
    GST_WARNING_OBJECT (src,
        "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
        proxy);
  }
512

513 514
  gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);

515
  gst_soup_http_src_reset (src);
516 517
}

518 519 520 521 522 523 524 525 526
static void
gst_soup_http_src_dispose (GObject * gobject)
{
  GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);

  GST_DEBUG_OBJECT (src, "dispose");

  gst_soup_http_src_session_close (src);

527 528 529 530 531
  if (src->external_session) {
    g_object_unref (src->external_session);
    src->external_session = NULL;
  }

532 533 534
  G_OBJECT_CLASS (parent_class)->dispose (gobject);
}

535
static void
536
gst_soup_http_src_finalize (GObject * gobject)
537
{
538
  GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
539

540 541
  GST_DEBUG_OBJECT (src, "finalize");

542
  g_mutex_clear (&src->mutex);
543 544
  g_cond_clear (&src->have_headers_cond);
  g_object_unref (src->cancellable);
545
  g_free (src->location);
546
  g_free (src->redirection_uri);
547
  g_free (src->user_agent);
548 549 550
  if (src->proxy != NULL) {
    soup_uri_free (src->proxy);
  }
551 552 553 554
  g_free (src->user_id);
  g_free (src->user_pw);
  g_free (src->proxy_id);
  g_free (src->proxy_pw);
555
  g_strfreev (src->cookies);
556

557 558 559 560 561
  if (src->extra_headers) {
    gst_structure_free (src->extra_headers);
    src->extra_headers = NULL;
  }

562 563
  g_free (src->ssl_ca_file);

564 565
  if (src->tls_database)
    g_object_unref (src->tls_database);
566
  g_free (src->method);
567

568 569 570
  if (src->tls_interaction)
    g_object_unref (src->tls_interaction);

571
  G_OBJECT_CLASS (parent_class)->finalize (gobject);
572 573 574
}

static void
575
gst_soup_http_src_set_property (GObject * object, guint prop_id,
576 577
    const GValue * value, GParamSpec * pspec)
{
578
  GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
579 580 581 582 583 584 585 586 587 588 589 590

  switch (prop_id) {
    case PROP_LOCATION:
    {
      const gchar *location;

      location = g_value_get_string (value);

      if (location == NULL) {
        GST_WARNING ("location property cannot be NULL");
        goto done;
      }
591
      if (!gst_soup_http_src_set_location (src, location, NULL)) {
592 593 594 595 596
        GST_WARNING ("badly formatted location");
        goto done;
      }
      break;
    }
597
    case PROP_USER_AGENT:
598
      g_free (src->user_agent);
599 600
      src->user_agent = g_value_dup_string (value);
      break;
601 602 603
    case PROP_IRADIO_MODE:
      src->iradio_mode = g_value_get_boolean (value);
      break;
604 605 606 607 608 609 610 611
    case PROP_AUTOMATIC_REDIRECT:
      src->automatic_redirect = g_value_get_boolean (value);
      break;
    case PROP_PROXY:
    {
      const gchar *proxy;

      proxy = g_value_get_string (value);
612
      if (!gst_soup_http_src_set_proxy (src, proxy)) {
613 614 615 616 617
        GST_WARNING ("badly formatted proxy URI");
        goto done;
      }
      break;
    }
618 619 620 621
    case PROP_COOKIES:
      g_strfreev (src->cookies);
      src->cookies = g_strdupv (g_value_get_boxed (value));
      break;
622 623 624
    case PROP_IS_LIVE:
      gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
      break;
625
    case PROP_USER_ID:
626
      g_free (src->user_id);
627 628 629
      src->user_id = g_value_dup_string (value);
      break;
    case PROP_USER_PW:
630
      g_free (src->user_pw);
631 632 633
      src->user_pw = g_value_dup_string (value);
      break;
    case PROP_PROXY_ID:
634
      g_free (src->proxy_id);
635 636 637
      src->proxy_id = g_value_dup_string (value);
      break;
    case PROP_PROXY_PW:
638
      g_free (src->proxy_pw);
639 640
      src->proxy_pw = g_value_dup_string (value);
      break;
641 642 643
    case PROP_TIMEOUT:
      src->timeout = g_value_get_uint (value);
      break;
644 645 646 647 648 649 650 651 652
    case PROP_EXTRA_HEADERS:{
      const GstStructure *s = gst_value_get_structure (value);

      if (src->extra_headers)
        gst_structure_free (src->extra_headers);

      src->extra_headers = s ? gst_structure_copy (s) : NULL;
      break;
    }
653 654 655
    case PROP_SOUP_LOG_LEVEL:
      src->log_level = g_value_get_enum (value);
      break;
656 657 658
    case PROP_COMPRESS:
      src->compress = g_value_get_boolean (value);
      break;
659 660 661
    case PROP_KEEP_ALIVE:
      src->keep_alive = g_value_get_boolean (value);
      break;
662 663 664 665
    case PROP_SSL_STRICT:
      src->ssl_strict = g_value_get_boolean (value);
      break;
    case PROP_SSL_CA_FILE:
666
      g_free (src->ssl_ca_file);
667 668 669 670 671
      src->ssl_ca_file = g_value_dup_string (value);
      break;
    case PROP_SSL_USE_SYSTEM_CA_FILE:
      src->ssl_use_system_ca_file = g_value_get_boolean (value);
      break;
672 673 674 675
    case PROP_TLS_DATABASE:
      g_clear_object (&src->tls_database);
      src->tls_database = g_value_dup_object (value);
      break;
676 677 678 679
    case PROP_TLS_INTERACTION:
      g_clear_object (&src->tls_interaction);
      src->tls_interaction = g_value_dup_object (value);
      break;
680 681 682
    case PROP_RETRIES:
      src->max_retries = g_value_get_int (value);
      break;
683
    case PROP_METHOD:
684
      g_free (src->method);
685 686
      src->method = g_value_dup_string (value);
      break;
687 688 689
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
690 691 692 693 694 695
  }
done:
  return;
}

static void
696
gst_soup_http_src_get_property (GObject * object, guint prop_id,
697 698
    GValue * value, GParamSpec * pspec)
{
699
  GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
700 701 702

  switch (prop_id) {
    case PROP_LOCATION:
703 704 705 706 707
      g_value_set_string (value, src->location);
      break;
    case PROP_USER_AGENT:
      g_value_set_string (value, src->user_agent);
      break;
708 709 710 711 712
    case PROP_AUTOMATIC_REDIRECT:
      g_value_set_boolean (value, src->automatic_redirect);
      break;
    case PROP_PROXY:
      if (src->proxy == NULL)
713
        g_value_set_static_string (value, "");
714 715 716 717
      else {
        char *proxy = soup_uri_to_string (src->proxy, FALSE);

        g_value_set_string (value, proxy);
718
        g_free (proxy);
719 720
      }
      break;
721 722 723
    case PROP_COOKIES:
      g_value_set_boxed (value, g_strdupv (src->cookies));
      break;
724 725 726
    case PROP_IS_LIVE:
      g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
      break;
727 728 729
    case PROP_IRADIO_MODE:
      g_value_set_boolean (value, src->iradio_mode);
      break;
730 731 732 733 734 735 736 737 738 739 740 741
    case PROP_USER_ID:
      g_value_set_string (value, src->user_id);
      break;
    case PROP_USER_PW:
      g_value_set_string (value, src->user_pw);
      break;
    case PROP_PROXY_ID:
      g_value_set_string (value, src->proxy_id);
      break;
    case PROP_PROXY_PW:
      g_value_set_string (value, src->proxy_pw);
      break;
742 743 744
    case PROP_TIMEOUT:
      g_value_set_uint (value, src->timeout);
      break;
745 746 747
    case PROP_EXTRA_HEADERS:
      gst_value_set_structure (value, src->extra_headers);
      break;
748 749 750
    case PROP_SOUP_LOG_LEVEL:
      g_value_set_enum (value, src->log_level);
      break;
751 752 753
    case PROP_COMPRESS:
      g_value_set_boolean (value, src->compress);
      break;
754 755 756
    case PROP_KEEP_ALIVE:
      g_value_set_boolean (value, src->keep_alive);
      break;
757 758 759 760 761 762 763
    case PROP_SSL_STRICT:
      g_value_set_boolean (value, src->ssl_strict);
      break;
    case PROP_SSL_CA_FILE:
      g_value_set_string (value, src->ssl_ca_file);
      break;
    case PROP_SSL_USE_SYSTEM_CA_FILE:
764
      g_value_set_boolean (value, src->ssl_use_system_ca_file);
765
      break;
766 767 768
    case PROP_TLS_DATABASE:
      g_value_set_object (value, src->tls_database);
      break;
769 770 771
    case PROP_TLS_INTERACTION:
      g_value_set_object (value, src->tls_interaction);
      break;
772 773 774
    case PROP_RETRIES:
      g_value_set_int (value, src->max_retries);
      break;
775 776 777
    case PROP_METHOD:
      g_value_set_string (value, src->method);
      break;
778 779 780 781 782 783
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

784
static gchar *
785
gst_soup_http_src_unicodify (const gchar * str)
786
{
787 788 789
  const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
    "GST_TAG_ENCODING", NULL
  };
790

791
  return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
792 793
}

794
static void
795
gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
796
{
797 798
  g_cancellable_cancel (src->cancellable);
  g_cond_signal (&src->have_headers_cond);
799 800 801
}

static gboolean
802 803
gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
    guint64 stop_offset)
804 805 806 807
{
  gchar buf[64];
  gint rc;

808
  soup_message_headers_remove (src->msg->request_headers, "Range");
809 810
  if (offset || stop_offset != -1) {
    if (stop_offset != -1) {
811 812
      g_assert (offset != stop_offset);

813
      rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
814 815
          G_GUINT64_FORMAT, offset, (stop_offset > 0) ? stop_offset - 1 :
          stop_offset);
816 817 818 819
    } else {
      rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
          offset);
    }
820 821
    if (rc > sizeof (buf) || rc < 0)
      return FALSE;
822
    soup_message_headers_append (src->msg->request_headers, "Range", buf);
823 824 825
  }
  src->read_position = offset;
  return TRUE;
826 827
}

828
static gboolean
829
_append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
830 831 832
{
  GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
  const gchar *field_name = g_quark_to_string (field_id);
833
  gchar *field_content = NULL;
834

835 836 837 838 839 840 841 842 843
  if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
    field_content = g_value_dup_string (value);
  } else {
    GValue dest = { 0, };

    g_value_init (&dest, G_TYPE_STRING);
    if (g_value_transform (value, &dest)) {
      field_content = g_value_dup_string (&dest);
    }
844 845 846
  }

  if (field_content == NULL) {
847 848
    GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
        "or can't be converted to a string", field_name);
849 850 851 852 853 854 855
    return FALSE;
  }

  GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
      field_content);
  soup_message_headers_append (src->msg->request_headers, field_name,
      field_content);
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889

  g_free (field_content);

  return TRUE;
}

static gboolean
_append_extra_headers (GQuark field_id, const GValue * value,
    gpointer user_data)
{
  if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
    guint n = gst_value_array_get_size (value);
    guint i;

    for (i = 0; i < n; i++) {
      const GValue *v = gst_value_array_get_value (value, i);

      if (!_append_extra_header (field_id, v, user_data))
        return FALSE;
    }
  } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
    guint n = gst_value_list_get_size (value);
    guint i;

    for (i = 0; i < n; i++) {
      const GValue *v = gst_value_list_get_value (value, i);

      if (!_append_extra_header (field_id, v, user_data))
        return FALSE;
    }
  } else {
    return _append_extra_header (field_id, value, user_data);
  }

890 891 892 893 894 895 896 897 898 899 900 901 902
  return TRUE;
}


static gboolean
gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
{
  if (!src->extra_headers)
    return TRUE;

  return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
static gboolean
gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
{
  if (src->session) {
    GST_DEBUG_OBJECT (src, "Session is already open");
    return TRUE;
  }

  if (!src->location) {
    GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
        ("Missing location property"));
    return FALSE;
  }

  if (!src->session) {
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    GstQuery *query;
    gboolean can_share = (src->timeout == DEFAULT_TIMEOUT)
        && (src->ssl_strict == DEFAULT_SSL_STRICT)
        && (src->tls_interaction == NULL) && (src->proxy == NULL)
        && (src->tls_database == DEFAULT_TLS_DATABASE)
        && (src->ssl_ca_file == DEFAULT_SSL_CA_FILE)
        && (src->ssl_use_system_ca_file == DEFAULT_SSL_USE_SYSTEM_CA_FILE);

    query = gst_query_new_context (GST_SOUP_SESSION_CONTEXT);
    if (gst_pad_peer_query (GST_BASE_SRC_PAD (src), query)) {
      GstContext *context;

      gst_query_parse_context (query, &context);
      gst_element_set_context (GST_ELEMENT_CAST (src), context);
    } else {
      GstMessage *message;

      message =
          gst_message_new_need_context (GST_OBJECT_CAST (src),
          GST_SOUP_SESSION_CONTEXT);
      gst_element_post_message (GST_ELEMENT_CAST (src), message);
    }
    gst_query_unref (query);

942
    GST_OBJECT_LOCK (src);
943 944 945 946
    if (src->external_session && (can_share || src->forced_external_session)) {
      GST_DEBUG_OBJECT (src, "Using external session %p",
          src->external_session);
      src->session = g_object_ref (src->external_session);
947
      src->session_is_shared = TRUE;
948
    } else {
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
      GST_DEBUG_OBJECT (src, "Creating session (can share %d)", can_share);

      /* We explicitly set User-Agent to NULL here and overwrite it per message
       * to be able to have the same session with different User-Agents per
       * source */
      if (src->proxy == NULL) {
        src->session =
            soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
            NULL, SOUP_SESSION_TIMEOUT, src->timeout,
            SOUP_SESSION_SSL_STRICT, src->ssl_strict,
            SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
      } else {
        src->session =
            soup_session_new_with_options (SOUP_SESSION_PROXY_URI, src->proxy,
            SOUP_SESSION_TIMEOUT, src->timeout,
            SOUP_SESSION_SSL_STRICT, src->ssl_strict,
            SOUP_SESSION_USER_AGENT, NULL,
            SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
      }

      if (src->session) {
        gst_soup_util_log_setup (src->session, src->log_level,
            GST_ELEMENT (src));
        soup_session_add_feature_by_type (src->session,
            SOUP_TYPE_CONTENT_DECODER);
974
        soup_session_add_feature_by_type (src->session, SOUP_TYPE_COOKIE_JAR);
975 976 977 978 979 980 981

        if (can_share) {
          GstContext *context;
          GstMessage *message;
          GstStructure *s;

          GST_DEBUG_OBJECT (src, "Sharing session %p", src->session);
982
          src->session_is_shared = TRUE;
983

984 985 986 987
          /* Unset the limit the number of maximum allowed connection */
          g_object_set (src->session, SOUP_SESSION_MAX_CONNS, G_MAXINT,
              SOUP_SESSION_MAX_CONNS_PER_HOST, G_MAXINT, NULL);

988 989 990 991 992
          context = gst_context_new (GST_SOUP_SESSION_CONTEXT, TRUE);
          s = gst_context_writable_structure (context);
          gst_structure_set (s, "session", SOUP_TYPE_SESSION, src->session,
              "force", G_TYPE_BOOLEAN, FALSE, NULL);

993 994
          gst_object_ref (src->session);
          GST_OBJECT_UNLOCK (src);
995 996 997 998
          gst_element_set_context (GST_ELEMENT_CAST (src), context);
          message =
              gst_message_new_have_context (GST_OBJECT_CAST (src), context);
          gst_element_post_message (GST_ELEMENT_CAST (src), message);
999 1000 1001 1002
          GST_OBJECT_LOCK (src);
          gst_object_unref (src->session);
        } else {
          src->session_is_shared = FALSE;
1003 1004
        }
      }
1005 1006 1007 1008
    }

    if (!src->session) {
      GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1009
          (NULL), ("Failed to create session"));
1010
      GST_OBJECT_UNLOCK (src);
1011 1012
      return FALSE;
    }
1013

1014 1015
    g_signal_connect (src->session, "authenticate",
        G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1016

1017
    if (!src->session_is_shared) {
1018 1019 1020 1021 1022 1023 1024 1025
      if (src->tls_database)
        g_object_set (src->session, "tls-database", src->tls_database, NULL);
      else if (src->ssl_ca_file)
        g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
      else
        g_object_set (src->session, "ssl-use-system-ca-file",
            src->ssl_use_system_ca_file, NULL);
    }
1026
    GST_OBJECT_UNLOCK (src);
1027 1028 1029
  } else {
    GST_DEBUG_OBJECT (src, "Re-using session");
  }
1030

1031 1032 1033
  return TRUE;
}

1034
static void
1035
gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
1036
{
1037 1038
  GST_DEBUG_OBJECT (src, "Closing session");

1039
  g_mutex_lock (&src->mutex);
1040 1041 1042 1043 1044 1045
  if (src->msg) {
    soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
    g_object_unref (src->msg);
    src->msg = NULL;
  }

1046
  if (src->session) {
1047
    if (!src->session_is_shared)
1048 1049 1050
      soup_session_abort (src->session);
    g_signal_handlers_disconnect_by_func (src->session,
        G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1051 1052 1053
    g_object_unref (src->session);
    src->session = NULL;
  }
1054

1055
  g_mutex_unlock (&src->mutex);
1056 1057
}

1058 1059 1060 1061
static void
gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
    SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
{
1062 1063 1064 1065
  /* Might be from another user of the shared session */
  if (!GST_IS_SOUP_HTTP_SRC (src) || msg != src->msg)
    return;

1066 1067 1068
  if (!retrying) {
    /* First time authentication only, if we fail and are called again with retry true fall through */
    if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1069 1070
      if (src->user_id && src->user_pw)
        soup_auth_authenticate (auth, src->user_id, src->user_pw);
1071
    } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1072 1073
      if (src->proxy_id && src->proxy_pw)
        soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
1074 1075 1076 1077
    }
  }
}

1078 1079 1080 1081 1082 1083
static void
insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
{
  GstStructure *headers = user_data;
  const GValue *gv;

1084 1085 1086
  if (!g_utf8_validate (name, -1, NULL) || !g_utf8_validate (value, -1, NULL))
    return;

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  gv = gst_structure_get_value (headers, name);
  if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
    GValue v = G_VALUE_INIT;

    g_value_init (&v, G_TYPE_STRING);
    g_value_set_string (&v, value);
    gst_value_array_append_value ((GValue *) gv, &v);
    g_value_unset (&v);
  } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
    GValue arr = G_VALUE_INIT;
    GValue v = G_VALUE_INIT;
    const gchar *old_value = g_value_get_string (gv);

    g_value_init (&arr, GST_TYPE_ARRAY);
    g_value_init (&v, G_TYPE_STRING);
    g_value_set_string (&v, old_value);
    gst_value_array_append_value (&arr, &v);
    g_value_set_string (&v, value);
    gst_value_array_append_value (&arr, &v);

    gst_structure_set_value (headers, name, &arr);
    g_value_unset (&v);
    g_value_unset (&arr);
  } else {
    gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
  }
}

1115
static GstFlowReturn
1116
gst_soup_http_src_got_headers (GstSoupHTTPSrc * src, SoupMessage * msg)
1117 1118
{
  const char *value;
1119
  GstTagList *tag_list;
1120
  GstBaseSrc *basesrc;
1121
  guint64 newsize;
1122
  GHashTable *params = NULL;
1123 1124
  GstEvent *http_headers_event;
  GstStructure *http_headers, *headers;
1125
  const gchar *accept_ranges;
1126

1127
  GST_INFO_OBJECT (src, "got headers");
1128

1129
  if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1130 1131 1132 1133
      src->proxy_id && src->proxy_pw) {
    /* wait for authenticate callback */
    return GST_FLOW_OK;
  }
1134

1135
  http_headers = gst_structure_new_empty ("http-headers");
1136 1137
  gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location,
      "http-status-code", G_TYPE_UINT, msg->status_code, NULL);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  if (src->redirection_uri)
    gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
        src->redirection_uri, NULL);
  headers = gst_structure_new_empty ("request-headers");
  soup_message_headers_foreach (msg->request_headers, insert_http_header,
      headers);
  gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
      headers, NULL);
  gst_structure_free (headers);
  headers = gst_structure_new_empty ("response-headers");
  soup_message_headers_foreach (msg->response_headers, insert_http_header,
      headers);
  gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
      headers, NULL);
  gst_structure_free (headers);

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
  gst_element_post_message (GST_ELEMENT_CAST (src),
      gst_message_new_element (GST_OBJECT_CAST (src),
          gst_structure_copy (http_headers)));

  if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
    /* force an error */
    gst_structure_free (http_headers);
    return gst_soup_http_src_parse_status (msg, src);
  }

  src->got_headers = TRUE;
  g_cond_broadcast (&src->have_headers_cond);

1167 1168 1169 1170 1171
  http_headers_event =
      gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
  gst_event_replace (&src->http_headers_event, http_headers_event);
  gst_event_unref (http_headers_event);

1172
  /* Parse Content-Length. */
1173 1174 1175 1176
  if (soup_message_headers_get_encoding (msg->response_headers) ==
      SOUP_ENCODING_CONTENT_LENGTH) {
    newsize = src->request_position +
        soup_message_headers_get_content_length (msg->response_headers);
1177 1178 1179
    if (!src->have_size || (src->content_size != newsize)) {
      src->content_size = newsize;
      src->have_size = TRUE;
1180
      src->seekable = TRUE;
1181
      GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1182 1183

      basesrc = GST_BASE_SRC_CAST (src);
1184
      basesrc->segment.duration = src->content_size;
1185
      gst_element_post_message (GST_ELEMENT (src),
1186
          gst_message_new_duration_changed (GST_OBJECT (src)));
1187
    }
1188
  }
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
  /* If the server reports Accept-Ranges: none we don't have to try
   * doing range requests at all
   */
  if ((accept_ranges =
          soup_message_headers_get_one (msg->response_headers,
              "Accept-Ranges"))) {
    if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
      src->seekable = FALSE;
  }

1200
  /* Icecast stuff */
1201
  tag_list = gst_tag_list_new_empty ();
1202

1203
  if ((value =
1204
          soup_message_headers_get_one (msg->response_headers,
1205
              "icy-metaint")) != NULL) {
1206
    gint icy_metaint;
1207

1208 1209 1210 1211 1212 1213 1214 1215
    if (g_utf8_validate (value, -1, NULL)) {
      icy_metaint = atoi (value);

      GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value,
          icy_metaint);
      if (icy_metaint > 0) {
        if (src->src_caps)
          gst_caps_unref (src->src_caps);
1216

1217 1218
        src->src_caps = gst_caps_new_simple ("application/x-icy",
            "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
Wim Taymans's avatar
Wim Taymans committed
1219

1220 1221
        gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
      }
1222
    }
1223 1224
  }
  if ((value =
1225 1226
          soup_message_headers_get_content_type (msg->response_headers,
              &params)) != NULL) {
1227 1228 1229
    if (!g_utf8_validate (value, -1, NULL)) {
      GST_WARNING_OBJECT (src, "Content-Type is invalid UTF-8");
    } else if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1230 1231 1232 1233
      gint channels = 2;
      gint rate = 44100;
      char *param;

1234 1235 1236
      GST_DEBUG_OBJECT (src, "Content-Type: %s", value);

      if (src->src_caps) {
1237
        gst_caps_unref (src->src_caps);
1238 1239
        src->src_caps = NULL;
      }
1240 1241

      param = g_hash_table_lookup (params, "channels");
1242 1243 1244 1245 1246 1247 1248
      if (param != NULL) {
        guint64 val = g_ascii_strtoull (param, NULL, 10);
        if (val < 64)
          channels = val;
        else
          channels = 0;
      }
1249 1250

      param = g_hash_table_lookup (params, "rate");
1251 1252 1253 1254 1255 1256 1257
      if (param != NULL) {
        guint64 val = g_ascii_strtoull (param, NULL, 10);
        if (val < G_MAXINT)
          rate = val;
        else
          rate = 0;
      }
1258

1259 1260 1261 1262 1263
      if (rate > 0 && channels > 0) {
        src->src_caps = gst_caps_new_simple ("audio/x-unaligned-raw",
            "format", G_TYPE_STRING, "S16BE",
            "layout", G_TYPE_STRING, "interleaved",
            "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
Wim Taymans's avatar
Wim Taymans committed
1264

1265 1266
        gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
      }
1267
    } else {
1268 1269
      GST_DEBUG_OBJECT (src, "Content-Type: %s", value);

1270
      /* Set the Content-Type field on the caps */
Wim Taymans's avatar
Wim Taymans committed
1271 1272
      if (src->src_caps) {
        src->src_caps = gst_caps_make_writable (src->src_caps);
1273 1274
        gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
            value, NULL);
Wim Taymans's avatar
Wim Taymans committed
1275 1276
        gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
      }
1277
    }
1278 1279
  }

1280 1281 1282
  if (params != NULL)
    g_hash_table_destroy (params);

1283
  if ((value =
1284
          soup_message_headers_get_one (msg->response_headers,
1285
              "icy-name")) != NULL) {
1286 1287 1288 1289 1290 1291 1292
    if (g_utf8_validate (value, -1, NULL)) {
      g_free (src->iradio_name);
      src->iradio_name = gst_soup_http_src_unicodify (value);
      if (src->iradio_name) {
        gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
            src->iradio_name, NULL);
      }
1293
    }
1294 1295
  }
  if ((value =
1296
          soup_message_headers_get_one (msg->response_headers,
1297
              "icy-genre")) != NULL) {
1298 1299 1300 1301 1302 1303 1304
    if (g_utf8_validate (value, -1, NULL)) {
      g_free (src->iradio_genre);
      src->iradio_genre = gst_soup_http_src_unicodify (value);
      if (src->iradio_genre) {
        gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
            src->iradio_genre, NULL);
      }
1305
    }
1306
  }
1307
  if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1308
      != NULL) {
1309 1310 1311 1312 1313 1314 1315
    if (g_utf8_validate (value, -1, NULL)) {
      g_free (src->iradio_url);
      src->iradio_url = gst_soup_http_src_unicodify (value);
      if (src->iradio_url) {
        gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
            src->iradio_url, NULL);
      }
1316 1317 1318 1319 1320
    }
  }
  if (!gst_tag_list_is_empty (tag_list)) {
    GST_DEBUG_OBJECT (src,
        "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1321
    gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1322
  } else {
1323
    gst_tag_list_unref (tag_list);