Commit Graph

192 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
Erik Dubbelboer f36c900902 header: match net/http CL+TE handling (#2190)
Match net/http behavior when requests or responses contain both
Content-Length and Transfer-Encoding.

Parse and validate Content-Length even when Transfer-Encoding is present, so
invalid lengths are rejected. For valid Content-Length with chunked
Transfer-Encoding, keep chunked framing as authoritative. Also apply the same
precedence when RequestHeader.DisableSpecialHeader is used.
2026-04-28 04:33:01 +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 eb38f5fc14 HTTP1/1 requires exactly one Host header (#2164)
Bring our parser more in line with net/http.
2026-03-28 03:10:37 +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
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 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
Erik Dubbelboer f9d84d7c52 Rewrite header parsing to improve spec compliance (#2030)
This change updates header parsing to match the behavior of net/http more closely.

**Breaking change**: headers delimited by `\n` (instead of `\r\n`) are no longer supported.

Previously, fasthttp accepted `\n` as a delimiter, which is not spec compliant.
This made it difficult to correctly parse headers containing both `\n` and `\r\n`.
2025-08-12 15:49:35 +02: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 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
Kashiwa 086a114445 fix: accept invalid headers with a space (#1953)
* fix: accept invalid headers with a space #1917

Make behavior consistent with net/http by allowing header keys and trailers containing spaces without canonicalizing them

* fix: lint paramTypeCombine

* fix: https://github.com/valyala/fasthttp/pull/1953#issuecomment-2660691298

* fix: golangci-lint nestingReduce
2025-02-19 19:49:48 +09: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
newacorn 3aa972e2fc Fix issues with tests interfering with each other in certain situations. (#1842)
In some cases, the goroutines started by one test do not terminate smoothly before the next round of tests begins, causing interference between tests.

Performance Impact: This results in test completion times not increasing linearly with the count value.

Correctness Impact: It affects the accuracy of memory allocation test cases.
2024-08-31 14:27:05 +02:00
newacorn c15489d7f5 Make the tests complete earlier. (#1848)
Reduce the test duration to 36% of the original time.
2024-08-31 14:25:11 +02:00
newacorn a1db411cc2 StreamRequestBody shouldn't read more data than actual need. (#1819)
* The StreamRequestBody should not read content beyond what is required.

The StreamRequestBody feature on the server side should not read content that does not belong to the current request body.This is more logical and consistent with the result of not using the StreamRequestBody feature.Fixes: https://github.com/valyala/fasthttp/issues/1816.

* Update server_test.go

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

* Update http.go

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

---------

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2024-08-11 15:35:28 +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
Erik Dubbelboer 2909827c57 Fix another flaky test 2024-06-02 12:29:30 +02:00
Oleksandr Redko 9c3915b1fc test: remove redundant error check (#1741) 2024-03-28 21:34:02 +01:00
Oleksandr Redko 190204cf1a Upgrade golangci-lint to v1.56.2; fix gocritic issues (#1722) 2024-02-21 05:51:28 +01:00
mopeneko b430b88e78 Implement GetRejectedConnectionsCount function (#1704)
* Implement `GetRejectedConnectionsCount`

* Implement test for `GetRejectedConnectionsCount`
2024-02-10 10:26:36 +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 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
Oleksandr Redko 4ec5c5a774 docs: fix typos in comments and tests 2023-08-30 14:13:13 +02:00
AutumnSun 0d0bbfee5a Auto add 'Vary' header after compression (#1585)
* Auto add 'Vary' header after compression

Add config `SetAddVaryHeaderForCompression` to enable
'Vary: Accept-Encoding' header when compression is used.

* feat: always set the Vary header

* create and use `ResponseHeader.AddVaryBytes`

* not export 'AddVaryBytes'
2023-07-02 12:40:26 +02:00
Oleksandr Redko ffdf59d04c Enable gofumpt linter; format code gofumpt -w . (#1576) 2023-06-13 15:18:59 +02:00
Erik Dubbelboer 829f5b2445 Don't run test that times out often in parallel 2023-05-23 10:14:50 +02:00
Erik Dubbelboer fa72f3cc39 Fix tests (#1552) 2023-05-07 14:32:14 +02:00
Oleksandr Redko 1dcf56222d test: refactor to use WriteString (#1546) 2023-04-28 17:39:58 +02:00
Oleksandr Redko 498a814fbf test: fix typos in function, error message, comment (#1512) 2023-03-06 16:55:21 +01:00
Oleksandr Redko 4ca6994c93 test: use Fprintf to simplify writing headers (#1510) 2023-03-06 11:13:37 +01:00
Oleksandr Redko 5147cecec7 docs: replace links to golang.org with go.dev (#1489)
- Replace https://godoc.org with https://pkg.go.dev.
- Replace https://golang.org with https://go.dev.
- Replace https://golang.org/pkg with https://pkg.go.dev.
- Replace https://blog.golang.org with https://go.dev/blog.
- Use https://pkg.go.dev/golang.org/x/net/http2  instead of non-existing https://http2.golang.org/.
- Remove trailing slashes.
2023-02-11 19:59:44 +08:00
Oleksandr Redko f84e2346ba Rename unexported funcs, vars to match common Go (#1488)
See https://github.com/golang/go/wiki/CodeReviewComments#initialisms
and https://go.dev/doc/effective_go#mixed-caps
2023-02-10 21:34:49 +08:00
Oleksandr Redko c0c5f9cd38 doc,test: correct typos (#1484) 2023-02-09 09:29:28 +01:00
Sergey Ponomarev 434022b461 server.go Simplify default Server name logic (#1467)
The serverName atomic.Value field is used as a cache. This is not needed and logic can be simplified.
See related #1458
2023-01-06 04:55:09 +01:00
kinggo b788e663c6 feat: support custom formvalue function (#1453) 2022-12-25 09:37:49 +01:00
kinggo 49951353c8 feat: add ShutdownWithContext (#1383) 2022-11-20 13:26:36 +02:00
pj d404f2db91 make RequestCtx's userdata accept keys that are of type: interface{} (#1387)
Co-authored-by: rocketlaunchr-cto <rocketlaunchr.cloud@gmail.com>
2022-10-06 16:25:32 +02:00
Aoang a696949f6c Deprecate Go 1.15 (#1379)
* Dropping support for 1.15.

* Replaces Go 1.16 Deprecated functions

* Update test build flag

* Fix import sort and comment

* Update github.com/klauspost/compress to v1.15.9

https://github.com/klauspost/compress improved performance and changed Minimum version is 1.16, this should be the final supported release for Go 1.16 (https://github.com/klauspost/compress/commit/6d0019a95afa3221f7522d1f2eed0033b5e79470) .
2022-09-15 22:28:25 +03:00
Erik Dubbelboer a5f448fc97 Improve Client timeout (#1346)
Don't run requests in a separate Goroutine anymore. Instead use proper
conn deadlines to enforce timeouts.

- Also contains some linting fixes.
2022-07-29 19:03:15 +02:00
Sergey Ponomarev c9f43eaa1b Response.ContentEncoding(): store as field and avoid using Header.SetCanonical() (#1311)
* Response.ContentEncoding(): store as field

The CE is not so often used for plain APIs responses and even not so often used for static files and on the fly compression.
But still it should be checked each time.
Also having a dedicated field getter and setter simplifies code

* header.go Use shorter Response.setNonSpecial() and Request.setNonSpecial() methods instead of SetCanonical()

The change should improve performance because the setSpecialHeader() call is omitted.
As a downside on adding a new basic header field all putHeader() must be replaced with a direct getter and setter.
2022-06-05 15:47:59 +02:00