Back to index

lightning-sunbird  0.9+nobinonly
TestURLParser.cpp
Go to the documentation of this file.
00001 #include "TestCommon.h"
00002 #include <stdio.h>
00003 #include "nsIURLParser.h"
00004 #include "nsCOMPtr.h"
00005 #include "nsIServiceManager.h"
00006 #include "nsNetCID.h"
00007 
00008 static void
00009 print_field(const char *label, char *str, PRInt32 len)
00010 {
00011     char c = str[len];
00012     str[len] = '\0';
00013     printf("[%s=%s]\n", label, str);
00014     str[len] = c;
00015 }
00016 
00017 #define PRINT_FIELD(x) \
00018         print_field(# x, x, x ## Len)
00019 
00020 #define PRINT_SUBFIELD(base, x) \
00021     PR_BEGIN_MACRO \
00022         if (x ## Len != -1) \
00023             print_field(# x, base + x ## Pos, x ## Len); \
00024     PR_END_MACRO
00025 
00026 static void
00027 parse_authority(nsIURLParser *urlParser, char *auth, PRInt32 authLen)
00028 {
00029     PRINT_FIELD(auth);
00030 
00031     PRUint32 usernamePos, passwordPos;
00032     PRInt32 usernameLen, passwordLen;
00033     PRUint32 hostnamePos;
00034     PRInt32 hostnameLen, port;
00035 
00036     urlParser->ParseAuthority(auth, authLen,
00037                               &usernamePos, &usernameLen,
00038                               &passwordPos, &passwordLen,
00039                               &hostnamePos, &hostnameLen,
00040                               &port);
00041 
00042     PRINT_SUBFIELD(auth, username);
00043     PRINT_SUBFIELD(auth, password);
00044     PRINT_SUBFIELD(auth, hostname);
00045     if (port != -1)
00046         printf("[port=%d]\n", port);
00047 }
00048 
00049 static void
00050 parse_file_path(nsIURLParser *urlParser, char *filepath, PRInt32 filepathLen)
00051 {
00052     PRINT_FIELD(filepath);
00053 
00054     PRUint32 dirPos, basePos, extPos;
00055     PRInt32 dirLen, baseLen, extLen;
00056 
00057     urlParser->ParseFilePath(filepath, filepathLen,
00058                              &dirPos, &dirLen,
00059                              &basePos, &baseLen,
00060                              &extPos, &extLen);
00061 
00062     PRINT_SUBFIELD(filepath, dir);
00063     PRINT_SUBFIELD(filepath, base);
00064     PRINT_SUBFIELD(filepath, ext);
00065 }
00066 
00067 static void
00068 parse_path(nsIURLParser *urlParser, char *path, PRInt32 pathLen)
00069 {
00070     PRINT_FIELD(path);
00071 
00072     PRUint32 filePos, paramPos, queryPos, refPos;
00073     PRInt32 fileLen, paramLen, queryLen, refLen;
00074 
00075     urlParser->ParsePath(path, pathLen,
00076                          &filePos, &fileLen,
00077                          &paramPos, &paramLen,
00078                          &queryPos, &queryLen,
00079                          &refPos, &refLen);
00080 
00081     if (fileLen != -1)
00082         parse_file_path(urlParser, path + filePos, fileLen);
00083     PRINT_SUBFIELD(path, param);
00084     PRINT_SUBFIELD(path, query);
00085     PRINT_SUBFIELD(path, ref);
00086 }
00087 
00088 int
00089 main(int argc, char **argv)
00090 {
00091     if (test_common_init(&argc, &argv) != 0)
00092         return -1;
00093 
00094     if (argc < 2) {
00095         printf("usage: TestURLParser [-std|-noauth|-auth] <url>\n");
00096         return -1;
00097     }
00098     nsCOMPtr<nsIURLParser> urlParser;
00099     if (strcmp(argv[1], "-noauth") == 0) {
00100         urlParser = do_GetService(NS_NOAUTHURLPARSER_CONTRACTID);
00101         argv[1] = argv[2];
00102     }
00103     else if (strcmp(argv[1], "-auth") == 0) {
00104         urlParser = do_GetService(NS_AUTHURLPARSER_CONTRACTID);
00105         argv[1] = argv[2];
00106     }
00107     else {
00108         urlParser = do_GetService(NS_STDURLPARSER_CONTRACTID);
00109         if (strcmp(argv[1], "-std") == 0)
00110             argv[1] = argv[2];
00111         else
00112             printf("assuming -std\n");
00113     }
00114     if (urlParser) {
00115         printf("have urlParser @%p\n", urlParser.get());
00116 
00117         char *spec = argv[1];
00118         PRUint32 schemePos, authPos, pathPos;
00119         PRInt32 schemeLen, authLen, pathLen;
00120 
00121         urlParser->ParseURL(spec, -1,
00122                             &schemePos, &schemeLen,
00123                             &authPos, &authLen,
00124                             &pathPos, &pathLen);
00125 
00126         if (schemeLen != -1)
00127             PRINT_SUBFIELD(spec, scheme);
00128         if (authLen != -1)
00129             parse_authority(urlParser, spec + authPos, authLen);
00130         if (pathLen != -1)
00131             parse_path(urlParser, spec + pathPos, pathLen);
00132     }
00133     else
00134         printf("no urlParser\n");
00135     return 0;
00136 }