FFmpeg
url.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Martin Storsjo
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "config.h"
22 #include "libavformat/url.h"
23 #include "libavformat/avformat.h"
24 
25 static void test_decompose(const char *url)
26 {
27  URLComponents uc;
28  int len, ret;
29 
30  printf("%s =>\n", url);
31  ret = ff_url_decompose(&uc, url, NULL);
32  if (ret < 0) {
33  printf(" error: %s\n", av_err2str(ret));
34  return;
35  }
36 #define PRINT_COMPONENT(comp) \
37  len = uc.url_component_end_##comp - uc.comp; \
38  if (len) printf(" "#comp": %.*s\n", len, uc.comp);
39  PRINT_COMPONENT(scheme);
40  PRINT_COMPONENT(authority);
41  PRINT_COMPONENT(userinfo);
42  PRINT_COMPONENT(host);
43  PRINT_COMPONENT(port);
44  PRINT_COMPONENT(path);
45  PRINT_COMPONENT(query);
47  printf("\n");
48 }
49 
50 static void test(const char *base, const char *rel)
51 {
52  char buf[200], buf2[200], buf_dos[200], buf_native[200];
53  int ret;
54 
55  ret = ff_make_absolute_url2(buf, sizeof(buf), base, rel, 0);
56  if (ret < 0) {
57  printf("%50s %-20s => error %s\n", base, rel, av_err2str(ret));
58  return;
59  }
60  printf("%50s %-20s => %s\n", base, rel, buf);
61  ret = ff_make_absolute_url2(buf_dos, sizeof(buf_dos), base, rel, 1);
62  if (ret < 0)
63  snprintf(buf_dos, sizeof(buf_dos), "error %s", av_err2str(ret));
64  ret = ff_make_absolute_url(buf_native, sizeof(buf_native), base, rel);
65  if (ret < 0)
66  snprintf(buf_native, sizeof(buf_native), "error %s", av_err2str(ret));
67  if (strcmp(buf, buf_dos))
68  printf("%50s %-20sDOS %s\n", base, rel, buf_dos);
69  if (HAVE_DOS_PATHS && strcmp(buf_dos, buf_native) ||
70  !HAVE_DOS_PATHS && strcmp(buf, buf_native))
71  printf("Native mismatch\n");
72  if (base) {
73  /* Test in-buffer replacement */
74  snprintf(buf2, sizeof(buf2), "%s", base);
75  ff_make_absolute_url2(buf2, sizeof(buf2), buf2, rel, 0);
76  if (strcmp(buf, buf2)) {
77  printf("In-place handling of %s + %s failed\n", base, rel);
78  exit(1);
79  }
80  }
81 }
82 
83 static void test2(const char *url)
84 {
85  char proto[64];
86  char auth[256];
87  char host[256];
88  char path[256];
89  int port=-1;
90 
91  av_url_split(proto, sizeof(proto), auth, sizeof(auth), host, sizeof(host), &port, path, sizeof(path), url);
92  printf("%-60s => %-15s %-15s %-15s %5d %s\n", url, proto, auth, host, port, path);
93 }
94 
95 int main(void)
96 {
97  printf("Testing ff_url_decompose:\n\n");
98  test_decompose("http://user:pass@ffmpeg:8080/dir/file?query#fragment");
99  test_decompose("http://ffmpeg/dir/file");
100  test_decompose("file:///dev/null");
101  test_decompose("file:/dev/null");
102  test_decompose("http://[::1]/dev/null");
103  test_decompose("http://[::1]:8080/dev/null");
104  test_decompose("//ffmpeg/dev/null");
105  test_decompose("test?url=http://server/path");
106  test_decompose("dummy.mp4#t=0:02:00,121.5");
107 
108  printf("Testing ff_make_absolute_url:\n");
109  test(NULL, "baz");
110  test("/foo/bar", "baz");
111  test("/foo/bar", "../baz");
112  test("/foo/bar", "/baz");
113  test("/foo/bar", "../../../baz");
114  test("http://server/foo/", "baz");
115  test("http://server/foo/bar", "baz");
116  test("http://server/foo/", "../baz");
117  test("http://server/foo/bar/123", "../../baz");
118  test("http://server/foo/bar/123", "/baz");
119  test("http://server/foo/bar/123", "https://other/url");
120  test("http://server/foo/bar?param=value/with/slashes", "/baz");
121  test("http://server/foo/bar?param&otherparam", "?someparam");
122  test("http://server/foo/bar", "//other/url");
123  test("http://server/foo/bar", "../../../../../other/url");
124  test("http://server/foo/bar", "/../../../../../other/url");
125  test("http://server/foo/bar", "/test/../../../../../other/url");
126  test("http://server/foo/bar", "/test/../../test/../../../other/url");
127  test("http://server/foo/bar", "file:../baz/qux");
128  test("http://server/foo//bar/", "../../");
129  test("file:../tmp/foo", "../bar/");
130  test("file:../tmp/foo", "file:../bar/");
131  test("http://server/foo/bar", "./");
132  test("http://server/foo/bar", ".dotfile");
133  test("http://server/foo/bar", "..doubledotfile");
134  test("http://server/foo/bar", "double..dotfile");
135  test("http://server/foo/bar", "doubledotfile..");
136  test("file1", "file2");
137  test("dir/file1", "file2");
138  test("dir/file1", "../file2");
139  test("dir\\file1", "file2");
140  test("\\\\srv\\shr\\file", "..\\..\\dummy");
141  test("\\\\srv\\shr\\file", "dummy");
142  test("\\\\srv\\shr\\file", "\\\\srv2\\shr2\\file2");
143  test("\\\\srv\\shr\\file", "d:/file");
144  test("C:\\dir\\a", "..\\file");
145  test("C:\\dir\\a", "\\\\srv\\shr\\file");
146  test("C:\\dir\\a", "d:\\file");
147  test("http://a/b", "\\\\srv\\shr\\file");
148  test("http://a/b", "//srv/shr/file");
149  test("http://a/b", "d:\\file");
150  test("http://a/b", "C:/file");
151 
152  /* From https://tools.ietf.org/html/rfc3986#section-5.4 */
153  test("http://a/b/c/d;p?q", "g:h"); // g:h
154  test("http://a/b/c/d;p?q", "g"); // http://a/b/c/g
155  test("http://a/b/c/d;p?q", "./g"); // http://a/b/c/g
156  test("http://a/b/c/d;p?q", "g/"); // http://a/b/c/g/
157  test("http://a/b/c/d;p?q", "/g"); // http://a/g
158  test("http://a/b/c/d;p?q", "//g"); // http://g
159  test("http://a/b/c/d;p?q", "?y"); // http://a/b/c/d;p?y
160  test("http://a/b/c/d;p?q", "g?y"); // http://a/b/c/g?y
161  test("http://a/b/c/d;p?q", "#s"); // http://a/b/c/d;p?q#s
162  test("http://a/b/c/d;p?q", "g#s"); // http://a/b/c/g#s
163  test("http://a/b/c/d;p?q", "g?y#s"); // http://a/b/c/g?y#s
164  test("http://a/b/c/d;p?q", ";x"); // http://a/b/c/;x
165  test("http://a/b/c/d;p?q", "g;x"); // http://a/b/c/g;x
166  test("http://a/b/c/d;p?q", "g;x?y#s"); // http://a/b/c/g;x?y#s
167  test("http://a/b/c/d;p?q", ""); // http://a/b/c/d;p?q
168  test("http://a/b/c/d;p?q", "."); // http://a/b/c/
169  test("http://a/b/c/d;p?q", "./"); // http://a/b/c/
170  test("http://a/b/c/d;p?q", ".."); // http://a/b/
171  test("http://a/b/c/d;p?q", "../"); // http://a/b/
172  test("http://a/b/c/d;p?q", "../g"); // http://a/b/g
173  test("http://a/b/c/d;p?q", "../.."); // http://a/
174  test("http://a/b/c/d;p?q", "../../"); // http://a/
175  test("http://a/b/c/d;p?q", "../../g"); // http://a/g
176  test("http://a/b/c/d;p?q", "../../../g"); // http://a/g
177  test("http://a/b/c/d;p?q", "../../../../g"); // http://a/g
178  test("http://a/b/c/d;p?q", "/./g"); // http://a/g
179  test("http://a/b/c/d;p?q", "/../g"); // http://a/g
180  test("http://a/b/c/d;p?q", "g."); // http://a/b/c/g.
181  test("http://a/b/c/d;p?q", ".g"); // http://a/b/c/.g
182  test("http://a/b/c/d;p?q", "g.."); // http://a/b/c/g..
183  test("http://a/b/c/d;p?q", "..g"); // http://a/b/c/..g
184  test("http://a/b/c/d;p?q", "./../g"); // http://a/b/g
185  test("http://a/b/c/d;p?q", "./g/."); // http://a/b/c/g/
186  test("http://a/b/c/d;p?q", "g/./h"); // http://a/b/c/g/h
187  test("http://a/b/c/d;p?q", "g/../h"); // http://a/b/c/h
188  test("http://a/b/c/d;p?q", "g;x=1/./y"); // http://a/b/c/g;x=1/y
189  test("http://a/b/c/d;p?q", "g;x=1/../y"); // http://a/b/c/y
190  test("http://a/b/c/d;p?q", "g?y/./x"); // http://a/b/c/g?y/./x
191  test("http://a/b/c/d;p?q", "g?y/../x"); // http://a/b/c/g?y/../x
192  test("http://a/b/c/d;p?q", "g#s/./x"); // http://a/b/c/g#s/./x
193  test("http://a/b/c/d;p?q", "g#s/../x"); // http://a/b/c/g#s/../x
194 
195  printf("\nTesting av_url_split:\n");
196  test2("/foo/bar");
197  test2("http://server/foo/");
198  test2("http://example.com/foo/bar");
199  test2("http://user:pass@localhost:8080/foo/bar/123");
200  test2("http://server/foo/bar?param=value/with/slashes");
201  test2("https://1l-lh.a.net/i/1LIVE_HDS@179577/master.m3u8");
202  test2("ftp://u:p%2B%2F2@ftp.pbt.com/ExportHD.mpg");
203  test2("https://key.dns.com?key_id=2&model_id=12345&&access_key=");
204  test2("http://example.com#tag");
205 
206  return 0;
207 }
ff_make_absolute_url2
int ff_make_absolute_url2(char *buf, int size, const char *base, const char *rel, int handle_dos_paths)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:195
URLComponents
Definition: url.h:369
base
uint8_t base
Definition: vp3data.h:128
fragment
Definition: dashdec.c:36
NULL
#define NULL
Definition: coverity.c:32
test
static void test(const char *base, const char *rel)
Definition: url.c:50
PRINT_COMPONENT
#define PRINT_COMPONENT(comp)
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
main
int main(void)
Definition: url.c:95
printf
printf("static const uint8_t my_array[100] = {\n")
av_url_split
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:358
url.h
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
avformat.h
test_decompose
static void test_decompose(const char *url)
Definition: url.c:25
ff_make_absolute_url
int ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:321
test2
static void test2(const char *url)
Definition: url.c:83
ff_url_decompose
int ff_url_decompose(URLComponents *uc, const char *url, const char *end)
Parse an URL to find the components.
Definition: url.c:91
snprintf
#define snprintf
Definition: snprintf.h:34