
/*!
 * Ext JS Connect
 * Copyright(c) 2010 Sencha Inc.
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var fs = require('fs'),
    Path = require('path'),
    utils = require('../utils'),
    Buffer = require('buffer').Buffer,
    parseUrl = require('url').parse,
    queryString = require('querystring');

/**
 * Default browser cache maxAge of one year.
 */

const MAX_AGE = 31536000;

/**
 * Static file server.
 *
 * Options:
 *
 *   - `root`     Root path from which to serve static files.
 *   - `maxAge` Browser cache maxAge
 *
 * @param {Object} options
 * @return {Function}
 * @api public
 */

module.exports = function staticProvider(options){
    var maxAge,
        root;

    // Support options object and root string
    if (typeof options == 'string') {
        root = options;
        maxAge = MAX_AGE;
    } else {
        options = options || {};
        maxAge = options.maxAge === undefined
            ? MAX_AGE
            : options.maxAge;
        root = process.connectEnv.staticRoot || options.root || process.cwd();
    }

    return function staticProvider(req, res, next) {
        if (req.method !== 'GET') return next();

        var url = parseUrl(req.url),
            pathname = url.pathname.replace(/\.\.+/g, '.'),
            filename = Path.join(root, queryString.unescape(pathname));

        if (filename[filename.length - 1] === '/') {
            filename += "index.html";
        }

        fs.stat(filename, function(err, stat){

            // Pass through for missing files, thow error for other problems
            if (err) {
                return err.errno === process.ENOENT
                    ? next()
                    : next(err);
            } else if (stat.isDirectory()) {
                return next();
            }

            // Serve the file directly using buffers
            function onRead(err, data) {
                if (err) return next(err);

                // For older versions of node convert the string to a buffer.
                if (typeof data === 'string') {
                    var b = new Buffer(data.length);
                    b.write(data, "binary");
                    data = b;
                }

                // Zero length buffers act funny, use a string
                if (data.length === 0) data = '';

                res.writeHead(200, {
                    "Content-Type": utils.mime.type(filename),
                    "Content-Length": data.length,
                    "Last-Modified": stat.mtime.toUTCString(),
                    "Cache-Control": "public max-age=" + maxAge
                });
                res.end(data);
            }

            // Node before 0.1.95 doesn't do buffers for fs.readFile
            if (process.version < "0.1.95" && process.version > "0.1.100") {
                // sys.debug("Warning: Old node version has slower static file loading");
                fs.readFile(filename, "binary", onRead);
            } else {
                fs.readFile(filename, onRead);
            }
        });
    };

};

@@ -1920,281 +1920,8 @@
 );%0A%0A
-                // For older versions of node convert the string to a buffer.%0A                if (typeof data === 'string') %7B%0A                    var b = new Buffer(data.length);%0A                    b.write(data, %22binary%22);%0A                    data = b;%0A                %7D%0A%0A
     
@@ -1975,24 +1975,24 @@
 se a string%0A
+
             
@@ -2378,326 +2378,8 @@
  %7D%0A%0A
-            // Node before 0.1.95 doesn't do buffers for fs.readFile%0A            if (process.version %3C %220.1.95%22 && process.version %3E %220.1.100%22) %7B%0A                // sys.debug(%22Warning: Old node version has slower static file loading%22);%0A                fs.readFile(filename, %22binary%22, onRead);%0A            %7D else %7B%0A    
     
@@ -2417,30 +2417,16 @@
 nRead);%0A
-            %7D%0A
         

Remove legacy readFile code, since 'connect' requires node v0.1.98, thus this should no longer apply.