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 page-loader test, released Aug 5, 2001.
17 # The Initial Developer of the Original Code is
18 # Netscape Communications Corporation.
19 # Portions created by the Initial Developer are Copyright (C) 2001
20 # the Initial Developer. All Rights Reserved.
23 # John Morrison <jrgm@netscape.com>, original author
24 # Heikki Toivonen <heikki@netscape.com>
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
38 # ***** END LICENSE BLOCK *****
41 use vars
qw($MagicString $ClientJS); # defined at end of file
44 # contains a set of URLs and other meta information about them
48 my $class = ref($proto) || $proto;
50 ClientJS => $ClientJS,
51 MagicString => $MagicString,
58 bless ($self, $class);
65 # Parse a config file in the current directory for info.
66 # All requests to the current cgi-bin path will use the same info;
67 # to set up specialized lists, create a separate cgi-bin subdir
73 my $file = "urllist.txt";
74 open(FILE, "< $file") ||
75 die "Can't open file $file: $!";
81 if (/^HTTPBASE:\s+(.*)$/i) {
82 $self->{HTTPBase} = $1;
83 } elsif (/^FILEBASE:\s+(.*)$/i) {
84 $self->{FileBase} = $1;
87 # each of the remaining lines are:
88 # (1) the subdirectory containing the content for this URL,
89 # (2) the name of the top-level document [optional, default='index.html']
90 # (3) mime type for this document [optional, default is text/html]
91 # (4) a character set for this document [optional, default is none]
94 # www.mozilla.org index.html
95 # www.aol.com default.xml text/xml
96 # www.jp.aol.com index.html text/html Shift_JIS
98 my @ary = split(/\s+/, $_);
99 $ary[1] ||= 'index.html';
100 push @{$self->{PageList}}, { Name => $ary[0],
101 URL => $ary[0] . '/' . $ary[1],
102 MimeType => $ary[2] || "text/html",
103 CharSet => $ary[3] || ''
108 # check that we have enough to go on
109 die "Did not read any URLs" unless scalar(@{$self->{PageList}});
110 die "Did not read a value for the http base" unless $self->{HTTPBase};
111 die "Did not read a value for the file base" unless $self->{FileBase};
113 $self->{Length} = scalar(@{$self->{PageList}});
114 $self->_createHashView();
119 sub _createHashView {
120 # repackages the array, so it can be referenced by name
122 for my $i (0..$self->lastidx) {
123 my $hash = $self->{PageList}[$i];
124 #warn $i, " ", $hash, " ", %$hash;
125 $self->{PageHash}{$hash->{Name}} = {
133 sub filebase { my $self = shift; return $self->{FileBase}; }
134 sub httpbase { my $self = shift; return $self->{HTTPBase}; }
135 sub length { my $self = shift; return $self->{Length}; }
136 sub lastidx { my $self = shift; return $self->{Length} - 1; }
137 sub magicString { my $self = shift; return $self->{MagicString}; }
138 sub clientJS { my $self = shift; return $self->{ClientJS}; }
142 # get the relative url by index or by name
145 if ($arg =~ /^\d+$/) {
146 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{URL} : "";
148 return $self->{PageHash}{$arg}{URL};
154 # get the charset for this URL, by index
157 if ($arg =~ /^\d+$/) {
158 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{CharSet} : "";
160 die "$arg' is not a numeric index";
166 # get the mimetype for this URL, by index
169 if ($arg =~ /^\d+$/) {
170 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{MimeType} : "";
172 die "$arg' is not a numeric index";
180 if ($arg =~ /^\d+$/) {
181 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{Name} : "";
183 #warn "You looked up the name using a name.";
192 if ($arg =~ /^\d+$/) {
193 #warn "You looked up the index using an index.";
196 return $self->{PageHash}{$arg}{Index};
204 die "Bogus index passed to PageData: $idx"
205 unless defined($idx) &&
208 $idx < $self->{Length};
214 # JS to insert in the static HTML pages to trigger client timimg and reloading.
215 # You must escape any '$', '@', '\n' contained in the JS code fragment. Otherwise,
216 # perl will attempt to interpret them, and silently convert " $foo " to " ".
218 # JS globals have been intentionally "uglified" with 'moztest_', to avoid collision
219 # with existing content in the page
221 $MagicString = '<!-- MOZ_INSERT_CONTENT_HOOK -->';
222 $ClientJS =<<"ENDOFJS";
226 function moztest_tokenizeQuery() {
228 var pairs = document.location.search.substring(1).split('&');
229 for (var i=0; i < pairs.length; i++) {
230 var pair = pairs[i].split('=');
231 query[pair[0]] = unescape(pair[1]);
236 function moztest_setLocationHref(href, useReplace) {
237 // false => "Location.href=url", not ".replace(url)"
239 document.location.replace(href);
241 document.location.href = href;
246 function moztest_nextRequest(c_part) {
247 function getValue(arg,def) {
248 return !isNaN(arg) ? parseInt(Number(arg)) : def;
250 var q = moztest_tokenizeQuery();
251 var index = getValue(q['index'], 0);
252 var cycle = getValue(q['cycle'], 0);
253 var maxcyc = getValue(q['maxcyc'], 1);
254 var replace = getValue(q['replace'], 0);
255 var nocache = getValue(q['nocache'], 0);
256 var delay = getValue(q['delay'], 0);
257 var timeout = getValue(q['timeout'], 30000);
258 var c_ts = getValue(q['c_ts'], Number.NaN);
261 var now = (new Date()).getTime();
262 var c_intvl = now - c_ts;
263 var c_ts = now + delay; // adjust for delay time
265 // Now make the request ...
266 g_moztest_Href = document.location.href.split('?')[0] +
267 "?c_part=" + c_part +
268 "&index=" + ++index + // increment the request index
270 "&maxcyc=" + maxcyc +
271 "&replace=" + replace +
272 "&nocache=" + nocache +
274 "&timeout=" + timeout +
275 "&c_intvl=" + c_intvl +
276 "&s_ts=" + g_moztest_ServerTime +
278 "&content=" + g_moztest_Content;
279 window.setTimeout("moztest_setLocationHref(g_moztest_Href,false);", delay);
283 function moztest_onDocumentLoad() {
284 var loadTime = (new Date()).getTime() - g_moztest_Start;
285 window.clearTimeout(g_moztest_safetyTimer); // the onload has fired, clear the safety
286 moztest_nextRequest(loadTime);
289 function moztest_safetyValve() {
290 moztest_nextRequest(Number.NaN); // if the onload never fires
293 // normal processing is to calculate load time and fetch another URL
294 window.onload = moztest_onDocumentLoad;
300 1; # return true from module