Sha256: 12ff92c6e7b4fa1235dc3093ac1782b01a44af87bcd2404eb2247d4317c66cf1

Contents?: true

Size: 1.98 KB

Versions: 26

Compression:

Stored size: 1.98 KB

Contents

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

/**
 * Conditional GET request support.
 *
 * @return {Function}
 * @api public
 */

module.exports = function conditionalGet(){
    return function conditionalGet(req, res, next) {
        // Skip all requests that are not conditional gets.
        if (!(req.method === "GET" &&
             (req.headers["if-modified-since"] || req.headers["if-none-match"])
           )) {
            next();
            return;
        }

        var since = req.headers["if-modified-since"],
            oldEtag = req.headers["if-none-match"],
            writeHead = res.writeHead,
            write = res.write,
            end = res.end;

        since = since && Date.parse(since).valueOf();

        res.writeHead = function (code, headers) {
            var lastModified = headers["Last-Modified"],
                etag = headers["Etag"];
            lastModified = lastModified && Date.parse(lastModified).valueOf();

            // If there is no match, then move on.
            if (!(code === 200 &&
                  (lastModified === since || oldEtag === etag)
               )) {
                res.writeHead = writeHead;
                res.writeHead(code, headers);
                return;
            }

            // Ignore writes
            res.write = function () {};

            res.end = function () {
                // Put the original functions back on.
                res.writeHead = writeHead;
                res.write = write;
                res.end = end;

                // Filter out any Content based headers since there is no
                // content.
                var newHeaders = {};
                headers.forEach(function (value, key) {
                    if (key.indexOf("Content") < 0) {
                        newHeaders[key] = value;
                    }
                });
                res.writeHead(304, newHeaders);
                res.end();
            };
        };

        next();
    };
};

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
vulcan-0.8.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.8.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.8.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.7.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.7.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.7.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.6.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.6.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.5.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.4.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.3.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.2.5 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.2.4 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.2.3 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.2.2 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.2.1 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.2.0 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.1.6 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.1.5 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js
vulcan-0.1.4 server/node_modules/connect-form/support/connect/lib/connect/middleware/conditionalGet.js