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
323 return gst_rtsp_url_get_request_uri_with_control (url, NULL);
324 }
325
326 /**
327 * gst_rtsp_url_get_request_uri_with_control:
328 * @url: a #GstRTSPUrl
329 * @control_path: an RTSP aggregate control path
330 *
331 * Get a newly allocated string describing the request URI for @url
332 * combined with the control path for @control_path
333 *
334 * Returns: a string with the request URI combined with the control path.
335 * g_free() after usage.
336 *
337 * Since: 1.18
338 */
339 gchar *
gst_rtsp_url_get_request_uri_with_control(const GstRTSPUrl * url,const gchar * control_path)340 gst_rtsp_url_get_request_uri_with_control (const GstRTSPUrl * url,
341 const gchar * control_path)
342 {
343
344 gchar *uri;
345 const gchar *pre_host;
346 const gchar *post_host;
347 const gchar *pre_query;
348 const gchar *query;
349 gboolean has_slash;
350 const gchar *slash;
351 const gchar *actual_control_path = NULL;
352
353 g_return_val_if_fail (url != NULL, NULL);
354
355 has_slash = g_str_has_suffix (url->abspath, "/");
356
357 if (control_path && strlen (control_path) > 0) {
358 gboolean control_has_slash;
359
360 /* treat wild card as empty control path */
361 if (g_strcmp0 (control_path, "*") == 0)
362 control_path = "";
363 control_has_slash = g_str_has_prefix (control_path, "/");
364 actual_control_path = control_path;
365 if (has_slash && control_has_slash) {
366 if (strlen (control_path) == 1) {
367 actual_control_path = NULL;
368 } else {
369 actual_control_path = control_path + 1;
370 }
371 } else {
372 has_slash = has_slash || control_has_slash;
373 }
374 }
375 slash = (!has_slash && (actual_control_path != NULL)) ? "/" : "";
376 if (!actual_control_path)
377 actual_control_path = "";
378
379 pre_host = url->family == GST_RTSP_FAM_INET6 ? "[" : "";
380 post_host = url->family == GST_RTSP_FAM_INET6 ? "]" : "";
381 pre_query = url->query ? "?" : "";
382 query = url->query ? url->query : "";
383
384 if (url->port != 0) {
385 uri =
386 g_strdup_printf ("rtsp://%s%s%s:%u%s%s%s%s%s", pre_host,
387 url->host, post_host, url->port, url->abspath,
388 slash, actual_control_path, pre_query, query);
389 } else {
390 uri =
391 g_strdup_printf ("rtsp://%s%s%s%s%s%s%s%s", pre_host, url->host,
392 post_host, url->abspath, slash, actual_control_path, pre_query, query);
393 }
394
395 return uri;
396 }
397
398 static int
hex_to_int(gchar c)399 hex_to_int (gchar c)
400 {
401 if (c >= '0' && c <= '9')
402 return c - '0';
403 else if (c >= 'a' && c <= 'f')
404 return c - 'a' + 10;
405 else if (c >= 'A' && c <= 'F')
406 return c - 'A' + 10;
407 else
408 return -1;
409 }
410
411 static void
unescape_path_component(gchar * comp)412 unescape_path_component (gchar * comp)
413 {
414 guint len = strlen (comp);
415 guint i;
416
417 for (i = 0; i + 2 < len; i++)
418 if (comp[i] == '%') {
419 int a, b;
420
421 a = hex_to_int (comp[i + 1]);
422 b = hex_to_int (comp[i + 2]);
423
424 /* The a||b check is to ensure that the byte is not '\0' */
425 if (a >= 0 && b >= 0 && (a || b)) {
426 comp[i] = (gchar) (a * 16 + b);
427 memmove (comp + i + 1, comp + i + 3, len - i - 3);
428 len -= 2;
429 comp[len] = '\0';
430 }
431 }
432 }
433
434 /**
435 * gst_rtsp_url_decode_path_components:
436 * @url: a #GstRTSPUrl
437 *
438 * Splits the path of @url on '/' boundaries, decoding the resulting components,
439 *
440 * The decoding performed by this routine is "URI decoding", as defined in RFC
441 * 3986, commonly known as percent-decoding. For example, a string "foo\%2fbar"
442 * will decode to "foo/bar" -- the \%2f being replaced by the corresponding byte
443 * with hex value 0x2f. Note that there is no guarantee that the resulting byte
444 * sequence is valid in any given encoding. As a special case, \%00 is not
445 * unescaped to NUL, as that would prematurely terminate the string.
446 *
447 * Also note that since paths usually start with a slash, the first component
448 * will usually be the empty string.
449 *
450 * Returns: (transfer full): %NULL-terminated array of URL components. Free with
451 * g_strfreev() when no longer needed.
452 */
453 gchar **
gst_rtsp_url_decode_path_components(const GstRTSPUrl * url)454 gst_rtsp_url_decode_path_components (const GstRTSPUrl * url)
455 {
456 gchar **ret;
457 guint i;
458
459 g_return_val_if_fail (url != NULL, NULL);
460 g_return_val_if_fail (url->abspath != NULL, NULL);
461
462 ret = g_strsplit (url->abspath, "/", -1);
463
464 for (i = 0; ret[i]; i++)
465 unescape_path_component (ret[i]);
466
467 return ret;
468 }
469