all files / lib/ ecstatic.js

88.13% Statements 141/160
84.35% Branches 124/147
83.33% Functions 15/18
88.05% Lines 140/159
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387    35×                         35× 50× 26× 26×     50×                       50× 50×     50×               49×                                     163× 163× 163× 161×         161×                   161×   159×   161× 73×     161×           161×       161×       35× 159× 159× 44×     12×   32×     14×           18×           115×     115× 45×       44× 18× 18×     18×     26× 22×                             70×           161×           159×     35×     72×       72× 72× 72× 72×       72×         72× 72×                           67×   67× 67×   67×     63×       67×     62× 62×         62×   62×       62×   62× 62×         35× 67× 24×     43×     43×   36×                                                 35× 35×     35× 84×   84×                 35× 161×   161× 455×   455×       455×       35× 10× 10×         150×             10×             10×   10×      
#! /usr/bin/env node
 
var path = require('path'),
    fs = require('fs'),
    url = require('url'),
    mime = require('mime'),
    urlJoin = require('url-join'),
    showDir = require('./ecstatic/showdir'),
    version = JSON.parse(
      fs.readFileSync(__dirname + '/../package.json').toString()
    ).version,
    status = require('./ecstatic/status-handlers'),
    generateEtag = require('./ecstatic/etag'),
    optsParser = require('./ecstatic/opts');
 
var ecstatic = module.exports = function (dir, options) {
  if (typeof dir !== 'string') {
    options = dir;
    dir = options.root;
  }
 
  var root = path.join(path.resolve(dir), '/'),
      opts = optsParser(options),
      cache = opts.cache,
      autoIndex = opts.autoIndex,
      baseDir = opts.baseDir,
      defaultExt = opts.defaultExt,
      handleError = opts.handleError,
      headers = opts.headers,
      serverHeader = opts.serverHeader,
      weakEtags = opts.weakEtags,
      handleOptionsMethod = opts.handleOptionsMethod;
 
  opts.root = dir;
  if (defaultExt && /^\./.test(defaultExt)) defaultExt = defaultExt.replace(/^\./, '');
 
  // Support hashes and .types files in mimeTypes @since 0.8
  if (opts.mimeTypes) {
    try {
      // You can pass a JSON blob here---useful for CLI use
      opts.mimeTypes = JSON.parse(opts.mimeTypes);
    } catch (e) {}
    if (typeof opts.mimeTypes === 'string') {
      mime.load(opts.mimeTypes);
    }
    else Eif (typeof opts.mimeTypes === 'object') {
      mime.define(opts.mimeTypes);
    }
  }
 
 
  return function middleware (req, res, next) {
 
    // Strip any null bytes from the url
    // This was at one point necessary because of an old bug in url.parse
    //
    // See: https://github.com/jfhbrook/node-ecstatic/issues/16#issuecomment-3039914
    // See: https://github.com/jfhbrook/node-ecstatic/commit/43f7e72a31524f88f47e367c3cc3af710e67c9f4
    //
    // But this opens up a regex dos attack vector! D:
    //
    // Based on some research (ie asking #node-dev if this is still an issue),
    // it's *probably* not an issue. :)
    /*
    while(req.url.indexOf('%00') !== -1) {
      req.url = req.url.replace(/\%00/g, '');
    }
    */
 
    // Figure out the path for the file from the given url
    var parsed = url.parse(req.url);
    try {
      decodeURIComponent(req.url); // check validity of url
      var pathname = decodePathname(parsed.pathname);
    }
    catch (err) {
      return status[400](res, next, { error: err });
    }
 
    var file = path.normalize(
          path.join(root,
            path.relative(
              path.join('/', baseDir),
              pathname
            )
          )
        ),
        gzipped = file + '.gz';
 
    if(serverHeader !== false) {
      // Set common headers.
      res.setHeader('server', 'ecstatic-'+version);
    }
    Object.keys(headers).forEach(function (key) {
      res.setHeader(key, headers[key])
    })
 
    Iif (req.method === 'OPTIONS' && handleOptionsMethod) {
      return res.end();
    }
 
    // TODO: This check is broken, which causes the 403 on the
    // expected 404.
    Iif (file.slice(0, root.length) !== root) {
      return status[403](res, next);
    }
 
    Iif (req.method && (req.method !== 'GET' && req.method !== 'HEAD' )) {
      return status[405](res, next);
    }
 
    function statFile() {
      fs.stat(file, function (err, stat) {
        if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) {
          if (req.statusCode == 404) {
            // This means we're already trying ./404.html and can not find it.
            // So send plain text response with 404 status code
            status[404](res, next);
          }
          else if (!path.extname(parsed.pathname).length && defaultExt) {
            // If there is no file extension in the path and we have a default
            // extension try filename and default extension combination before rendering 404.html.
            middleware({
              url: parsed.pathname + '.' + defaultExt + ((parsed.search)? parsed.search:'')
            }, res, next);
          }
          else {
            // Try to serve default ./404.html
            middleware({
              url: (handleError ? ('/' + path.join(baseDir, '404.' + defaultExt)) : req.url),
              statusCode: 404
            }, res, next);
          }
        }
        else Iif (err) {
          status[500](res, next, { error: err });
        }
        else if (stat.isDirectory()) {
          if (!autoIndex && !opts.showDir) {
            status[404](res, next);
            return;
          }
 
          // 302 to / if necessary
          if (!parsed.pathname.match(/\/$/)) {
            res.statusCode = 302;
            res.setHeader('location', parsed.pathname + '/' +
              (parsed.query? ('?' + parsed.query):'')
            );
            return res.end();
          }
 
          if (autoIndex) {
            return middleware({
              url: urlJoin(encodeURIComponent(pathname), '/index.' + defaultExt)
            }, res, function (err) {
              Iif (err) {
                return status[500](res, next, { error: err });
              }
              Eif (opts.showDir) {
                return showDir(opts, stat)(req, res);
              }
 
              return status[403](res, next);
            });
          }
 
          Eif (opts.showDir) {
            return showDir(opts, stat)(req, res);
          }
 
        }
        else {
          serve(stat);
        }
      });
    }
 
    // Look for a gzipped file if this is turned on
    if (opts.gzip && shouldCompress(req)) {
      fs.stat(gzipped, function (err, stat) {
        Eif (!err && stat.isFile()) {
          file = gzipped;
          return serve(stat);
        } else {
          statFile();
        }
      });
    } else {
      statFile();
    }
 
    function serve(stat) {
      // Do a MIME lookup, fall back to octet-stream and handle gzip
      // special case.
      var defaultType = opts.contentType || 'application/octet-stream',
          contentType = mime.lookup(file, defaultType),
          charSet;
 
      Eif (contentType) {
        charSet = mime.charsets.lookup(contentType, 'utf-8');
        Eif (charSet) {
          contentType += '; charset=' + charSet;
        }
      }
 
      if (path.extname(file) === '.gz') {
        res.setHeader('Content-Encoding', 'gzip');
 
        // strip gz ending and lookup mime type
        contentType = mime.lookup(path.basename(file, ".gz"), defaultType);
      }
 
      var range = (req.headers && req.headers['range']);
      if (range) {
        var total = stat.size;
        var parts = range.replace(/bytes=/, "").split("-");
        var partialstart = parts[0];
        var partialend = parts[1];
        var start = parseInt(partialstart, 10);
        var end = Math.min(total-1, partialend ? parseInt(partialend, 10) : total-1);
        var chunksize = (end-start)+1;
        if (start > end || isNaN(start) || isNaN(end)) {
          return status['416'](res, next);
        }
        var fstream = fs.createReadStream(file, {start: start, end: end});
        fstream.on('error', function (err) {
          status['500'](res, next, { error: err });
        });
        res.on('close', function () {
           fstream.destroy();
        });
        res.writeHead(206, {
          'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
          'Accept-Ranges': 'bytes',
          'Content-Length': chunksize,
          'Content-Type': contentType
        });
        fstream.pipe(res);
        return;
      }
 
      // TODO: Helper for this, with default headers.
      var lastModified = (new Date(stat.mtime)).toUTCString(),
          etag = generateEtag(stat, weakEtags);
      res.setHeader('last-modified', lastModified);
      res.setHeader('etag', etag);
 
      if (typeof cache === 'function') {
        var requestSpecificCache = cache(pathname);
        if (typeof requestSpecificCache === 'number') {
          requestSpecificCache = 'max-age=' + requestSpecificCache;
        }
        res.setHeader('cache-control', requestSpecificCache);
      } else {
        res.setHeader('cache-control', cache);
      }
 
      // Return a 304 if necessary
      if (shouldReturn304(req, lastModified, etag)) {
        return status[304](res, next);
      }
 
      res.setHeader('content-length', stat.size);
      res.setHeader('content-type', contentType);
 
      // set the response statusCode if we have a request statusCode.
      // This only can happen if we have a 404 with some kind of 404.html
      // In all other cases where we have a file we serve the 200
      res.statusCode = req.statusCode || 200;
 
      Iif (req.method === "HEAD") {
        return res.end();
      }
 
      var stream = fs.createReadStream(file);
 
      stream.pipe(res);
      stream.on('error', function (err) {
        status['500'](res, next, { error: err });
      });
    }
 
    function shouldReturn304(req, serverLastModified, serverEtag) {
      if (!req || !req.headers) {
        return false;
      }
 
      var clientModifiedSince = req.headers['if-modified-since'],
          clientEtag = req.headers['if-none-match'];
 
      if (!clientModifiedSince && !clientEtag) {
        // Client did not provide any conditional caching headers
        return false;
      }
 
      Eif (clientModifiedSince) {
        // Catch "illegal access" dates that will crash v8
        // https://github.com/jfhbrook/node-ecstatic/pull/179
        try {
          var clientModifiedDate = new Date(Date.parse(clientModifiedSince));
        }
        catch (err) { return false }
 
        Iif (clientModifiedDate.toString() === 'Invalid Date') {
          return false;
        }
        // If the client's copy is older than the server's, don't return 304
        Iif (clientModifiedDate < new Date(serverLastModified)) {
          return false;
        }
      }
 
      if (clientEtag) {
        // Do a strong or weak etag comparison based on setting
        // https://www.ietf.org/rfc/rfc2616.txt Section 13.3.3
        Iif (opts.weakCompare && clientEtag !== serverEtag
          && clientEtag !== ('W/' + serverEtag) && ('W/' + clientEtag) !== serverEtag) {
          return false;
        } else if (!opts.weakCompare && (clientEtag !== serverEtag || clientEtag.indexOf('W/') === 0)) {
          return false;
        }
      }
 
      return true;
    }
  };
};
 
ecstatic.version = version;
ecstatic.showDir = showDir;
 
// Check to see if we should try to compress a file with gzip.
function shouldCompress(req) {
  var headers = req.headers;
 
  return headers && headers['accept-encoding'] &&
    headers['accept-encoding']
      .split(",")
      .some(function (el) {
        return ['*','compress', 'gzip', 'deflate'].indexOf(el) != -1;
      })
    ;
}
 
// See: https://github.com/jesusabdullah/node-ecstatic/issues/109
function decodePathname(pathname) {
  var pieces = pathname.replace(/\\/g,"/").split('/');
 
  return pieces.map(function (piece) {
    piece = decodeURIComponent(piece);
 
    Iif (process.platform === 'win32' && /\\/.test(piece)) {
      throw new Error('Invalid forward slash character');
    }
 
    return piece;
  }).join('/');
}
 
if (!module.parent) {
  var defaults = require('./ecstatic/defaults.json')
  var http = require('http'),
      opts = require('minimist')(process.argv.slice(2), {
        alias: require('./ecstatic/aliases.json'),
        default: defaults,
        boolean: Object.keys(defaults).filter(function (key) {
          return typeof defaults[key] === 'boolean'
        })
      }),
      envPORT = parseInt(process.env.PORT, 10),
      port = envPORT > 1024 && envPORT <= 65536 ? envPORT : opts.port || opts.p || 8000,
      dir = opts.root || opts._[0] || process.cwd();
 
  Iif (opts.help || opts.h) {
    var u = console.error;
    u('usage: ecstatic [dir] {options} --port PORT');
    u('see https://npm.im/ecstatic for more docs');
    return;
  }
 
  http.createServer(ecstatic(dir, opts))
    .listen(port, function () {
      console.log('ecstatic serving ' + dir + ' at http://0.0.0.0:' + port);
    });
}