« index
Coverage for /Users/yunong/workspace/node-restify/lib/plugins/pre/user_agent.js : 70%
48 lines |
34 run |
14 missing |
0 partial |
5 blocks |
0 blocks run |
5 blocks missing
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 | // Copyright 2012 Mark Cavage, Inc. All rights reserved. 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. */ function userAgentConnection(opts) { assert.optionalObject(opts, 'options'); opts = opts || {}; 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; |