Skip to content

Instantly share code, notes, and snippets.

@litmaj0r
Forked from b225ccc-zz/range.js
Last active August 29, 2015 14:19
Show Gist options
  • Save litmaj0r/c93488a3342fafa4b1b3 to your computer and use it in GitHub Desktop.
Save litmaj0r/c93488a3342fafa4b1b3 to your computer and use it in GitHub Desktop.

Revisions

  1. litmaj0r revised this gist Apr 15, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions range.js
    Original file line number Diff line number Diff line change
    @@ -38,12 +38,12 @@ function onRequest(servReq, servResp, cliReq) {

    // check if range values exceed max
    if (range.type === 'bytes') {
    range.forEach(function (r) {
    for(var i = 0; i < range.length; i++) {
    if (r.start > config.max_range_value || r.end > config.max_range_value) {
    bail_4xx(servResp, 416, 'Range value exceeds allowed maximum');
    bail_4xx(servResp, 416, 'Range value exceeds allowed maximum');
    return;
    }
    });
    }
    }
    }

  2. @b225ccc b225ccc revised this gist Apr 15, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions range.js
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,7 @@ function onRequest(servReq, servResp, cliReq) {
    range.forEach(function (r) {
    if (r.start > config.max_range_value || r.end > config.max_range_value) {
    bail_4xx(servResp, 416, 'Range value exceeds allowed maximum');
    return;
    }
    });
    }
  3. @b225ccc b225ccc renamed this gist Apr 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @b225ccc b225ccc created this gist Apr 15, 2015.
    58 changes: 58 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    'use strict';

    var vsm = require('lrs/virtualServerModule');
    var parseRange = require('range-parser');

    // Change config as needed.
    var config = {
    vs: 'vs_http', // name of virtual-server
    max_range_value: 1e9, // 1 GB
    max_ranges: 10
    };

    function bail_4xx(servResp, code, message) {
    code = code || 404;
    message = message || '';
    servResp.writeHead(code);
    servResp.end(message);
    return;
    }

    function onRequest(servReq, servResp, cliReq) {
    if ('range' in servReq.headers) {

    // parse range header
    var range = parseRange(1e12, servReq.headers.range);

    // check from parsing error code
    if (typeof range === 'number' && range < 0) {
    bail_4xx(servResp, 416, 'Malformed header or invalid range');
    return;
    }

    // check if number of ranges exceeds max
    if (range.length > config.max_ranges) {
    bail_4xx(servResp, 416, 'Too many ranges');
    return;
    }

    // check if range values exceed max
    if (range.type === 'bytes') {
    range.forEach(function (r) {
    if (r.start > config.max_range_value || r.end > config.max_range_value) {
    bail_4xx(servResp, 416, 'Range value exceeds allowed maximum');
    }
    });
    }
    }

    // all checks passed; process the request
    cliReq();
    }

    vsm.on('exist', config.vs, function(vs) {
    console.log("Script '" + __scriptname +
    "' installed on Virtual Server '" +
    vs.id + "'");
    vs.on('request', onRequest);
    });