Bug 398185 - Add byte range request support to JS httpd - r+=sayrer

This commit is contained in:
Chris Double 2008-09-25 15:30:35 +12:00
parent 85938eeb67
commit b73459c8fc
2 changed files with 114 additions and 2 deletions

View File

@ -2269,9 +2269,16 @@ ServerHandler.prototype =
if (!file.exists())
throw HTTP_404;
var offset = 0;
if (metadata.hasHeader("Range")) {
response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
offset = parseInt(metadata.getHeader("Range").match(/\d+/));
}
// finally...
dumpn("*** handling '" + path + "' as mapping to " + file.path);
this._writeFileResponse(metadata, file, response);
this._writeFileResponse(metadata, file, response, offset);
},
/**
@ -2284,8 +2291,10 @@ ServerHandler.prototype =
* the file which is to be sent in the response
* @param response : Response
* the response to which the file should be written
* @param offset: integer
* the byte offset to skip to when writing
*/
_writeFileResponse: function(metadata, file, response)
_writeFileResponse: function(metadata, file, response, offset)
{
const PR_RDONLY = 0x01;
@ -2321,6 +2330,16 @@ ServerHandler.prototype =
var fis = new FileInputStream(file, PR_RDONLY, 0444,
Ci.nsIFileInputStream.CLOSE_ON_EOF);
offset = offset || 0;
if (offset != 0) {
if (fis instanceof Ci.nsISeekableStream) {
fis.seek(Ci.nsISeekableStream.SEEK_SET, offset);
}
else {
dumpn("*** file stream is not seekable, failed to seek to offset " + offset);
throw HTTP_416;
}
}
response.bodyOutputStream.writeFrom(fis, file.fileSize);
fis.close();

View File

@ -0,0 +1,93 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is MozJSHTTP code.
*
* The Initial Developer of the Original Code is
* Chris Double <chris.double@double.co.nz>.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// checks if a byte range request and non-byte range request retrieve the
// correct data.
const PREFIX = "http://localhost:4444";
var tests =
[
new Test(PREFIX + "/normal-file.txt",
init_byterange, start_byterange, stop_byterange),
new Test(PREFIX + "/normal-file.txt",
null, start_normal, stop_normal)
];
function run_test()
{
var srv = createServer();
var dir = do_get_file("netwerk/test/httpserver/test/data/name-scheme/");
srv.registerDirectory("/", dir);
srv.start(4444);
runHttpTests(tests, function() { srv.stop(); });
}
function start_normal(ch, cx)
{
do_check_eq(ch.responseStatus, 200);
do_check_eq(ch.getResponseHeader("Content-Type"), "text/plain");
}
function stop_normal(ch, cx, status, data)
{
do_check_eq(data[0], 84);
do_check_eq(data[20], 10);
do_check_eq(data.length, 21);
}
function init_byterange(ch)
{
ch.setRequestHeader("Range", "bytes=10-", false);
}
function start_byterange(ch, cx)
{
do_check_eq(ch.responseStatus, 206);
do_check_eq(ch.getResponseHeader("Content-Type"), "text/plain");
}
function stop_byterange(ch, cx, status, data)
{
do_check_eq(data[0], 100);
do_check_eq(data[10], 10);
do_check_eq(data.length, 11);
}