1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestURLParser.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,135 @@ 1.4 +#include "TestCommon.h" 1.5 +#include <stdio.h> 1.6 +#include "nsIURLParser.h" 1.7 +#include "nsCOMPtr.h" 1.8 +#include "nsIServiceManager.h" 1.9 +#include "nsNetCID.h" 1.10 +#include "nsServiceManagerUtils.h" 1.11 + 1.12 +static void 1.13 +print_field(const char *label, char *str, int32_t len) 1.14 +{ 1.15 + char c = str[len]; 1.16 + str[len] = '\0'; 1.17 + printf("[%s=%s]\n", label, str); 1.18 + str[len] = c; 1.19 +} 1.20 + 1.21 +#define PRINT_FIELD(x) \ 1.22 + print_field(# x, x, x ## Len) 1.23 + 1.24 +#define PRINT_SUBFIELD(base, x) \ 1.25 + PR_BEGIN_MACRO \ 1.26 + if (x ## Len != -1) \ 1.27 + print_field(# x, base + x ## Pos, x ## Len); \ 1.28 + PR_END_MACRO 1.29 + 1.30 +static void 1.31 +parse_authority(nsIURLParser *urlParser, char *auth, int32_t authLen) 1.32 +{ 1.33 + PRINT_FIELD(auth); 1.34 + 1.35 + uint32_t usernamePos, passwordPos; 1.36 + int32_t usernameLen, passwordLen; 1.37 + uint32_t hostnamePos; 1.38 + int32_t hostnameLen, port; 1.39 + 1.40 + urlParser->ParseAuthority(auth, authLen, 1.41 + &usernamePos, &usernameLen, 1.42 + &passwordPos, &passwordLen, 1.43 + &hostnamePos, &hostnameLen, 1.44 + &port); 1.45 + 1.46 + PRINT_SUBFIELD(auth, username); 1.47 + PRINT_SUBFIELD(auth, password); 1.48 + PRINT_SUBFIELD(auth, hostname); 1.49 + if (port != -1) 1.50 + printf("[port=%d]\n", port); 1.51 +} 1.52 + 1.53 +static void 1.54 +parse_file_path(nsIURLParser *urlParser, char *filepath, int32_t filepathLen) 1.55 +{ 1.56 + PRINT_FIELD(filepath); 1.57 + 1.58 + uint32_t dirPos, basePos, extPos; 1.59 + int32_t dirLen, baseLen, extLen; 1.60 + 1.61 + urlParser->ParseFilePath(filepath, filepathLen, 1.62 + &dirPos, &dirLen, 1.63 + &basePos, &baseLen, 1.64 + &extPos, &extLen); 1.65 + 1.66 + PRINT_SUBFIELD(filepath, dir); 1.67 + PRINT_SUBFIELD(filepath, base); 1.68 + PRINT_SUBFIELD(filepath, ext); 1.69 +} 1.70 + 1.71 +static void 1.72 +parse_path(nsIURLParser *urlParser, char *path, int32_t pathLen) 1.73 +{ 1.74 + PRINT_FIELD(path); 1.75 + 1.76 + uint32_t filePos, queryPos, refPos; 1.77 + int32_t fileLen, queryLen, refLen; 1.78 + 1.79 + urlParser->ParsePath(path, pathLen, 1.80 + &filePos, &fileLen, 1.81 + &queryPos, &queryLen, 1.82 + &refPos, &refLen); 1.83 + 1.84 + if (fileLen != -1) 1.85 + parse_file_path(urlParser, path + filePos, fileLen); 1.86 + PRINT_SUBFIELD(path, query); 1.87 + PRINT_SUBFIELD(path, ref); 1.88 +} 1.89 + 1.90 +int 1.91 +main(int argc, char **argv) 1.92 +{ 1.93 + if (test_common_init(&argc, &argv) != 0) 1.94 + return -1; 1.95 + 1.96 + if (argc < 2) { 1.97 + printf("usage: TestURLParser [-std|-noauth|-auth] <url>\n"); 1.98 + return -1; 1.99 + } 1.100 + nsCOMPtr<nsIURLParser> urlParser; 1.101 + if (strcmp(argv[1], "-noauth") == 0) { 1.102 + urlParser = do_GetService(NS_NOAUTHURLPARSER_CONTRACTID); 1.103 + argv[1] = argv[2]; 1.104 + } 1.105 + else if (strcmp(argv[1], "-auth") == 0) { 1.106 + urlParser = do_GetService(NS_AUTHURLPARSER_CONTRACTID); 1.107 + argv[1] = argv[2]; 1.108 + } 1.109 + else { 1.110 + urlParser = do_GetService(NS_STDURLPARSER_CONTRACTID); 1.111 + if (strcmp(argv[1], "-std") == 0) 1.112 + argv[1] = argv[2]; 1.113 + else 1.114 + printf("assuming -std\n"); 1.115 + } 1.116 + if (urlParser) { 1.117 + printf("have urlParser @%p\n", static_cast<void*>(urlParser.get())); 1.118 + 1.119 + char *spec = argv[1]; 1.120 + uint32_t schemePos, authPos, pathPos; 1.121 + int32_t schemeLen, authLen, pathLen; 1.122 + 1.123 + urlParser->ParseURL(spec, -1, 1.124 + &schemePos, &schemeLen, 1.125 + &authPos, &authLen, 1.126 + &pathPos, &pathLen); 1.127 + 1.128 + if (schemeLen != -1) 1.129 + PRINT_SUBFIELD(spec, scheme); 1.130 + if (authLen != -1) 1.131 + parse_authority(urlParser, spec + authPos, authLen); 1.132 + if (pathLen != -1) 1.133 + parse_path(urlParser, spec + pathPos, pathLen); 1.134 + } 1.135 + else 1.136 + printf("no urlParser\n"); 1.137 + return 0; 1.138 +}