You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
8.7KB

  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. #include "config.h"
  21. #include "libavformat/url.h"
  22. #include "libavformat/avformat.h"
  23. static void test_decompose(const char *url)
  24. {
  25. URLComponents uc;
  26. int len, ret;
  27. printf("%s =>\n", url);
  28. ret = ff_url_decompose(&uc, url, NULL);
  29. if (ret < 0) {
  30. printf(" error: %s\n", av_err2str(ret));
  31. return;
  32. }
  33. #define PRINT_COMPONENT(comp) \
  34. len = uc.url_component_end_##comp - uc.comp; \
  35. if (len) printf(" "#comp": %.*s\n", len, uc.comp);
  36. PRINT_COMPONENT(scheme);
  37. PRINT_COMPONENT(authority);
  38. PRINT_COMPONENT(userinfo);
  39. PRINT_COMPONENT(host);
  40. PRINT_COMPONENT(port);
  41. PRINT_COMPONENT(path);
  42. PRINT_COMPONENT(query);
  43. PRINT_COMPONENT(fragment);
  44. printf("\n");
  45. }
  46. static void test(const char *base, const char *rel)
  47. {
  48. char buf[200], buf2[200], buf_dos[200], buf_native[200];
  49. int ret;
  50. ret = ff_make_absolute_url2(buf, sizeof(buf), base, rel, 0);
  51. if (ret < 0) {
  52. printf("%50s %-20s => error %s\n", base, rel, av_err2str(ret));
  53. return;
  54. }
  55. printf("%50s %-20s => %s\n", base, rel, buf);
  56. ret = ff_make_absolute_url2(buf_dos, sizeof(buf_dos), base, rel, 1);
  57. if (ret < 0)
  58. snprintf(buf_dos, sizeof(buf_dos), "error %s", av_err2str(ret));
  59. ret = ff_make_absolute_url(buf_native, sizeof(buf_native), base, rel);
  60. if (ret < 0)
  61. snprintf(buf_native, sizeof(buf_native), "error %s", av_err2str(ret));
  62. if (strcmp(buf, buf_dos))
  63. printf("%50s %-20sDOS %s\n", base, rel, buf_dos);
  64. if (HAVE_DOS_PATHS && strcmp(buf_dos, buf_native) ||
  65. !HAVE_DOS_PATHS && strcmp(buf, buf_native))
  66. printf("Native mismatch\n");
  67. if (base) {
  68. /* Test in-buffer replacement */
  69. snprintf(buf2, sizeof(buf2), "%s", base);
  70. ff_make_absolute_url2(buf2, sizeof(buf2), buf2, rel, 0);
  71. if (strcmp(buf, buf2)) {
  72. printf("In-place handling of %s + %s failed\n", base, rel);
  73. exit(1);
  74. }
  75. }
  76. }
  77. static void test2(const char *url)
  78. {
  79. char proto[64];
  80. char auth[256];
  81. char host[256];
  82. char path[256];
  83. int port=-1;
  84. av_url_split(proto, sizeof(proto), auth, sizeof(auth), host, sizeof(host), &port, path, sizeof(path), url);
  85. printf("%-60s => %-15s %-15s %-15s %5d %s\n", url, proto, auth, host, port, path);
  86. }
  87. int main(void)
  88. {
  89. printf("Testing ff_url_decompose:\n\n");
  90. test_decompose("http://user:pass@ffmpeg:8080/dir/file?query#fragment");
  91. test_decompose("http://ffmpeg/dir/file");
  92. test_decompose("file:///dev/null");
  93. test_decompose("file:/dev/null");
  94. test_decompose("http://[::1]/dev/null");
  95. test_decompose("http://[::1]:8080/dev/null");
  96. test_decompose("//ffmpeg/dev/null");
  97. test_decompose("test?url=http://server/path");
  98. test_decompose("dummy.mp4#t=0:02:00,121.5");
  99. printf("Testing ff_make_absolute_url:\n");
  100. test(NULL, "baz");
  101. test("/foo/bar", "baz");
  102. test("/foo/bar", "../baz");
  103. test("/foo/bar", "/baz");
  104. test("/foo/bar", "../../../baz");
  105. test("http://server/foo/", "baz");
  106. test("http://server/foo/bar", "baz");
  107. test("http://server/foo/", "../baz");
  108. test("http://server/foo/bar/123", "../../baz");
  109. test("http://server/foo/bar/123", "/baz");
  110. test("http://server/foo/bar/123", "https://other/url");
  111. test("http://server/foo/bar?param=value/with/slashes", "/baz");
  112. test("http://server/foo/bar?param&otherparam", "?someparam");
  113. test("http://server/foo/bar", "//other/url");
  114. test("http://server/foo/bar", "../../../../../other/url");
  115. test("http://server/foo/bar", "/../../../../../other/url");
  116. test("http://server/foo/bar", "/test/../../../../../other/url");
  117. test("http://server/foo/bar", "/test/../../test/../../../other/url");
  118. test("http://server/foo/bar", "file:../baz/qux");
  119. test("http://server/foo//bar/", "../../");
  120. test("file:../tmp/foo", "../bar/");
  121. test("file:../tmp/foo", "file:../bar/");
  122. test("http://server/foo/bar", "./");
  123. test("http://server/foo/bar", ".dotfile");
  124. test("http://server/foo/bar", "..doubledotfile");
  125. test("http://server/foo/bar", "double..dotfile");
  126. test("http://server/foo/bar", "doubledotfile..");
  127. test("file1", "file2");
  128. test("dir/file1", "file2");
  129. test("dir/file1", "../file2");
  130. test("dir\\file1", "file2");
  131. test("\\\\srv\\shr\\file", "..\\..\\dummy");
  132. test("\\\\srv\\shr\\file", "dummy");
  133. test("\\\\srv\\shr\\file", "\\\\srv2\\shr2\\file2");
  134. test("\\\\srv\\shr\\file", "d:/file");
  135. test("C:\\dir\\a", "..\\file");
  136. test("C:\\dir\\a", "\\\\srv\\shr\\file");
  137. test("C:\\dir\\a", "d:\\file");
  138. test("http://a/b", "\\\\srv\\shr\\file");
  139. test("http://a/b", "//srv/shr/file");
  140. test("http://a/b", "d:\\file");
  141. test("http://a/b", "C:/file");
  142. /* From https://tools.ietf.org/html/rfc3986#section-5.4 */
  143. test("http://a/b/c/d;p?q", "g:h"); // g:h
  144. test("http://a/b/c/d;p?q", "g"); // http://a/b/c/g
  145. test("http://a/b/c/d;p?q", "./g"); // http://a/b/c/g
  146. test("http://a/b/c/d;p?q", "g/"); // http://a/b/c/g/
  147. test("http://a/b/c/d;p?q", "/g"); // http://a/g
  148. test("http://a/b/c/d;p?q", "//g"); // http://g
  149. test("http://a/b/c/d;p?q", "?y"); // http://a/b/c/d;p?y
  150. test("http://a/b/c/d;p?q", "g?y"); // http://a/b/c/g?y
  151. test("http://a/b/c/d;p?q", "#s"); // http://a/b/c/d;p?q#s
  152. test("http://a/b/c/d;p?q", "g#s"); // http://a/b/c/g#s
  153. test("http://a/b/c/d;p?q", "g?y#s"); // http://a/b/c/g?y#s
  154. test("http://a/b/c/d;p?q", ";x"); // http://a/b/c/;x
  155. test("http://a/b/c/d;p?q", "g;x"); // http://a/b/c/g;x
  156. test("http://a/b/c/d;p?q", "g;x?y#s"); // http://a/b/c/g;x?y#s
  157. test("http://a/b/c/d;p?q", ""); // http://a/b/c/d;p?q
  158. test("http://a/b/c/d;p?q", "."); // http://a/b/c/
  159. test("http://a/b/c/d;p?q", "./"); // http://a/b/c/
  160. test("http://a/b/c/d;p?q", ".."); // http://a/b/
  161. test("http://a/b/c/d;p?q", "../"); // http://a/b/
  162. test("http://a/b/c/d;p?q", "../g"); // http://a/b/g
  163. test("http://a/b/c/d;p?q", "../.."); // http://a/
  164. test("http://a/b/c/d;p?q", "../../"); // http://a/
  165. test("http://a/b/c/d;p?q", "../../g"); // http://a/g
  166. test("http://a/b/c/d;p?q", "../../../g"); // http://a/g
  167. test("http://a/b/c/d;p?q", "../../../../g"); // http://a/g
  168. test("http://a/b/c/d;p?q", "/./g"); // http://a/g
  169. test("http://a/b/c/d;p?q", "/../g"); // http://a/g
  170. test("http://a/b/c/d;p?q", "g."); // http://a/b/c/g.
  171. test("http://a/b/c/d;p?q", ".g"); // http://a/b/c/.g
  172. test("http://a/b/c/d;p?q", "g.."); // http://a/b/c/g..
  173. test("http://a/b/c/d;p?q", "..g"); // http://a/b/c/..g
  174. test("http://a/b/c/d;p?q", "./../g"); // http://a/b/g
  175. test("http://a/b/c/d;p?q", "./g/."); // http://a/b/c/g/
  176. test("http://a/b/c/d;p?q", "g/./h"); // http://a/b/c/g/h
  177. test("http://a/b/c/d;p?q", "g/../h"); // http://a/b/c/h
  178. test("http://a/b/c/d;p?q", "g;x=1/./y"); // http://a/b/c/g;x=1/y
  179. test("http://a/b/c/d;p?q", "g;x=1/../y"); // http://a/b/c/y
  180. test("http://a/b/c/d;p?q", "g?y/./x"); // http://a/b/c/g?y/./x
  181. test("http://a/b/c/d;p?q", "g?y/../x"); // http://a/b/c/g?y/../x
  182. test("http://a/b/c/d;p?q", "g#s/./x"); // http://a/b/c/g#s/./x
  183. test("http://a/b/c/d;p?q", "g#s/../x"); // http://a/b/c/g#s/../x
  184. printf("\nTesting av_url_split:\n");
  185. test2("/foo/bar");
  186. test2("http://server/foo/");
  187. test2("http://example.com/foo/bar");
  188. test2("http://user:pass@localhost:8080/foo/bar/123");
  189. test2("http://server/foo/bar?param=value/with/slashes");
  190. test2("https://1l-lh.a.net/i/1LIVE_HDS@179577/master.m3u8");
  191. test2("ftp://u:p%2B%2F2@ftp.pbt.com/ExportHD.mpg");
  192. test2("https://key.dns.com?key_id=2&model_id=12345&&access_key=");
  193. test2("http://example.com#tag");
  194. return 0;
  195. }