Skip to content

Commit

Permalink
Exported the 'Range' header parser to be usable in the 'GET' and 'HEA…
Browse files Browse the repository at this point in the history
…D' methods
  • Loading branch information
AdrienCastex committed Aug 3, 2017
1 parent 6ebfd98 commit a2aabce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
31 changes: 21 additions & 10 deletions lib/server/v2/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ var RangedStream = (function (_super) {
};
return RangedStream;
}(stream_1.Transform));
function parseRangeHeader(mimeType, size, range) {
var createMultipart = function (range) {
return '--' + separator + '\r\nContent-Type: ' + mimeType + '\r\nContent-Range: bytes ' + range.min + '-' + range.max + '/*\r\n\r\n';
};
var endMultipart = function () {
return '\r\n--' + separator + '--';
};
var ranges = range.split(',').map(function (block) { return parseRangeBlock(size, block); });
var separator = Array.apply(null, { length: 20 }).map(function () { return String.fromCharCode('a'.charCodeAt(0) + Math.floor(Math.random() * 26)); }).join('');
var len = ranges.reduce(function (previous, mm) { return mm.max - mm.min + 1 + previous; }, 0) + (ranges.length > 1 ? ranges.reduce(function (previous, mm) { return createMultipart(mm).length + previous; }, endMultipart().length + '\r\n'.length * (ranges.length - 1)) : 0);
return {
ranges: ranges,
separator: separator,
len: len,
createMultipart: createMultipart,
endMultipart: endMultipart
};
}
exports.parseRangeHeader = parseRangeHeader;
function parseRangeBlock(size, block) {
size -= 1;
var rRange = /([0-9]+)-([0-9]+)/;
Expand Down Expand Up @@ -139,15 +158,7 @@ var default_1 = (function () {
//ctx.invokeEvent('read', r);
if (range) {
try {
var createMultipart_1 = function (range) {
return '--' + separator_1 + '\r\nContent-Type: ' + mimeType + '\r\nContent-Range: bytes ' + range.min + '-' + range.max + '/*\r\n\r\n';
};
var endMultipart_1 = function () {
return '\r\n--' + separator_1 + '--';
};
var ranges_1 = range.split(',').map(function (block) { return parseRangeBlock(size, block); });
var separator_1 = Array.apply(null, { length: 20 }).map(function () { return String.fromCharCode('a'.charCodeAt(0) + Math.floor(Math.random() * 26)); }).join('');
var len = ranges_1.reduce(function (previous, mm) { return mm.max - mm.min + 1 + previous; }, 0) + (ranges_1.length > 1 ? ranges_1.reduce(function (previous, mm) { return createMultipart_1(mm).length + previous; }, endMultipart_1().length + '\r\n'.length * (ranges_1.length - 1)) : 0);
var _a = parseRangeHeader(mimeType, size, range), ranges_1 = _a.ranges, separator = _a.separator, len = _a.len, createMultipart_1 = _a.createMultipart, endMultipart_1 = _a.endMultipart;
ctx.setCode(WebDAVRequest_1.HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes');
ctx.response.setHeader('Content-Length', len.toString());
Expand All @@ -157,7 +168,7 @@ var default_1 = (function () {
rstream.on('end', callback);
return rstream.pipe(new RangedStream(ranges_1[0].min, ranges_1[0].max)).pipe(ctx.response);
}
ctx.response.setHeader('Content-Type', 'multipart/byteranges; boundary=' + separator_1);
ctx.response.setHeader('Content-Type', 'multipart/byteranges; boundary=' + separator);
var multi_1 = new MultipleRangedStream(ranges_1);
rstream.pipe(multi_1);
var current_1 = 0;
Expand Down
34 changes: 24 additions & 10 deletions src/server/v2/commands/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ interface IRange
max : number
}

export function parseRangeHeader(mimeType : string, size : number, range : string)
{
const createMultipart = (range : IRange) => {
return '--' + separator + '\r\nContent-Type: ' + mimeType + '\r\nContent-Range: bytes ' + range.min + '-' + range.max + '/*\r\n\r\n';
};
const endMultipart = () => {
return '\r\n--' + separator + '--';
};

const ranges = range.split(',').map((block) => parseRangeBlock(size, block));
const separator = Array.apply(null, {length: 20}).map(() => String.fromCharCode('a'.charCodeAt(0) + Math.floor(Math.random() * 26))).join('');
const len = ranges.reduce((previous, mm) => mm.max - mm.min + 1 + previous, 0) + (ranges.length > 1 ? ranges.reduce((previous, mm) => createMultipart(mm).length + previous, endMultipart().length + '\r\n'.length * (ranges.length - 1)) : 0);

return {
ranges,
separator,
len,
createMultipart,
endMultipart
}

}

function parseRangeBlock(size : number, block : string) : IRange
{
size -= 1;
Expand Down Expand Up @@ -167,16 +190,7 @@ export default class implements HTTPMethod
{
try
{
const createMultipart = (range : IRange) => {
return '--' + separator + '\r\nContent-Type: ' + mimeType + '\r\nContent-Range: bytes ' + range.min + '-' + range.max + '/*\r\n\r\n';
};
const endMultipart = () => {
return '\r\n--' + separator + '--';
};

const ranges = range.split(',').map((block) => parseRangeBlock(size, block));
const separator = Array.apply(null, {length: 20}).map(() => String.fromCharCode('a'.charCodeAt(0) + Math.floor(Math.random() * 26))).join('');
const len = ranges.reduce((previous, mm) => mm.max - mm.min + 1 + previous, 0) + (ranges.length > 1 ? ranges.reduce((previous, mm) => createMultipart(mm).length + previous, endMultipart().length + '\r\n'.length * (ranges.length - 1)) : 0);
const { ranges, separator, len, createMultipart, endMultipart } = parseRangeHeader(mimeType, size, range);

ctx.setCode(HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes')
Expand Down

0 comments on commit a2aabce

Please sign in to comment.