• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42 
43 /**
44  * SECTION:gstrtspurl
45  * @title: GstRTSPUrl
46  * @short_description: handling RTSP urls
47  *
48  * Provides helper functions to handle RTSP urls.
49  */
50 #ifdef HAVE_CONFIG_H
51 #include "config.h"
52 #endif
53 
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "gstrtspurl.h"
58 
59 G_DEFINE_BOXED_TYPE (GstRTSPUrl, gst_rtsp_url,
60     (GBoxedCopyFunc) gst_rtsp_url_copy, (GBoxedFreeFunc) gst_rtsp_url_free);
61 
62 static const struct
63 {
64   const char scheme[6];
65   GstRTSPLowerTrans transports;
66 } rtsp_schemes_map[] = {
67   {
68   "rtsp", GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_UDP |
69         GST_RTSP_LOWER_TRANS_UDP_MCAST}, {
70   "rtspu", GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST}, {
71   "rtspt", GST_RTSP_LOWER_TRANS_TCP}, {
72   "rtsph", GST_RTSP_LOWER_TRANS_HTTP | GST_RTSP_LOWER_TRANS_TCP}, {
73   "rtsps", GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_UDP |
74         GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TLS}, {
75   "rtspsu",
76         GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST |
77         GST_RTSP_LOWER_TRANS_TLS}, {
78   "rtspst", GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_TLS}, {
79   "rtspsh",
80         GST_RTSP_LOWER_TRANS_HTTP | GST_RTSP_LOWER_TRANS_TCP |
81         GST_RTSP_LOWER_TRANS_TLS}
82 };
83 
84 /* format is rtsp[u]://[user:passwd@]host[:port]/abspath[?query] where host
85  * is a host name, an IPv4 dotted decimal address ("aaa.bbb.ccc.ddd") or an
86  * [IPv6] address ("[aabb:ccdd:eeff:gghh::sstt]" note the brackets around the
87  * address to allow the distinction between ':' as an IPv6 hexgroup separator
88  * and as a host/port separator) */
89 
90 /**
91  * gst_rtsp_url_parse:
92  * @urlstr: the url string to parse
93  * @url: (out): location to hold the result.
94  *
95  * Parse the RTSP @urlstr into a newly allocated #GstRTSPUrl. Free after usage
96  * with gst_rtsp_url_free().
97  *
98  * Returns: a #GstRTSPResult.
99  */
100 GstRTSPResult
gst_rtsp_url_parse(const gchar * urlstr,GstRTSPUrl ** url)101 gst_rtsp_url_parse (const gchar * urlstr, GstRTSPUrl ** url)
102 {
103   GstRTSPUrl *res;
104   gchar *p, *delim, *at, *col;
105   gchar *host_end = NULL;
106   guint i;
107 
108   g_return_val_if_fail (urlstr != NULL, GST_RTSP_EINVAL);
109   g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
110 
111   res = g_new0 (GstRTSPUrl, 1);
112 
113   p = (gchar *) urlstr;
114 
115   col = strstr (p, "://");
116   if (col == NULL)
117     goto invalid;
118 
119   for (i = 0; i < G_N_ELEMENTS (rtsp_schemes_map); i++) {
120     if (g_ascii_strncasecmp (rtsp_schemes_map[i].scheme, p, col - p) == 0) {
121       res->transports = rtsp_schemes_map[i].transports;
122       p = col + 3;
123       break;
124     }
125   }
126 
127   if (res->transports == GST_RTSP_LOWER_TRANS_UNKNOWN)
128     goto invalid;
129 
130   delim = strpbrk (p, "/?");
131   at = strchr (p, '@');
132 
133   if (at && delim && at > delim)
134     at = NULL;
135 
136   if (at) {
137     col = strchr (p, ':');
138 
139     /* must have a ':' and it must be before the '@' */
140     if (col == NULL || col > at)
141       goto invalid;
142 
143     res->user = g_uri_unescape_segment (p, col, NULL);
144     col++;
145     res->passwd = g_uri_unescape_segment (col, at, NULL);
146 
147     /* move to host */
148     p = at + 1;
149   }
150 
151   if (*p == '[') {
152     res->family = GST_RTSP_FAM_INET6;
153 
154     /* we have an IPv6 address in the URL, find the ending ] which must be
155      * before any delimiter */
156     host_end = strchr (++p, ']');
157     if (!host_end || (delim && host_end >= delim))
158       goto invalid;
159 
160     /* a port specifier must follow the address immediately */
161     col = host_end[1] == ':' ? host_end + 1 : NULL;
162   } else {
163     res->family = GST_RTSP_FAM_INET;
164 
165     col = strchr (p, ':');
166 
167     /* we have a ':' and a delimiter but the ':' is after the delimiter, it's
168      * not really part of the hostname */
169     if (col && delim && col >= delim)
170       col = NULL;
171 
172     host_end = col ? col : delim;
173   }
174 
175   if (!host_end)
176     res->host = g_strdup (p);
177   else {
178     res->host = g_strndup (p, host_end - p);
179 
180     if (col) {
181       res->port = strtoul (col + 1, NULL, 10);
182     } else {
183       /* no port specified, set to 0. gst_rtsp_url_get_port() will return the
184        * default port */
185       res->port = 0;
186     }
187   }
188   p = delim;
189 
190   if (p && *p == '/') {
191     delim = strchr (p, '?');
192     if (!delim)
193       res->abspath = g_strdup (p);
194     else
195       res->abspath = g_strndup (p, delim - p);
196     p = delim;
197   } else {
198     /* IQinVision IQeye 1080p fails if a path '/' is provided
199      * and RTSP does not mandate that a non-zero-length path
200      * must be used */
201     res->abspath = g_strdup ("");
202   }
203 
204   if (p && *p == '?')
205     res->query = g_strdup (p + 1);
206 
207   *url = res;
208 
209   return GST_RTSP_OK;
210 
211   /* ERRORS */
212 invalid:
213   {
214     gst_rtsp_url_free (res);
215     return GST_RTSP_EINVAL;
216   }
217 }
218 
219 /**
220  * gst_rtsp_url_copy:
221  * @url: a #GstRTSPUrl
222  *
223  * Make a copy of @url.
224  *
225  * Returns: a copy of @url. Free with gst_rtsp_url_free () after usage.
226  */
227 GstRTSPUrl *
gst_rtsp_url_copy(const GstRTSPUrl * url)228 gst_rtsp_url_copy (const GstRTSPUrl * url)
229 {
230   GstRTSPUrl *res;
231 
232   g_return_val_if_fail (url != NULL, NULL);
233 
234   res = g_new0 (GstRTSPUrl, 1);
235 
236   res->transports = url->transports;
237   res->family = url->family;
238   res->user = g_strdup (url->user);
239   res->passwd = g_strdup (url->passwd);
240   res->host = g_strdup (url->host);
241   res->port = url->port;
242   res->abspath = g_strdup (url->abspath);
243   res->query = g_strdup (url->query);
244 
245   return res;
246 }
247 
248 /**
249  * gst_rtsp_url_free:
250  * @url: a #GstRTSPUrl
251  *
252  * Free the memory used by @url.
253  */
254 void
gst_rtsp_url_free(GstRTSPUrl * url)255 gst_rtsp_url_free (GstRTSPUrl * url)
256 {
257   if (url == NULL)
258     return;
259 
260   g_free (url->user);
261   g_free (url->passwd);
262   g_free (url->host);
263   g_free (url->abspath);
264   g_free (url->query);
265   g_free (url);
266 }
267 
268 /**
269  * gst_rtsp_url_set_port:
270  * @url: a #GstRTSPUrl
271  * @port: the port
272  *
273  * Set the port number in @url to @port.
274  *
275  * Returns: #GST_RTSP_OK.
276  */
277 GstRTSPResult
gst_rtsp_url_set_port(GstRTSPUrl * url,guint16 port)278 gst_rtsp_url_set_port (GstRTSPUrl * url, guint16 port)
279 {
280   g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
281 
282   url->port = port;
283 
284   return GST_RTSP_OK;
285 }
286 
287 /**
288  * gst_rtsp_url_get_port:
289  * @url: a #GstRTSPUrl
290  * @port: (out): location to hold the port
291  *
292  * Get the port number of @url.
293  *
294  * Returns: #GST_RTSP_OK.
295  */
296 GstRTSPResult
gst_rtsp_url_get_port(const GstRTSPUrl * url,guint16 * port)297 gst_rtsp_url_get_port (const GstRTSPUrl * url, guint16 * port)
298 {
299   g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
300   g_return_val_if_fail (port != NULL, GST_RTSP_EINVAL);
301 
302   /* if a port was specified, use that else use the default port. */
303   if (url->port != 0)
304     *port = url->port;
305   else
306     *port = GST_RTSP_DEFAULT_PORT;
307 
308   return GST_RTSP_OK;
309 }
310 
311 /**
312  * gst_rtsp_url_get_request_uri:
313  * @url: a #GstRTSPUrl
314  *
315  * Get a newly allocated string describing the request URI for @url.
316  *
317  * Returns: a string with the request URI. g_free() after usage.
318  */
319 gchar *
gst_rtsp_url_get_request_uri(const GstRTSPUrl * url)320 gst_rtsp_url_get_request_uri (const GstRTSPUrl * url)
321 {
322   gchar *uri;
323   const gchar *pre_host;
324   const gchar *post_host;
325   const gchar *pre_query;
326   const gchar *query;
327 
328   g_return_val_if_fail (url != NULL, NULL);
329 
330   pre_host = url->family == GST_RTSP_FAM_INET6 ? "[" : "";
331   post_host = url->family == GST_RTSP_FAM_INET6 ? "]" : "";
332   pre_query = url->query ? "?" : "";
333   query = url->query ? url->query : "";
334 
335   if (url->port != 0) {
336     uri = g_strdup_printf ("rtsp://%s%s%s:%u%s%s%s", pre_host, url->host,
337         post_host, url->port, url->abspath, pre_query, query);
338   } else {
339     uri = g_strdup_printf ("rtsp://%s%s%s%s%s%s", pre_host, url->host,
340         post_host, url->abspath, pre_query, query);
341   }
342 
343   return uri;
344 }
345 
346 static int
hex_to_int(gchar c)347 hex_to_int (gchar c)
348 {
349   if (c >= '0' && c <= '9')
350     return c - '0';
351   else if (c >= 'a' && c <= 'f')
352     return c - 'a' + 10;
353   else if (c >= 'A' && c <= 'F')
354     return c - 'A' + 10;
355   else
356     return -1;
357 }
358 
359 static void
unescape_path_component(gchar * comp)360 unescape_path_component (gchar * comp)
361 {
362   guint len = strlen (comp);
363   guint i;
364 
365   for (i = 0; i + 2 < len; i++)
366     if (comp[i] == '%') {
367       int a, b;
368 
369       a = hex_to_int (comp[i + 1]);
370       b = hex_to_int (comp[i + 2]);
371 
372       /* The a||b check is to ensure that the byte is not '\0' */
373       if (a >= 0 && b >= 0 && (a || b)) {
374         comp[i] = (gchar) (a * 16 + b);
375         memmove (comp + i + 1, comp + i + 3, len - i - 3);
376         len -= 2;
377         comp[len] = '\0';
378       }
379     }
380 }
381 
382 /**
383  * gst_rtsp_url_decode_path_components:
384  * @url: a #GstRTSPUrl
385  *
386  * Splits the path of @url on '/' boundaries, decoding the resulting components,
387  *
388  * The decoding performed by this routine is "URI decoding", as defined in RFC
389  * 3986, commonly known as percent-decoding. For example, a string "foo\%2fbar"
390  * will decode to "foo/bar" -- the \%2f being replaced by the corresponding byte
391  * with hex value 0x2f. Note that there is no guarantee that the resulting byte
392  * sequence is valid in any given encoding. As a special case, \%00 is not
393  * unescaped to NUL, as that would prematurely terminate the string.
394  *
395  * Also note that since paths usually start with a slash, the first component
396  * will usually be the empty string.
397  *
398  * Returns: (transfer full): %NULL-terminated array of URL components. Free with
399  * g_strfreev() when no longer needed.
400  */
401 gchar **
gst_rtsp_url_decode_path_components(const GstRTSPUrl * url)402 gst_rtsp_url_decode_path_components (const GstRTSPUrl * url)
403 {
404   gchar **ret;
405   guint i;
406 
407   g_return_val_if_fail (url != NULL, NULL);
408   g_return_val_if_fail (url->abspath != NULL, NULL);
409 
410   ret = g_strsplit (url->abspath, "/", -1);
411 
412   for (i = 0; ret[i]; i++)
413     unescape_path_component (ret[i]);
414 
415   return ret;
416 }
417