content/media/test/dash_detect_stream_switch.sjs

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

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

mercurial