add default User-Agent
This commit is contained in:
parent
e22baa8eae
commit
4b9a1f07ee
|
@ -0,0 +1,15 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var request = require('../');
|
||||||
|
request({
|
||||||
|
url: 'https://postb.in/1588134650162-6019286897499?hello=world'
|
||||||
|
//headers: { 'user-agent': false }
|
||||||
|
//headers: { 'user-agent': 'test/1.0' }
|
||||||
|
//userAgent: 'test/1.1' // not presently implemented
|
||||||
|
})
|
||||||
|
.then(function(resp) {
|
||||||
|
console.log(resp.body);
|
||||||
|
})
|
||||||
|
.catch(function(err) {
|
||||||
|
console.error(err);
|
||||||
|
});
|
42
index.js
42
index.js
|
@ -3,6 +3,8 @@
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var https = require('https');
|
var https = require('https');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
var os = require('os');
|
||||||
|
var pkg = require('./package.json');
|
||||||
|
|
||||||
function debug() {
|
function debug() {
|
||||||
if (module.exports.debug) {
|
if (module.exports.debug) {
|
||||||
|
@ -237,6 +239,16 @@ function setDefaults(defs) {
|
||||||
|
|
||||||
finalOpts.method = opts.method;
|
finalOpts.method = opts.method;
|
||||||
finalOpts.headers = JSON.parse(JSON.stringify(opts.headers));
|
finalOpts.headers = JSON.parse(JSON.stringify(opts.headers));
|
||||||
|
|
||||||
|
var uaHeader = getHeaderName(finalOpts, 'User-Agent') || 'User-Agent';
|
||||||
|
// set a default user-agent
|
||||||
|
if (!finalOpts.headers[uaHeader]) {
|
||||||
|
if (false === finalOpts.headers[uaHeader]) {
|
||||||
|
delete finalOpts.headers[uaHeader];
|
||||||
|
} else {
|
||||||
|
finalOpts.headers[uaHeader] = getUserAgent(opts.userAgent);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (_body) {
|
if (_body) {
|
||||||
// Most APIs expect (or require) Content-Length except in the case of multipart uploads
|
// Most APIs expect (or require) Content-Length except in the case of multipart uploads
|
||||||
// Transfer-Encoding: Chunked (the default) is generally only well-supported downstream
|
// Transfer-Encoding: Chunked (the default) is generally only well-supported downstream
|
||||||
|
@ -368,6 +380,7 @@ function setDefaults(defs) {
|
||||||
if ('function' === typeof _body.pipe) {
|
if ('function' === typeof _body.pipe) {
|
||||||
// used for chunked encoding
|
// used for chunked encoding
|
||||||
_body.pipe(req);
|
_body.pipe(req);
|
||||||
|
_body.on('error', cb);
|
||||||
} else {
|
} else {
|
||||||
// used for known content-length
|
// used for known content-length
|
||||||
req.end(_body);
|
req.end(_body);
|
||||||
|
@ -511,6 +524,34 @@ function setDefaults(defs) {
|
||||||
return smartUrequest;
|
return smartUrequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nodeUa =
|
||||||
|
'@root+request/' +
|
||||||
|
pkg.version +
|
||||||
|
' ' +
|
||||||
|
process.release.name +
|
||||||
|
'/' +
|
||||||
|
process.version +
|
||||||
|
' ' +
|
||||||
|
os.platform() +
|
||||||
|
'/' +
|
||||||
|
os.release() +
|
||||||
|
' ' +
|
||||||
|
os.type() +
|
||||||
|
'/' +
|
||||||
|
process.arch;
|
||||||
|
function getUserAgent(additional) {
|
||||||
|
// See https://tools.ietf.org/html/rfc8555#section-6.1
|
||||||
|
// And https://tools.ietf.org/html/rfc7231#section-5.5.3
|
||||||
|
// And https://community.letsencrypt.org/t/user-agent-flag-explained/3843/2
|
||||||
|
|
||||||
|
var ua = nodeUa;
|
||||||
|
if (additional) {
|
||||||
|
ua = additional + ' ' + ua;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ua;
|
||||||
|
}
|
||||||
|
|
||||||
var _defaults = {
|
var _defaults = {
|
||||||
sendImmediately: true,
|
sendImmediately: true,
|
||||||
method: '',
|
method: '',
|
||||||
|
@ -536,6 +577,7 @@ module.exports._keys = Object.keys(_defaults).concat([
|
||||||
'auth',
|
'auth',
|
||||||
'formData',
|
'formData',
|
||||||
'FormData'
|
'FormData'
|
||||||
|
//'userAgent'
|
||||||
]);
|
]);
|
||||||
module.exports.debug =
|
module.exports.debug =
|
||||||
-1 !== (process.env.NODE_DEBUG || '').split(/\s+/g).indexOf('urequest');
|
-1 !== (process.env.NODE_DEBUG || '').split(/\s+/g).indexOf('urequest');
|
||||||
|
|
Loading…
Reference in New Issue