michael@0: # michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: package PageData; michael@0: use strict; michael@0: use vars qw($MagicString $ClientJS); # defined at end of file michael@0: michael@0: # michael@0: # contains a set of URLs and other meta information about them michael@0: # michael@0: sub new { michael@0: my $proto = shift; michael@0: my $class = ref($proto) || $proto; michael@0: my $self = { michael@0: ClientJS => $ClientJS, michael@0: MagicString => $MagicString, michael@0: PageHash => {}, michael@0: PageList => [], michael@0: Length => undef, michael@0: FileBase => undef, michael@0: HTTPBase => undef michael@0: }; michael@0: bless ($self, $class); michael@0: $self->_init(); michael@0: return $self; michael@0: } michael@0: michael@0: michael@0: # michael@0: # Parse a config file in the current directory for info. michael@0: # All requests to the current cgi-bin path will use the same info; michael@0: # to set up specialized lists, create a separate cgi-bin subdir michael@0: # michael@0: sub _init { michael@0: michael@0: my $self = shift; michael@0: michael@0: my $file = "urllist.txt"; michael@0: open(FILE, "< $file") || michael@0: die "Can't open file $file: $!"; michael@0: michael@0: while () { michael@0: next if /^$/; michael@0: next if /^#|^\s+#/; michael@0: s/\s+#.*$//; michael@0: if (/^HTTPBASE:\s+(.*)$/i) { michael@0: $self->{HTTPBase} = $1; michael@0: } elsif (/^FILEBASE:\s+(.*)$/i) { michael@0: $self->{FileBase} = $1; michael@0: } else { michael@0: # michael@0: # each of the remaining lines are: michael@0: # (1) the subdirectory containing the content for this URL, michael@0: # (2) the name of the top-level document [optional, default='index.html'] michael@0: # (3) mime type for this document [optional, default is text/html] michael@0: # (4) a character set for this document [optional, default is none] michael@0: # e.g., michael@0: # home.netscape.com michael@0: # www.mozilla.org index.html michael@0: # www.aol.com default.xml text/xml michael@0: # www.jp.aol.com index.html text/html Shift_JIS michael@0: # michael@0: my @ary = split(/\s+/, $_); michael@0: $ary[1] ||= 'index.html'; michael@0: push @{$self->{PageList}}, { Name => $ary[0], michael@0: URL => $ary[0] . '/' . $ary[1], michael@0: MimeType => $ary[2] || "text/html", michael@0: CharSet => $ary[3] || '' michael@0: }; michael@0: } michael@0: } michael@0: michael@0: # check that we have enough to go on michael@0: die "Did not read any URLs" unless scalar(@{$self->{PageList}}); michael@0: die "Did not read a value for the http base" unless $self->{HTTPBase}; michael@0: die "Did not read a value for the file base" unless $self->{FileBase}; michael@0: michael@0: $self->{Length} = scalar(@{$self->{PageList}}); michael@0: $self->_createHashView(); michael@0: michael@0: } michael@0: michael@0: michael@0: sub _createHashView { michael@0: # repackages the array, so it can be referenced by name michael@0: my $self = shift; michael@0: for my $i (0..$self->lastidx) { michael@0: my $hash = $self->{PageList}[$i]; michael@0: #warn $i, " ", $hash, " ", %$hash; michael@0: $self->{PageHash}{$hash->{Name}} = { michael@0: Index => $i, michael@0: URL => $hash->{URL}, michael@0: }; michael@0: } michael@0: } michael@0: michael@0: michael@0: sub filebase { my $self = shift; return $self->{FileBase}; } michael@0: sub httpbase { my $self = shift; return $self->{HTTPBase}; } michael@0: sub length { my $self = shift; return $self->{Length}; } michael@0: sub lastidx { my $self = shift; return $self->{Length} - 1; } michael@0: sub magicString { my $self = shift; return $self->{MagicString}; } michael@0: sub clientJS { my $self = shift; return $self->{ClientJS}; } michael@0: michael@0: michael@0: sub url { michael@0: # get the relative url by index or by name michael@0: my $self = shift; michael@0: my $arg = shift; michael@0: if ($arg =~ /^\d+$/) { michael@0: return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{URL} : ""; michael@0: } else { michael@0: return $self->{PageHash}{$arg}{URL}; michael@0: } michael@0: } michael@0: michael@0: michael@0: sub charset { michael@0: # get the charset for this URL, by index michael@0: my $self = shift; michael@0: my $arg = shift; michael@0: if ($arg =~ /^\d+$/) { michael@0: return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{CharSet} : ""; michael@0: } else { michael@0: die "$arg' is not a numeric index"; michael@0: } michael@0: } michael@0: michael@0: michael@0: sub mimetype { michael@0: # get the mimetype for this URL, by index michael@0: my $self = shift; michael@0: my $arg = shift; michael@0: if ($arg =~ /^\d+$/) { michael@0: return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{MimeType} : ""; michael@0: } else { michael@0: die "$arg' is not a numeric index"; michael@0: } michael@0: } michael@0: michael@0: michael@0: sub name { michael@0: my $self = shift; michael@0: my $arg = shift; michael@0: if ($arg =~ /^\d+$/) { michael@0: return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{Name} : ""; michael@0: } else { michael@0: #warn "You looked up the name using a name."; michael@0: return $arg; michael@0: } michael@0: } michael@0: michael@0: michael@0: sub index { michael@0: my $self = shift; michael@0: my $arg = shift; michael@0: if ($arg =~ /^\d+$/) { michael@0: #warn "You looked up the index using an index."; michael@0: return $arg; michael@0: } else { michael@0: return $self->{PageHash}{$arg}{Index}; michael@0: } michael@0: } michael@0: michael@0: michael@0: sub _checkIndex { michael@0: my $self = shift; michael@0: my $idx = shift; michael@0: die "Bogus index passed to PageData: $idx" michael@0: unless defined($idx) && michael@0: $idx =~ /^\d+$/ && michael@0: $idx >= 0 && michael@0: $idx < $self->{Length}; michael@0: return 1; michael@0: } michael@0: michael@0: michael@0: # michael@0: # JS to insert in the static HTML pages to trigger client timimg and reloading. michael@0: # You must escape any '$', '@', '\n' contained in the JS code fragment. Otherwise, michael@0: # perl will attempt to interpret them, and silently convert " $foo " to " ". michael@0: # michael@0: # JS globals have been intentionally "uglified" with 'moztest_', to avoid collision michael@0: # with existing content in the page michael@0: # michael@0: $MagicString = ''; michael@0: $ClientJS =<<"ENDOFJS"; michael@0: michael@0: // "Location.href=url", not ".replace(url)" michael@0: if (useReplace) { michael@0: document.location.replace(href); michael@0: } else { michael@0: document.location.href = href; michael@0: } michael@0: } michael@0: michael@0: var g_moztest_Href; michael@0: function moztest_nextRequest(c_part) { michael@0: function getValue(arg,def) { michael@0: return !isNaN(arg) ? parseInt(Number(arg)) : def; michael@0: } michael@0: var q = moztest_tokenizeQuery(); michael@0: var index = getValue(q['index'], 0); michael@0: var cycle = getValue(q['cycle'], 0); michael@0: var maxcyc = getValue(q['maxcyc'], 1); michael@0: var replace = getValue(q['replace'], 0); michael@0: var nocache = getValue(q['nocache'], 0); michael@0: var delay = getValue(q['delay'], 0); michael@0: var timeout = getValue(q['timeout'], 30000); michael@0: var c_ts = getValue(q['c_ts'], Number.NaN); michael@0: michael@0: // check for times michael@0: var now = (new Date()).getTime(); michael@0: var c_intvl = now - c_ts; michael@0: var c_ts = now + delay; // adjust for delay time michael@0: michael@0: // Now make the request ... michael@0: g_moztest_Href = document.location.href.split('?')[0] + michael@0: "?c_part=" + c_part + michael@0: "&index=" + ++index + // increment the request index michael@0: "&id=" + q['id'] + michael@0: "&maxcyc=" + maxcyc + michael@0: "&replace=" + replace + michael@0: "&nocache=" + nocache + michael@0: "&delay=" + delay + michael@0: "&timeout=" + timeout + michael@0: "&c_intvl=" + c_intvl + michael@0: "&s_ts=" + g_moztest_ServerTime + michael@0: "&c_ts=" + c_ts + michael@0: "&content=" + g_moztest_Content; michael@0: window.setTimeout("moztest_setLocationHref(g_moztest_Href,false);", delay); michael@0: return true; michael@0: } michael@0: michael@0: function moztest_onDocumentLoad() { michael@0: var loadTime = (new Date()).getTime() - g_moztest_Start; michael@0: window.clearTimeout(g_moztest_safetyTimer); // the onload has fired, clear the safety michael@0: moztest_nextRequest(loadTime); michael@0: } michael@0: michael@0: function moztest_safetyValve() { michael@0: moztest_nextRequest(Number.NaN); // if the onload never fires michael@0: } michael@0: michael@0: // normal processing is to calculate load time and fetch another URL michael@0: window.onload = moztest_onDocumentLoad; michael@0: michael@0: //]]> michael@0: michael@0: ENDOFJS michael@0: michael@0: 1; # return true from module