Name: js-handler/node_modules/restify/lib/plugins/pre/user_agent.js
| 1: | // Copyright 2012 Mark Cavage, Inc. All rights reserved. |
| 2: | |
| 3: | var assert = require('assert-plus'); |
| 4: | |
| 5: | |
| 6: | |
| 7: | ///--- API |
| 8: | |
| 9: | /** |
| 10: | * This basically exists for curl. curl on HEAD requests usually |
| 11: | * just sits there and hangs, unless you explicitly set |
| 12: | * Connection:close. And in general, you probably want to set |
| 13: | * Connection: close to curl anyway. |
| 14: | * |
| 15: | * Also, because curl spits out an annoying message to stderr about |
| 16: | * remaining bytes if content-length is set, this plugin also drops |
| 17: | * the content-length header (some user agents handle it and want it, |
| 18: | * curl does not). |
| 19: | * |
| 20: | * To be slightly more generic, the options block takes a user |
| 21: | * agent regexp, however. |
| 22: | */ |
| 23: | function userAgentConnection(opts) { |
| 24: | assert.optionalObject(opts, 'options'); |
| 25: | opts = opts || {}; |
| 26: | assert.optionalObject(opts.userAgentRegExp, 'options.userAgentRegExp'); |
| 27: | |
| 28: | var re = opts.userAgentRegExp; |
| 29: | if (!re) |
| 30: | re = /^curl.+/; |
| 31: | |
| 32: | function handleUserAgent(req, res, next) { |
| 33: | var ua = req.headers['user-agent']; |
| 34: | |
| 35: | if (ua && re.test(ua)) |
| 36: | res.setHeader('Connection', 'close'); |
| 37: | |
| 38: | if (req.method === 'HEAD') { |
| 39: | res.once('header', |
| 40: | res.removeHeader.bind(res, 'content-length')); |
| 41: | } |
| 42: | |
| 43: | next(); |
| 44: | } |
| 45: | |
| 46: | return (handleUserAgent); |
| 47: | } |
| 48: | |
| 49: | module.exports = userAgentConnection; |
