Revert accidental checkin to NSS.
[wine-gecko.git] / netwerk / test / TestPageLoad.cpp
blob5709e16078d143dea40066acd056da53d9090e12
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
13 * License.
15 * The Original Code is Mozilla Communicator client 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.
22 * Contributor(s):
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 "TestCommon.h"
39 #include "nsNetUtil.h"
40 #include "nsIServiceManager.h"
41 #include "nsIInterfaceRequestor.h"
42 #include "nsIInterfaceRequestorUtils.h"
43 #include "nsIProgressEventSink.h"
44 #include "nsIComponentManager.h"
45 #include "prprf.h"
46 #include "nsXPCOM.h"
47 #include "nsISupportsPrimitives.h"
48 #include "prlong.h"
49 #include "plstr.h"
50 #include "nsSupportsArray.h"
51 #include "nsIComponentRegistrar.h"
53 int getStrLine(const char *src, char *str, int ind, int max);
54 nsresult auxLoad(char *uriBuf);
55 //----------------------------------------------------------------------
58 #define RETURN_IF_FAILED(rv, step) \
59 PR_BEGIN_MACRO \
60 if (NS_FAILED(rv)) { \
61 printf(">>> %s failed: rv=%x\n", step, rv); \
62 return rv;\
63 } \
64 PR_END_MACRO
66 static nsCString globalStream;
67 //static char urlBuf[256];
68 static nsCOMPtr<nsIURI> baseURI;
69 static nsCOMPtr<nsISupportsArray> uriList;
71 //Temp, should remove:
72 static int numStart=0;
73 static int numFound=0;
75 static PRInt32 gKeepRunning = 0;
78 //--------writer fun----------------------
80 static NS_METHOD streamParse (nsIInputStream* in,
81 void* closure,
82 const char* fromRawSegment,
83 PRUint32 toOffset,
84 PRUint32 count,
85 PRUint32 *writeCount) {
87 char parseBuf[2048], loc[2048], lineBuf[2048];
88 char *loc_t, *loc_t2;
89 int i = 0;
90 char *tmp;
92 if(!globalStream.IsEmpty()) {
93 globalStream.Append(fromRawSegment);
94 tmp = ToNewCString(globalStream);
95 //printf("\n>>NOW:\n^^^^^\n%s\n^^^^^^^^^^^^^^", tmp);
96 } else {
97 tmp = (char *)fromRawSegment;
100 while(i < (int)count) {
101 i = getStrLine(tmp, lineBuf, i, count);
102 if(i < 0) {
103 *writeCount = count;
104 return NS_OK;
106 parseBuf[0]='\0';
107 if((loc_t=PL_strcasestr(lineBuf, "img"))!= NULL
108 || (loc_t=PL_strcasestr(lineBuf, "script"))!=NULL) {
109 loc_t2=PL_strcasestr(loc_t, "src");
110 if(loc_t2!=NULL) {
111 loc_t2+=3;
112 strcpy(loc, loc_t2);
113 sscanf(loc, "=\"%[^\"]", parseBuf);
114 if(parseBuf[0]=='\0')
115 sscanf(loc, "=%s", parseBuf);
116 if(parseBuf[0]!='\0'){
117 numFound++;
118 auxLoad(parseBuf);
123 /***NEED BETTER CHECK FOR STYLESHEETS
124 if((loc_t=PL_strcasestr(lineBuf, "link"))!= NULL) {
125 loc_t2=PL_strcasestr(loc_t, "href");
126 if(loc_t2!=NULL) {
127 loc_t2+=4;
128 strcpy(loc, loc_t2);
129 //printf("%s\n", loc);
130 sscanf(loc, "=\"%[^\"]", parseBuf);
131 if(parseBuf[0]!='\0'){
132 //printf("%s\n", parseBuf);
133 numFound++;
134 auxLoad(parseBuf);
139 if((loc_t=PL_strcasestr(lineBuf, "background"))!=NULL) {
140 loc_t+=10;
141 strcpy(loc, loc_t);
142 sscanf(loc, "=\"%[^\"]", parseBuf);
143 if(parseBuf[0]!='\0') {
144 numFound++;
145 auxLoad(parseBuf);
148 i++;
151 *writeCount = count;
152 return NS_OK;
155 //-----------------------------------------------------------------------------
156 // nsIStreamListener implementation
157 //-----------------------------------------------------------------------------
159 class MyListener : public nsIStreamListener
161 public:
162 NS_DECL_ISUPPORTS
163 NS_DECL_NSIREQUESTOBSERVER
164 NS_DECL_NSISTREAMLISTENER
166 MyListener() { }
167 virtual ~MyListener() {}
170 NS_IMPL_ISUPPORTS2(MyListener,
171 nsIRequestObserver,
172 nsIStreamListener)
174 NS_IMETHODIMP
175 MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
177 //printf(">>> OnStartRequest\n");
178 numStart++;
179 return NS_OK;
182 NS_IMETHODIMP
183 MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
185 //printf(">>> OnStopRequest status=%x\n", status);
186 if (--gKeepRunning == 0)
187 QuitPumpingEvents();
188 return NS_OK;
191 NS_IMETHODIMP
192 MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
193 nsIInputStream *stream,
194 PRUint32 offset, PRUint32 count)
196 //printf(">>> OnDataAvailable [count=%u]\n", count);
197 nsresult rv = NS_ERROR_FAILURE;
198 PRUint32 bytesRead=0;
199 char buf[1024];
201 if(ctxt == nsnull) {
202 bytesRead=0;
203 rv = stream->ReadSegments(streamParse, &offset, count, &bytesRead);
204 } else {
205 while (count) {
206 PRUint32 amount = PR_MIN(count, sizeof(buf));
207 rv = stream->Read(buf, amount, &bytesRead);
208 count -= bytesRead;
212 if (NS_FAILED(rv)) {
213 printf(">>> stream->Read failed with rv=%x\n", rv);
214 return rv;
217 return NS_OK;
220 //-----------------------------------------------------------------------------
221 // NotificationCallbacks implementation
222 //-----------------------------------------------------------------------------
224 class MyNotifications : public nsIInterfaceRequestor
225 , public nsIProgressEventSink
227 public:
228 NS_DECL_ISUPPORTS
229 NS_DECL_NSIINTERFACEREQUESTOR
230 NS_DECL_NSIPROGRESSEVENTSINK
232 MyNotifications() { }
233 virtual ~MyNotifications() {}
236 NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
237 nsIInterfaceRequestor,
238 nsIProgressEventSink)
240 NS_IMETHODIMP
241 MyNotifications::GetInterface(const nsIID &iid, void **result)
243 return QueryInterface(iid, result);
246 NS_IMETHODIMP
247 MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
248 nsresult status, const PRUnichar *statusText)
250 //printf("status: %x\n", status);
251 return NS_OK;
254 NS_IMETHODIMP
255 MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
256 PRUint64 progress, PRUint64 progressMax)
258 // char buf[100];
259 // PR_snprintf(buf, sizeof(buf), "%llu/%llu\n", progress, progressMax);
260 // printf("%s", buf);
261 return NS_OK;
264 //-----------------------------------------------------------------------------
265 // main, etc..
266 //-----------------------------------------------------------------------------
268 //---------getStrLine Helper function---------------
269 //Finds a newline in src starting at ind. Puts the
270 //line in str (must be big enough). Returns the index
271 //of the newline, or -1 if at end of string. If reaches
272 //end of string ('\0'), then will copy contents to
273 //globalStream.
274 int getStrLine(const char *src, char *str, int ind, int max) {
275 char c = src[ind];
276 int i=0;
277 globalStream.Assign('\0');
278 while(c!='\n' && c!='\0' && i<max) {
279 str[i] = src[ind];
280 i++; ind++;
281 c = src[ind];
283 str[i]='\0';
284 if(i==max || c=='\0') {
285 globalStream.Assign(str);
286 //printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
287 return -1;
289 return ind;
292 //----------AUX LOAD-----------
293 nsresult auxLoad(char *uriBuf)
295 nsresult rv;
297 nsCOMPtr<nsISupportsPRBool> myBool = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
299 nsCOMPtr<nsIURI> uri;
300 nsCOMPtr<nsIChannel> chan;
301 nsCOMPtr<nsIStreamListener> listener = new MyListener();
302 nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
304 printf("Getting: %s", uriBuf);
306 //If relative link
307 if(strncmp(uriBuf, "http:", 5)) {
308 //Relative link
309 rv = NS_NewURI(getter_AddRefs(uri), uriBuf, baseURI);
310 if (NS_FAILED(rv)) return(rv);
311 } else {
312 //Absolute link, no base needed
313 rv = NS_NewURI(getter_AddRefs(uri), uriBuf);
314 if (NS_FAILED(rv)) return(rv);
317 //Compare to see if exists
318 PRUint32 num;
319 uriList->Count(&num);
320 PRBool equal;
321 nsCOMPtr<nsIURI> uriTmp;
322 for(PRUint32 i = 0; i < num; i++) {
323 uriList->GetElementAt(i, getter_AddRefs(uriTmp));
324 uri->Equals(uriTmp, &equal);
325 if(equal) {
326 printf("(duplicate, canceling) %s\n",uriBuf);
327 return NS_OK;
330 printf("\n");
331 uriList->AppendElement(uri);
332 rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
333 RETURN_IF_FAILED(rv, "NS_NewChannel");
335 gKeepRunning++;
336 rv = chan->AsyncOpen(listener, myBool);
337 RETURN_IF_FAILED(rv, "AsyncOpen");
339 return NS_OK;
343 //---------Buffer writer fun---------
346 //---------MAIN-----------
348 int main(int argc, char **argv)
350 if (test_common_init(&argc, &argv) != 0)
351 return -1;
353 nsresult rv;
355 if (argc == 1) {
356 printf("usage: TestPageLoad <url>\n");
357 return -1;
360 nsCOMPtr<nsIServiceManager> servMan;
361 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
362 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
363 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
364 if (registrar)
365 registrar->AutoRegister(nsnull);
367 PRTime start, finish;
369 uriList = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv);
370 RETURN_IF_FAILED(rv, "do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID)");
372 printf("Loading necko ... \n");
373 nsCOMPtr<nsIChannel> chan;
374 nsCOMPtr<nsIStreamListener> listener = new MyListener();
375 nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
377 rv = NS_NewURI(getter_AddRefs(baseURI), argv[1]);
378 RETURN_IF_FAILED(rv, "NS_NewURI");
380 rv = NS_NewChannel(getter_AddRefs(chan), baseURI, nsnull, nsnull, callbacks);
381 RETURN_IF_FAILED(rv, "NS_OpenURI");
382 gKeepRunning++;
384 //TIMER STARTED-----------------------
385 printf("Starting clock ... \n");
386 start = PR_Now();
387 rv = chan->AsyncOpen(listener, nsnull);
388 RETURN_IF_FAILED(rv, "AsyncOpen");
390 PumpEvents();
392 finish = PR_Now();
393 PRUint32 totalTime32;
394 PRUint64 totalTime64;
395 LL_SUB(totalTime64, finish, start);
396 LL_L2UI(totalTime32, totalTime64);
398 printf("\n\n--------------------\nAll done:\nnum found:%d\nnum start:%d\n", numFound, numStart);
400 printf("\n\n>>PageLoadTime>>%u>>\n\n", totalTime32);
401 } // this scopes the nsCOMPtrs
402 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
403 rv = NS_ShutdownXPCOM(nsnull);
404 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
405 return 0;