2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 use vars
qw($MagicString $ClientJS); # defined at end of file
10 # contains a set of URLs and other meta information about them
14 my $class = ref($proto) || $proto;
16 ClientJS => $ClientJS,
17 MagicString => $MagicString,
24 bless ($self, $class);
31 # Parse a config file in the current directory for info.
32 # All requests to the current cgi-bin path will use the same info;
33 # to set up specialized lists, create a separate cgi-bin subdir
39 my $file = "urllist.txt";
40 open(FILE, "< $file") ||
41 die "Can't open file $file: $!";
47 if (/^HTTPBASE:\s+(.*)$/i) {
48 $self->{HTTPBase} = $1;
49 } elsif (/^FILEBASE:\s+(.*)$/i) {
50 $self->{FileBase} = $1;
53 # each of the remaining lines are:
54 # (1) the subdirectory containing the content for this URL,
55 # (2) the name of the top-level document [optional, default='index.html']
56 # (3) mime type for this document [optional, default is text/html]
57 # (4) a character set for this document [optional, default is none]
60 # www.mozilla.org index.html
61 # www.aol.com default.xml text/xml
62 # www.jp.aol.com index.html text/html Shift_JIS
64 my @ary = split(/\s+/, $_);
65 $ary[1] ||= 'index.html';
66 push @{$self->{PageList}}, { Name => $ary[0],
67 URL => $ary[0] . '/' . $ary[1],
68 MimeType => $ary[2] || "text/html",
69 CharSet => $ary[3] || ''
74 # check that we have enough to go on
75 die "Did not read any URLs" unless scalar(@{$self->{PageList}});
76 die "Did not read a value for the http base" unless $self->{HTTPBase};
77 die "Did not read a value for the file base" unless $self->{FileBase};
79 $self->{Length} = scalar(@{$self->{PageList}});
80 $self->_createHashView();
86 # repackages the array, so it can be referenced by name
88 for my $i (0..$self->lastidx) {
89 my $hash = $self->{PageList}[$i];
90 #warn $i, " ", $hash, " ", %$hash;
91 $self->{PageHash}{$hash->{Name}} = {
99 sub filebase { my $self = shift; return $self->{FileBase}; }
100 sub httpbase { my $self = shift; return $self->{HTTPBase}; }
101 sub length { my $self = shift; return $self->{Length}; }
102 sub lastidx { my $self = shift; return $self->{Length} - 1; }
103 sub magicString { my $self = shift; return $self->{MagicString}; }
104 sub clientJS { my $self = shift; return $self->{ClientJS}; }
108 # get the relative url by index or by name
111 if ($arg =~ /^\d+$/) {
112 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{URL} : "";
114 return $self->{PageHash}{$arg}{URL};
120 # get the charset for this URL, by index
123 if ($arg =~ /^\d+$/) {
124 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{CharSet} : "";
126 die "$arg' is not a numeric index";
132 # get the mimetype for this URL, by index
135 if ($arg =~ /^\d+$/) {
136 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{MimeType} : "";
138 die "$arg' is not a numeric index";
146 if ($arg =~ /^\d+$/) {
147 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{Name} : "";
149 #warn "You looked up the name using a name.";
158 if ($arg =~ /^\d+$/) {
159 #warn "You looked up the index using an index.";
162 return $self->{PageHash}{$arg}{Index};
170 die "Bogus index passed to PageData: $idx"
171 unless defined($idx) &&
174 $idx < $self->{Length};
180 # JS to insert in the static HTML pages to trigger client timimg and reloading.
181 # You must escape any '$', '@', '\n' contained in the JS code fragment. Otherwise,
182 # perl will attempt to interpret them, and silently convert " $foo " to " ".
184 # JS globals have been intentionally "uglified" with 'moztest_', to avoid collision
185 # with existing content in the page
187 $MagicString = '<!-- MOZ_INSERT_CONTENT_HOOK -->';
188 $ClientJS =<<"ENDOFJS";
192 function moztest_tokenizeQuery() {
194 var pairs = document.location.search.substring(1).split('&');
195 for (var i=0; i < pairs.length; i++) {
196 var pair = pairs[i].split('=');
197 query[pair[0]] = unescape(pair[1]);
202 function moztest_setLocationHref(href, useReplace) {
203 // false => "Location.href=url", not ".replace(url)"
205 document.location.replace(href);
207 document.location.href = href;
212 function moztest_nextRequest(c_part) {
213 function getValue(arg,def) {
214 return !isNaN(arg) ? parseInt(Number(arg)) : def;
216 var q = moztest_tokenizeQuery();
217 var index = getValue(q['index'], 0);
218 var cycle = getValue(q['cycle'], 0);
219 var maxcyc = getValue(q['maxcyc'], 1);
220 var replace = getValue(q['replace'], 0);
221 var nocache = getValue(q['nocache'], 0);
222 var delay = getValue(q['delay'], 0);
223 var timeout = getValue(q['timeout'], 30000);
224 var c_ts = getValue(q['c_ts'], Number.NaN);
227 var now = (new Date()).getTime();
228 var c_intvl = now - c_ts;
229 var c_ts = now + delay; // adjust for delay time
231 // Now make the request ...
232 g_moztest_Href = document.location.href.split('?')[0] +
233 "?c_part=" + c_part +
234 "&index=" + ++index + // increment the request index
236 "&maxcyc=" + maxcyc +
237 "&replace=" + replace +
238 "&nocache=" + nocache +
240 "&timeout=" + timeout +
241 "&c_intvl=" + c_intvl +
242 "&s_ts=" + g_moztest_ServerTime +
244 "&content=" + g_moztest_Content;
245 window.setTimeout("moztest_setLocationHref(g_moztest_Href,false);", delay);
249 function moztest_onDocumentLoad() {
250 var loadTime = (new Date()).getTime() - g_moztest_Start;
251 window.clearTimeout(g_moztest_safetyTimer); // the onload has fired, clear the safety
252 moztest_nextRequest(loadTime);
255 function moztest_safetyValve() {
256 moztest_nextRequest(Number.NaN); // if the onload never fires
259 // normal processing is to calculate load time and fetch another URL
260 window.onload = moztest_onDocumentLoad;
266 1; # return true from module