PageRenderTime 25ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/mozapps/update/test/chrome/update.sjs

http://github.com/zpao/v8monkey
Unknown | 262 lines | 238 code | 24 blank | 0 comment | 0 complexity | 05af4999b0e9e43886f2a2f26e368888 MD5 | raw file
  1/* Any copyright is dedicated to the Public Domain.
  2 * http://creativecommons.org/publicdomain/zero/1.0/
  3 */
  4
  5/**
  6 * Server side http server script for application update tests.
  7 *
  8 * !IMPORTANT - Since xpcshell used by the http server is launched with -v 170
  9 * this file must not use features greater than JavaScript 1.7.
 10 */
 11
 12const AUS_Cc = Components.classes;
 13const AUS_Ci = Components.interfaces;
 14
 15#include ../sharedUpdateXML.js
 16
 17const URL_HOST = "http://example.com/";
 18const URL_PATH = "chrome/toolkit/mozapps/update/test/chrome/";
 19const URL_UPDATE = URL_HOST + URL_PATH + "update.sjs";
 20const SERVICE_URL = URL_HOST + URL_PATH + FILE_SIMPLE_MAR;
 21
 22const SLOW_MAR_DOWNLOAD_INTERVAL = 100;
 23var gTimer;
 24
 25function handleRequest(aRequest, aResponse) {
 26  var params = { };
 27  if (aRequest.queryString)
 28    params = parseQueryString(aRequest.queryString);
 29
 30  var statusCode = params.statusCode ? parseInt(params.statusCode) : 200;
 31  var statusReason = params.statusReason ? params.statusReason : "OK";
 32  aResponse.setStatusLine(aRequest.httpVersion, statusCode, statusReason);
 33  aResponse.setHeader("Cache-Control", "no-cache", false);
 34  
 35  if (params.addonID) {
 36    aResponse.write(getUpdateRDF(params));
 37    return;
 38  }
 39
 40  // When a mar download is started by the update service it can finish
 41  // downloading before the ui has loaded. By specifying a serviceURL for the
 42  // update patch that points to this file and has a slowDownloadMar param the
 43  // mar will be downloaded asynchronously which will allow the ui to load
 44  // before the download completes.
 45  if (params.slowDownloadMar) {
 46    aResponse.processAsync();
 47    aResponse.setHeader("Content-Type", "binary/octet-stream");
 48    aResponse.setHeader("Content-Length", SIZE_SIMPLE_MAR);
 49    var marFile = AUS_Cc["@mozilla.org/file/directory_service;1"].
 50                  getService(AUS_Ci.nsIProperties).
 51                  get("CurWorkD", AUS_Ci.nsILocalFile);
 52    var path = URL_PATH + FILE_SIMPLE_MAR;
 53    var pathParts = path.split("/");
 54    for(var i = 0; i < pathParts.length; ++i)
 55      marFile.append(pathParts[i]);
 56    var contents = readFileBytes(marFile);
 57    gTimer = AUS_Cc["@mozilla.org/timer;1"].
 58             createInstance(AUS_Ci.nsITimer);
 59    gTimer.initWithCallback(function(aTimer) {
 60      aResponse.write(contents);
 61      aResponse.finish();
 62    }, SLOW_MAR_DOWNLOAD_INTERVAL, AUS_Ci.nsITimer.TYPE_ONE_SHOT);
 63    return;
 64  }
 65
 66  if (params.uiURL) {
 67    var remoteType = "";
 68    if (!params.remoteNoTypeAttr &&
 69        (params.uiURL == "BILLBOARD" || params.uiURL == "LICENSE")) {
 70      remoteType = " " + params.uiURL.toLowerCase() + "=\"1\"";
 71    }
 72    aResponse.write("<html><head><meta http-equiv=\"content-type\" content=" +
 73                    "\"text/html; charset=utf-8\"></head><body" +
 74                    remoteType + ">" + params.uiURL +
 75                    "<br><br>this is a test mar that will not affect your " +
 76                    "build.</body></html>");
 77    return;
 78  }
 79
 80  if (params.xmlMalformed) {
 81    aResponse.write("xml error");
 82    return;
 83  }
 84
 85  if (params.noUpdates) {
 86    aResponse.write(getRemoteUpdatesXMLString(""));
 87    return;
 88  }
 89
 90  var hash;
 91  var patches = "";
 92  if (!params.partialPatchOnly) {
 93    hash = SHA512_HASH_SIMPLE_MAR + (params.invalidCompleteHash ? "e" : "");
 94    patches += getRemotePatchString("complete", SERVICE_URL, "SHA512",
 95                                    hash, SIZE_SIMPLE_MAR);
 96  }
 97
 98  if (!params.completePatchOnly) {
 99    hash = SHA512_HASH_SIMPLE_MAR + (params.invalidPartialHash ? "e" : "");
100    patches += getRemotePatchString("partial", SERVICE_URL, "SHA512",
101                                    hash, SIZE_SIMPLE_MAR);
102  }
103
104  var type = params.type ? params.type : "major";
105  var name = params.name ? params.name : "App Update Test";
106  var appVersion = params.appVersion ? params.appVersion : "99.9";
107  var displayVersion = params.displayVersion ? params.displayVersion
108                                             : "version " + appVersion;
109  var platformVersion = params.platformVersion ? params.platformVersion : "99.8";
110  var buildID = params.buildID ? params.buildID : "01234567890123";
111  // XXXrstrong - not specifying a detailsURL will cause a leak due to bug 470244
112//  var detailsURL = params.showDetails ? URL_UPDATE + "?uiURL=DETAILS" : null;
113  var detailsURL = URL_UPDATE + "?uiURL=DETAILS";
114  var billboardURL = params.showBillboard ? URL_UPDATE + "?uiURL=BILLBOARD" : null;
115  if (billboardURL && params.remoteNoTypeAttr)
116    billboardURL += "&amp;remoteNoTypeAttr=1";
117  if (params.billboard404)
118    billboardURL = URL_HOST + URL_PATH + "missing.html";
119  var licenseURL = params.showLicense ? URL_UPDATE + "?uiURL=LICENSE" : null;
120  if (licenseURL && params.remoteNoTypeAttr)
121    licenseURL += "&amp;remoteNoTypeAttr=1";
122  if (params.license404)
123    licenseURL = URL_HOST + URL_PATH + "missing.html";
124  var showPrompt = params.showPrompt ? "true" : null;
125  var showNever = params.showNever ? "true" : null;
126  var showSurvey = params.showSurvey ? "true" : null;
127
128  // For testing the deprecated update xml format
129  if (params.oldFormat) {
130    appVersion = null;
131    displayVersion = null;
132    billboardURL = null;
133    showPrompt = null;
134    showNever = null;
135    showSurvey = null;
136    detailsURL = URL_UPDATE + "?uiURL=BILLBOARD";
137    if (params.remoteNoTypeAttr)
138      detailsURL += "&amp;remoteNoTypeAttr=1";
139    var extensionVersion = params.appVersion ? params.appVersion : "99.9";
140    var version = params.displayVersion ? params.displayVersion
141                                        : "version " + extensionVersion;
142  }
143
144  var updates = getRemoteUpdateString(patches, type, "App Update Test",
145                                      displayVersion, appVersion,
146                                      platformVersion, buildID, detailsURL,
147                                      billboardURL, licenseURL, showPrompt,
148                                      showNever, showSurvey, version,
149                                      extensionVersion);
150
151  aResponse.write(getRemoteUpdatesXMLString(updates));
152}
153
154/**
155 * Helper function to create a JS object representing the url parameters from
156 * the request's queryString.
157 *
158 * @param  aQueryString
159 *         The request's query string.
160 * @return A JS object representing the url parameters from the request's
161 *         queryString.
162 */
163function parseQueryString(aQueryString) {
164  var paramArray = aQueryString.split("&");
165  var regex = /^([^=]+)=(.*)$/;
166  var params = {};
167  for (var i = 0, sz = paramArray.length; i < sz; i++) {
168    var match = regex.exec(paramArray[i]);
169    if (!match)
170      throw "Bad parameter in queryString!  '" + paramArray[i] + "'";
171    params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
172  }
173
174  return params;
175}
176
177/**
178 * Helper function to gets the string representation of the contents of the
179 * add-on's update manifest file.
180 *
181 * @param  aParams
182 *         A JS object representing the url parameters from the request's
183 *         queryString.
184 * @return A string representation of the contents of the add-on's update
185 *         manifest file.
186 */
187function getUpdateRDF(aParams) {
188  var addonVersion;
189  var addonID = aParams.addonID;
190  var addonUpdateType = addonID.split("_")[0];
191  var maxVersion = aParams.platformVersion;
192
193  switch (addonUpdateType) {
194    case "updatecompatibility":
195      // Use "1.0" for the add-on version for the compatibility update case since
196      // the tests create all add-ons with "1.0" for the version.
197      addonVersion = "1.0";
198      break;
199    case "updateversion":
200      // Use "2.0" for the add-on version for the version update case since the
201      // tests create all add-ons with "1.0" for the version.
202      addonVersion = "2.0";
203      break;
204    default:
205      return "<?xml version=\"1.0\"?>\n" +
206             "<RDF:RDF xmlns:RDF=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" " +
207             "         xmlns:em=\"http://www.mozilla.org/2004/em-rdf#\">\n" +
208             "</RDF:RDF>\n";
209  }
210
211  return "<?xml version=\"1.0\"?>\n" +
212         "<RDF:RDF xmlns:RDF=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" " +
213         "         xmlns:em=\"http://www.mozilla.org/2004/em-rdf#\">\n" +
214         "  <RDF:Description about=\"urn:mozilla:extension:" + addonID + "\">\n" +
215         "    <em:updates>\n" +
216         "      <RDF:Seq>\n" +
217         "        <RDF:li resource=\"urn:mozilla:extension:" + addonID + ":" + addonVersion + "\"/>\n" +
218         "      </RDF:Seq>\n" +
219         "    </em:updates>\n" +
220         "  </RDF:Description>\n" +
221         "  <RDF:Description about=\"urn:mozilla:extension:" + addonID + ":" + addonVersion + "\">\n" +
222         "    <em:version>" + addonVersion + "</em:version>\n" +
223         "    <em:targetApplication>\n" +
224         "      <RDF:Description>\n" +
225         "        <em:id>toolkit@mozilla.org</em:id>\n" +
226         "        <em:minVersion>0</em:minVersion>\n" +
227         "        <em:maxVersion>" + maxVersion + "</em:maxVersion>\n" +
228         "        <em:updateLink>" + URL_HOST + URL_PATH + "</em:updateLink>\n" +
229         "        <em:updateHash>sha256:0</em:updateHash>\n" + 
230         "      </RDF:Description>\n" +
231         "    </em:targetApplication>\n" +
232         "  </RDF:Description>\n" +
233         "</RDF:RDF>\n";
234}
235
236/**
237 * Reads the binary contents of a file and returns it as a string.
238 *
239 * @param  aFile
240 *         The file to read from.
241 * @return The contents of the file as a string.
242 */
243function readFileBytes(aFile) {
244  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
245            createInstance(AUS_Ci.nsIFileInputStream);
246  fis.init(aFile, -1, -1, false);
247  var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
248            createInstance(AUS_Ci.nsIBinaryInputStream);
249  bis.setInputStream(fis);
250  var data = [];
251  var count = fis.available();
252  while (count > 0) {
253    var bytes = bis.readByteArray(Math.min(65535, count));
254    data.push(String.fromCharCode.apply(null, bytes));
255    count -= bytes.length;
256    if (bytes.length == 0)
257      throw "Nothing read from input stream!";
258  }
259  data.join('');
260  fis.close();
261  return data.toString();
262}