2 * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
3 * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
4 * Copyright (C) <2006> Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more
21 #include "gstneonhttpsrc.h"
26 #endif /* _HAVE_UNISTD_H */
28 #include <ne_redirect.h>
30 #define STATUS_IS_REDIRECTION(status) ((status) >= 300 && (status) < 400)
32 GST_DEBUG_CATEGORY_STATIC (neonhttpsrc_debug);
33 #define GST_CAT_DEFAULT neonhttpsrc_debug
35 #define MAX_READ_SIZE (4 * 1024)
37 /* max number of HTTP redirects, when iterating over a sequence of HTTP 3xx status code */
38 #define MAX_HTTP_REDIRECTS_NUMBER 5
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
45 #define HTTP_SOCKET_ERROR -2
46 #define HTTP_REQUEST_WRONG_PROXY -1
47 #define HTTP_DEFAULT_PORT 80
48 #define HTTPS_DEFAULT_PORT 443
49 #define HTTP_DEFAULT_HOST "localhost"
51 /* default properties */
52 #define DEFAULT_LOCATION "http://"HTTP_DEFAULT_HOST":"G_STRINGIFY(HTTP_DEFAULT_PORT)
53 #define DEFAULT_PROXY ""
54 #define DEFAULT_USER_AGENT "GStreamer neonhttpsrc"
55 #define DEFAULT_IRADIO_MODE FALSE
56 #define DEFAULT_IRADIO_NAME NULL
57 #define DEFAULT_IRADIO_GENRE NULL
58 #define DEFAULT_IRADIO_URL NULL
59 #define DEFAULT_AUTOMATIC_REDIRECT TRUE
60 #define DEFAULT_ACCEPT_SELF_SIGNED FALSE
61 #define DEFAULT_NEON_HTTP_DEBUG FALSE
73 PROP_AUTOMATIC_REDIRECT,
74 PROP_ACCEPT_SELF_SIGNED,
75 #ifndef GST_DISABLE_GST_DEBUG
80 static void gst_neonhttp_src_uri_handler_init (gpointer g_iface,
82 static void gst_neonhttp_src_dispose (GObject * gobject);
83 static void gst_neonhttp_src_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_neonhttp_src_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
88 static GstFlowReturn gst_neonhttp_src_create (GstPushSrc * psrc,
90 static gboolean gst_neonhttp_src_start (GstBaseSrc * bsrc);
91 static gboolean gst_neonhttp_src_stop (GstBaseSrc * bsrc);
92 static gboolean gst_neonhttp_src_get_size (GstBaseSrc * bsrc, guint64 * size);
93 static gboolean gst_neonhttp_src_is_seekable (GstBaseSrc * bsrc);
94 static gboolean gst_neonhttp_src_do_seek (GstBaseSrc * bsrc,
95 GstSegment * segment);
97 static gboolean gst_neonhttp_src_set_proxy (GstNeonhttpSrc * src,
99 static gboolean gst_neonhttp_src_set_location (GstNeonhttpSrc * src,
101 static gint gst_neonhttp_src_send_request_and_redirect (GstNeonhttpSrc * src,
102 ne_session ** ses, ne_request ** req, gint64 offset, gboolean do_redir);
103 static gint gst_neonhttp_src_request_dispatch (GstNeonhttpSrc * src,
105 static void gst_neonhttp_src_close_session (GstNeonhttpSrc * src);
106 static gchar *gst_neonhttp_src_unicodify (const gchar * str);
107 static void oom_callback (void);
110 _urihandler_init (GType type)
112 static const GInterfaceInfo urihandler_info = {
113 gst_neonhttp_src_uri_handler_init,
118 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
120 GST_DEBUG_CATEGORY_INIT (neonhttpsrc_debug, "neonhttpsrc", 0,
124 GST_BOILERPLATE_FULL (GstNeonhttpSrc, gst_neonhttp_src, GstPushSrc,
125 GST_TYPE_PUSH_SRC, _urihandler_init);
128 gst_neonhttp_src_base_init (gpointer g_class)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
132 gst_element_class_add_pad_template (element_class,
133 gst_static_pad_template_get (&srctemplate));
135 gst_element_class_set_details_simple (element_class, "HTTP client source",
137 "Receive data as a client over the network via HTTP using NEON",
138 "Edgard Lima <edgard.lima@indt.org.br>, "
139 "Rosfran Borges <rosfran.borges@indt.org.br>, "
140 "Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>");
144 gst_neonhttp_src_class_init (GstNeonhttpSrcClass * klass)
146 GObjectClass *gobject_class;
147 GstBaseSrcClass *gstbasesrc_class;
148 GstPushSrcClass *gstpushsrc_class;
150 gobject_class = (GObjectClass *) klass;
151 gstbasesrc_class = (GstBaseSrcClass *) klass;
152 gstpushsrc_class = (GstPushSrcClass *) klass;
154 gobject_class->set_property = gst_neonhttp_src_set_property;
155 gobject_class->get_property = gst_neonhttp_src_get_property;
156 gobject_class->dispose = gst_neonhttp_src_dispose;
158 g_object_class_install_property
159 (gobject_class, PROP_LOCATION,
160 g_param_spec_string ("location", "Location",
161 "Location to read from", "", G_PARAM_READWRITE));
163 g_object_class_install_property
164 (gobject_class, PROP_PROXY,
165 g_param_spec_string ("proxy", "Proxy",
166 "Proxy server to use, in the form HOSTNAME:PORT. "
167 "Defaults to the http_proxy environment variable",
168 "", G_PARAM_READWRITE));
170 g_object_class_install_property
171 (gobject_class, PROP_USER_AGENT,
172 g_param_spec_string ("user-agent", "User-Agent",
173 "Value of the User-Agent HTTP request header field",
174 "GStreamer neonhttpsrc", G_PARAM_READWRITE));
176 g_object_class_install_property
177 (gobject_class, PROP_IRADIO_MODE,
178 g_param_spec_boolean ("iradio-mode", "iradio-mode",
179 "Enable internet radio mode (extraction of shoutcast/icecast metadata)",
180 FALSE, G_PARAM_READWRITE));
182 g_object_class_install_property (gobject_class,
184 g_param_spec_string ("iradio-name",
185 "iradio-name", "Name of the stream", NULL, G_PARAM_READABLE));
187 g_object_class_install_property (gobject_class,
189 g_param_spec_string ("iradio-genre",
190 "iradio-genre", "Genre of the stream", NULL, G_PARAM_READABLE));
192 g_object_class_install_property (gobject_class,
194 g_param_spec_string ("iradio-url",
196 "Homepage URL for radio stream", NULL, G_PARAM_READABLE));
198 g_object_class_install_property
199 (gobject_class, PROP_AUTOMATIC_REDIRECT,
200 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
201 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
202 TRUE, G_PARAM_READWRITE));
204 g_object_class_install_property
205 (gobject_class, PROP_ACCEPT_SELF_SIGNED,
206 g_param_spec_boolean ("accept-self-signed", "accept-self-signed",
207 "Accept self-signed SSL/TLS certificates",
208 DEFAULT_ACCEPT_SELF_SIGNED, G_PARAM_READWRITE));
210 #ifndef GST_DISABLE_GST_DEBUG
211 g_object_class_install_property
212 (gobject_class, PROP_NEON_HTTP_DEBUG,
213 g_param_spec_boolean ("neon-http-debug", "neon-http-debug",
214 "Enable Neon HTTP debug messages",
215 DEFAULT_NEON_HTTP_DEBUG, G_PARAM_READWRITE));
218 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_neonhttp_src_start);
219 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_neonhttp_src_stop);
220 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_neonhttp_src_get_size);
221 gstbasesrc_class->is_seekable =
222 GST_DEBUG_FUNCPTR (gst_neonhttp_src_is_seekable);
223 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_neonhttp_src_do_seek);
225 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_neonhttp_src_create);
227 GST_DEBUG_CATEGORY_INIT (neonhttpsrc_debug, "neonhttpsrc", 0,
228 "NEON HTTP Client Source");
232 gst_neonhttp_src_init (GstNeonhttpSrc * src, GstNeonhttpSrcClass * g_class)
236 src->neon_http_debug = DEFAULT_NEON_HTTP_DEBUG;
237 src->iradio_mode = DEFAULT_IRADIO_MODE;
238 src->iradio_name = DEFAULT_IRADIO_NAME;
239 src->iradio_genre = DEFAULT_IRADIO_GENRE;
240 src->iradio_url = DEFAULT_IRADIO_URL;
241 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
242 src->automatic_redirect = DEFAULT_AUTOMATIC_REDIRECT;
243 src->accept_self_signed = DEFAULT_ACCEPT_SELF_SIGNED;
247 memset (&src->uri, 0, sizeof (src->uri));
248 memset (&src->proxy, 0, sizeof (src->proxy));
249 src->content_size = -1;
250 src->icy_caps = NULL;
251 src->icy_metaint = 0;
252 src->seekable = TRUE;
254 gst_neonhttp_src_set_location (src, DEFAULT_LOCATION);
256 /* configure proxy */
257 str = g_getenv ("http_proxy");
258 if (str && !gst_neonhttp_src_set_proxy (src, str)) {
259 GST_WARNING_OBJECT (src,
260 "The proxy set on http_proxy env var ('%s') cannot be parsed.", str);
265 gst_neonhttp_src_dispose (GObject * gobject)
267 GstNeonhttpSrc *src = GST_NEONHTTP_SRC (gobject);
269 ne_uri_free (&src->uri);
270 ne_uri_free (&src->proxy);
272 g_free (src->user_agent);
273 g_free (src->iradio_name);
274 g_free (src->iradio_genre);
275 g_free (src->iradio_url);
278 gst_caps_unref (src->icy_caps);
279 src->icy_caps = NULL;
283 ne_request_destroy (src->request);
288 ne_close_connection (src->session);
289 ne_session_destroy (src->session);
294 ne_free (src->location);
296 if (src->query_string) {
297 ne_free (src->query_string);
300 G_OBJECT_CLASS (parent_class)->dispose (gobject);
304 gst_neonhttp_src_set_property (GObject * object, guint prop_id,
305 const GValue * value, GParamSpec * pspec)
307 GstNeonhttpSrc *src = GST_NEONHTTP_SRC (object);
314 proxy = g_value_get_string (value);
317 GST_WARNING ("proxy property cannot be NULL");
320 if (!gst_neonhttp_src_set_proxy (src, proxy)) {
321 GST_WARNING ("badly formated proxy");
328 const gchar *location;
330 location = g_value_get_string (value);
332 if (location == NULL) {
333 GST_WARNING ("location property cannot be NULL");
336 if (!gst_neonhttp_src_set_location (src, location)) {
337 GST_WARNING ("badly formated location");
342 case PROP_USER_AGENT:
344 g_free (src->user_agent);
345 src->user_agent = g_strdup (g_value_get_string (value));
347 case PROP_IRADIO_MODE:
348 src->iradio_mode = g_value_get_boolean (value);
350 case PROP_AUTOMATIC_REDIRECT:
351 src->automatic_redirect = g_value_get_boolean (value);
353 case PROP_ACCEPT_SELF_SIGNED:
354 src->accept_self_signed = g_value_get_boolean (value);
356 #ifndef GST_DISABLE_GST_DEBUG
357 case PROP_NEON_HTTP_DEBUG:
358 src->neon_http_debug = g_value_get_boolean (value);
362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 gst_neonhttp_src_get_property (GObject * object, guint prop_id,
371 GValue * value, GParamSpec * pspec)
373 GstNeonhttpSrc *neonhttpsrc = GST_NEONHTTP_SRC (object);
380 if (neonhttpsrc->proxy.host) {
381 str = ne_uri_unparse (&neonhttpsrc->proxy);
384 g_value_set_string (value, str);
387 g_value_set_static_string (value, "");
395 if (neonhttpsrc->uri.host) {
396 str = ne_uri_unparse (&neonhttpsrc->uri);
399 g_value_set_string (value, str);
402 g_value_set_static_string (value, "");
406 case PROP_USER_AGENT:
407 g_value_set_string (value, neonhttpsrc->user_agent);
409 case PROP_IRADIO_MODE:
410 g_value_set_boolean (value, neonhttpsrc->iradio_mode);
412 case PROP_IRADIO_NAME:
413 g_value_set_string (value, neonhttpsrc->iradio_name);
415 case PROP_IRADIO_GENRE:
416 g_value_set_string (value, neonhttpsrc->iradio_genre);
418 case PROP_IRADIO_URL:
419 g_value_set_string (value, neonhttpsrc->iradio_url);
421 case PROP_AUTOMATIC_REDIRECT:
422 g_value_set_boolean (value, neonhttpsrc->automatic_redirect);
424 case PROP_ACCEPT_SELF_SIGNED:
425 g_value_set_boolean (value, neonhttpsrc->accept_self_signed);
427 #ifndef GST_DISABLE_GST_DEBUG
428 case PROP_NEON_HTTP_DEBUG:
429 g_value_set_boolean (value, neonhttpsrc->neon_http_debug);
433 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
442 GST_ERROR ("memory exeception in neon");
446 gst_neonhttp_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
453 src = GST_NEONHTTP_SRC (psrc);
454 basesrc = GST_BASE_SRC_CAST (psrc);
456 /* The caller should know the number of bytes and not read beyond EOS. */
457 if (G_UNLIKELY (src->eos))
460 /* Create the buffer. */
461 ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (basesrc),
462 basesrc->segment.last_stop, basesrc->blocksize,
463 src->icy_caps ? src->icy_caps :
464 GST_PAD_CAPS (GST_BASE_SRC_PAD (basesrc)), outbuf);
466 if (G_UNLIKELY (ret != GST_FLOW_OK))
469 read = gst_neonhttp_src_request_dispatch (src, *outbuf);
470 if (G_UNLIKELY (read < 0))
473 GST_LOG_OBJECT (src, "returning %u bytes", GST_BUFFER_SIZE (*outbuf));
481 GST_DEBUG_OBJECT (src, "EOS reached");
482 return GST_FLOW_UNEXPECTED;
486 GST_ELEMENT_ERROR (src, RESOURCE, READ,
487 (NULL), ("Could not read any bytes (%i, %s)", read,
488 ne_get_error (src->session)));
489 gst_buffer_unref (*outbuf);
491 return GST_FLOW_ERROR;
495 /* create a socket for connecting to remote server */
497 gst_neonhttp_src_start (GstBaseSrc * bsrc)
499 GstNeonhttpSrc *src = GST_NEONHTTP_SRC (bsrc);
500 const gchar *content_length;
503 #ifndef GST_DISABLE_GST_DEBUG
504 if (src->neon_http_debug)
505 ne_debug_init (stderr, NE_DBG_HTTP);
508 ne_oom_callback (oom_callback);
510 res = ne_sock_init ();
514 res = gst_neonhttp_src_send_request_and_redirect (src,
515 &src->session, &src->request, 0, src->automatic_redirect);
517 if (res != NE_OK || !src->session) {
518 if (res == HTTP_SOCKET_ERROR) {
520 } else if (res == HTTP_REQUEST_WRONG_PROXY) {
523 goto begin_req_failed;
527 content_length = ne_get_response_header (src->request, "Content-Length");
530 src->content_size = g_ascii_strtoull (content_length, NULL, 10);
532 src->content_size = -1;
534 if (src->iradio_mode) {
536 const gchar *str_value;
539 str_value = ne_get_response_header (src->request, "icy-metaint");
541 if (sscanf (str_value, "%d", &gint_value) == 1) {
543 gst_caps_unref (src->icy_caps);
544 src->icy_caps = NULL;
546 src->icy_metaint = gint_value;
547 src->icy_caps = gst_caps_new_simple ("application/x-icy",
548 "metadata-interval", G_TYPE_INT, src->icy_metaint, NULL);
552 str_value = ne_get_response_header (src->request, "icy-name");
554 if (src->iradio_name) {
555 g_free (src->iradio_name);
556 src->iradio_name = NULL;
558 src->iradio_name = gst_neonhttp_src_unicodify (str_value);
560 str_value = ne_get_response_header (src->request, "icy-genre");
562 if (src->iradio_genre) {
563 g_free (src->iradio_genre);
564 src->iradio_genre = NULL;
566 src->iradio_genre = gst_neonhttp_src_unicodify (str_value);
568 str_value = ne_get_response_header (src->request, "icy-url");
570 if (src->iradio_url) {
571 g_free (src->iradio_url);
572 src->iradio_url = NULL;
574 src->iradio_url = gst_neonhttp_src_unicodify (str_value);
583 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
584 ("ne_sock_init() failed: %d", res));
589 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
590 ("HTTP Request failed when opening socket: %d", res));
595 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
596 ("Proxy Server URI is invalid - make sure that either both proxy host "
597 "and port are specified or neither."));
602 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
603 ("Could not begin request: %d", res));
608 /* close the socket and associated resources
609 * used both to recover from errors and go to NULL state */
611 gst_neonhttp_src_stop (GstBaseSrc * bsrc)
615 src = GST_NEONHTTP_SRC (bsrc);
617 if (src->iradio_name) {
618 g_free (src->iradio_name);
619 src->iradio_name = NULL;
622 if (src->iradio_genre) {
623 g_free (src->iradio_genre);
624 src->iradio_genre = NULL;
627 if (src->iradio_url) {
628 g_free (src->iradio_url);
629 src->iradio_url = NULL;
633 gst_caps_unref (src->icy_caps);
634 src->icy_caps = NULL;
638 src->content_size = -1;
639 src->read_position = 0;
640 src->seekable = TRUE;
642 gst_neonhttp_src_close_session (src);
644 #ifndef GST_DISABLE_GST_DEBUG
645 ne_debug_init (NULL, 0);
647 ne_oom_callback (NULL);
654 gst_neonhttp_src_get_size (GstBaseSrc * bsrc, guint64 * size)
658 src = GST_NEONHTTP_SRC (bsrc);
660 if (src->content_size == -1)
663 *size = src->content_size;
669 gst_neonhttp_src_is_seekable (GstBaseSrc * bsrc)
675 gst_neonhttp_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
679 ne_session *session = NULL;
680 ne_request *request = NULL;
682 src = GST_NEONHTTP_SRC (bsrc);
687 if (src->read_position == segment->start)
690 res = gst_neonhttp_src_send_request_and_redirect (src,
691 &session, &request, segment->start, src->automatic_redirect);
693 /* if we are able to seek, replace the session */
694 if (res == NE_OK && session) {
695 gst_neonhttp_src_close_session (src);
696 src->session = session;
697 src->request = request;
698 src->read_position = segment->start;
706 gst_neonhttp_src_set_location (GstNeonhttpSrc * src, const gchar * uri)
708 ne_uri_free (&src->uri);
710 ne_free (src->location);
711 src->location = NULL;
713 if (src->query_string) {
714 ne_free (src->query_string);
715 src->query_string = NULL;
718 if (ne_uri_parse (uri, &src->uri) != 0)
721 if (src->uri.scheme == NULL)
722 src->uri.scheme = g_strdup ("http");
724 if (src->uri.host == NULL)
725 src->uri.host = g_strdup (DEFAULT_LOCATION);
727 if (src->uri.port == 0) {
728 if (!strcmp (src->uri.scheme, "https"))
729 src->uri.port = HTTPS_DEFAULT_PORT;
731 src->uri.port = HTTP_DEFAULT_PORT;
735 src->uri.path = g_strdup ("");
737 src->query_string = g_strjoin ("?", src->uri.path, src->uri.query, NULL);
739 src->location = ne_uri_unparse (&src->uri);
747 ne_free (src->location);
748 src->location = NULL;
750 if (src->query_string) {
751 ne_free (src->query_string);
752 src->query_string = NULL;
754 ne_uri_free (&src->uri);
760 gst_neonhttp_src_set_proxy (GstNeonhttpSrc * src, const char *uri)
762 ne_uri_free (&src->proxy);
764 if (ne_uri_parse (uri, &src->proxy) != 0)
767 if (src->proxy.scheme)
768 GST_WARNING ("The proxy schema shouldn't be defined (schema is '%s')",
771 if (src->proxy.host && !src->proxy.port)
774 if (!src->proxy.path || src->proxy.userinfo)
781 ne_uri_free (&src->proxy);
787 ssl_verify_callback (void *data, int failures, const ne_ssl_certificate * cert)
789 GstNeonhttpSrc *src = GST_NEONHTTP_SRC (data);
791 if ((failures & NE_SSL_UNTRUSTED) &&
792 src->accept_self_signed && !ne_ssl_cert_signedby (cert)) {
793 GST_ELEMENT_INFO (src, RESOURCE, READ,
794 (NULL), ("Accepting self-signed server certificate"));
796 failures &= ~NE_SSL_UNTRUSTED;
799 if (failures & NE_SSL_NOTYETVALID)
800 GST_ELEMENT_ERROR (src, RESOURCE, READ,
801 (NULL), ("Server certificate not valid yet"));
802 if (failures & NE_SSL_EXPIRED)
803 GST_ELEMENT_ERROR (src, RESOURCE, READ,
804 (NULL), ("Server certificate has expired"));
805 if (failures & NE_SSL_IDMISMATCH)
806 GST_ELEMENT_ERROR (src, RESOURCE, READ,
807 (NULL), ("Server certificate doesn't match hostname"));
808 if (failures & NE_SSL_UNTRUSTED)
809 GST_ELEMENT_ERROR (src, RESOURCE, READ,
810 (NULL), ("Server certificate signer not trusted"));
812 GST_DEBUG_OBJECT (src, "failures: %d\n", failures);
817 /* Try to send the HTTP request to the Icecast server, and if possible deals with
818 * all the probable redirections (HTTP status code == 3xx)
821 gst_neonhttp_src_send_request_and_redirect (GstNeonhttpSrc * src,
822 ne_session ** ses, ne_request ** req, gint64 offset, gboolean do_redir)
824 ne_session *session = NULL;
825 ne_request *request = NULL;
827 gint http_status = 0;
828 guint request_count = 0;
831 if (src->proxy.host && src->proxy.port) {
833 ne_session_create (src->uri.scheme, src->uri.host, src->uri.port);
834 ne_session_proxy (session, src->proxy.host, src->proxy.port);
835 } else if (src->proxy.host || src->proxy.port) {
836 /* both proxy host and port must be specified or none */
837 return HTTP_REQUEST_WRONG_PROXY;
840 ne_session_create (src->uri.scheme, src->uri.host, src->uri.port);
843 ne_set_session_flag (session, NE_SESSFLAG_ICYPROTO, 1);
844 ne_ssl_set_verify (session, ssl_verify_callback, src);
846 request = ne_request_create (session, "GET", src->query_string);
848 if (src->user_agent) {
849 ne_add_request_header (request, "User-Agent", src->user_agent);
852 if (src->iradio_mode) {
853 ne_add_request_header (request, "icy-metadata", "1");
857 ne_print_request_header (request, "Range",
858 "bytes=%" G_GINT64_FORMAT "-", offset);
861 res = ne_begin_request (request);
864 /* When the HTTP status code is 3xx, it is not the SHOUTcast streaming content yet;
865 * Reload the HTTP request with a new URI value */
866 http_status = ne_get_status (request)->code;
867 if (STATUS_IS_REDIRECTION (http_status) && do_redir) {
870 /* the new URI value to go when redirecting can be found on the 'Location' HTTP header */
871 redir = ne_get_response_header (request, "Location");
873 ne_uri_free (&src->uri);
874 gst_neonhttp_src_set_location (src, redir);
875 GST_LOG_OBJECT (src, "Got HTTP Status Code %d", http_status);
876 GST_LOG_OBJECT (src, "Using 'Location' header [%s]", src->uri.host);
881 if ((res != NE_OK) ||
882 (offset == 0 && http_status != 200) ||
883 (offset > 0 && http_status != 206 &&
884 !STATUS_IS_REDIRECTION (http_status))) {
885 ne_request_destroy (request);
887 ne_close_connection (session);
888 ne_session_destroy (session);
890 if (offset > 0 && http_status != 206 &&
891 !STATUS_IS_REDIRECTION (http_status)) {
892 src->seekable = FALSE;
897 if (STATUS_IS_REDIRECTION (http_status) && do_redir) {
899 GST_LOG_OBJECT (src, "redirect request_count is now %d", request_count);
900 if (request_count < MAX_HTTP_REDIRECTS_NUMBER && do_redir) {
901 GST_INFO_OBJECT (src, "Redirecting to %s", src->uri.host);
903 GST_WARNING_OBJECT (src, "Will not redirect, try again with a "
904 "different URI or redirect location %s", src->uri.host);
906 /* FIXME: when not redirecting automatically, shouldn't we post a
907 * redirect element message on the bus? */
909 /* do the redirect, go back to send another HTTP request now using the 'Location' */
910 } while (do_redir && (request_count < MAX_HTTP_REDIRECTS_NUMBER)
911 && STATUS_IS_REDIRECTION (http_status));
922 gst_neonhttp_src_request_dispatch (GstNeonhttpSrc * src, GstBuffer * outbuf)
926 gint sizetoread = GST_BUFFER_SIZE (outbuf);
928 /* Loop sending the request:
929 * Retry whilst authentication fails and we supply it. */
933 while (sizetoread > 0) {
934 len = ne_read_response_block (src->request,
935 (gchar *) GST_BUFFER_DATA (outbuf) + read, sizetoread);
945 GST_BUFFER_SIZE (outbuf) = read;
950 } else if (len == 0) {
951 ret = ne_end_request (src->request);
952 if (ret != NE_RETRY) {
963 src->read_position += read;
970 gst_neonhttp_src_close_session (GstNeonhttpSrc * src)
973 ne_request_destroy (src->request);
978 ne_close_connection (src->session);
979 ne_session_destroy (src->session);
984 /* The following two charset mangling functions were copied from gnomevfssrc.
985 * Preserve them under the unverified assumption that they do something vaguely
989 unicodify (const gchar * str, gint len, ...)
991 gchar *ret = NULL, *cset;
993 gsize bytes_read, bytes_written;
995 if (g_utf8_validate (str, len, NULL))
996 return g_strndup (str, len >= 0 ? len : strlen (str));
998 va_start (args, len);
999 while ((cset = va_arg (args, gchar *)) != NULL) {
1000 if (!strcmp (cset, "locale"))
1001 ret = g_locale_to_utf8 (str, len, &bytes_read, &bytes_written, NULL);
1003 ret = g_convert (str, len, "UTF-8", cset,
1004 &bytes_read, &bytes_written, NULL);
1014 gst_neonhttp_src_unicodify (const gchar * str)
1016 return unicodify (str, -1, "locale", "ISO-8859-1", NULL);
1019 /* GstURIHandler Interface */
1021 gst_neonhttp_src_uri_get_type (void)
1027 gst_neonhttp_src_uri_get_protocols (void)
1029 static const gchar *protocols[] = { "http", "https", NULL };
1030 return (gchar **) protocols;
1033 static const gchar *
1034 gst_neonhttp_src_uri_get_uri (GstURIHandler * handler)
1036 GstNeonhttpSrc *src = GST_NEONHTTP_SRC (handler);
1038 return src->location;
1042 gst_neonhttp_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1044 GstNeonhttpSrc *src = GST_NEONHTTP_SRC (handler);
1046 return gst_neonhttp_src_set_location (src, uri);
1050 gst_neonhttp_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1052 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1054 iface->get_type = gst_neonhttp_src_uri_get_type;
1055 iface->get_protocols = gst_neonhttp_src_uri_get_protocols;
1056 iface->get_uri = gst_neonhttp_src_uri_get_uri;
1057 iface->set_uri = gst_neonhttp_src_uri_set_uri;
1060 /* entry point to initialize the plug-in
1061 * initialize the plug-in itself
1062 * register the element factories and pad templates
1063 * register the features
1066 plugin_init (GstPlugin * plugin)
1068 return gst_element_register (plugin, "neonhttpsrc", GST_RANK_NONE,
1069 GST_TYPE_NEONHTTP_SRC);
1072 /* this is the structure that gst-register looks for
1073 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1074 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1077 "lib neon http client src",
1078 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)