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.

121 lines
4.1KB

  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 "libavformat/url.h"
  21. #include "libavformat/avformat.h"
  22. static void test_decompose(const char *url)
  23. {
  24. URLComponents uc;
  25. int len, ret;
  26. printf("%s =>\n", url);
  27. ret = ff_url_decompose(&uc, url, NULL);
  28. if (ret < 0) {
  29. printf(" error: %s\n", av_err2str(ret));
  30. return;
  31. }
  32. #define PRINT_COMPONENT(comp) \
  33. len = uc.url_component_end_##comp - uc.comp; \
  34. if (len) printf(" "#comp": %.*s\n", len, uc.comp);
  35. PRINT_COMPONENT(scheme);
  36. PRINT_COMPONENT(authority);
  37. PRINT_COMPONENT(userinfo);
  38. PRINT_COMPONENT(host);
  39. PRINT_COMPONENT(port);
  40. PRINT_COMPONENT(path);
  41. PRINT_COMPONENT(query);
  42. PRINT_COMPONENT(fragment);
  43. printf("\n");
  44. }
  45. static void test(const char *base, const char *rel)
  46. {
  47. char buf[200], buf2[200];
  48. ff_make_absolute_url(buf, sizeof(buf), base, rel);
  49. printf("%50s %-20s => %s\n", base, rel, buf);
  50. if (base) {
  51. /* Test in-buffer replacement */
  52. snprintf(buf2, sizeof(buf2), "%s", base);
  53. ff_make_absolute_url(buf2, sizeof(buf2), buf2, rel);
  54. if (strcmp(buf, buf2)) {
  55. printf("In-place handling of %s + %s failed\n", base, rel);
  56. exit(1);
  57. }
  58. }
  59. }
  60. static void test2(const char *url)
  61. {
  62. char proto[64];
  63. char auth[256];
  64. char host[256];
  65. char path[256];
  66. int port=-1;
  67. av_url_split(proto, sizeof(proto), auth, sizeof(auth), host, sizeof(host), &port, path, sizeof(path), url);
  68. printf("%-60s => %-15s %-15s %-15s %5d %s\n", url, proto, auth, host, port, path);
  69. }
  70. int main(void)
  71. {
  72. printf("Testing ff_url_decompose:\n\n");
  73. test_decompose("http://user:pass@ffmpeg:8080/dir/file?query#fragment");
  74. test_decompose("http://ffmpeg/dir/file");
  75. test_decompose("file:///dev/null");
  76. test_decompose("file:/dev/null");
  77. test_decompose("http://[::1]/dev/null");
  78. test_decompose("http://[::1]:8080/dev/null");
  79. test_decompose("//ffmpeg/dev/null");
  80. printf("Testing ff_make_absolute_url:\n");
  81. test(NULL, "baz");
  82. test("/foo/bar", "baz");
  83. test("/foo/bar", "../baz");
  84. test("/foo/bar", "/baz");
  85. test("/foo/bar", "../../../baz");
  86. test("http://server/foo/", "baz");
  87. test("http://server/foo/bar", "baz");
  88. test("http://server/foo/", "../baz");
  89. test("http://server/foo/bar/123", "../../baz");
  90. test("http://server/foo/bar/123", "/baz");
  91. test("http://server/foo/bar/123", "https://other/url");
  92. test("http://server/foo/bar?param=value/with/slashes", "/baz");
  93. test("http://server/foo/bar?param&otherparam", "?someparam");
  94. test("http://server/foo/bar", "//other/url");
  95. test("http://server/foo/bar", "../../../../../other/url");
  96. test("http://server/foo/bar", "/../../../../../other/url");
  97. test("http://server/foo/bar", "/test/../../../../../other/url");
  98. test("http://server/foo/bar", "/test/../../test/../../../other/url");
  99. printf("\nTesting av_url_split:\n");
  100. test2("/foo/bar");
  101. test2("http://server/foo/");
  102. test2("http://example.com/foo/bar");
  103. test2("http://user:pass@localhost:8080/foo/bar/123");
  104. test2("http://server/foo/bar?param=value/with/slashes");
  105. test2("https://1l-lh.a.net/i/1LIVE_HDS@179577/master.m3u8");
  106. test2("ftp://u:p%2B%2F2@ftp.pbt.com/ExportHD.mpg");
  107. test2("https://key.dns.com?key_id=2&model_id=12345&&access_key=");
  108. test2("http://example.com#tag");
  109. return 0;
  110. }