Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | var Cc = Components.classes; |
michael@0 | 2 | var Ci = Components.interfaces; |
michael@0 | 3 | var Cu = Components.utils; |
michael@0 | 4 | |
michael@0 | 5 | // From prio.h |
michael@0 | 6 | const PR_RDWR = 0x04; |
michael@0 | 7 | const PR_CREATE_FILE = 0x08; |
michael@0 | 8 | const PR_TRUNCATE = 0x20; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | var gBasePath = "tests/dom/apps/tests/"; |
michael@0 | 14 | var gMiniManifestTemplate = "file_packaged_app.template.webapp"; |
michael@0 | 15 | var gAppTemplate = "file_packaged_app.template.html"; |
michael@0 | 16 | var gAppName = "appname"; |
michael@0 | 17 | var gDevName = "devname"; |
michael@0 | 18 | var gDevUrl = "http://dev.url"; |
michael@0 | 19 | |
michael@0 | 20 | function handleRequest(request, response) { |
michael@0 | 21 | var query = getQuery(request); |
michael@0 | 22 | |
michael@0 | 23 | response.setHeader("Access-Control-Allow-Origin", "*", false); |
michael@0 | 24 | |
michael@0 | 25 | var packageSize = ("packageSize" in query) ? query.packageSize : 0; |
michael@0 | 26 | var appName = ("appName" in query) ? query.appName : gAppName; |
michael@0 | 27 | var devName = ("devName" in query) ? query.devName : gDevName; |
michael@0 | 28 | var devUrl = ("devUrl" in query) ? query.devUrl : gDevUrl; |
michael@0 | 29 | |
michael@0 | 30 | // If this is a version update, update state, prepare the manifest, |
michael@0 | 31 | // the application package and return. |
michael@0 | 32 | if ("setVersion" in query) { |
michael@0 | 33 | var version = query.setVersion; |
michael@0 | 34 | setState("version", version); |
michael@0 | 35 | var packageVersion = ("dontUpdatePackage" in query) ? version - 1 : version; |
michael@0 | 36 | var packageName = "test_packaged_app_" + packageVersion + ".zip"; |
michael@0 | 37 | |
michael@0 | 38 | setState("packageName", packageName); |
michael@0 | 39 | var packagePath = "/" + gBasePath + "file_packaged_app.sjs?getPackage=" + |
michael@0 | 40 | packageName; |
michael@0 | 41 | setState("packagePath", packagePath); |
michael@0 | 42 | |
michael@0 | 43 | if (version == packageVersion) { |
michael@0 | 44 | // Create the application package. |
michael@0 | 45 | var zipWriter = Cc["@mozilla.org/zipwriter;1"] |
michael@0 | 46 | .createInstance(Ci.nsIZipWriter); |
michael@0 | 47 | var zipFile = FileUtils.getFile("TmpD", [packageName]); |
michael@0 | 48 | zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); |
michael@0 | 49 | |
michael@0 | 50 | // We want to run some tests without the manifest included in the zip. |
michael@0 | 51 | if (version != "0") { |
michael@0 | 52 | var manifestTemplate = gBasePath + gMiniManifestTemplate; |
michael@0 | 53 | var manifest = makeResource(manifestTemplate, version, packagePath, |
michael@0 | 54 | packageSize, appName, devName, devUrl); |
michael@0 | 55 | addZipEntry(zipWriter, manifest, "manifest.webapp"); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | var appTemplate = gBasePath + gAppTemplate; |
michael@0 | 59 | var app = makeResource(appTemplate, version, packagePath, packageSize, |
michael@0 | 60 | appName, devName, devUrl); |
michael@0 | 61 | addZipEntry(zipWriter, app, "index.html"); |
michael@0 | 62 | |
michael@0 | 63 | zipWriter.close(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | response.setHeader("Content-Type", "text/html", false); |
michael@0 | 67 | response.write("OK"); |
michael@0 | 68 | return; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | // Get the version from server state |
michael@0 | 72 | var version = Number(getState("version")); |
michael@0 | 73 | var packageName = String(getState("packageName")); |
michael@0 | 74 | var packagePath = String(getState("packagePath")); |
michael@0 | 75 | |
michael@0 | 76 | var etag = getEtag(request, version); |
michael@0 | 77 | |
michael@0 | 78 | if (etagMatches(request, etag)) { |
michael@0 | 79 | dump("Etags Match. Sending 304\n"); |
michael@0 | 80 | response.setStatusLine(request.httpVersion, "304", "Not modified"); |
michael@0 | 81 | return; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | response.setHeader("Etag", etag, false); |
michael@0 | 85 | |
michael@0 | 86 | // Serve the application package corresponding to the requested app version. |
michael@0 | 87 | if ("getPackage" in query) { |
michael@0 | 88 | var resource = readFile(packageName, true); |
michael@0 | 89 | response.setHeader("Content-Type", |
michael@0 | 90 | "Content-Type: application/java-archive", false); |
michael@0 | 91 | response.write(resource); |
michael@0 | 92 | return; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Serve the mini-manifest corresponding to the requested app version. |
michael@0 | 96 | if ("getManifest" in query) { |
michael@0 | 97 | var template = gBasePath + gMiniManifestTemplate; |
michael@0 | 98 | if (!("noManifestContentType" in query)) { |
michael@0 | 99 | response.setHeader("Content-Type", |
michael@0 | 100 | "application/x-web-app-manifest+json", false); |
michael@0 | 101 | } |
michael@0 | 102 | packagePath = "wrongPackagePath" in query ? "" : packagePath; |
michael@0 | 103 | var manifest = makeResource(template, version, packagePath, packageSize, |
michael@0 | 104 | appName, devName, devUrl); |
michael@0 | 105 | response.write(manifest); |
michael@0 | 106 | return; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | response.setHeader("Content-type", "text-html", false); |
michael@0 | 110 | response.write("KO"); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | function getQuery(request) { |
michael@0 | 114 | var query = {}; |
michael@0 | 115 | request.queryString.split('&').forEach(function (val) { |
michael@0 | 116 | var [name, value] = val.split('='); |
michael@0 | 117 | query[name] = unescape(value); |
michael@0 | 118 | }); |
michael@0 | 119 | return query; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | function getEtag(request, version) { |
michael@0 | 123 | return request.queryString.replace(/&/g, '-').replace(/=/g, '-') + |
michael@0 | 124 | '-' + version; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | function etagMatches(request, etag) { |
michael@0 | 128 | return request.hasHeader("If-None-Match") && |
michael@0 | 129 | request.getHeader("If-None-Match") == etag; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // File and resources helpers |
michael@0 | 133 | |
michael@0 | 134 | function addZipEntry(zipWriter, entry, entryName) { |
michael@0 | 135 | var stream = Cc["@mozilla.org/io/string-input-stream;1"] |
michael@0 | 136 | .createInstance(Ci.nsIStringInputStream); |
michael@0 | 137 | stream.setData(entry, entry.length); |
michael@0 | 138 | zipWriter.addEntryStream(entryName, Date.now(), |
michael@0 | 139 | Ci.nsIZipWriter.COMPRESSION_BEST, stream, false); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | function readFile(path, fromTmp) { |
michael@0 | 143 | var dir = fromTmp ? "TmpD" : "CurWorkD"; |
michael@0 | 144 | var file = Cc["@mozilla.org/file/directory_service;1"] |
michael@0 | 145 | .getService(Ci.nsIProperties) |
michael@0 | 146 | .get(dir, Ci.nsILocalFile); |
michael@0 | 147 | var fstream = Cc["@mozilla.org/network/file-input-stream;1"] |
michael@0 | 148 | .createInstance(Ci.nsIFileInputStream); |
michael@0 | 149 | var split = path.split("/"); |
michael@0 | 150 | for(var i = 0; i < split.length; ++i) { |
michael@0 | 151 | file.append(split[i]); |
michael@0 | 152 | } |
michael@0 | 153 | fstream.init(file, -1, 0, 0); |
michael@0 | 154 | var data = NetUtil.readInputStreamToString(fstream, fstream.available()); |
michael@0 | 155 | fstream.close(); |
michael@0 | 156 | return data; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | function makeResource(templatePath, version, packagePath, packageSize, |
michael@0 | 160 | appName, developerName, developerUrl) { |
michael@0 | 161 | var res = readFile(templatePath, false) |
michael@0 | 162 | .replace(/VERSIONTOKEN/g, version) |
michael@0 | 163 | .replace(/PACKAGEPATHTOKEN/g, packagePath) |
michael@0 | 164 | .replace(/PACKAGESIZETOKEN/g, packageSize) |
michael@0 | 165 | .replace(/NAMETOKEN/g, appName) |
michael@0 | 166 | .replace(/DEVELOPERTOKEN/g, developerName) |
michael@0 | 167 | .replace(/DEVELOPERURLTOKEN/g, developerUrl); |
michael@0 | 168 | return res; |
michael@0 | 169 | } |