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