michael@0: #include "TestCommon.h" michael@0: #include michael@0: #include "nsIURLParser.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: static void michael@0: print_field(const char *label, char *str, int32_t len) michael@0: { michael@0: char c = str[len]; michael@0: str[len] = '\0'; michael@0: printf("[%s=%s]\n", label, str); michael@0: str[len] = c; michael@0: } michael@0: michael@0: #define PRINT_FIELD(x) \ michael@0: print_field(# x, x, x ## Len) michael@0: michael@0: #define PRINT_SUBFIELD(base, x) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (x ## Len != -1) \ michael@0: print_field(# x, base + x ## Pos, x ## Len); \ michael@0: PR_END_MACRO michael@0: michael@0: static void michael@0: parse_authority(nsIURLParser *urlParser, char *auth, int32_t authLen) michael@0: { michael@0: PRINT_FIELD(auth); michael@0: michael@0: uint32_t usernamePos, passwordPos; michael@0: int32_t usernameLen, passwordLen; michael@0: uint32_t hostnamePos; michael@0: int32_t hostnameLen, port; michael@0: michael@0: urlParser->ParseAuthority(auth, authLen, michael@0: &usernamePos, &usernameLen, michael@0: &passwordPos, &passwordLen, michael@0: &hostnamePos, &hostnameLen, michael@0: &port); michael@0: michael@0: PRINT_SUBFIELD(auth, username); michael@0: PRINT_SUBFIELD(auth, password); michael@0: PRINT_SUBFIELD(auth, hostname); michael@0: if (port != -1) michael@0: printf("[port=%d]\n", port); michael@0: } michael@0: michael@0: static void michael@0: parse_file_path(nsIURLParser *urlParser, char *filepath, int32_t filepathLen) michael@0: { michael@0: PRINT_FIELD(filepath); michael@0: michael@0: uint32_t dirPos, basePos, extPos; michael@0: int32_t dirLen, baseLen, extLen; michael@0: michael@0: urlParser->ParseFilePath(filepath, filepathLen, michael@0: &dirPos, &dirLen, michael@0: &basePos, &baseLen, michael@0: &extPos, &extLen); michael@0: michael@0: PRINT_SUBFIELD(filepath, dir); michael@0: PRINT_SUBFIELD(filepath, base); michael@0: PRINT_SUBFIELD(filepath, ext); michael@0: } michael@0: michael@0: static void michael@0: parse_path(nsIURLParser *urlParser, char *path, int32_t pathLen) michael@0: { michael@0: PRINT_FIELD(path); michael@0: michael@0: uint32_t filePos, queryPos, refPos; michael@0: int32_t fileLen, queryLen, refLen; michael@0: michael@0: urlParser->ParsePath(path, pathLen, michael@0: &filePos, &fileLen, michael@0: &queryPos, &queryLen, michael@0: &refPos, &refLen); michael@0: michael@0: if (fileLen != -1) michael@0: parse_file_path(urlParser, path + filePos, fileLen); michael@0: PRINT_SUBFIELD(path, query); michael@0: PRINT_SUBFIELD(path, ref); michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: if (argc < 2) { michael@0: printf("usage: TestURLParser [-std|-noauth|-auth] \n"); michael@0: return -1; michael@0: } michael@0: nsCOMPtr urlParser; michael@0: if (strcmp(argv[1], "-noauth") == 0) { michael@0: urlParser = do_GetService(NS_NOAUTHURLPARSER_CONTRACTID); michael@0: argv[1] = argv[2]; michael@0: } michael@0: else if (strcmp(argv[1], "-auth") == 0) { michael@0: urlParser = do_GetService(NS_AUTHURLPARSER_CONTRACTID); michael@0: argv[1] = argv[2]; michael@0: } michael@0: else { michael@0: urlParser = do_GetService(NS_STDURLPARSER_CONTRACTID); michael@0: if (strcmp(argv[1], "-std") == 0) michael@0: argv[1] = argv[2]; michael@0: else michael@0: printf("assuming -std\n"); michael@0: } michael@0: if (urlParser) { michael@0: printf("have urlParser @%p\n", static_cast(urlParser.get())); michael@0: michael@0: char *spec = argv[1]; michael@0: uint32_t schemePos, authPos, pathPos; michael@0: int32_t schemeLen, authLen, pathLen; michael@0: michael@0: urlParser->ParseURL(spec, -1, michael@0: &schemePos, &schemeLen, michael@0: &authPos, &authLen, michael@0: &pathPos, &pathLen); michael@0: michael@0: if (schemeLen != -1) michael@0: PRINT_SUBFIELD(spec, scheme); michael@0: if (authLen != -1) michael@0: parse_authority(urlParser, spec + authPos, authLen); michael@0: if (pathLen != -1) michael@0: parse_path(urlParser, spec + pathPos, pathLen); michael@0: } michael@0: else michael@0: printf("no urlParser\n"); michael@0: return 0; michael@0: }