Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: JavaScript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* dash_detect_stream_switch.sjs |
michael@0 | 8 | * |
michael@0 | 9 | * Parses requests for DASH manifests and ensures stream switching takes place |
michael@0 | 10 | * by verifying the subsegments downloaded and the streams they belong to. |
michael@0 | 11 | * If unexpected subsegments (byte ranges) are requested, the script will |
michael@0 | 12 | * will respond with a 404. |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | var DEBUG = false; |
michael@0 | 16 | |
michael@0 | 17 | function parseQuery(request, key) { |
michael@0 | 18 | var params = request.queryString.split('&'); |
michael@0 | 19 | if (DEBUG) { |
michael@0 | 20 | dump("DASH-SJS: request params = \"" + params + "\"\n"); |
michael@0 | 21 | } |
michael@0 | 22 | for (var j = 0; j < params.length; ++j) { |
michael@0 | 23 | var p = params[j]; |
michael@0 | 24 | if (p == key) |
michael@0 | 25 | return true; |
michael@0 | 26 | if (p.indexOf(key + "=") === 0) |
michael@0 | 27 | return p.substring(key.length + 1); |
michael@0 | 28 | if (p.indexOf("=") < 0 && key === "") |
michael@0 | 29 | return p; |
michael@0 | 30 | } |
michael@0 | 31 | return false; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function handleRequest(request, response) |
michael@0 | 35 | { |
michael@0 | 36 | try { |
michael@0 | 37 | var name = parseQuery(request, "name"); |
michael@0 | 38 | var range = request.hasHeader("Range") ? request.getHeader("Range") |
michael@0 | 39 | : undefined; |
michael@0 | 40 | |
michael@0 | 41 | // Should not get request for 1st subsegment from 2nd stream, nor 2nd |
michael@0 | 42 | // subsegment from 1st stream. |
michael@0 | 43 | if (name == "dash-webm-video-320x180.webm" && range == "bytes=25514-32767" || |
michael@0 | 44 | name == "dash-webm-video-428x240.webm" && range == "bytes=228-35852") |
michael@0 | 45 | { |
michael@0 | 46 | throw "Should not request " + name + " with byte-range " + range; |
michael@0 | 47 | } else { |
michael@0 | 48 | var rangeSplit = range.split("="); |
michael@0 | 49 | if (rangeSplit.length != 2) { |
michael@0 | 50 | throw "DASH-SJS: ERROR: invalid number of tokens (" + rangeSplit.length + |
michael@0 | 51 | ") delimited by \'=\' in \'Range\' header."; |
michael@0 | 52 | } |
michael@0 | 53 | var offsets = rangeSplit[1].split("-"); |
michael@0 | 54 | if (offsets.length != 2) { |
michael@0 | 55 | throw "DASH-SJS: ERROR: invalid number of tokens (" + offsets.length + |
michael@0 | 56 | ") delimited by \'-\' in \'Range\' header."; |
michael@0 | 57 | } |
michael@0 | 58 | var startOffset = parseInt(offsets[0]); |
michael@0 | 59 | var endOffset = parseInt(offsets[1]); |
michael@0 | 60 | var file = Components.classes["@mozilla.org/file/directory_service;1"]. |
michael@0 | 61 | getService(Components.interfaces.nsIProperties). |
michael@0 | 62 | get("CurWorkD", Components.interfaces.nsILocalFile); |
michael@0 | 63 | var fis = Components.classes['@mozilla.org/network/file-input-stream;1']. |
michael@0 | 64 | createInstance(Components.interfaces.nsIFileInputStream); |
michael@0 | 65 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"]. |
michael@0 | 66 | createInstance(Components.interfaces.nsIBinaryInputStream); |
michael@0 | 67 | |
michael@0 | 68 | var paths = "tests/content/media/test/" + name; |
michael@0 | 69 | var split = paths.split("/"); |
michael@0 | 70 | for (var i = 0; i < split.length; ++i) { |
michael@0 | 71 | file.append(split[i]); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | fis.init(file, -1, -1, false); |
michael@0 | 75 | // Exception: start offset should be within file bounds. |
michael@0 | 76 | if (startOffset > file.fileSize) { |
michael@0 | 77 | throw "Starting offset [" + startOffset + "] is after end of file [" + |
michael@0 | 78 | file.fileSize + "]."; |
michael@0 | 79 | } |
michael@0 | 80 | // End offset may be too large in the MPD. Real world HTTP servers just |
michael@0 | 81 | // return what data they can; do the same here - reduce the end offset. |
michael@0 | 82 | if (endOffset >= file.fileSize) { |
michael@0 | 83 | if (DEBUG) { |
michael@0 | 84 | dump("DASH-SJS: reducing endOffset [" + endOffset + "] to fileSize [" + |
michael@0 | 85 | (file.fileSize-1) + "]\n"); |
michael@0 | 86 | } |
michael@0 | 87 | endOffset = file.fileSize-1; |
michael@0 | 88 | } |
michael@0 | 89 | fis.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, startOffset); |
michael@0 | 90 | bis.setInputStream(fis); |
michael@0 | 91 | |
michael@0 | 92 | var byteLengthToRead = endOffset + 1 - startOffset; |
michael@0 | 93 | var totalBytesExpected = byteLengthToRead + startOffset; |
michael@0 | 94 | if (DEBUG) { |
michael@0 | 95 | dump("DASH-SJS: byteLengthToRead = " + byteLengthToRead + |
michael@0 | 96 | " byteLengthToRead+startOffset = " + totalBytesExpected + |
michael@0 | 97 | " fileSize = " + file.fileSize + "\n"); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | var bytes = bis.readBytes(byteLengthToRead); |
michael@0 | 101 | response.setStatusLine(request.httpVersion, 206, "Partial Content"); |
michael@0 | 102 | response.setHeader("Content-Length", ""+bytes.length, false); |
michael@0 | 103 | response.setHeader("Content-Type", "application/dash+xml", false); |
michael@0 | 104 | var contentRange = "bytes " + startOffset + "-" + endOffset + "/" + |
michael@0 | 105 | file.fileSize; |
michael@0 | 106 | response.setHeader("Content-Range", contentRange, false); |
michael@0 | 107 | response.write(bytes, bytes.length); |
michael@0 | 108 | bis.close(); |
michael@0 | 109 | } |
michael@0 | 110 | } catch (e) { |
michael@0 | 111 | dump ("DASH-SJS-ERROR: " + e + "\n"); |
michael@0 | 112 | response.setStatusLine(request.httpVersion, 404, "Not found"); |
michael@0 | 113 | } |
michael@0 | 114 | } |