Blame gst-libs/gst/rtsp/gstrtspurl.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
/*
Packit 971217
 * Unless otherwise indicated, Source Code is licensed under MIT license.
Packit 971217
 * See further explanation attached in License Statement (distributed in the file
Packit 971217
 * LICENSE).
Packit 971217
 *
Packit 971217
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
Packit 971217
 * this software and associated documentation files (the "Software"), to deal in
Packit 971217
 * the Software without restriction, including without limitation the rights to
Packit 971217
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 971217
 * of the Software, and to permit persons to whom the Software is furnished to do
Packit 971217
 * so, subject to the following conditions:
Packit 971217
 *
Packit 971217
 * The above copyright notice and this permission notice shall be included in all
Packit 971217
 * copies or substantial portions of the Software.
Packit 971217
 *
Packit 971217
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 971217
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 971217
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit 971217
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit 971217
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 971217
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 971217
 * SOFTWARE.
Packit 971217
 */
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstrtspurl
Packit 971217
 * @title: GstRTSPUrl
Packit 971217
 * @short_description: handling RTSP urls
Packit 971217
 *
Packit 971217
 * Provides helper functions to handle RTSP urls.
Packit 971217
 */
Packit 971217
Packit 971217
#include <stdlib.h>
Packit 971217
#include <string.h>
Packit 971217
Packit 971217
#include "gstrtspurl.h"
Packit 971217
Packit 971217
G_DEFINE_BOXED_TYPE (GstRTSPUrl, gst_rtsp_url,
Packit 971217
    (GBoxedCopyFunc) gst_rtsp_url_copy, (GBoxedFreeFunc) gst_rtsp_url_free);
Packit 971217
Packit 971217
static const struct
Packit 971217
{
Packit 971217
  const char scheme[6];
Packit 971217
  GstRTSPLowerTrans transports;
Packit 971217
} rtsp_schemes_map[] = {
Packit 971217
  {
Packit 971217
  "rtsp", GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_UDP |
Packit 971217
        GST_RTSP_LOWER_TRANS_UDP_MCAST}, {
Packit 971217
  "rtspu", GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST}, {
Packit 971217
  "rtspt", GST_RTSP_LOWER_TRANS_TCP}, {
Packit 971217
  "rtsph", GST_RTSP_LOWER_TRANS_HTTP | GST_RTSP_LOWER_TRANS_TCP}, {
Packit 971217
  "rtsps", GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_UDP |
Packit 971217
        GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TLS}, {
Packit 971217
  "rtspsu",
Packit 971217
        GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST |
Packit 971217
        GST_RTSP_LOWER_TRANS_TLS}, {
Packit 971217
  "rtspst", GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_TLS}, {
Packit 971217
  "rtspsh",
Packit 971217
        GST_RTSP_LOWER_TRANS_HTTP | GST_RTSP_LOWER_TRANS_TCP |
Packit 971217
        GST_RTSP_LOWER_TRANS_TLS}
Packit 971217
};
Packit 971217
Packit 971217
/* format is rtsp[u]://[user:passwd@]host[:port]/abspath[?query] where host
Packit 971217
 * is a host name, an IPv4 dotted decimal address ("aaa.bbb.ccc.ddd") or an
Packit 971217
 * [IPv6] address ("[aabb:ccdd:eeff:gghh::sstt]" note the brackets around the
Packit 971217
 * address to allow the distinction between ':' as an IPv6 hexgroup separator
Packit 971217
 * and as a host/port separator) */
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_parse:
Packit 971217
 * @urlstr: the url string to parse
Packit 971217
 * @url: (out): location to hold the result.
Packit 971217
 *
Packit 971217
 * Parse the RTSP @urlstr into a newly allocated #GstRTSPUrl. Free after usage
Packit 971217
 * with gst_rtsp_url_free().
Packit 971217
 *
Packit 971217
 * Returns: a #GstRTSPResult.
Packit 971217
 */
Packit 971217
GstRTSPResult
Packit 971217
gst_rtsp_url_parse (const gchar * urlstr, GstRTSPUrl ** url)
Packit 971217
{
Packit 971217
  GstRTSPUrl *res;
Packit 971217
  gchar *p, *delim, *at, *col;
Packit 971217
  gchar *host_end = NULL;
Packit 971217
  guint i;
Packit 971217
Packit 971217
  g_return_val_if_fail (urlstr != NULL, GST_RTSP_EINVAL);
Packit 971217
  g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
Packit 971217
Packit 971217
  res = g_new0 (GstRTSPUrl, 1);
Packit 971217
Packit 971217
  p = (gchar *) urlstr;
Packit 971217
Packit 971217
  col = strstr (p, "://");
Packit 971217
  if (col == NULL)
Packit 971217
    goto invalid;
Packit 971217
Packit 971217
  for (i = 0; i < G_N_ELEMENTS (rtsp_schemes_map); i++) {
Packit 971217
    if (g_ascii_strncasecmp (rtsp_schemes_map[i].scheme, p, col - p) == 0) {
Packit 971217
      res->transports = rtsp_schemes_map[i].transports;
Packit 971217
      p = col + 3;
Packit 971217
      break;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  if (res->transports == GST_RTSP_LOWER_TRANS_UNKNOWN)
Packit 971217
    goto invalid;
Packit 971217
Packit 971217
  delim = strpbrk (p, "/?");
Packit 971217
  at = strchr (p, '@');
Packit 971217
Packit 971217
  if (at && delim && at > delim)
Packit 971217
    at = NULL;
Packit 971217
Packit 971217
  if (at) {
Packit 971217
    col = strchr (p, ':');
Packit 971217
Packit 971217
    /* must have a ':' and it must be before the '@' */
Packit 971217
    if (col == NULL || col > at)
Packit 971217
      goto invalid;
Packit 971217
Packit 971217
    res->user = g_uri_unescape_segment (p, col, NULL);
Packit 971217
    col++;
Packit 971217
    res->passwd = g_uri_unescape_segment (col, at, NULL);
Packit 971217
Packit 971217
    /* move to host */
Packit 971217
    p = at + 1;
Packit 971217
  }
Packit 971217
Packit 971217
  if (*p == '[') {
Packit 971217
    res->family = GST_RTSP_FAM_INET6;
Packit 971217
Packit 971217
    /* we have an IPv6 address in the URL, find the ending ] which must be
Packit 971217
     * before any delimiter */
Packit 971217
    host_end = strchr (++p, ']');
Packit 971217
    if (!host_end || (delim && host_end >= delim))
Packit 971217
      goto invalid;
Packit 971217
Packit 971217
    /* a port specifier must follow the address immediately */
Packit 971217
    col = host_end[1] == ':' ? host_end + 1 : NULL;
Packit 971217
  } else {
Packit 971217
    res->family = GST_RTSP_FAM_INET;
Packit 971217
Packit 971217
    col = strchr (p, ':');
Packit 971217
Packit 971217
    /* we have a ':' and a delimiter but the ':' is after the delimiter, it's
Packit 971217
     * not really part of the hostname */
Packit 971217
    if (col && delim && col >= delim)
Packit 971217
      col = NULL;
Packit 971217
Packit 971217
    host_end = col ? col : delim;
Packit 971217
  }
Packit 971217
Packit 971217
  if (!host_end)
Packit 971217
    res->host = g_strdup (p);
Packit 971217
  else {
Packit 971217
    res->host = g_strndup (p, host_end - p);
Packit 971217
Packit 971217
    if (col) {
Packit 971217
      res->port = strtoul (col + 1, NULL, 10);
Packit 971217
    } else {
Packit 971217
      /* no port specified, set to 0. gst_rtsp_url_get_port() will return the
Packit 971217
       * default port */
Packit 971217
      res->port = 0;
Packit 971217
    }
Packit 971217
  }
Packit 971217
  p = delim;
Packit 971217
Packit 971217
  if (p && *p == '/') {
Packit 971217
    delim = strchr (p, '?');
Packit 971217
    if (!delim)
Packit 971217
      res->abspath = g_strdup (p);
Packit 971217
    else
Packit 971217
      res->abspath = g_strndup (p, delim - p);
Packit 971217
    p = delim;
Packit 971217
  } else {
Packit 971217
    /* IQinVision IQeye 1080p fails if a path '/' is provided
Packit 971217
     * and RTSP does not mandate that a non-zero-length path
Packit 971217
     * must be used */
Packit 971217
    res->abspath = g_strdup ("");
Packit 971217
  }
Packit 971217
Packit 971217
  if (p && *p == '?')
Packit 971217
    res->query = g_strdup (p + 1);
Packit 971217
Packit 971217
  *url = res;
Packit 971217
Packit 971217
  return GST_RTSP_OK;
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
invalid:
Packit 971217
  {
Packit 971217
    gst_rtsp_url_free (res);
Packit 971217
    return GST_RTSP_EINVAL;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_copy:
Packit 971217
 * @url: a #GstRTSPUrl
Packit 971217
 *
Packit 971217
 * Make a copy of @url.
Packit 971217
 *
Packit 971217
 * Returns: a copy of @url. Free with gst_rtsp_url_free () after usage.
Packit 971217
 */
Packit 971217
GstRTSPUrl *
Packit 971217
gst_rtsp_url_copy (const GstRTSPUrl * url)
Packit 971217
{
Packit 971217
  GstRTSPUrl *res;
Packit 971217
Packit 971217
  g_return_val_if_fail (url != NULL, NULL);
Packit 971217
Packit 971217
  res = g_new0 (GstRTSPUrl, 1);
Packit 971217
Packit 971217
  res->transports = url->transports;
Packit 971217
  res->family = url->family;
Packit 971217
  res->user = g_strdup (url->user);
Packit 971217
  res->passwd = g_strdup (url->passwd);
Packit 971217
  res->host = g_strdup (url->host);
Packit 971217
  res->port = url->port;
Packit 971217
  res->abspath = g_strdup (url->abspath);
Packit 971217
  res->query = g_strdup (url->query);
Packit 971217
Packit 971217
  return res;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_free:
Packit 971217
 * @url: a #GstRTSPUrl
Packit 971217
 *
Packit 971217
 * Free the memory used by @url.
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_rtsp_url_free (GstRTSPUrl * url)
Packit 971217
{
Packit 971217
  if (url == NULL)
Packit 971217
    return;
Packit 971217
Packit 971217
  g_free (url->user);
Packit 971217
  g_free (url->passwd);
Packit 971217
  g_free (url->host);
Packit 971217
  g_free (url->abspath);
Packit 971217
  g_free (url->query);
Packit 971217
  g_free (url);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_set_port:
Packit 971217
 * @url: a #GstRTSPUrl
Packit 971217
 * @port: the port
Packit 971217
 *
Packit 971217
 * Set the port number in @url to @port.
Packit 971217
 *
Packit 971217
 * Returns: #GST_RTSP_OK.
Packit 971217
 */
Packit 971217
GstRTSPResult
Packit 971217
gst_rtsp_url_set_port (GstRTSPUrl * url, guint16 port)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
Packit 971217
Packit 971217
  url->port = port;
Packit 971217
Packit 971217
  return GST_RTSP_OK;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_get_port:
Packit 971217
 * @url: a #GstRTSPUrl
Packit 971217
 * @port: (out): location to hold the port
Packit 971217
 *
Packit 971217
 * Get the port number of @url.
Packit 971217
 *
Packit 971217
 * Returns: #GST_RTSP_OK.
Packit 971217
 */
Packit 971217
GstRTSPResult
Packit 971217
gst_rtsp_url_get_port (const GstRTSPUrl * url, guint16 * port)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
Packit 971217
  g_return_val_if_fail (port != NULL, GST_RTSP_EINVAL);
Packit 971217
Packit 971217
  /* if a port was specified, use that else use the default port. */
Packit 971217
  if (url->port != 0)
Packit 971217
    *port = url->port;
Packit 971217
  else
Packit 971217
    *port = GST_RTSP_DEFAULT_PORT;
Packit 971217
Packit 971217
  return GST_RTSP_OK;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_get_request_uri:
Packit 971217
 * @url: a #GstRTSPUrl
Packit 971217
 *
Packit 971217
 * Get a newly allocated string describing the request URI for @url.
Packit 971217
 *
Packit 971217
 * Returns: a string with the request URI. g_free() after usage.
Packit 971217
 */
Packit 971217
gchar *
Packit 971217
gst_rtsp_url_get_request_uri (const GstRTSPUrl * url)
Packit 971217
{
Packit 971217
  gchar *uri;
Packit 971217
  const gchar *pre_host;
Packit 971217
  const gchar *post_host;
Packit 971217
  const gchar *pre_query;
Packit 971217
  const gchar *query;
Packit 971217
Packit 971217
  g_return_val_if_fail (url != NULL, NULL);
Packit 971217
Packit 971217
  pre_host = url->family == GST_RTSP_FAM_INET6 ? "[" : "";
Packit 971217
  post_host = url->family == GST_RTSP_FAM_INET6 ? "]" : "";
Packit 971217
  pre_query = url->query ? "?" : "";
Packit 971217
  query = url->query ? url->query : "";
Packit 971217
Packit 971217
  if (url->port != 0) {
Packit 971217
    uri = g_strdup_printf ("rtsp://%s%s%s:%u%s%s%s", pre_host, url->host,
Packit 971217
        post_host, url->port, url->abspath, pre_query, query);
Packit 971217
  } else {
Packit 971217
    uri = g_strdup_printf ("rtsp://%s%s%s%s%s%s", pre_host, url->host,
Packit 971217
        post_host, url->abspath, pre_query, query);
Packit 971217
  }
Packit 971217
Packit 971217
  return uri;
Packit 971217
}
Packit 971217
Packit 971217
static int
Packit 971217
hex_to_int (gchar c)
Packit 971217
{
Packit 971217
  if (c >= '0' && c <= '9')
Packit 971217
    return c - '0';
Packit 971217
  else if (c >= 'a' && c <= 'f')
Packit 971217
    return c - 'a' + 10;
Packit 971217
  else if (c >= 'A' && c <= 'F')
Packit 971217
    return c - 'A' + 10;
Packit 971217
  else
Packit 971217
    return -1;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
unescape_path_component (gchar * comp)
Packit 971217
{
Packit 971217
  guint len = strlen (comp);
Packit 971217
  guint i;
Packit 971217
Packit 971217
  for (i = 0; i + 2 < len; i++)
Packit 971217
    if (comp[i] == '%') {
Packit 971217
      int a, b;
Packit 971217
Packit 971217
      a = hex_to_int (comp[i + 1]);
Packit 971217
      b = hex_to_int (comp[i + 2]);
Packit 971217
Packit 971217
      /* The a||b check is to ensure that the byte is not '\0' */
Packit 971217
      if (a >= 0 && b >= 0 && (a || b)) {
Packit 971217
        comp[i] = (gchar) (a * 16 + b);
Packit 971217
        memmove (comp + i + 1, comp + i + 3, len - i - 3);
Packit 971217
        len -= 2;
Packit 971217
        comp[len] = '\0';
Packit 971217
      }
Packit 971217
    }
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_rtsp_url_decode_path_components:
Packit 971217
 * @url: a #GstRTSPUrl
Packit 971217
 *
Packit 971217
 * Splits the path of @url on '/' boundaries, decoding the resulting components,
Packit 971217
 *
Packit 971217
 * The decoding performed by this routine is "URI decoding", as defined in RFC
Packit 971217
 * 3986, commonly known as percent-decoding. For example, a string "foo\%2fbar"
Packit 971217
 * will decode to "foo/bar" -- the \%2f being replaced by the corresponding byte
Packit 971217
 * with hex value 0x2f. Note that there is no guarantee that the resulting byte
Packit 971217
 * sequence is valid in any given encoding. As a special case, \%00 is not
Packit 971217
 * unescaped to NUL, as that would prematurely terminate the string.
Packit 971217
 *
Packit 971217
 * Also note that since paths usually start with a slash, the first component
Packit 971217
 * will usually be the empty string.
Packit 971217
 *
Packit 971217
 * Returns: (transfer full): %NULL-terminated array of URL components. Free with
Packit 971217
 * g_strfreev() when no longer needed.
Packit 971217
 */
Packit 971217
gchar **
Packit 971217
gst_rtsp_url_decode_path_components (const GstRTSPUrl * url)
Packit 971217
{
Packit 971217
  gchar **ret;
Packit 971217
  guint i;
Packit 971217
Packit 971217
  g_return_val_if_fail (url != NULL, NULL);
Packit 971217
  g_return_val_if_fail (url->abspath != NULL, NULL);
Packit 971217
Packit 971217
  ret = g_strsplit (url->abspath, "/", -1);
Packit 971217
Packit 971217
  for (i = 0; ret[i]; i++)
Packit 971217
    unescape_path_component (ret[i]);
Packit 971217
Packit 971217
  return ret;
Packit 971217
}