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