Commit 35a1d58
committed
gh-150499: http.server: drain unread request body on persistent connections
Follow-up to gh-150499 (paired with #150500 which lands the §3.3.3
rules). Wrap rfile with a small byte-counting reader for the duration
of the request and, after the handler returns, drain any unread
declared body up to a 1 MiB cap (or close the connection if the
remainder is larger). Without this, the next iteration of the
keep-alive loop parses the leftover body as a request line, per RFC
7230 section 6.3.
The wrapper proxies read, read1, readline, readinto, and readinto1 and
falls back to __getattr__ for everything else, so existing handlers
(including the ServerHandler chain in wsgiref.simple_server, which
passes self.rfile directly to WSGI applications) continue to work
unchanged.
Add RFC7230BodyDrainTestCase asserting that the leftover body is not
parsed as the next request line on a persistent connection.
Signed-off-by: tonghuaroot <tonghuaroot@gmail.com>1 parent 776573c commit 35a1d58
3 files changed
Lines changed: 144 additions & 1 deletion
File tree
- Lib
- http
- test
- Misc/NEWS.d/next/Library
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
173 | 213 | | |
174 | 214 | | |
175 | 215 | | |
| |||
426 | 466 | | |
427 | 467 | | |
428 | 468 | | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
429 | 474 | | |
430 | 475 | | |
431 | 476 | | |
| |||
434 | 479 | | |
435 | 480 | | |
436 | 481 | | |
| 482 | + | |
| 483 | + | |
437 | 484 | | |
438 | | - | |
| 485 | + | |
439 | 486 | | |
440 | 487 | | |
441 | 488 | | |
| |||
445 | 492 | | |
446 | 493 | | |
447 | 494 | | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
448 | 498 | | |
449 | 499 | | |
450 | 500 | | |
| 501 | + | |
451 | 502 | | |
452 | 503 | | |
453 | 504 | | |
| |||
457 | 508 | | |
458 | 509 | | |
459 | 510 | | |
| 511 | + | |
460 | 512 | | |
461 | 513 | | |
462 | 514 | | |
463 | 515 | | |
464 | 516 | | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
465 | 545 | | |
466 | 546 | | |
467 | 547 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
367 | 425 | | |
368 | 426 | | |
369 | 427 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments