-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat: add 400 response for broken client request to instead of empty response #1829
Conversation
lib/application.js
Outdated
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>' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还是这么顽固。。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接放到 400.html 然后 readFile 吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atian25 我没问题,看大家的一个决定。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段是没办法走到 egg-onerror 那边的处理逻辑的是么?
There was a problem hiding this comment.
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。
lib/application.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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 和端口
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
方便排查问题
There was a problem hiding this comment.
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.
test/lib/cluster/app_worker.test.js
Outdated
@@ -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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
连续发起请求2次,验证多次 clientError 也能够正确处理。
test/lib/cluster/app_worker.test.js
Outdated
@@ -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* () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改为 async 吧
Codecov Report
@@ Coverage Diff @@
## master #1829 +/- ##
==========================================
+ Coverage 99.58% 99.59% +<.01%
==========================================
Files 29 29
Lines 729 735 +6
==========================================
+ Hits 726 732 +6
Misses 3 3
Continue to review full report at Codecov.
|
lib/application.js
Outdated
@@ -66,6 +93,8 @@ class Application extends EggApplication { | |||
this.coreLogger.error(err); | |||
}, | |||
}); | |||
|
|||
server.on('clientError', this[ON_CLIENT_ERROR].bind(this)); |
There was a problem hiding this comment.
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))
统一风格
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外我建议这里 onClientError 的 handler 不要用 symbol,给使用者一个覆盖的机会
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有需要覆盖的需求再加吧,感觉这是一个兜底的,应该很少会自定义吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dead-horse 你指的是使用者在自己应用中继承这个类然后重载 onClientError
函数吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atian25 如果我是使用者,我觉得我会想去自定义
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这种路径不是正常用户能触发的吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atian25 这只是一种情况,clientError
可不止这一种情况。举个最简单的例子,用户的请求被运营商劫持了,然后运营商代码有误,把请求包改错了。
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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.
@XadillaX pick 到 1.x 分支吧 |
feat: add 400 response for broken client request to instead of empty response (#1829)
@dead-horse 我来操作吗? |
嗯 |
强如死月! |
Checklist
npm test
passesAffected 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.