Back out changeset fecc8ed9e813.
[mozilla-central.git] / tools / page-loader / PageData.pm
blob8659e3062c219151a536124d3f8b541756855f6b
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 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.
22 # Contributor(s):
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 *****
39 package PageData;
40 use strict;
41 use vars qw($MagicString $ClientJS); # defined at end of file
44 # contains a set of URLs and other meta information about them
46 sub new {
47 my $proto = shift;
48 my $class = ref($proto) || $proto;
49 my $self = {
50 ClientJS => $ClientJS,
51 MagicString => $MagicString,
52 PageHash => {},
53 PageList => [],
54 Length => undef,
55 FileBase => undef,
56 HTTPBase => undef
58 bless ($self, $class);
59 $self->_init();
60 return $self;
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
69 sub _init {
71 my $self = shift;
73 my $file = "urllist.txt";
74 open(FILE, "< $file") ||
75 die "Can't open file $file: $!";
77 while (<FILE>) {
78 next if /^$/;
79 next if /^#|^\s+#/;
80 s/\s+#.*$//;
81 if (/^HTTPBASE:\s+(.*)$/i) {
82 $self->{HTTPBase} = $1;
83 } elsif (/^FILEBASE:\s+(.*)$/i) {
84 $self->{FileBase} = $1;
85 } else {
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]
92 # e.g.,
93 # home.netscape.com
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
121 my $self = shift;
122 for my $i (0..$self->lastidx) {
123 my $hash = $self->{PageList}[$i];
124 #warn $i, " ", $hash, " ", %$hash;
125 $self->{PageHash}{$hash->{Name}} = {
126 Index => $i,
127 URL => $hash->{URL},
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}; }
141 sub url {
142 # get the relative url by index or by name
143 my $self = shift;
144 my $arg = shift;
145 if ($arg =~ /^\d+$/) {
146 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{URL} : "";
147 } else {
148 return $self->{PageHash}{$arg}{URL};
153 sub charset {
154 # get the charset for this URL, by index
155 my $self = shift;
156 my $arg = shift;
157 if ($arg =~ /^\d+$/) {
158 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{CharSet} : "";
159 } else {
160 die "$arg' is not a numeric index";
165 sub mimetype {
166 # get the mimetype for this URL, by index
167 my $self = shift;
168 my $arg = shift;
169 if ($arg =~ /^\d+$/) {
170 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{MimeType} : "";
171 } else {
172 die "$arg' is not a numeric index";
177 sub name {
178 my $self = shift;
179 my $arg = shift;
180 if ($arg =~ /^\d+$/) {
181 return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{Name} : "";
182 } else {
183 #warn "You looked up the name using a name.";
184 return $arg;
189 sub index {
190 my $self = shift;
191 my $arg = shift;
192 if ($arg =~ /^\d+$/) {
193 #warn "You looked up the index using an index.";
194 return $arg;
195 } else {
196 return $self->{PageHash}{$arg}{Index};
201 sub _checkIndex {
202 my $self = shift;
203 my $idx = shift;
204 die "Bogus index passed to PageData: $idx"
205 unless defined($idx) &&
206 $idx =~ /^\d+$/ &&
207 $idx >= 0 &&
208 $idx < $self->{Length};
209 return 1;
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";
224 //<![CDATA[
226 function moztest_tokenizeQuery() {
227 var query = {};
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]);
233 return query;
236 function moztest_setLocationHref(href, useReplace) {
237 // false => "Location.href=url", not ".replace(url)"
238 if (useReplace) {
239 document.location.replace(href);
240 } else {
241 document.location.href = href;
245 var g_moztest_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);
260 // check for times
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
269 "&id=" + q['id'] +
270 "&maxcyc=" + maxcyc +
271 "&replace=" + replace +
272 "&nocache=" + nocache +
273 "&delay=" + delay +
274 "&timeout=" + timeout +
275 "&c_intvl=" + c_intvl +
276 "&s_ts=" + g_moztest_ServerTime +
277 "&c_ts=" + c_ts +
278 "&content=" + g_moztest_Content;
279 window.setTimeout("moztest_setLocationHref(g_moztest_Href,false);", delay);
280 return true;
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;
296 //]]>
298 ENDOFJS
300 1; # return true from module