wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
 
'use strict';
 
var assert = require('assert-plus');
 
///--- API
 
/**
 * This basically exists for `curl`. `curl` on `HEAD` requests usually
 * just sits there and hangs, unless you explicitly set
 * Connection:close. And in general, you probably want to set
 * Connection: close to curl anyway.
 *
 * Also, because curl spits out an annoying message to stderr about
 * remaining bytes if content-length is set, this plugin also drops
 * the `content-length` header (some user agents handle it and want it,
 * curl does not).
 *
 * To be slightly more generic, the options block takes a user
 * agent regexp, however.
 *
 * @public
 * @function userAgentConnection
 * @param    {Object} [options] - an options object
 * @param    {RegExp} [options.userAgentRegExp=/^curl.+/] - matching any
 *                                                        user-agents applicable
 * @returns  {Function} Handler
 */
function userAgentConnection(options) {
    var opts = options || {};
    assert.optionalObject(opts, 'options');
    assert.optionalObject(opts.userAgentRegExp, 'options.userAgentRegExp');
 
    var re = opts.userAgentRegExp;
 
    if (!re) {
        re = /^curl.+/;
    }
 
    function handleUserAgent(req, res, next) {
        var ua = req.headers['user-agent'];
 
        if (ua && re.test(ua)) {
            res.setHeader('Connection', 'close');
 
            if (req.method === 'HEAD') {
                res.once(
                    'header',
                    res.removeHeader.bind(res, 'content-length')
                );
            }
        }
 
        next();
    }
 
    return handleUserAgent;
}
 
module.exports = userAgentConnection;