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