Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 16, 2024
1 parent f096409 commit d353b30
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 86 deletions.
61 changes: 0 additions & 61 deletions lib/core/httpclient_allow_h2.js

This file was deleted.

11 changes: 10 additions & 1 deletion lib/core/httpclient_next.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const { HttpClient } = require('urllib-next');
const ms = require('humanize-ms');

const SSRF_HTTPCLIENT = Symbol('SSRF_HTTPCLIENT');

const mainNodejsVersion = parseInt(process.versions.node.split('.')[0]);
let HttpClient;
if (mainNodejsVersion >= 18) {
// urllib@4 only works on Node.js >= 18
HttpClient = require('urllib4').HttpClient;
} else {
HttpClient = require('urllib-next').HttpClient;
}

class HttpClientNext extends HttpClient {
constructor(app, options) {
normalizeConfig(app);
Expand Down
12 changes: 1 addition & 11 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ const utils = require('./core/utils');
const BaseContextClass = require('./core/base_context_class');
const BaseHookClass = require('./core/base_hook_class');

let HttpClientAllowH2 = HttpClientNext;
const mainNodejsVersion = parseInt(process.versions.node.split('.')[0]);
if (mainNodejsVersion >= 18) {
// urllib@4 only works on Node.js >= 18
HttpClientAllowH2 = require('./core/httpclient_allow_h2');
}

const HTTPCLIENT = Symbol('EggApplication#httpclient');
const LOGGERS = Symbol('EggApplication#loggers');
const EGG_PATH = Symbol.for('egg#eggPath');
Expand Down Expand Up @@ -58,7 +51,6 @@ class EggApplication extends EggCore {
this.ContextHttpClient = ContextHttpClient;
this.HttpClient = HttpClient;
this.HttpClientNext = HttpClientNext;
this.HttpClientAllowH2 = HttpClientAllowH2;

this.loader.loadConfig();

Expand Down Expand Up @@ -300,9 +292,7 @@ class EggApplication extends EggCore {
*/
createHttpClient(options) {
let httpClient;
if (this.config.httpclient.allowH2) {
httpClient = new this.HttpClientAllowH2(this, options);
} else if (this.config.httpclient.useHttpClientNext) {
if (this.config.httpclient.useHttpClientNext || this.config.httpclient.allowH2) {
httpClient = new this.HttpClientNext(this, options);
} else if (this.config.httpclient.enableDNSCache) {
httpClient = new DNSCacheHttpClient(this, options);
Expand Down
18 changes: 7 additions & 11 deletions test/fixtures/apps/httpclient-allowH2/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
const assert = require('assert');

module.exports = app => {
class CustomHttpClient extends app.HttpClientAllowH2 {
request(url, opt) {
return new Promise(resolve => {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
resolve();
}).then(() => {
return super.request(url, opt);
});
class CustomHttpClient extends app.HttpClientNext {
async request(url, opt) {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
return await super.request(url, opt);
}

curl(url, opt) {
return this.request(url, opt);
async curl(url, opt) {
return await this.request(url, opt);
}
}
app.HttpClientAllowH2 = CustomHttpClient;
app.HttpClientNext = CustomHttpClient;
};
7 changes: 5 additions & 2 deletions test/lib/core/httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,15 @@ describe('test/lib/core/httpclient.test.js', () => {
after(() => app.close());

it('should work on http2', async () => {
const res = await app.httpclient.request(url);
const res = await app.httpclient.request(url, {
timeout: 5000,
});
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
// assert.equal(sensitiveHeaders in res.headers, false);
const res2 = await app.httpclient.request('https://registry.npmmirror.com/urllib/latest', {
dataType: 'json',
timeout: 5000,
});
assert.equal(res2.status, 200);
assert.equal(res2.data.name, 'urllib');
Expand All @@ -327,7 +330,7 @@ describe('test/lib/core/httpclient.test.js', () => {
await app.httpclient.curl(`${url}/timeout`);
}, err => {
assert.equal(err.name, 'HttpClientRequestTimeoutError');
assert(err.message.includes('Request timeout for 99 ms'));
assert.match(err.message, /timeout for 99 ms/);
return true;
});
});
Expand Down

0 comments on commit d353b30

Please sign in to comment.