~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/test/TestURLParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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