Commit Graph

398 Commits

Author SHA1 Message Date
Erik Dubbelboer bac672751b bug: AppendCert/AppendCertEmbed not thread-safe (#2251) (#2281) 2026-06-06 11:29:39 +02:00
Erik Dubbelboer 31f7138f8c bug: closeIdleConns TOCTOU race with serveConn idle timestamp (#2250) (#2280) 2026-06-06 11:29:29 +02:00
Erik Dubbelboer 21f66e0a2f bug: double concurrency counter increment in ServeConn causes counter leak (#2238) (#2275) 2026-06-06 11:26:22 +02:00
Y.Horie 46bfd1b9d8 fix(server): return ErrMissingFile when MultipartForm.File is nil (#2260)
RequestCtx.FormFile previously returned (nil, nil) when the parsed
multipart form had a nil File map: the code returned the err variable
from the preceding successful MultipartForm() call, which is always
nil at that point. Callers checking `if err != nil` proceeded with a
nil *multipart.FileHeader, causing nil pointer dereferences downstream.

Return ErrMissingFile instead, matching the existing semantics for the
missing-key case immediately below. Fixes #2235.

Signed-off-by: Y.Horie <u5.horie@gmail.com>
2026-06-02 19:35:29 +08:00
Erik Dubbelboer 97b38d3a48 server: document SaveMultipartFile path trust requirement 2026-04-27 07:36:20 +02:00
Yevgeniy Miretskiy 19e4b24955 feat: add ExpectHandler for richer Expect: 100-continue handling (#2175)
* feat: add ExpectHandler for richer Expect: 100-continue handling

ContinueHandler only returns a bool, limiting the server to either
accepting (100) or rejecting with 417. ExpectHandler allows returning
any HTTP status code, and closes the connection on rejection since
the client may have already started sending body data per RFC 9110.

ExpectHandler takes precedence when both handlers are set.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* refactor: use *RequestCtx in ExpectHandler for richer access

Allows callers to inspect RemoteAddr, TLS state, or any other
connection metadata alongside headers, addressing reviewer feedback.
Documents that the response must not be modified by the handler.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

* Update server.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

* Update server.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2026-04-27 05:58:04 +02:00
Erik Dubbelboer 3ac3b24635 server: keep hijacked reader out of pool (#2184)
When KeepHijackedConns is enabled, the hijacked connection may outlive the
HijackHandler. The wrapper continues reading through the buffered reader
after the handler returns, so returning that reader to the pool can let
another connection reset it while the hijacked connection is still in use.

Keep the buffered reader owned by the escaped hijacked connection in
keep-open mode. Add a regression test that forces reader-pool reuse
and verifies buffered data remains available after the handler returns.
2026-04-27 05:28:40 +02:00
Erik Dubbelboer 19b39dd08a Sanitize redirect Location header to prevent CRLF injection (#2186)
Route RequestCtx.Redirect Location updates through the canonical response
header setter so CR and LF bytes are normalized before serialization.

Add regression coverage for query-only and fragment-only redirects containing
CRLF, and verify the serialized response cannot emit an injected header line.
2026-04-27 05:28:30 +02:00
Erik Dubbelboer 1f00bc7c28 server: apply ReadTimeout before first byte with ReduceMemoryUsage (#2183)
On new connections with ReduceMemoryUsage enabled, serveConn could reach
acquireByteReader before installing a read deadline. That left the first
blocking read outside ReadTimeout and allowed silent clients to keep the
connection open until some external timeout closed it.

Apply ReadTimeout before the first read on a new connection, while keeping
the existing idle-timeout behavior for keep-alive requests. Add a regression
test that verifies the server closes a silent ReduceMemoryUsage connection
after the first-byte timeout.
2026-04-25 03:02:03 +02:00
Erik Dubbelboer d238e60fed Add ServeFileLiteral, ServeFSLiteral and SendFileLiteral (#2163)
ServeFile and ServeFS interpret the path as a URI, so percent-encoded
sequences are decoded and characters like '?' and '#' act as URI
delimiters. This makes it impossible to serve files whose names
contain those characters.

Changing this behavior would be backwards incompatible. So instead the
new ServeFileLiteral, ServeFSLiteral and SendFileLiteral are added.

The new Literal variants percent-encode the path before setting it as
the request URI, preserving every byte of the original filesystem path.

Thanks to @thesmartshadow for reporting this issue.
2026-03-23 03:21:36 +01:00
Erik Dubbelboer f0d5d9a5cb Add WithLimit methods for uncompression (#2147)
* Add WithLimit methods for uncompression

The current uncompress methods don't enforce a memory limit and are
susceptible to things like zip bombs. This pull introduces new methods
so retain backwards compatibility. The old methods might be deprecated
in the future.

* Fix suggestion
2026-02-22 18:13:40 +01:00
Erik Dubbelboer c2b317d47d Go 1.26 and golangci-lint updates (#2146)
Keep Go 1.24 compatibility for now (by not using `wg.Go()`).
2026-02-21 10:28:39 +01:00
dependabot[bot] fb6b6d160c chore(deps): bump securego/gosec from 2.22.10 to 2.22.11 (#2110)
* chore(deps): bump securego/gosec from 2.22.10 to 2.22.11

Bumps [securego/gosec](https://github.com/securego/gosec) from 2.22.10 to 2.22.11.
- [Release notes](https://github.com/securego/gosec/releases)
- [Commits](https://github.com/securego/gosec/compare/v2.22.10...v2.22.11)

---
updated-dependencies:
- dependency-name: securego/gosec
  dependency-version: 2.22.11
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Ignore invalid warnings

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2025-12-18 16:34:33 +01:00
RW 2a82a57b9d Expose header parsing error variables (#2096) 2025-11-12 02:31:36 +01:00
Erik Dubbelboer f18eb9ef0c Reject backslash absolute URIs and cache parse errors (#2075)
Keep our server behaviour the same as net/http.
2025-10-05 02:47:11 +02:00
Erik Dubbelboer 0ad54a45d9 Update lint and fix new lint errors 2025-09-28 02:59:00 +02:00
Erik Dubbelboer 4d25421ae5 Drop Go 1.23 support (#2065)
The Go team doesn't support this version anymore. This is required to
update our golang.org/x/... dependencies.
2025-09-10 22:16:01 +02:00
cui 28b7880b32 server: refactor to use atomic type (#2058) 2025-08-24 13:49:14 +02:00
shawn wang 9e457ebd98 mod acceptConn (#2005)
* add connKeepAliveer interface{}.
* use connKeepAliveer insteadof *net.TCPConn to set TCPKeepalive and TCPKeepalivePeriod
2025-05-07 14:14:06 +09:00
Max Denushev 41a1449627 feat: move user values to Request structure (#1999)
Co-authored-by: Max Denushev <denushev@tochka.com>
2025-05-03 15:10:15 +09:00
pj a05560dd7e implement early hints (#1996) 2025-04-21 13:01:35 +02:00
Erik Dubbelboer 752b0e7004 Remove idleConns mutex for every request (#1986)
Locking and unlocking a mutex multiple times per request is a major
slowdown that we can avoid with clever use of atomics.

Before:
```
BenchmarkServerGet100ReqPerConn10KClients-12    	 84167428	       867.7 ns/op
```
After:
```
BenchmarkServerGet100ReqPerConn10KClients-12    	187397954	       386.3 ns/op
```
2025-04-02 05:43:14 +02:00
Erik Dubbelboer 4891fc5304 Update golangci-lint to v2 (#1980) 2025-03-25 06:40:55 +01:00
Erik Dubbelboer 69dc7b1280 Update the supported version to the same as Go itself (#1967)
Some of our dependences require the supported versions of Go.

For example github.com/golang/crypto now requires 1.23 or higher.
See: https://github.com/golang/crypto/commit/89ff08d67c4d79f9ac619aaf1f7388888798651fa

For more information on the new policy of the Go team see: https://github.com/golang/go/issues/69095

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-02-26 04:39:17 +01:00
不插电 f203307887 fix: ShutdownWithContext and ctx.Done() exist race. (#1908)
* fix: ShutdownWithContext and ctx.Done() exist race.

* fix: Even if ln.Close() err, the Shutdown process should still proceed.

* refactor: remove END label.
2024-12-08 12:52:06 +01:00
Kashiwa 803c25e54c fix RequestCtx is canceled (#1879) (#1890)
Create done channel in fakeServer during the initialization of newRequestCtx to prevent the done channel from being nil
2024-10-27 20:15:57 +01:00
Kashiwa 5745601565 Represent stateName by []string instead of map (#1871) 2024-09-21 16:11:46 +02:00
Erik Dubbelboer c3050516d9 Fix lint and security issues
gosec was failing after the last update introduced some new checks.
2024-09-07 15:22:05 +02:00
newacorn d31f4ef7d5 When StreamRequestBody is set to true, we cannot safely release br. (#1844)
For example, when using chunked encoding, it's possible that `br` has only read the request headers.
2024-08-31 14:41:54 +02:00
newacorn 43c7b83ee5 We cannot avoid continuing to call SetReadDeadline and similar methods after the connection is closed. (#1835)
In the current implementation, there are several places where it's assumed that SetReadDeadline and similar functions logically won't affect a closed connection. However, these assumptions may not hold in certain specific situations. Therefore, instead of asserting that such errors will never occur, simply return the errors encountered during the execution of these methods.
2024-08-21 22:42:09 +02:00
Juan Calderon-Perez 1fb3453165 Use Named Fields and Align Structures to Reduce Memory Usage (#1814)
* Use Named fields and Align Structures to Reduce Memory Usage

* Remove extra spaces
2024-08-02 22:26:52 +02:00
byte0o a7d488a91c Fix possible race condition on request ctx done #1662 (#1806)
* Fix possible race condition on request ctx done #1662

* Fix possible race condition on request ctx done #1662

* Fix Comment

* fix remove the use of lock

* fix remove Comment
2024-07-23 21:15:57 +02:00
Oleksandr Redko e88bd48f45 refactor: do not return error as it is always nil (#1759) 2024-04-21 19:57:04 +02:00
Oleksandr Redko bbc7bd04e2 refactor: rename error local variables (#1738) 2024-03-25 14:30:27 +01:00
Oleksandr Redko 3166afd835 Enable few gocritic checks; fix up issues (#1728) 2024-03-02 16:19:05 +01:00
Co1a 5f81476d7c feat:support zstd compress and uncompressed (#1701)
* feat:support zstd compress and uncompressed

* fix:real & stackless write using different pool to avoid get stackless.writer

* fix:zstd normalize compress level

* Change empty string checks to be more idiomatic (#1684)

* chore:lint fix and rebase with master

* chore:remove 1.18 test & upgrade compress version

* fix:error default compress level

* Fix lint

---------

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2024-02-21 07:21:52 +01:00
mopeneko b430b88e78 Implement GetRejectedConnectionsCount function (#1704)
* Implement `GetRejectedConnectionsCount`

* Implement test for `GetRejectedConnectionsCount`
2024-02-10 10:26:36 +01:00
Oleksandr Redko 28615eba55 Change empty string checks to be more idiomatic (#1684) 2024-01-04 15:05:38 +01:00
Oleksandr Redko 9d6b470260 chore: Add missing dots at the end of comments (#1677) 2023-12-13 13:56:24 +08:00
Oleksandr Redko 12949de784 chore: Set max line length to 130 characters (#1676) 2023-12-05 19:49:41 +01:00
Jille Timmermans 9b4e42affa bug: Flush the write buffer before putting it to the pool (#1672)
A few lines later we check if `s.ReduceMemoryUsage && hijackHandler == nil`
and call releaseWriter. We need to flush the buffer before returning it
to the pool to avoid the data getting lost.
2023-12-02 18:04:12 +01:00
Oleksandr Redko f196617f55 chore: Use 'any' instead of 'interface{}' (#1666)
gofmt -w -r "interface{} -> any" -l .
2023-11-24 11:33:04 +01:00
Oleksandr Redko d3397c64ed Enable wastedassign, whitespace linters; fix issues (#1665) 2023-11-24 11:32:11 +01:00
Alexander Kolesov c6a17b9a57 Don't put hijackConn structure back to the pool on Close if KeepHijackedConns is enabled. (#1629) 2023-10-06 21:34:33 +02:00
Oleksandr Redko 4ec5c5a774 docs: fix typos in comments and tests 2023-08-30 14:13:13 +02:00
Oleksandr Redko 9aa666e814 Enable gocritic linter; fix lint issues (#1612) 2023-08-29 20:08:37 +02:00
Zhang Xiaopei 6eb224954a fix:fasthttp server with tlsConfig (#1595)
* fix:fasthttp server with tlsConfig

Signed-off-by: zhangweiyu <zhangweiyu2@huawei.com>

* fix:fasthttp server with tlsConfig

Signed-off-by: zhangweiyu <zhangweiyu2@huawei.com>

* Update server.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

* fix:fasthttp server with tlsConfig

Signed-off-by: zhangweiyu <zhangweiyu2@huawei.com>

* fix:fasthttp server with tlsConfig

Signed-off-by: zhangweiyu <zhangweiyu2@huawei.com>

---------

Signed-off-by: zhangweiyu <zhangweiyu2@huawei.com>
Co-authored-by: zhangweiyu <zhangweiyu2@huawei.com>
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2023-07-18 13:00:15 +02:00
Oleksandr Redko d229959216 Remove unnecessary indent blocks (#1586) 2023-06-26 14:13:16 +02:00
Oleksandr Redko ffdf59d04c Enable gofumpt linter; format code gofumpt -w . (#1576) 2023-06-13 15:18:59 +02:00
Moritz Poldrack d0f2727a4d get rid of some panics (#1526)
* client: simplify (*HostClient).do()

Remove an allocation in favour of deferring a call to release the
response.

* client: remove panic in dialAddr

Return an error instead of panicking if the user supplied a nonsensical
DialFunc.

* compression: remove panic on invalid compression level

If a compression level exceeding gzip's boundaries is provided, fasthttp
will panic. Instead it would be better to handle this error for them by
limiting it to the minimum or maximum value, depending on the direction
the user has exceeded the limits.

Clamp the value of gzip to always be between gzip.BestSpeed and
gzip.BestCompression.

* peripconn: remove panic on negative connection count

When a negative count is reached when unregistering a connection, a
panic is caused even though data-integrity is not at risk.

Replace the panic() with a simple clamp on the value to ensure the
value does not exceed it's expected lower bounds.

References: #1504

* compress: remove error on failed nonblocking writes

Since there is no way of handling or even logging non-critical errors in
stateless non-blocking writecalls, just drop them and hope the user
notices and tries again.

* workerPool: remove panic on redundant Start and Stop calls

Instead of panicking for invalid behaviour, it's preferable to just turn
the function into a noop.

* http: remove panic on invalid form boundary

* http: remove panic on negative reads

Since bufio already panics on negative reads, it is not necessary to do
so as well. If the length is zero and for some reason no error is
returned, readBodyIdentity and appendBodyFixedSize now errors in these
cases.

Link: https://github.com/golang/go/blob/851f6fd61425c810959c7ab51e6dc86f8a63c970/src/bufio/bufio.go#L246

* fs: remove panic on negative reader count

When a negative count is reached when unregistering a reader, a panic is
thrown even though data-integrity is not at risk.

Replace the panic() with a simple clamp on the value to ensure the
value does not exceed it's expected lower bounds.

* server: remove panic in favour of a segfault

Panicking with "BUG: " obscures the error. As the segfault causes a
panic anyway, just let the chaos unfold.

* server: remove panic in favour of returning an error

Writing on a timed-out response is not endangering data integrity and
just fails.

* chore: add comments to all panics

* chore: fix minor typo
2023-03-30 03:38:28 +02:00