Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | // Parse the query string, and give us the value for a certain key, or false if |
michael@0 | 2 | // it does not exist. |
michael@0 | 3 | function parseQuery(request, key) { |
michael@0 | 4 | var params = request.queryString.split('?')[0].split('&'); |
michael@0 | 5 | for (var j = 0; j < params.length; ++j) { |
michael@0 | 6 | var p = params[j]; |
michael@0 | 7 | if (p == key) |
michael@0 | 8 | return true; |
michael@0 | 9 | if (p.indexOf(key + "=") == 0) |
michael@0 | 10 | return p.substring(key.length + 1); |
michael@0 | 11 | if (p.indexOf("=") < 0 && key == "") |
michael@0 | 12 | return p; |
michael@0 | 13 | } |
michael@0 | 14 | return false; |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | function handleRequest(request, response) { |
michael@0 | 18 | try { |
michael@0 | 19 | // Get the filename to send back. |
michael@0 | 20 | var filename = parseQuery(request, "file"); |
michael@0 | 21 | |
michael@0 | 22 | const CC = Components.Constructor; |
michael@0 | 23 | const BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", |
michael@0 | 24 | "nsIBinaryOutputStream", |
michael@0 | 25 | "setOutputStream"); |
michael@0 | 26 | var file = Components.classes["@mozilla.org/file/directory_service;1"]. |
michael@0 | 27 | getService(Components.interfaces.nsIProperties). |
michael@0 | 28 | get("CurWorkD", Components.interfaces.nsILocalFile); |
michael@0 | 29 | var fis = Components.classes['@mozilla.org/network/file-input-stream;1']. |
michael@0 | 30 | createInstance(Components.interfaces.nsIFileInputStream); |
michael@0 | 31 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"]. |
michael@0 | 32 | createInstance(Components.interfaces.nsIBinaryInputStream); |
michael@0 | 33 | var paths = "tests/content/media/test/" + filename; |
michael@0 | 34 | dump(paths + '\n'); |
michael@0 | 35 | var split = paths.split("/"); |
michael@0 | 36 | for(var i = 0; i < split.length; ++i) { |
michael@0 | 37 | file.append(split[i]); |
michael@0 | 38 | } |
michael@0 | 39 | fis.init(file, -1, -1, false); |
michael@0 | 40 | |
michael@0 | 41 | // handle range requests |
michael@0 | 42 | var partialstart = 0, |
michael@0 | 43 | partialend = file.fileSize - 1; |
michael@0 | 44 | if (request.hasHeader("Range")) { |
michael@0 | 45 | var range = request.getHeader("Range"); |
michael@0 | 46 | var parts = range.replace(/bytes=/, "").split("-"); |
michael@0 | 47 | var partialstart = parts[0]; |
michael@0 | 48 | var partialend = parts[1]; |
michael@0 | 49 | if (!partialend.length) { |
michael@0 | 50 | partialend = file.fileSize - 1; |
michael@0 | 51 | } |
michael@0 | 52 | response.setStatusLine(request.httpVersion, 206, "Partial Content"); |
michael@0 | 53 | var contentRange = "bytes " + partialstart + "-" + partialend + "/" + file.fileSize; |
michael@0 | 54 | response.setHeader("Content-Range", contentRange); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | fis.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, partialstart); |
michael@0 | 58 | bis.setInputStream(fis); |
michael@0 | 59 | |
michael@0 | 60 | var sendContentType = parseQuery(request, "nomime"); |
michael@0 | 61 | if (sendContentType == false) { |
michael@0 | 62 | var contentType = parseQuery(request, "type"); |
michael@0 | 63 | if (contentType == false) { |
michael@0 | 64 | // This should not happen. |
michael@0 | 65 | dump("No type specified without having \'nomime\' in parameters."); |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | response.setHeader("Content-Type", contentType, false); |
michael@0 | 69 | } |
michael@0 | 70 | response.setHeader("Content-Length", ""+bis.available(), false); |
michael@0 | 71 | |
michael@0 | 72 | var bytes = bis.readBytes(bis.available()); |
michael@0 | 73 | response.write(bytes, bytes.length); |
michael@0 | 74 | } catch (e) { |
michael@0 | 75 | dump ("ERROR : " + e + "\n"); |
michael@0 | 76 | } |
michael@0 | 77 | } |