1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsIResProtocolHandler.h"
39 #include "nsIServiceManager.h"
40 #include "nsIIOService.h"
41 #include "nsIInputStream.h"
42 #include "nsIComponentManager.h"
43 #include "nsIComponentRegistrar.h"
44 #include "nsIStreamListener.h"
45 #include "nsIEventQueueService.h"
50 static NS_DEFINE_CID(kIOServiceCID
, NS_IOSERVICE_CID
);
51 static NS_DEFINE_CID(kEventQueueServiceCID
, NS_EVENTQUEUESERVICE_CID
);
53 ////////////////////////////////////////////////////////////////////////////////
60 nsCOMPtr
<nsIIOService
> serv(do_GetService(kIOServiceCID
, &rv
));
61 if (NS_FAILED(rv
)) return rv
;
63 nsCOMPtr
<nsIProtocolHandler
> ph
;
64 rv
= serv
->GetProtocolHandler("res", getter_AddRefs(ph
));
65 if (NS_FAILED(rv
)) return rv
;
67 nsCOMPtr
<nsIResProtocolHandler
> resPH
= do_QueryInterface(ph
, &rv
);
68 if (NS_FAILED(rv
)) return rv
;
70 rv
= resPH
->AppendSubstitution("foo", "file://y|/");
71 if (NS_FAILED(rv
)) return rv
;
73 rv
= resPH
->AppendSubstitution("foo", "file://y|/mozilla/dist/win32_D.OBJ/bin/");
74 if (NS_FAILED(rv
)) return rv
;
79 ////////////////////////////////////////////////////////////////////////////////
82 TestOpenInputStream(const char* url
)
86 nsCOMPtr
<nsIIOService
> serv(do_GetService(kIOServiceCID
, &rv
));
87 if (NS_FAILED(rv
)) return rv
;
89 nsCOMPtr
<nsIChannel
> channel
;
90 rv
= serv
->NewChannel(url
,
92 getter_AddRefs(channel
));
93 if (NS_FAILED(rv
)) return rv
;
95 nsCOMPtr
<nsIInputStream
> in
;
96 rv
= channel
->Open(getter_AddRefs(in
));
98 fprintf(stdout
, "failed to OpenInputStream for %s\n", url
);
105 rv
= in
->Read(buf
, sizeof(buf
), &amt
);
106 if (NS_FAILED(rv
)) return rv
;
107 if (amt
== 0) break; // eof
111 fputc(*str
++, stdout
);
114 nsCOMPtr
<nsIURI
> uri
;
117 rv
= channel
->GetOriginalURI(getter_AddRefs(uri
));
118 if (NS_FAILED(rv
)) return rv
;
119 rv
= uri
->GetSpec(&str
);
120 if (NS_FAILED(rv
)) return rv
;
121 fprintf(stdout
, "%s resolved to ", str
);
124 rv
= channel
->GetURI(getter_AddRefs(uri
));
125 if (NS_FAILED(rv
)) return rv
;
126 rv
= uri
->GetSpec(&str
);
127 if (NS_FAILED(rv
)) return rv
;
128 fprintf(stdout
, "%s\n", str
);
134 ////////////////////////////////////////////////////////////////////////////////
136 PRBool gDone
= PR_FALSE
;
137 nsIEventQueue
* gEventQ
= nsnull
;
139 class Listener
: public nsIStreamListener
145 virtual ~Listener() {}
147 NS_IMETHOD
OnStartRequest(nsIRequest
*request
, nsISupports
*ctxt
) {
149 nsCOMPtr
<nsIURI
> uri
;
150 nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(request
);
152 rv
= channel
->GetURI(getter_AddRefs(uri
));
153 if (NS_SUCCEEDED(rv
)) {
155 rv
= uri
->GetSpec(&str
);
156 if (NS_SUCCEEDED(rv
)) {
157 fprintf(stdout
, "Starting to load %s\n", str
);
164 NS_IMETHOD
OnStopRequest(nsIRequest
*request
, nsISupports
*ctxt
,
167 nsCOMPtr
<nsIURI
> uri
;
168 nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(request
);
170 rv
= channel
->GetURI(getter_AddRefs(uri
));
171 if (NS_SUCCEEDED(rv
)) {
173 rv
= uri
->GetSpec(&str
);
174 if (NS_SUCCEEDED(rv
)) {
175 fprintf(stdout
, "Ending load %s, status=%x\n", str
, aStatus
);
183 NS_IMETHOD
OnDataAvailable(nsIRequest
*request
, nsISupports
*ctxt
,
184 nsIInputStream
*inStr
,
185 PRUint32 sourceOffset
, PRUint32 count
) {
190 rv
= inStr
->Read(buf
, sizeof(buf
), &amt
);
201 NS_IMPL_ISUPPORTS2(Listener
, nsIStreamListener
, nsIRequestObserver
)
204 TestAsyncRead(const char* url
)
208 nsCOMPtr
<nsIEventQueueService
> eventQService
=
209 do_GetService(kEventQueueServiceCID
, &rv
);
210 if (NS_FAILED(rv
)) return rv
;
212 rv
= eventQService
->GetThreadEventQueue(NS_CURRENT_THREAD
, &gEventQ
);
213 if (NS_FAILED(rv
)) return rv
;
215 nsCOMPtr
<nsIIOService
> serv(do_GetService(kIOServiceCID
, &rv
));
216 if (NS_FAILED(rv
)) return rv
;
218 nsCOMPtr
<nsIChannel
> channel
;
219 rv
= serv
->NewChannel(url
,
221 getter_AddRefs(channel
));
222 if (NS_FAILED(rv
)) return rv
;
224 nsCOMPtr
<nsIStreamListener
> listener
= new Listener();
225 if (listener
== nsnull
)
226 return NS_ERROR_OUT_OF_MEMORY
;
227 rv
= channel
->AsyncOpen(nsnull
, listener
);
228 if (NS_FAILED(rv
)) return rv
;
232 rv
= gEventQ
->GetEvent(&event
);
233 if (NS_FAILED(rv
)) return rv
;
234 rv
= gEventQ
->HandleEvent(event
);
235 if (NS_FAILED(rv
)) return rv
;
242 main(int argc
, char* argv
[])
246 nsCOMPtr
<nsIServiceManager
> servMan
;
247 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
248 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
249 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
251 registrar
->AutoRegister(nsnull
);
253 NS_ASSERTION(NS_SUCCEEDED(rv
), "AutoregisterComponents failed");
256 printf("usage: %s resource://foo/<path-to-resolve>\n", argv
[0]);
261 NS_ASSERTION(NS_SUCCEEDED(rv
), "SetupMapping failed");
262 if (NS_FAILED(rv
)) return rv
;
264 rv
= TestOpenInputStream(argv
[1]);
265 NS_ASSERTION(NS_SUCCEEDED(rv
), "TestOpenInputStream failed");
267 rv
= TestAsyncRead(argv
[1]);
268 NS_ASSERTION(NS_SUCCEEDED(rv
), "TestAsyncRead failed");
269 } // this scopes the nsCOMPtrs
270 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
271 rv
= NS_ShutdownXPCOM(nsnull
);
272 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");
276 ////////////////////////////////////////////////////////////////////////////////