Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 400 response for broken client request to instead of empty response #1829

Merged
merged 6 commits into from
Dec 15, 2017

Conversation

XadillaX
Copy link
Member

@XadillaX XadillaX commented Dec 14, 2017

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

application

Description of change

Egg.js will do no response while client error. This PR makes server response a 400 Bad Request instead.

@XadillaX XadillaX changed the title feat: add 400 response for broken client request to instead of empty … feat: add 400 response for broken client request to instead of empty response Dec 14, 2017
const WARN_CONFUSED_CONFIG = Symbol('Application#warnConfusedConfig');
const EGG_LOADER = Symbol.for('egg#loader');
const EGG_PATH = Symbol.for('egg#eggPath');
const CLUSTER_CLIENTS = Symbol.for('egg#clusterClients');

// client error => 400 Bad Request
// Refs: https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_event_clienterror
const DEFAULT_BAD_REQUEST_HTML = '<html>' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是这么顽固。。。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接放到 400.html 然后 readFile 吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atian25 我没问题,看大家的一个决定。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段是没办法走到 egg-onerror 那边的处理逻辑的是么?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段是没办法走到 egg-onerror 那边的处理逻辑的是么?

语义上走不了。请求刚到,数据包还没解析完就要返回了,这个时候任何 resp 之类的 Wrapper 还没包上去,拿到的就是一个 raw 的 Socket 对象,你输出内容必须要输出一个 raw Response Packet。

@@ -55,6 +70,14 @@ class Application extends EggApplication {
return path.join(__dirname, '..');
}

[ON_CLIENT_ERROR](err, socket) {
this.logger.warn('A client error [%s] occurred: %s', err.code, err.message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'[clientError] A client error [%s] occurred: %s'

然后记录下请求的 ip 和端口

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方便排查问题

Copy link
Member Author

@XadillaX XadillaX Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提了个 PR:nodejs/node#17672,不知道人家收不收。

如果收了的话,这个 logger 还能顺便将错误的 Packet Buffer 给输出来。


If this PR can be merged, we can output the raw packet buffer via logger.

@@ -19,6 +19,41 @@ describe('test/lib/cluster/app_worker.test.js', () => {
.expect('true');
});

it('should response 400 bad request when HTTP request packet broken', () => {
const test = app.httpRequest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

连续发起请求2次,验证多次 clientError 也能够正确处理。

@@ -19,6 +19,50 @@ describe('test/lib/cluster/app_worker.test.js', () => {
.expect('true');
});

it('should response 400 bad request when HTTP request packet broken', function* () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改为 async 吧

@codecov-io
Copy link

codecov-io commented Dec 14, 2017

Codecov Report

Merging #1829 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1829      +/-   ##
==========================================
+ Coverage   99.58%   99.59%   +<.01%     
==========================================
  Files          29       29              
  Lines         729      735       +6     
==========================================
+ Hits          726      732       +6     
  Misses          3        3
Impacted Files Coverage Δ
lib/application.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0ee9f2...9362034. Read the comment docs.

@@ -66,6 +93,8 @@ class Application extends EggApplication {
this.coreLogger.error(err);
},
});

server.on('clientError', this[ON_CLIENT_ERROR].bind(this));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

server.on('clientError', err => this[ON_CLIENT_ERROR](err)) 统一风格

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外我建议这里 onClientError 的 handler 不要用 symbol,给使用者一个覆盖的机会

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有需要覆盖的需求再加吧,感觉这是一个兜底的,应该很少会自定义吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dead-horse 你指的是使用者在自己应用中继承这个类然后重载 onClientError 函数吗?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atian25 如果我是使用者,我觉得我会想去自定义

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种路径不是正常用户能触发的吧

Copy link
Member Author

@XadillaX XadillaX Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atian25 这只是一种情况,clientError 可不止这一种情况。举个最简单的例子,用户的请求被运营商劫持了,然后运营商代码有误,把请求包改错了。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用者在自己应用中继承这个类然后重载 onClientError 函数吗

使用者直接在 extends/app.js 中就可以覆盖默认的 onClientError 实现了

@@ -55,6 +69,18 @@ class Application extends EggApplication {
return path.join(__dirname, '..');
}

onClientError(err, socket) {
this.logger.error('A client (%s:%d) error [%s] occurred: %s',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提了个 PR:nodejs/node#17672

不知道人家收不收。

如果收了的话,这个 logger 还能顺便将错误的 Packet Buffer 给输出来。


If this PR can be merged, we can output the raw packet buffer via logger.

@dead-horse dead-horse merged commit 40df153 into eggjs:master Dec 15, 2017
@dead-horse
Copy link
Member

@XadillaX pick 到 1.x 分支吧

popomore pushed a commit that referenced this pull request Dec 15, 2017
feat: add 400 response for broken client request to instead of empty response (#1829)
@XadillaX
Copy link
Member Author

@dead-horse 我来操作吗?

@atian25
Copy link
Member

atian25 commented Dec 15, 2017

@fengmk2
Copy link
Member

fengmk2 commented Oct 14, 2023

强如死月!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants