feat: auto add header Host

This commit is contained in:
DIYgod
2020-02-15 21:32:16 +08:00
parent befd42d2a8
commit 48b27fe811
2 changed files with 11 additions and 13 deletions

View File

@@ -86,14 +86,12 @@ const requestWrapper = (url, options) => {
// referer // referer
if (!options.headers.referer && !options.headers.Referer) { if (!options.headers.referer && !options.headers.Referer) {
const protocol = url.match('https?://'); const urlHandler = new URL(url);
url = url.replace(/(https?:)?\/\//, ''); options.headers.referer = urlHandler.origin;
const domain = url }
.split('.') if (!options.headers.host && !options.headers.Host) {
.slice(-2) const urlHandler = new URL(url);
.join('.') options.headers.host = urlHandler.host;
.split('/')[0];
options.headers.referer = (protocol ? protocol[0] : 'https://') + 'www.' + domain;
} }
}; };

View File

@@ -40,18 +40,18 @@ describe('got', () => {
it('headers', async () => { it('headers', async () => {
nock(/rsshub\.test/) nock(/rsshub\.test/)
.get(/.*/) .get(/.*/)
.times(4) .times(3)
.reply(function() { .reply(function() {
expect(this.req.headers.server).toBe('RSSHub'); expect(this.req.headers.server).toBe('RSSHub');
expect(this.req.headers.referer).toBe('http://www.rsshub.test'); expect(this.req.headers.referer).toBe('http://api.rsshub.test');
expect(this.req.headers.host).toBe('api.rsshub.test');
return [200, simpleResponse]; return [200, simpleResponse];
}); });
await got.get('http://rsshub.test/test');
await got.get('http://rsshub.test');
await got.get('http://api.rsshub.test/test'); await got.get('http://api.rsshub.test/test');
await got.get('http://api.rsshub.test');
await parser.parseURL('http://rsshub.test/test'); await parser.parseURL('http://api.rsshub.test/test');
}); });
it('proxy socks', async () => { it('proxy socks', async () => {