Repository navigation
Conversation
Move the request_body_timeout deadlines and the 413 on MaxBytesError out of go_read_post() into an io.Reader, so other readers of the body can share them.
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 12, 2026
Allocate the textproto.Reader that reads part headers once per Reader instead of once per part. Parts are read sequentially from the same bufio.Reader, and readMIMEHeader copies the keys and values it returns. Measured with the BenchmarkNextPart added by CL "mime/multipart: don't allocate a discarded map". Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadForm/fields/maxMemory=0-15 5.952µ ± 6% 6.130µ ± 4% ~ (p=0.190 n=10) NextPart-15 30.04µ ± 4% 28.45µ ± 1% -5.27% (p=0.000 n=10) geomean 16.75µ 16.99µ +1.49% │ old │ new │ │ B/op │ B/op vs base │ ReadForm/fields/maxMemory=0-15 21.54Ki ± 0% 21.12Ki ± 0% -1.98% (p=0.000 n=10) NextPart-15 71.09Ki ± 0% 66.42Ki ± 0% -6.56% (p=0.000 n=10) geomean 34.83Ki 33.92Ki -2.63% │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 164.0 ± 0% -5.20% (p=0.000 n=10) NextPart-15 807.0 ± 0% 708.0 ± 0% -12.27% (p=0.000 n=10) geomean 258.4 242.2 -6.25%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 12, 2026
newPart allocated an empty header map, then populateHeaders replaced it with the map returned by readMIMEHeader. A part is discarded when populateHeaders fails, so the first map was never used. Add BenchmarkNextPart, which reads 100 small parts. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadForm/fields/maxMemory=0-15 5.952µ ± 6% 6.053µ ± 2% ~ (p=0.529 n=10) NextPart-15 30.04µ ± 4% 29.74µ ± 4% ~ (p=0.218 n=10) geomean 16.75µ 16.96µ +1.28% │ old │ new │ │ B/op │ B/op vs base │ ReadForm/fields/maxMemory=0-15 21.54Ki ± 0% 21.07Ki ± 0% -2.20% (p=0.000 n=10) NextPart-15 71.09Ki ± 0% 66.37Ki ± 0% -6.63% (p=0.000 n=10) geomean 34.83Ki 33.87Ki -2.78% │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 163.0 ± 0% -5.78% (p=0.000 n=10) NextPart-15 807.0 ± 0% 707.0 ± 0% -12.39% (p=0.000 n=10) geomean 258.4 241.1 -6.68%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 12, 2026
consumeValue built every quoted-string value in a strings.Builder. When the value contains no backslash, it is a substring of the input: return it without allocating. The Content-Disposition header of multipart/form-data parts carries such values. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ParseMediaType-15 19.08µ ± 5% 17.00µ ± 1% -10.90% (p=0.000 n=10) │ old │ new │ │ B/op │ B/op vs base │ ParseMediaType-15 39.50Ki ± 0% 38.21Ki ± 0% -3.25% (p=0.000 n=10) │ old │ new │ │ allocs/op │ allocs/op vs base │ ParseMediaType-15 444.0 ± 0% 354.0 ± 0% -20.27% (p=0.000 n=10) The allocations of mime/multipart's ReadForm drop as well: │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 163.0 ± 0% -5.78% (p=0.000 n=10)
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 12, 2026
Store the textproto.Reader that reads part headers in the Reader instead of allocating one per part. readHeader clears it before returning, even on panic, so that the buffer of a large folded header can still be collected. Measured with the BenchmarkNextPart added by CL "mime/multipart: don't allocate a discarded map". Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadForm/fields/maxMemory=0-15 6.561µ ± 10% 6.170µ ± 6% -5.96% (p=0.019 n=10) NextPart-15 31.20µ ± 4% 30.46µ ± 18% ~ (p=0.353 n=10) geomean 14.31µ 13.71µ -4.18% │ old │ new │ │ B/op │ B/op vs base │ ReadForm/fields/maxMemory=0-15 21.54Ki ± 0% 21.10Ki ± 0% -2.04% (p=0.000 n=10) NextPart-15 71.09Ki ± 0% 66.40Ki ± 0% -6.59% (p=0.000 n=10) geomean 39.13Ki 37.43Ki -4.34% │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 163.0 ± 0% -5.78% (p=0.000 n=10) NextPart-15 807.0 ± 0% 707.0 ± 0% -12.39% (p=0.000 n=10) geomean 373.6 339.5 -9.15%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 12, 2026
newPart allocated an empty header map, then populateHeaders replaced it with the map returned by readMIMEHeader. A part is discarded when populateHeaders fails, so the first map was never used. Add BenchmarkNextPart, which reads 100 small parts. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadForm/fields/maxMemory=0-15 5.952µ ± 6% 6.053µ ± 2% ~ (p=0.529 n=10) NextPart-15 30.04µ ± 4% 29.74µ ± 4% ~ (p=0.218 n=10) geomean 13.37µ 13.42µ +0.33% │ old │ new │ │ B/op │ B/op vs base │ ReadForm/fields/maxMemory=0-15 21.54Ki ± 0% 21.07Ki ± 0% -2.20% (p=0.000 n=10) NextPart-15 71.09Ki ± 0% 66.37Ki ± 0% -6.63% (p=0.000 n=10) geomean 39.13Ki 37.40Ki -4.44% │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 163.0 ± 0% -5.78% (p=0.000 n=10) NextPart-15 807.0 ± 0% 707.0 ± 0% -12.39% (p=0.000 n=10) geomean 373.6 339.5 -9.15%
This was referenced Nov 12, 2026
dunglas
force-pushed
the
feat/go-form-parser
branch
2 times, most recently
from
November 13, 2026 13:19
1cabbc0 to
9a7c14f
Compare
The experimental go_form_parser option parses multipart/form-data and application/x-www-form-urlencoded bodies, including request_parse_body(), with memory-safe Go parsers instead of PHP's C ones. Where PHP warns and runs the script with partial data, the request is rejected: 413 for post_max_size, max_input_vars, max_file_uploads and max_multipart_body_parts, 400 for bodies breaking RFC 7578. request_parse_body() throws instead. session.upload_progress isn't supported, by design.
Reuse the entries, names and values of parsed bodies across requests, and build the C block from uploads and entries without concatenating them.
dunglas
force-pushed
the
feat/go-form-parser
branch
from
November 13, 2026 14:17
622e5be to
7581708
Compare
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 13, 2026
Store the textproto.Reader that reads part headers in the Reader instead of allocating one per part. readHeader clears it before returning, even on panic, so that the buffer of a large folded header can still be collected. Add BenchmarkNextPart, which reads 100 small parts. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadForm/fields/maxMemory=0-15 6.561µ ± 10% 6.170µ ± 6% -5.96% (p=0.019 n=10) NextPart-15 31.20µ ± 4% 30.46µ ± 18% ~ (p=0.353 n=10) geomean 14.31µ 13.71µ -4.18% │ old │ new │ │ B/op │ B/op vs base │ ReadForm/fields/maxMemory=0-15 21.54Ki ± 0% 21.10Ki ± 0% -2.04% (p=0.000 n=10) NextPart-15 71.09Ki ± 0% 66.40Ki ± 0% -6.59% (p=0.000 n=10) geomean 39.13Ki 37.43Ki -4.34% │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 163.0 ± 0% -5.78% (p=0.000 n=10) NextPart-15 807.0 ± 0% 707.0 ± 0% -12.39% (p=0.000 n=10) geomean 373.6 339.5 -9.15%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 15, 2026
When the buffered data holds no full boundary, scanUntilBoundary looks for the last newline character, to check whether the data ends with the start of a boundary. bytes.LastIndexByte scans backward one byte at a time. In "\r\n" mode, a body without '\r', such as a text file with Unix line endings, makes it scan the whole buffer for every 4 KiB read. Only the last len(nlDashBoundary) bytes can hold a partial boundary, since bytes.Index would have found a full one: search there. Add BenchmarkPartRead, which reads a part of 1 MiB. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ PartRead/text-15 290.09µ ± 3% 37.73µ ± 4% -86.99% (p=0.000 n=12) PartRead/binary-15 62.14µ ± 1% 61.52µ ± 1% ~ (p=0.219 n=12) geomean 134.3µ 48.18µ -64.12% │ old │ new │ │ B/s │ B/s vs base │ PartRead/text-15 3.366Gi ± 3% 25.885Gi ± 4% +668.92% (p=0.000 n=12) PartRead/binary-15 15.72Gi ± 1% 15.87Gi ± 1% ~ (p=0.219 n=12) geomean 7.273Gi 20.27Gi +178.68%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 15, 2026
validHeaderValueByte runs on every byte of every header value. It tests the byte against a 128-bit bitmap, which takes two variable shifts. The only rejected bytes are the control characters other than HTAB, and DEL: compare against them directly. CL 410714 added this bitmap along with the one in validHeaderFieldByte, whose set of allowed bytes is irregular. The valid value bytes are HTAB and everything from SP up except DEL, so the comparison is shorter on every architecture: 9 instructions instead of 18 on amd64, 12 instead of 20 on arm64, and about half as many on riscv64, ppc64le and 386. TestReadMIMEHeaderBytes already checks all 256 byte values. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: net/textproto cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadMIMEHeader/client_headers-15 1110.5n ± 3% 957.1n ± 3% -13.81% (p=0.000 n=12) ReadMIMEHeader/server_headers-15 781.2n ± 1% 687.9n ± 3% -11.94% (p=0.000 n=12) Uncommon-15 160.0n ± 5% 160.3n ± 6% ~ (p=0.966 n=12) geomean 517.8n 472.6n -8.73% pkg: net/http cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadRequestChrome-15 1164.0n ± 5% 971.9n ± 11% -16.50% (p=0.000 n=10) ReadRequestCurl-15 488.1n ± 6% 464.8n ± 19% ~ (p=0.218 n=10) ReadRequestApachebench-15 487.1n ± 8% 464.9n ± 8% ~ (p=0.052 n=10) ReadRequestSiege-15 658.8n ± 3% 618.8n ± 3% -6.07% (p=0.000 n=10) ReadRequestWrk-15 348.7n ± 5% 343.8n ± 2% ~ (p=0.280 n=10) geomean 576.3n 537.0n -6.81%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 15, 2026
validHeaderValueByte tests each byte of a header value against a 128-bit bitmap, which https://go.dev/cl/410714 added along with the one in validHeaderFieldByte. Unlike valid name bytes, valid value bytes form simple ranges: HTAB, and SP and above except DEL. Compare against them directly, which takes about half as many instructions on amd64, arm64, riscv64, ppc64le and 386. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: net/textproto cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadMIMEHeader/client_headers-15 1110.5n ± 3% 957.1n ± 3% -13.81% (p=0.000 n=12) ReadMIMEHeader/server_headers-15 781.2n ± 1% 687.9n ± 3% -11.94% (p=0.000 n=12) Uncommon-15 160.0n ± 5% 160.3n ± 6% ~ (p=0.966 n=12) geomean 517.8n 472.6n -8.73% pkg: net/http cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadRequestChrome-15 1164.0n ± 5% 971.9n ± 11% -16.50% (p=0.000 n=10) ReadRequestCurl-15 488.1n ± 6% 464.8n ± 19% ~ (p=0.218 n=10) ReadRequestApachebench-15 487.1n ± 8% 464.9n ± 8% ~ (p=0.052 n=10) ReadRequestSiege-15 658.8n ± 3% 618.8n ± 3% -6.07% (p=0.000 n=10) ReadRequestWrk-15 348.7n ± 5% 343.8n ± 2% ~ (p=0.280 n=10) geomean 576.3n 537.0n -6.81%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 15, 2026
ReadForm copies each part with io.CopyN into a new bytes.Buffer, which allocates a LimitedReader and the buffer. Values also allocate at least 512 bytes of buffer, then copy them into a string. Reuse one LimitedReader for all parts and one buffer for all values. File parts keep their own buffer, since FileHeader holds on to it. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ ReadForm/fields/maxMemory=0-15 5.965µ ± 2% 5.380µ ± 3% -9.81% (p=0.000 n=15) ReadForm/fields/maxMemory=1048576-15 5.988µ ± 2% 5.322µ ± 4% -11.12% (p=0.000 n=15) ReadForm/files/maxMemory=0-15 148.9µ ± 2% 149.2µ ± 3% ~ (p=0.744 n=15) ReadForm/files/maxMemory=1048576-15 8.212µ ± 2% 8.140µ ± 3% ~ (p=0.367 n=15) geomean 14.46µ 13.65µ -5.55% │ old │ new │ │ B/op │ B/op vs base │ ReadForm/fields/maxMemory=0-15 21.54Ki ± 0% 16.33Ki ± 0% -24.17% (p=0.000 n=15) ReadForm/fields/maxMemory=1048576-15 21.54Ki ± 0% 16.33Ki ± 0% -24.18% (p=0.000 n=15) ReadForm/files/maxMemory=0-15 67.50Ki ± 0% 66.80Ki ± 0% -1.03% (p=0.000 n=15) ReadForm/files/maxMemory=1048576-15 23.03Ki ± 0% 22.35Ki ± 0% -2.96% (p=0.000 n=15) geomean 29.14Ki 25.12Ki -13.80% │ old │ new │ │ allocs/op │ allocs/op vs base │ ReadForm/fields/maxMemory=0-15 173.0 ± 0% 145.0 ± 0% -16.18% (p=0.000 n=15) ReadForm/fields/maxMemory=1048576-15 173.0 ± 0% 145.0 ± 0% -16.18% (p=0.000 n=15) ReadForm/files/maxMemory=0-15 247.0 ± 0% 228.0 ± 0% -7.69% (p=0.000 n=15) ReadForm/files/maxMemory=1048576-15 193.0 ± 0% 174.0 ± 0% -9.84% (p=0.000 n=15) geomean 194.4 169.9 -12.56%
dunglas
added a commit
to dunglas/go
that referenced
this pull request
Nov 15, 2026
When the buffer holds no full boundary, scanUntilBoundary searches backward for the last newline character, to check whether the buffer ends with the start of a boundary. With CRLF line endings, a body without '\r', such as a text file with Unix line endings, makes it scan the whole 4 KiB buffer, byte by byte, on every read. A partial boundary can only start in the last len(nlDashBoundary) bytes, so search there only. Add BenchmarkPartRead, which reads a 1 MiB part. Optimization found while working on php/frankenphp#2835. goos: darwin goarch: arm64 pkg: mime/multipart cpu: Apple M5 Pro │ old │ new │ │ sec/op │ sec/op vs base │ PartRead/text-15 290.09µ ± 3% 37.73µ ± 4% -86.99% (p=0.000 n=12) PartRead/binary-15 62.14µ ± 1% 61.52µ ± 1% ~ (p=0.219 n=12) geomean 134.3µ 48.18µ -64.12% │ old │ new │ │ B/s │ B/s vs base │ PartRead/text-15 3.366Gi ± 3% 25.885Gi ± 4% +668.92% (p=0.000 n=12) PartRead/binary-15 15.72Gi ± 1% 15.87Gi ± 1% ~ (p=0.219 n=12) geomean 7.273Gi 20.27Gi +178.68%
This was referenced Nov 15, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an experimental
go_form_parseroption that parsesmultipart/form-dataandapplication/x-www-form-urlencodedrequest bodies, includingrequest_parse_body(), with Go instead of PHP's C parsers (main/rfc1867.c,add_post_vars()).{ frankenphp { go_form_parser } }Why
PHP's body parsers have a long CVE history, and FrankenPHP is directly exposed:
Content-Lengthslips pastpost_max_size, unlimited headers per part, empty file inputs not counted inmax_file_uploads. The advisory names FrankenPHP as likely susceptible.multipart_buffer_headers[in names corrupts$_FILESWith the Go parser, Go's memory-safe
mime/multipartparses the body, every part counts againstmax_multipart_body_parts, the body is capped atpost_max_size, and the upload progress callback never runs. Upgrading PHP stays the only complete fix for PHP2Shell. Names still go throughphp_register_variable_ex(), shared with$_GETand$_COOKIE.How
malloc'd block that C registers and frees: no Go frame is on the stack when PHP bails out.$_POST,$_FILES,UPLOAD_ERR_*, input filters,is_uploaded_file()andmove_uploaded_file()behave as with PHP's parsers. A differential test compares both parsers, in module and worker mode.post_max_size,max_input_vars,max_file_uploadsandmax_multipart_body_parts, 400 for bodies breaking RFC 7578.request_parse_body()throws instead.session.upload_progressisn't supported, by design: it blocks a PHP thread for the whole upload. The docs point toXMLHttpRequestupload progress events (fetch()has none) and to resumable uploads (tus, IETF draft), which dunglas/caddy-tus brings to Caddy and FrankenPHP.Benchmarks
In-process (
BenchmarkFormParser, worker mode, Apple M5 Pro, 6 runs, benchstat):Symfony 8.1 (worker mode,
prod, Docker, 10 CPUs,ohawith 32 connections for 20 s, req/s):Uploads are written in 64 KiB chunks and urlencoded bodies are decoded in place with no Go allocation. Multipart bodies with many fields stay slower:
mime/multipartandmime.ParseMediaTypeallocate two maps per part, which drives the GC under load. That's why the option is opt-in. Changes in review in Go reduce these allocations: CL 837185, CL 862941 and CL 862984.As an experiment, the
exp/custom-multipartbranch forks themime/multipartreader to parse parts without allocating: 3 allocations instead of 2,409 for 200 fields. In the Symfony benchmark, 200 multipart fields go from -36% to +48% compared to PHP, the other requests don't change. A differential fuzz test checks the fork againstmime/multipart. It isn't part of this PR because it means maintaining a copy of the standard library. I proposed golang/go#83051 to bring these gains directly in Go stdlib.Symfony and WordPress apps (forms, uploads, login, comments, REST media uploads) return the same results with both parsers.