1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/page-loader/PageData.pm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,266 @@ 1.4 +# 1.5 +# This Source Code Form is subject to the terms of the Mozilla Public 1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.8 +package PageData; 1.9 +use strict; 1.10 +use vars qw($MagicString $ClientJS); # defined at end of file 1.11 + 1.12 +# 1.13 +# contains a set of URLs and other meta information about them 1.14 +# 1.15 +sub new { 1.16 + my $proto = shift; 1.17 + my $class = ref($proto) || $proto; 1.18 + my $self = { 1.19 + ClientJS => $ClientJS, 1.20 + MagicString => $MagicString, 1.21 + PageHash => {}, 1.22 + PageList => [], 1.23 + Length => undef, 1.24 + FileBase => undef, 1.25 + HTTPBase => undef 1.26 + }; 1.27 + bless ($self, $class); 1.28 + $self->_init(); 1.29 + return $self; 1.30 +} 1.31 + 1.32 + 1.33 +# 1.34 +# Parse a config file in the current directory for info. 1.35 +# All requests to the current cgi-bin path will use the same info; 1.36 +# to set up specialized lists, create a separate cgi-bin subdir 1.37 +# 1.38 +sub _init { 1.39 + 1.40 + my $self = shift; 1.41 + 1.42 + my $file = "urllist.txt"; 1.43 + open(FILE, "< $file") || 1.44 + die "Can't open file $file: $!"; 1.45 + 1.46 + while (<FILE>) { 1.47 + next if /^$/; 1.48 + next if /^#|^\s+#/; 1.49 + s/\s+#.*$//; 1.50 + if (/^HTTPBASE:\s+(.*)$/i) { 1.51 + $self->{HTTPBase} = $1; 1.52 + } elsif (/^FILEBASE:\s+(.*)$/i) { 1.53 + $self->{FileBase} = $1; 1.54 + } else { 1.55 + # 1.56 + # each of the remaining lines are: 1.57 + # (1) the subdirectory containing the content for this URL, 1.58 + # (2) the name of the top-level document [optional, default='index.html'] 1.59 + # (3) mime type for this document [optional, default is text/html] 1.60 + # (4) a character set for this document [optional, default is none] 1.61 + # e.g., 1.62 + # home.netscape.com 1.63 + # www.mozilla.org index.html 1.64 + # www.aol.com default.xml text/xml 1.65 + # www.jp.aol.com index.html text/html Shift_JIS 1.66 + # 1.67 + my @ary = split(/\s+/, $_); 1.68 + $ary[1] ||= 'index.html'; 1.69 + push @{$self->{PageList}}, { Name => $ary[0], 1.70 + URL => $ary[0] . '/' . $ary[1], 1.71 + MimeType => $ary[2] || "text/html", 1.72 + CharSet => $ary[3] || '' 1.73 + }; 1.74 + } 1.75 + } 1.76 + 1.77 + # check that we have enough to go on 1.78 + die "Did not read any URLs" unless scalar(@{$self->{PageList}}); 1.79 + die "Did not read a value for the http base" unless $self->{HTTPBase}; 1.80 + die "Did not read a value for the file base" unless $self->{FileBase}; 1.81 + 1.82 + $self->{Length} = scalar(@{$self->{PageList}}); 1.83 + $self->_createHashView(); 1.84 + 1.85 +} 1.86 + 1.87 + 1.88 +sub _createHashView { 1.89 + # repackages the array, so it can be referenced by name 1.90 + my $self = shift; 1.91 + for my $i (0..$self->lastidx) { 1.92 + my $hash = $self->{PageList}[$i]; 1.93 + #warn $i, " ", $hash, " ", %$hash; 1.94 + $self->{PageHash}{$hash->{Name}} = { 1.95 + Index => $i, 1.96 + URL => $hash->{URL}, 1.97 + }; 1.98 + } 1.99 +} 1.100 + 1.101 + 1.102 +sub filebase { my $self = shift; return $self->{FileBase}; } 1.103 +sub httpbase { my $self = shift; return $self->{HTTPBase}; } 1.104 +sub length { my $self = shift; return $self->{Length}; } 1.105 +sub lastidx { my $self = shift; return $self->{Length} - 1; } 1.106 +sub magicString { my $self = shift; return $self->{MagicString}; } 1.107 +sub clientJS { my $self = shift; return $self->{ClientJS}; } 1.108 + 1.109 + 1.110 +sub url { 1.111 + # get the relative url by index or by name 1.112 + my $self = shift; 1.113 + my $arg = shift; 1.114 + if ($arg =~ /^\d+$/) { 1.115 + return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{URL} : ""; 1.116 + } else { 1.117 + return $self->{PageHash}{$arg}{URL}; 1.118 + } 1.119 +} 1.120 + 1.121 + 1.122 +sub charset { 1.123 + # get the charset for this URL, by index 1.124 + my $self = shift; 1.125 + my $arg = shift; 1.126 + if ($arg =~ /^\d+$/) { 1.127 + return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{CharSet} : ""; 1.128 + } else { 1.129 + die "$arg' is not a numeric index"; 1.130 + } 1.131 +} 1.132 + 1.133 + 1.134 +sub mimetype { 1.135 + # get the mimetype for this URL, by index 1.136 + my $self = shift; 1.137 + my $arg = shift; 1.138 + if ($arg =~ /^\d+$/) { 1.139 + return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{MimeType} : ""; 1.140 + } else { 1.141 + die "$arg' is not a numeric index"; 1.142 + } 1.143 +} 1.144 + 1.145 + 1.146 +sub name { 1.147 + my $self = shift; 1.148 + my $arg = shift; 1.149 + if ($arg =~ /^\d+$/) { 1.150 + return $self->_checkIndex($arg) ? $self->{PageList}[$arg]{Name} : ""; 1.151 + } else { 1.152 + #warn "You looked up the name using a name."; 1.153 + return $arg; 1.154 + } 1.155 +} 1.156 + 1.157 + 1.158 +sub index { 1.159 + my $self = shift; 1.160 + my $arg = shift; 1.161 + if ($arg =~ /^\d+$/) { 1.162 + #warn "You looked up the index using an index."; 1.163 + return $arg; 1.164 + } else { 1.165 + return $self->{PageHash}{$arg}{Index}; 1.166 + } 1.167 +} 1.168 + 1.169 + 1.170 +sub _checkIndex { 1.171 + my $self = shift; 1.172 + my $idx = shift; 1.173 + die "Bogus index passed to PageData: $idx" 1.174 + unless defined($idx) && 1.175 + $idx =~ /^\d+$/ && 1.176 + $idx >= 0 && 1.177 + $idx < $self->{Length}; 1.178 + return 1; 1.179 +} 1.180 + 1.181 + 1.182 +# 1.183 +# JS to insert in the static HTML pages to trigger client timimg and reloading. 1.184 +# You must escape any '$', '@', '\n' contained in the JS code fragment. Otherwise, 1.185 +# perl will attempt to interpret them, and silently convert " $foo " to " ". 1.186 +# 1.187 +# JS globals have been intentionally "uglified" with 'moztest_', to avoid collision 1.188 +# with existing content in the page 1.189 +# 1.190 +$MagicString = '<!-- MOZ_INSERT_CONTENT_HOOK -->'; 1.191 +$ClientJS =<<"ENDOFJS"; 1.192 + 1.193 +//<![CDATA[ 1.194 + 1.195 +function moztest_tokenizeQuery() { 1.196 + var query = {}; 1.197 + var pairs = document.location.search.substring(1).split('&'); 1.198 + for (var i=0; i < pairs.length; i++) { 1.199 + var pair = pairs[i].split('='); 1.200 + query[pair[0]] = unescape(pair[1]); 1.201 + } 1.202 + return query; 1.203 +} 1.204 + 1.205 +function moztest_setLocationHref(href, useReplace) { 1.206 + // false => "Location.href=url", not ".replace(url)" 1.207 + if (useReplace) { 1.208 + document.location.replace(href); 1.209 + } else { 1.210 + document.location.href = href; 1.211 + } 1.212 +} 1.213 + 1.214 +var g_moztest_Href; 1.215 +function moztest_nextRequest(c_part) { 1.216 + function getValue(arg,def) { 1.217 + return !isNaN(arg) ? parseInt(Number(arg)) : def; 1.218 + } 1.219 + var q = moztest_tokenizeQuery(); 1.220 + var index = getValue(q['index'], 0); 1.221 + var cycle = getValue(q['cycle'], 0); 1.222 + var maxcyc = getValue(q['maxcyc'], 1); 1.223 + var replace = getValue(q['replace'], 0); 1.224 + var nocache = getValue(q['nocache'], 0); 1.225 + var delay = getValue(q['delay'], 0); 1.226 + var timeout = getValue(q['timeout'], 30000); 1.227 + var c_ts = getValue(q['c_ts'], Number.NaN); 1.228 + 1.229 + // check for times 1.230 + var now = (new Date()).getTime(); 1.231 + var c_intvl = now - c_ts; 1.232 + var c_ts = now + delay; // adjust for delay time 1.233 + 1.234 + // Now make the request ... 1.235 + g_moztest_Href = document.location.href.split('?')[0] + 1.236 + "?c_part=" + c_part + 1.237 + "&index=" + ++index + // increment the request index 1.238 + "&id=" + q['id'] + 1.239 + "&maxcyc=" + maxcyc + 1.240 + "&replace=" + replace + 1.241 + "&nocache=" + nocache + 1.242 + "&delay=" + delay + 1.243 + "&timeout=" + timeout + 1.244 + "&c_intvl=" + c_intvl + 1.245 + "&s_ts=" + g_moztest_ServerTime + 1.246 + "&c_ts=" + c_ts + 1.247 + "&content=" + g_moztest_Content; 1.248 + window.setTimeout("moztest_setLocationHref(g_moztest_Href,false);", delay); 1.249 + return true; 1.250 +} 1.251 + 1.252 +function moztest_onDocumentLoad() { 1.253 + var loadTime = (new Date()).getTime() - g_moztest_Start; 1.254 + window.clearTimeout(g_moztest_safetyTimer); // the onload has fired, clear the safety 1.255 + moztest_nextRequest(loadTime); 1.256 +} 1.257 + 1.258 +function moztest_safetyValve() { 1.259 + moztest_nextRequest(Number.NaN); // if the onload never fires 1.260 +} 1.261 + 1.262 +// normal processing is to calculate load time and fetch another URL 1.263 +window.onload = moztest_onDocumentLoad; 1.264 + 1.265 +//]]> 1.266 + 1.267 +ENDOFJS 1.268 + 1.269 +1; # return true from module