feat: request wrapper

This commit is contained in:
DIYgod
2019-08-06 13:00:22 +08:00
parent 08878e1d52
commit 6f3a7836f2
4 changed files with 14 additions and 6 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ const config = require('./config');
const Koa = require('koa');
const fs = require('fs');
const logger = require('./utils/logger');
require('./utils/agent-wrapper');
require('./utils/request-wrapper');
const onerror = require('./middleware/onerror');
const header = require('./middleware/header');
-1
View File
@@ -43,7 +43,6 @@ const custom = got.extend({
},
headers: {
'user-agent': config.ua,
'x-app': 'RSSHub',
},
});
custom.all = (list) => Promise.all(list);
@@ -47,7 +47,7 @@ if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.p
}
}
const agentWrapper = (url, options) => {
const requestWrapper = (url, options) => {
if (agent && new RegExp(config.proxy.url_regex).test(url)) {
let agentResult;
try {
@@ -62,6 +62,16 @@ const agentWrapper = (url, options) => {
}
logger.info(`Proxy for ${url}`);
}
let hasUA = false;
for (const header in options.headers) {
if (header.toLowerCase() === 'user-agent') {
hasUA = true;
}
}
if (!hasUA) {
options.headers['user-agent'] = config.ua;
}
options.headers['x-app'] = 'RSSHub';
};
const httpWrap = (func) => {
@@ -69,9 +79,9 @@ const httpWrap = (func) => {
return function(url, request) {
if (typeof url === 'object') {
const req = url;
agentWrapper(req.url || req.href || `${req.protocol}//${req.hostname}${req.path}`, req);
requestWrapper(req.url || req.href || `${req.protocol}//${req.hostname}${req.path}`, req);
} else {
agentWrapper(url, request);
requestWrapper(url, request);
}
return origin.apply(this, arguments);
};
-1
View File
@@ -7,7 +7,6 @@ const parser = new Parser({
},
headers: {
'User-Agent': config.ua,
'X-APP': 'RSSHub',
},
});