MagicStack / httptools

Fast HTTP parser
MIT License
1.21k stars 83 forks source link

How to do an early stop? #87

Open Kludex opened 2 years ago

Kludex commented 2 years ago

Hi there :wave:

I'm trying to implement a flag --limit-request-header-count on uvicorn, which I implemented changing the value of an attribute too_many_headers and raising an exception from on_header callback, see https://github.com/encode/uvicorn/pull/1683/files.

My question is: is my implementation as intended? Should the HttpParserCallbackError send more information when an exception happens from one of the callbacks?

Kludex commented 2 years ago

It looks like checking the count doesn't make much sense if we can limit the size of the headers. How can I do it for httptools? Should it be on httptools itself?

Kludex commented 2 years ago

Ok. Now I understand more. What I did doesn't make any sense.

httptools does something cool here: https://github.com/MagicStack/httptools/blob/e2d1a464d113418be26e0b2f36945f032c92a017/httptools/parser/parser.pyx#L166-L183

Still, I cannot do anything on uvicorn to limit the size of the headers/body, because I need to do it before it's parsed.

Ref.: https://github.com/nodejs/node/tree/fd36a8dadba7fcbd74f0ab82a8ee1b3150ce5167/deps/llhttp/src (this is for myself)