TestURLParser.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "TestCommon.h"
  2. #include <stdio.h>
  3. #include "nsIURLParser.h"
  4. #include "nsCOMPtr.h"
  5. #include "nsIServiceManager.h"
  6. #include "nsNetCID.h"
  7. #include "nsServiceManagerUtils.h"
  8. static void
  9. print_field(const char *label, char *str, int32_t len)
  10. {
  11. char c = str[len];
  12. str[len] = '\0';
  13. printf("[%s=%s]\n", label, str);
  14. str[len] = c;
  15. }
  16. #define PRINT_FIELD(x) \
  17. print_field(# x, x, x ## Len)
  18. #define PRINT_SUBFIELD(base, x) \
  19. PR_BEGIN_MACRO \
  20. if (x ## Len != -1) \
  21. print_field(# x, base + x ## Pos, x ## Len); \
  22. PR_END_MACRO
  23. static void
  24. parse_authority(nsIURLParser *urlParser, char *auth, int32_t authLen)
  25. {
  26. PRINT_FIELD(auth);
  27. uint32_t usernamePos, passwordPos;
  28. int32_t usernameLen, passwordLen;
  29. uint32_t hostnamePos;
  30. int32_t hostnameLen, port;
  31. urlParser->ParseAuthority(auth, authLen,
  32. &usernamePos, &usernameLen,
  33. &passwordPos, &passwordLen,
  34. &hostnamePos, &hostnameLen,
  35. &port);
  36. PRINT_SUBFIELD(auth, username);
  37. PRINT_SUBFIELD(auth, password);
  38. PRINT_SUBFIELD(auth, hostname);
  39. if (port != -1)
  40. printf("[port=%d]\n", port);
  41. }
  42. static void
  43. parse_file_path(nsIURLParser *urlParser, char *filepath, int32_t filepathLen)
  44. {
  45. PRINT_FIELD(filepath);
  46. uint32_t dirPos, basePos, extPos;
  47. int32_t dirLen, baseLen, extLen;
  48. urlParser->ParseFilePath(filepath, filepathLen,
  49. &dirPos, &dirLen,
  50. &basePos, &baseLen,
  51. &extPos, &extLen);
  52. PRINT_SUBFIELD(filepath, dir);
  53. PRINT_SUBFIELD(filepath, base);
  54. PRINT_SUBFIELD(filepath, ext);
  55. }
  56. static void
  57. parse_path(nsIURLParser *urlParser, char *path, int32_t pathLen)
  58. {
  59. PRINT_FIELD(path);
  60. uint32_t filePos, queryPos, refPos;
  61. int32_t fileLen, queryLen, refLen;
  62. urlParser->ParsePath(path, pathLen,
  63. &filePos, &fileLen,
  64. &queryPos, &queryLen,
  65. &refPos, &refLen);
  66. if (fileLen != -1)
  67. parse_file_path(urlParser, path + filePos, fileLen);
  68. PRINT_SUBFIELD(path, query);
  69. PRINT_SUBFIELD(path, ref);
  70. }
  71. int
  72. main(int argc, char **argv)
  73. {
  74. if (test_common_init(&argc, &argv) != 0)
  75. return -1;
  76. if (argc < 2) {
  77. printf("usage: TestURLParser [-std|-noauth|-auth] <url>\n");
  78. return -1;
  79. }
  80. nsCOMPtr<nsIURLParser> urlParser;
  81. if (strcmp(argv[1], "-noauth") == 0) {
  82. urlParser = do_GetService(NS_NOAUTHURLPARSER_CONTRACTID);
  83. argv[1] = argv[2];
  84. }
  85. else if (strcmp(argv[1], "-auth") == 0) {
  86. urlParser = do_GetService(NS_AUTHURLPARSER_CONTRACTID);
  87. argv[1] = argv[2];
  88. }
  89. else {
  90. urlParser = do_GetService(NS_STDURLPARSER_CONTRACTID);
  91. if (strcmp(argv[1], "-std") == 0)
  92. argv[1] = argv[2];
  93. else
  94. printf("assuming -std\n");
  95. }
  96. if (urlParser) {
  97. printf("have urlParser @%p\n", static_cast<void*>(urlParser.get()));
  98. char *spec = argv[1];
  99. uint32_t schemePos, authPos, pathPos;
  100. int32_t schemeLen, authLen, pathLen;
  101. urlParser->ParseURL(spec, -1,
  102. &schemePos, &schemeLen,
  103. &authPos, &authLen,
  104. &pathPos, &pathLen);
  105. if (schemeLen != -1)
  106. PRINT_SUBFIELD(spec, scheme);
  107. if (authLen != -1)
  108. parse_authority(urlParser, spec + authPos, authLen);
  109. if (pathLen != -1)
  110. parse_path(urlParser, spec + pathPos, pathLen);
  111. }
  112. else
  113. printf("no urlParser\n");
  114. return 0;
  115. }