Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / netwerk / test / TestURLParser.cpp
blob43f126e727d88dc858898094e4de6041fe5706b0
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"
9 static void
10 print_field(const char *label, char *str, int32_t len)
12 char c = str[len];
13 str[len] = '\0';
14 printf("[%s=%s]\n", label, str);
15 str[len] = c;
18 #define PRINT_FIELD(x) \
19 print_field(# x, x, x ## Len)
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
27 static void
28 parse_authority(nsIURLParser *urlParser, char *auth, int32_t authLen)
30 PRINT_FIELD(auth);
32 uint32_t usernamePos, passwordPos;
33 int32_t usernameLen, passwordLen;
34 uint32_t hostnamePos;
35 int32_t hostnameLen, port;
37 urlParser->ParseAuthority(auth, authLen,
38 &usernamePos, &usernameLen,
39 &passwordPos, &passwordLen,
40 &hostnamePos, &hostnameLen,
41 &port);
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);
50 static void
51 parse_file_path(nsIURLParser *urlParser, char *filepath, int32_t filepathLen)
53 PRINT_FIELD(filepath);
55 uint32_t dirPos, basePos, extPos;
56 int32_t dirLen, baseLen, extLen;
58 urlParser->ParseFilePath(filepath, filepathLen,
59 &dirPos, &dirLen,
60 &basePos, &baseLen,
61 &extPos, &extLen);
63 PRINT_SUBFIELD(filepath, dir);
64 PRINT_SUBFIELD(filepath, base);
65 PRINT_SUBFIELD(filepath, ext);
68 static void
69 parse_path(nsIURLParser *urlParser, char *path, int32_t pathLen)
71 PRINT_FIELD(path);
73 uint32_t filePos, queryPos, refPos;
74 int32_t fileLen, queryLen, refLen;
76 urlParser->ParsePath(path, pathLen,
77 &filePos, &fileLen,
78 &queryPos, &queryLen,
79 &refPos, &refLen);
81 if (fileLen != -1)
82 parse_file_path(urlParser, path + filePos, fileLen);
83 PRINT_SUBFIELD(path, query);
84 PRINT_SUBFIELD(path, ref);
87 int
88 main(int argc, char **argv)
90 if (test_common_init(&argc, &argv) != 0)
91 return -1;
93 if (argc < 2) {
94 printf("usage: TestURLParser [-std|-noauth|-auth] <url>\n");
95 return -1;
97 nsCOMPtr<nsIURLParser> urlParser;
98 if (strcmp(argv[1], "-noauth") == 0) {
99 urlParser = do_GetService(NS_NOAUTHURLPARSER_CONTRACTID);
100 argv[1] = argv[2];
102 else if (strcmp(argv[1], "-auth") == 0) {
103 urlParser = do_GetService(NS_AUTHURLPARSER_CONTRACTID);
104 argv[1] = argv[2];
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");
113 if (urlParser) {
114 printf("have urlParser @%p\n", static_cast<void*>(urlParser.get()));
116 char *spec = argv[1];
117 uint32_t schemePos, authPos, pathPos;
118 int32_t schemeLen, authLen, pathLen;
120 urlParser->ParseURL(spec, -1,
121 &schemePos, &schemeLen,
122 &authPos, &authLen,
123 &pathPos, &pathLen);
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);
132 else
133 printf("no urlParser\n");
134 return 0;