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

« back to all changes in this revision

Viewing changes to mozilla/netwerk/test/TestHttp.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 "nsNetUtil.h"
 
2
#include "nsIEventQueueService.h"
 
3
#include "nsIServiceManager.h"
 
4
#include "nsIComponentRegistrar.h"
 
5
#include "nsIInterfaceRequestor.h"
 
6
#include "nsIInterfaceRequestorUtils.h"
 
7
#include "nsIProgressEventSink.h"
 
8
 
 
9
#define RETURN_IF_FAILED(rv, step) \
 
10
    PR_BEGIN_MACRO \
 
11
    if (NS_FAILED(rv)) { \
 
12
        printf(">>> %s failed: rv=%x\n", step, rv); \
 
13
        return rv;\
 
14
    } \
 
15
    PR_END_MACRO
 
16
 
 
17
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
 
18
static nsIEventQueue* gEventQ = nsnull;
 
19
static PRBool gKeepRunning = PR_TRUE;
 
20
 
 
21
//-----------------------------------------------------------------------------
 
22
// nsIStreamListener implementation
 
23
//-----------------------------------------------------------------------------
 
24
 
 
25
class MyListener : public nsIStreamListener
 
26
{
 
27
public:
 
28
    NS_DECL_ISUPPORTS
 
29
    NS_DECL_NSIREQUESTOBSERVER
 
30
    NS_DECL_NSISTREAMLISTENER
 
31
 
 
32
    MyListener() { }
 
33
    virtual ~MyListener() {}
 
34
};
 
35
 
 
36
NS_IMPL_ISUPPORTS2(MyListener,
 
37
                   nsIRequestObserver,
 
38
                   nsIStreamListener)
 
39
 
 
40
NS_IMETHODIMP
 
41
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
 
42
{
 
43
    printf(">>> OnStartRequest\n");
 
44
    return NS_OK;
 
45
}
 
46
 
 
47
NS_IMETHODIMP
 
48
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
 
49
{
 
50
    printf(">>> OnStopRequest status=%x\n", status);
 
51
    gKeepRunning = PR_FALSE;
 
52
    return NS_OK;
 
53
}
 
54
 
 
55
NS_IMETHODIMP
 
56
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
 
57
                            nsIInputStream *stream,
 
58
                            PRUint32 offset, PRUint32 count)
 
59
{
 
60
    printf(">>> OnDataAvailable [count=%u]\n", count);
 
61
 
 
62
    char buf[256];
 
63
    nsresult rv;
 
64
    PRUint32 bytesRead=0;
 
65
 
 
66
    while (count) {
 
67
        PRUint32 amount = PR_MIN(count, sizeof(buf));
 
68
 
 
69
        rv = stream->Read(buf, amount, &bytesRead);
 
70
        if (NS_FAILED(rv)) {
 
71
            printf(">>> stream->Read failed with rv=%x\n", rv);
 
72
            return rv;
 
73
        }
 
74
 
 
75
        fwrite(buf, 1, bytesRead, stdout);
 
76
 
 
77
        count -= bytesRead;
 
78
    }
 
79
    return NS_OK;
 
80
}
 
81
 
 
82
//-----------------------------------------------------------------------------
 
83
// NotificationCallbacks implementation
 
84
//-----------------------------------------------------------------------------
 
85
 
 
86
class MyNotifications : public nsIInterfaceRequestor
 
87
                      , public nsIProgressEventSink
 
88
{
 
89
public:
 
90
    NS_DECL_ISUPPORTS
 
91
    NS_DECL_NSIINTERFACEREQUESTOR
 
92
    NS_DECL_NSIPROGRESSEVENTSINK
 
93
 
 
94
    MyNotifications() { }
 
95
    virtual ~MyNotifications() {}
 
96
};
 
97
 
 
98
NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
 
99
                              nsIInterfaceRequestor,
 
100
                              nsIProgressEventSink)
 
101
 
 
102
NS_IMETHODIMP
 
103
MyNotifications::GetInterface(const nsIID &iid, void **result)
 
104
{
 
105
    return QueryInterface(iid, result);
 
106
}
 
107
 
 
108
NS_IMETHODIMP
 
109
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
 
110
                          nsresult status, const PRUnichar *statusText)
 
111
{
 
112
    printf("status: %x\n", status);
 
113
    return NS_OK;
 
114
}
 
115
 
 
116
NS_IMETHODIMP
 
117
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
 
118
                            PRUint32 progress, PRUint32 progressMax)
 
119
{
 
120
    printf("progress: %u/%u\n", progress, progressMax);
 
121
    return NS_OK;
 
122
}
 
123
 
 
124
//-----------------------------------------------------------------------------
 
125
// main, etc..
 
126
//-----------------------------------------------------------------------------
 
127
 
 
128
 
 
129
int main(int argc, char **argv)
 
130
{
 
131
    nsresult rv;
 
132
 
 
133
    if (argc == 1) {
 
134
        printf("usage: TestHttp <url>\n");
 
135
        return -1;
 
136
    }
 
137
    {
 
138
        nsCOMPtr<nsIServiceManager> servMan;
 
139
        NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
 
140
        nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
 
141
        NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
 
142
        if (registrar)
 
143
            registrar->AutoRegister(nsnull);
 
144
 
 
145
        // Create the Event Queue for this thread...
 
146
        nsCOMPtr<nsIEventQueueService> eqs =
 
147
                 do_GetService(kEventQueueServiceCID, &rv);
 
148
        RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
 
149
 
 
150
        rv = eqs->CreateMonitoredThreadEventQueue();
 
151
        RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
 
152
 
 
153
        rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
 
154
        RETURN_IF_FAILED(rv, "GetThreadEventQueue");
 
155
 
 
156
        nsCOMPtr<nsIURI> uri;
 
157
        nsCOMPtr<nsIChannel> chan;
 
158
        nsCOMPtr<nsIStreamListener> listener = new MyListener();
 
159
        nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
 
160
 
 
161
        rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
 
162
        RETURN_IF_FAILED(rv, "NS_NewURI");
 
163
 
 
164
        rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
 
165
        RETURN_IF_FAILED(rv, "NS_OpenURI");
 
166
 
 
167
        rv = chan->AsyncOpen(listener, nsnull);
 
168
        RETURN_IF_FAILED(rv, "AsyncOpen");
 
169
 
 
170
        while (gKeepRunning)
 
171
            gEventQ->ProcessPendingEvents();
 
172
 
 
173
        printf(">>> done\n");
 
174
    } // this scopes the nsCOMPtrs
 
175
    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
 
176
    rv = NS_ShutdownXPCOM(nsnull);
 
177
    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
 
178
    return 0;
 
179
}