Enforce `FormParser` limits in parser callbacks

Merged
KL
6mo ago
+39-11

Follow-up to #3329. The max_fields and max_part_size checks ran while draining buffered messages, after QuerystringParser.write() had already tokenized the whole chunk. When a application/x-www-form-urlencoded body arrives as a single chunk (e.g. via the test client, or after request.body() has cached it), the parser tokenizes and buffers every field before the drain loop reaches the limit check.

This moves the count and size checks into the on_field_* callbacks, which run during write(), so parsing stops as soon as a limit is crossed regardless of how the body is chunked. Behaviour and error messages are unchanged; only the point at which parsing aborts moves earlier.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.