Commit Graph

144 Commits

Author SHA1 Message Date
Erik Dubbelboer 3aa09fee51 fix(client): avoid dst aliasing after timeout 2026-06-13 06:09:18 +02:00
Y.Horie 0643f42190 ci: re-enable gocritic deferInLoop check (#2288)
deferInLoop was disabled in .golangci.yml, so CI could not catch defer
statements added inside for loops. Such defers run only at function exit,
not per iteration, leaking file descriptors, connections, or locks under
load.

Re-enable the check and fix the two existing occurrences in test code
instead of excluding test files:

- inmemory listener test: deferred conn.Close() inside the accept loop;
  move the per-connection handling into a closure so each connection is
  closed when done.
- TestClientManyServers: every server must stay up until the test ends,
  so hoist the defer out of the loop and stop all servers in a single
  deferred loop.

Fixes #2233

Signed-off-by: Y.Horie <u5.horie@gmail.com>
2026-06-13 05:49:39 +02:00
Y.Horie cb4dc24ca0 fix(client): switch to GET on 303 redirect per RFC 9110 (#2265)
Per RFC 9110 section 15.4.4, a 303 (See Other) response must be followed
by a body-less GET regardless of the original method. Handle 303
explicitly: change any non-GET/HEAD method to GET and always drop the
body, including when the original request was already a GET/HEAD.

Clear every header that frames the dropped body: Content-Length and
Content-Type, plus Transfer-Encoding and Trailer (per RFC 9112 a body is
signaled by Content-Length or Transfer-Encoding, and a Trailer only
applies to a chunked body). 301/302 keep their POST-to-GET behavior, and
307/308 still preserve the method and body.

Fixes #2240

Signed-off-by: Y.Horie <u5.horie@gmail.com>
2026-06-08 10:00:17 +08:00
Erik Dubbelboer ebaf0e20f8 security: TLS verification silently disabled for malformed addresses (#2236) (#2274) 2026-06-06 16:31:37 +02:00
Erik Dubbelboer 82190178c8 bug: TCPDialer.tcpAddrsClean() goroutine leaks — no shutdown mechanism (#2222) (#2273) 2026-06-06 11:25:39 +02:00
Erik Dubbelboer eee784158a bug: data race on pipeline client c.chR during worker drain (#2220) (#2272) 2026-06-06 11:24:52 +02:00
Ali 6d7682aa72 client: add aggregate connection count metrics (#2201)
* client: add aggregate connection count metrics

* client: remove redundant TestClientConnectionCountsEmpty
2026-05-17 12:14:05 +09:00
Erik Dubbelboer c1e4fdd5b5 Update to go1.25 as minimal version (#2205) 2026-05-16 04:08:08 +02:00
Erik Dubbelboer 3eab931bf1 Sanitize first-line header setters to prevent CRLF injection (#2182)
Prevent request and response first-line setters from serializing
embedded CR or LF bytes into the start line.

Route SetMethod, SetRequestURI, SetProtocol, and SetStatusMessage
through the existing newline sanitization used by other header-value
setters. This preserves behavior for valid inputs while preventing
header injection through malformed first-line values.

Thanks to @vnykmshr for reporting this issue.
2026-04-25 03:01:52 +02:00
Erik Dubbelboer 6cd91f311b Match net/http sensitive header redirect policy (#2181)
* Match net/http sensitive header redirect policy

Strip sensitive headers in DoRedirects matching net/http's redirect policy.

Reported by @vnykmshr

* Add more headers
2026-04-25 03:01:41 +02:00
Max Denushev c2e2a6c3e5 feat(client): add RetryIfErrUpstream function to handle upstream information (#2176)
* feat(client): add RetryIfErrUpstream function to handle upstream information on retry

* feat(client): lint

* feat(client): review comment fixes

* feat(client): lint

* feat(client): lint

---------

Co-authored-by: Max Denushev <denushev@tochka.com>
2026-04-20 15:38:48 +09: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
Aabishkar Aryal e7d310fd76 Add DNS cache management methods for TCPDialer (#2072)
* Add DNS cache management methods for TCPDialer

Resolves #2066

This commit introduces two new methods for managing DNS cache in TCPDialer:

1. FlushDNSCache() - Clears all cached DNS entries, forcing fresh lookups
2. CleanDNSCache() - Removes only expired entries based on DNSCacheDuration

Key changes:
- Add FlushDNSCache() and CleanDNSCache() methods to TCPDialer
- Add global FlushDNSCache() and CleanDNSCache() functions for default dialer
- Refactor tcpAddrsClean() to extract reusable cleanExpiredDNSEntries() method
- Add comprehensive tests with mock resolver to verify caching behavior

Use case: Users can now set longer cache durations (e.g., 30 minutes) and
manually refresh DNS when needed, providing better control over DNS resolution
timing while maintaining performance benefits of caching.

* Remove CleanDNSCache method to reduce the API surface layer and related tests from TCPDialer

* fix: resolve godot linter issue in client_test.go

Add missing period to comment to comply with godot linter rule requiring
comments to end with proper punctuation.
2025-09-25 14:53:57 +08: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
Oleksandr Redko 51b4947320 test: replace atomic operations with atomic types (#2048) 2025-08-07 19:02:12 -04:00
Erik Dubbelboer b1c27881cb Try to fix tests with dial timeouts (#1940)
Use github.com and .io instead of example.com and google.com as they are
probably more reliable within Github actions.
2025-01-15 05:47:54 +01:00
Oleksandr Redko 56fd74b8c0 chore: Upgrade golangci-lint to 1.61.0 (#1887) 2024-10-22 13:03:34 +02:00
newacorn b33e869e2e Another implementation of RetryIfErr (#1850)
* Support for delayed retry logic
* Support for resetting the request timeout before retrying
* Users can decide whether to retry based on the request error and the number of retries.
Estimating the time required for a request to complete is relatively straightforward, but when retries are factored in, it becomes challenging to gauge this time accurately.
2024-09-01 09:17:10 +02:00
newacorn d5c7d8953d fix: The client write operation did not immediately return upon encountering an RST packet. (#1849)
The current client implementation does not immediately return when encountering an RST packet while sending a request, but instead ignores it. This behavior is inconsistent with the net/http package and does not make logical sense.
2024-08-31 14:52:13 +02: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 f789432e77 Fix potential data race reports when the -race flag is enabled. (#1847)
The `testClientDoTimeoutError` and `testClientGetTimeoutSuccess` test function does not immediately return or fatal when the test fails. Instead, it continues to stringify the byte slice that was passed as the `dst` parameter to the `Client.GetTimeout` method. If the test fails due to a timeout, the request may still be ongoing, and there might be a data race between writing to `dst` and the stringification operation.
2024-08-31 14:26:07 +02:00
Erik Dubbelboer df8335f9ab A response without a body can't have trailers (#1825) 2024-08-20 23:10:04 +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 e6d9374bea Fix common recurring CI issues (#1784) 2024-06-02 12:08:27 +02:00
Oleksandr Redko 1c3ba3b2f0 test: replace panic with returning error (#1747) 2024-04-02 17:00:44 +01:00
Oleksandr Redko 7e1fb71854 Enable perfsprint linter; fix up lint issues (#1727) 2024-03-02 16:21:23 +01:00
Oleksandr Redko 190204cf1a Upgrade golangci-lint to v1.56.2; fix gocritic issues (#1722) 2024-02-21 05:51:28 +01:00
Oleksandr Redko 9d6b470260 chore: Add missing dots at the end of comments (#1677) 2023-12-13 13:56:24 +08:00
Aviv Carmi 8ca7a9c89c add support for custom dial function with timeouts (#1669)
* add support for custom dial function with timeouts

* fix linting

---------

Co-authored-by: Aviv Carmi <aviv@perimeterx.com>
2023-11-27 13:46:43 +01:00
Scott Kidder 42bd7bb7e2 Allow redirect URI path to not be normalized. (#1638)
* Allow redirect URI path to not be normalized.

* Introduce DisableRedirectPathNormalizing field to Request

* Use field name as start of comment.

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

---------

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2023-10-30 19:08:51 +01:00
Erik Dubbelboer 8cc5539af7 Fix various request timeout issues
A timeout of 0 should timeout immediately. When retrying requests the
timeout should be updated to reflect the already passed time

Fixes https://github.com/valyala/fasthttp/issues/1617
2023-09-09 14:28:52 +02:00
Oleksandr Redko 9aa666e814 Enable gocritic linter; fix lint issues (#1612) 2023-08-29 20:08:37 +02:00
Tim 54fdc7a73c Abstracts the RoundTripper interface and provides a default implement (#1602)
* Abstracts the RoundTripper interface and provides a default implementation for enhanced extensibility (#1601)

* test: Add custom transport test case (#1601)

* Make default RoundTripper implmention none public

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

---------

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2023-08-10 09:43:26 +02:00
byte0o 9bc8e480c4 Request timeout settings for the same domain name are reused (#1558)
* Update client.go

fix client http SetReadDeadline/SetWriteDeadline Deadline is reused

* delete Deadline comments  fix test singleEchoConn implement SetWriteDeadline/SetReadDeadline

* fix test SetReadDeadline/SetWriteDeadline none implement

---------

Co-authored-by: gaoping <gaoping1@wps.cn>
2023-05-14 12:55:02 +02:00
Erik Dubbelboer fa72f3cc39 Fix tests (#1552) 2023-05-07 14:32:14 +02:00
Oleksandr Redko a281f1d21f client: HostClient.Do hangs out when ErrConnPoolStrategyNotImpl (#1515) 2023-03-14 22:36:38 +01:00
Oleksandr Redko 7846101dc6 client: fix Do hangs when configure host client fails (#1514) 2023-03-11 17:48:33 +01:00
Igor Menshenin 74a050705b Immediately return ErrTimeout if deadline is already reached. (#1497)
* fix: Immediately return ErrTimeout if deadline is already reached.

* test: Added tests for negative timeout

---------

Co-authored-by: Igor Menshenin <igor@native.rent>
2023-03-03 21:25:39 +01:00
Sergey Ponomarev 51048b0620 client.go Simplify default UA logic (#1466)
The getClientName() checks if !NoDefaultUserAgentHeader then returns the Client.Name field.
But it also saves it to atomic field clientName. This is not needed and logic can be simplified.
Previously the clientName vas a byte slice that was copied from c.Name and cached. See 02e0722fb7

Fix #1458
2023-01-02 12:51:39 +08:00
Sergey Ponomarev c50de95952 client.go fix addMissingPort() (#1444)
* client.go Make addMissingPort() public

It's needed for those who creates the instance of the HostClient manually.

* client.go fix AddMissingPort()

Previously for IPv6 addresses the default port wasn't added.
The fix adding a test and optimization that should avoid itoa() call and reduce a memory usage
2022-11-28 08:06:09 +01:00
brian-armstrong-discord f095481f00 Request.SetTimeout (#1415)
* Request.SetTimeout

This functionally works the same as e.g. Client.DoTimeout(), but we can
also use it for Client.DoRedirect(). There is no way as far as I can
tell to set a timeout on a DoRedirect call, so this makes it possible.

* tests

* docs
2022-11-03 21:25:19 +01: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
fare83 c94be05897 use timeout insteadof read/writetimeout when timeout lower than read/… (#1336)
* use timeout insteadof read/writetimeout when timeout lower than read/writetimeout

* use deadtime; fix test timeout;

Co-authored-by: 徐焱 <xuyan4@staff.sina.com.cn>
2022-07-06 14:03:50 +02:00
Erik Dubbelboer 7a5afddf5b Use %v for errors and %q for strings (#1262)
Mostly in tests.
2022-04-01 18:11:16 +02:00
Erik Dubbelboer c6742630ef Fix race conditions in tests 2022-03-18 08:28:53 +01:00
ArminBTVS 1a5f2f40c6 Read response when client closes connection #1232 (#1233)
* Read response when client closes connection #1232

* Fix edge case were client responds with invalid header

* Follow linter suggestions for tests

* Changes after review

* Reafactor error check after review

* Handle connection reset on windows

* Remove format string from test where not needed

* Run connection reset tests not on Windows
2022-03-14 10:53:16 +01:00
Sergey Ponomarev 6a3cc2325b uri_test.go use example.com for clearness (#1212)
* uri_test.go replace xxx.com with example.com

* uri_test.go replace foobar.com with example.com

* uri_test.go use example.com inside of testURIUpdate()

* uri_test.go use example.com instead of google.com

* uri_test.go use example.com instead of google.com

* uri_test.go testURIUpdate() host with port
2022-02-09 10:19:34 +01:00
ArminBTVS 8d7953eda7 Fix scheme check for not yet parsed requests (#1203)
* Fix scheme check for not yet parsed requests

At this point the request might not be parsed yet and set. In that case uri is empty and isHttps() returns always false. I don't expect this is intended?

Otherwise URL() must be called before actually passing the request to client.Do()

* Add test

* Please linter
2022-01-31 22:02:58 +08:00
Erik Dubbelboer e9db537178 Use %w to wrap errors (#1175) 2021-12-13 09:41:34 +01:00
Erik Dubbelboer a50f59be52 Increase various test timeouts
Github workers on windows are slow.
2021-08-24 11:18:22 +02:00