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