Skip to content

Commit

Permalink
Made the 'Content-Length' header optional in the response when no siz…
Browse files Browse the repository at this point in the history
…e can be obtained
  • Loading branch information
AdrienCastex committed Jul 1, 2017
1 parent 55102b5 commit 0f1cdcd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 71 deletions.
67 changes: 33 additions & 34 deletions lib/server/v2/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var __extends = (this && this.__extends) || (function () {
})();
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var Errors_1 = require("../../../Errors");
var stream_1 = require("stream");
var RangedStream = (function (_super) {
__extends(RangedStream, _super);
Expand Down Expand Up @@ -64,48 +63,48 @@ var default_1 = (function () {
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
return callback();
}
r.mimeType(targetSource, function (e, mimeType) { return process.nextTick(function () {
if (e) {
var range = ctx.headers.find('Range');
r.size(targetSource, function (e, size) { return process.nextTick(function () {
if (e && !range) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
r.openReadStream(targetSource, function (e, rstream) { return process.nextTick(function () {
r.mimeType(targetSource, function (e, mimeType) { return process.nextTick(function () {
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
//ctx.invokeEvent('read', r);
var range = ctx.headers.find('Range');
if (range) {
var rex = /([0-9]+)/g;
var min = parseInt(rex.exec(range)[1], 10);
var max = parseInt(rex.exec(range)[1], 10);
ctx.setCode(WebDAVRequest_1.HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes');
ctx.response.setHeader('Content-Type', mimeType);
ctx.response.setHeader('Content-Length', (max - min).toString());
ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*');
rstream.on('end', callback);
rstream.pipe(new RangedStream(min, max)).pipe(ctx.response);
}
else {
r.size(targetSource, function (e, size) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
}
else {
ctx.setCode(WebDAVRequest_1.HTTPCodes.OK);
ctx.response.setHeader('Accept-Ranges', 'bytes');
ctx.response.setHeader('Content-Type', mimeType);
r.openReadStream(targetSource, function (e, rstream) {
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
return callback();
}
//ctx.invokeEvent('read', r);
if (range) {
var rex = /([0-9]+)/g;
var min = parseInt(rex.exec(range)[1], 10);
var max = parseInt(rex.exec(range)[1], 10);
ctx.setCode(WebDAVRequest_1.HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes');
ctx.response.setHeader('Content-Type', mimeType);
ctx.response.setHeader('Content-Length', (max - min).toString());
ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*');
rstream.on('end', callback);
rstream.pipe(new RangedStream(min, max)).pipe(ctx.response);
}
else {
ctx.setCode(WebDAVRequest_1.HTTPCodes.OK);
ctx.response.setHeader('Accept-Ranges', 'bytes');
ctx.response.setHeader('Content-Type', mimeType);
if (size !== null && size !== undefined && size > -1)
ctx.response.setHeader('Content-Length', size.toString());
rstream.on('end', callback);
rstream.pipe(ctx.response);
}
});
}
rstream.on('end', callback);
rstream.pipe(ctx.response);
}
});
}); });
}); });
});
Expand Down
74 changes: 37 additions & 37 deletions src/server/v2/commands/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,58 +65,58 @@ export default class implements HTTPMethod
return callback();
}

r.mimeType(targetSource, (e, mimeType) => process.nextTick(() => {
if(e)
const range = ctx.headers.find('Range');
r.size(targetSource, (e, size) => process.nextTick(() => {
if(e && !range)
{
if(!ctx.setCodeFromError(e))
ctx.setCode(HTTPCodes.InternalServerError)
return callback();
}

r.openReadStream(targetSource, (e, rstream) => process.nextTick(() => {
r.mimeType(targetSource, (e, mimeType) => process.nextTick(() => {
if(e)
{
if(!ctx.setCodeFromError(e))
ctx.setCode(HTTPCodes.MethodNotAllowed)
ctx.setCode(HTTPCodes.InternalServerError)
return callback();
}
//ctx.invokeEvent('read', r);

const range = ctx.headers.find('Range');
if(range)
{
const rex = /([0-9]+)/g;
const min = parseInt(rex.exec(range)[1], 10);
const max = parseInt(rex.exec(range)[1], 10);
r.openReadStream(targetSource, (e, rstream) => {
if(e)
{
if(!ctx.setCodeFromError(e))
ctx.setCode(HTTPCodes.MethodNotAllowed)
return callback();
}
//ctx.invokeEvent('read', r);

ctx.setCode(HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes')
ctx.response.setHeader('Content-Type', mimeType)
ctx.response.setHeader('Content-Length', (max - min).toString())
ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*')
if(range)
{
const rex = /([0-9]+)/g;
const min = parseInt(rex.exec(range)[1], 10);
const max = parseInt(rex.exec(range)[1], 10);

rstream.on('end', callback);
rstream.pipe(new RangedStream(min, max)).pipe(ctx.response);
}
else
{
r.size(targetSource, (e, size) => {
if(e)
{
ctx.setCode(e === Errors.ResourceNotFound ? HTTPCodes.NotFound : HTTPCodes.InternalServerError)
callback();
}
else
{
ctx.setCode(HTTPCodes.OK);
ctx.response.setHeader('Accept-Ranges', 'bytes')
ctx.response.setHeader('Content-Type', mimeType);
ctx.setCode(HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes')
ctx.response.setHeader('Content-Type', mimeType)
ctx.response.setHeader('Content-Length', (max - min).toString())
ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*')

rstream.on('end', callback);
rstream.pipe(new RangedStream(min, max)).pipe(ctx.response);
}
else
{
ctx.setCode(HTTPCodes.OK);
ctx.response.setHeader('Accept-Ranges', 'bytes')
ctx.response.setHeader('Content-Type', mimeType);
if(size !== null && size !== undefined && size > -1)
ctx.response.setHeader('Content-Length', size.toString());
rstream.on('end', callback);
rstream.pipe(ctx.response);
}
})
}
rstream.on('end', callback);
rstream.pipe(ctx.response);
}
})
}))
}))
})
Expand Down

0 comments on commit 0f1cdcd

Please sign in to comment.