Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync 2024年03月12日 #4

Merged
merged 22 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
dd1f3b9
update build tag (#1695)
tylitianrui Jan 18, 2024
287e361
Make Fuzz tests deterministic
erikdubbelboer Jan 20, 2024
48dd2d0
Try fixing oss-fuzz running out of memory and skipping a lot
erikdubbelboer Jan 30, 2024
dfb7e62
add DisableDNSResolution for TCPDialer. Sometimes, users do not need …
xuxiao415 Feb 10, 2024
b430b88
Implement `GetRejectedConnectionsCount` function (#1704)
mopeneko Feb 10, 2024
9fa5688
Update all github actions, add go1.22 (#1707)
gaby Feb 10, 2024
20c2c48
chore(deps): bump golangci/golangci-lint-action from 3 to 4 (#1711)
dependabot[bot] Feb 10, 2024
82bc7c4
bump securego/gosec from 2.17.0 to 2.18.2
erikdubbelboer Feb 10, 2024
a8cb5d5
Bump dependencies (#1718)
erikdubbelboer Feb 10, 2024
3327266
Follow RFCs 7230 and 9112 for HTTP versions (#1710)
erikdubbelboer Feb 11, 2024
bce5766
Prevent request smuggling (#1719)
erikdubbelboer Feb 11, 2024
56cb753
chore(deps): bump securego/gosec from 2.18.2 to 2.19.0 (#1720)
dependabot[bot] Feb 13, 2024
aefd080
adaptor ResponseWriter - adding Hijack method and pass proper fields …
gilwo Feb 17, 2024
a537e47
Remove unnecessary build tag go1.21 (#1721)
alexandear Feb 21, 2024
190204c
Upgrade golangci-lint to v1.56.2; fix gocritic issues (#1722)
alexandear Feb 21, 2024
4c326e8
Limit memory for fuzz testing
erikdubbelboer Feb 21, 2024
5f81476
feat:support zstd compress and uncompressed (#1701)
Max-Cheng Feb 21, 2024
9c69fea
chore(deps): bump golang.org/x/crypto from 0.19.0 to 0.20.0 (#1725)
dependabot[bot] Feb 27, 2024
3166afd
Enable few gocritic checks; fix up issues (#1728)
alexandear Mar 2, 2024
bdd459a
test: remove `//nolint:govet` comments (#1729)
alexandear Mar 2, 2024
7e1fb71
Enable perfsprint linter; fix up lint issues (#1727)
alexandear Mar 2, 2024
222c0bf
Update deps
erikdubbelboer Mar 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
2 changes: 1 addition & 1 deletion .github/workflows/cifuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
dry-run: false
language: go
- name: Upload Crash
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure() && steps.build.outcome == 'success'
with:
name: artifacts
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ on:
branches:
- master
pull_request:

permissions:
# Required: allow read access to the content for analysis.
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
pull-requests: read
# Optional: Allow write access to checks to allow the action to annotate code in the PR.
checks: write

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20.x
- run: go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v4
with:
version: v1.55.2
version: v1.56.2
args: --verbose
6 changes: 3 additions & 3 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
env:
GO111MODULE: on
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Run Gosec Security Scanner
uses: securego/gosec@v2.17.0
uses: securego/gosec@v2.19.0
with:
args: '-exclude=G104,G304,G402 ./...'
args: '-exclude=G103,G104,G304,G402 ./...'
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x]
go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

Expand Down
15 changes: 12 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ linters:
- noctx
- nonamedreturns
- paralleltest
- perfsprint
- testableexamples
- testpackage
- thelper
Expand Down Expand Up @@ -69,8 +68,18 @@ linters-settings:
"-ST1000", # at least one file in a package should have a package comment
]
gocritic:
enabled-checks:
- emptyStringTest
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- deferInLoop
- importShadow
- sloppyReassign
- unnamedResult
- whyNoLint

issues:
# Show all issues from a linter.
Expand Down
10 changes: 5 additions & 5 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ func testCopyTo(t *testing.T, a *Args) {
var b Args
a.CopyTo(&b)

if !reflect.DeepEqual(*a, b) { //nolint:govet
t.Fatalf("ArgsCopyTo fail, a: \n%+v\nb: \n%+v\n", *a, b) //nolint:govet
if !reflect.DeepEqual(a, &b) {
t.Fatalf("ArgsCopyTo fail, a: \n%+v\nb: \n%+v\n", a, &b)
}

b.VisitAll(func(k, _ []byte) {
Expand Down Expand Up @@ -443,13 +443,13 @@ func TestArgsSetGetDel(t *testing.T) {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
a.Del(k)
if string(a.Peek(k)) != "" {
if len(a.Peek(k)) != 0 {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), "")
}
}

a.Parse("aaa=xxx&bb=aa")
if string(a.Peek("foo0")) != "" {
if len(a.Peek("foo0")) != 0 {
t.Fatalf("Unexpected value %q", a.Peek("foo0"))
}
if string(a.Peek("aaa")) != "xxx" {
Expand All @@ -474,7 +474,7 @@ func TestArgsSetGetDel(t *testing.T) {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), v)
}
a.Del(k)
if string(a.Peek(k)) != "" {
if len(a.Peek(k)) != 0 {
t.Fatalf("Unexpected value: %q. Expected %q", a.Peek(k), "")
}
}
Expand Down
12 changes: 6 additions & 6 deletions brotli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func testBrotliCompressSingleCase(s string) error {
func TestCompressHandlerBrotliLevel(t *testing.T) {
t.Parallel()

expectedBody := string(createFixedBody(2e4))
expectedBody := createFixedBody(2e4)
h := CompressHandlerBrotliLevel(func(ctx *RequestCtx) {
ctx.WriteString(expectedBody) //nolint:errcheck
ctx.Write(expectedBody) //nolint:errcheck
}, CompressBrotliDefaultCompression, CompressDefaultCompression)

var ctx RequestCtx
Expand All @@ -121,11 +121,11 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
if len(ce) != 0 {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
}
body := resp.Body()
if string(body) != expectedBody {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}

Expand All @@ -148,7 +148,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(body) != expectedBody {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}

Expand All @@ -171,7 +171,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(body) != expectedBody {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}
}
2 changes: 1 addition & 1 deletion bytesconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func AppendHTMLEscape(dst []byte, s string) []byte {
case '\'':
sub = "'" // "'" is shorter than "'" and apos was not in HTML until HTML5.
}
if len(sub) > 0 {
if sub != "" {
dst = append(dst, s[prev:i]...)
dst = append(dst, sub...)
prev = i + 1
Expand Down
4 changes: 2 additions & 2 deletions bytesconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package fasthttp
import (
"bufio"
"bytes"
"fmt"
"html"
"net"
"net/url"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -118,7 +118,7 @@ func testAppendIPv4(t *testing.T, ipStr string, isValid bool) {
}

func testAppendUint(t *testing.T, n int) {
expectedS := fmt.Sprintf("%d", n)
expectedS := strconv.Itoa(n)
s := AppendUint(nil, n)
if string(s) != expectedS {
t.Fatalf("unexpected uint %q. Expecting %q. n=%d", s, expectedS, n)
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func TestClientParseConn(t *testing.T) {
t.Fatalf("req RemoteAddr parse addr fail: %q, hope: %q", res.RemoteAddr().String(), host)
}

if !regexp.MustCompile(`^127\.0\.0\.1:[0-9]{4,5}$`).MatchString(res.LocalAddr().String()) {
if !regexp.MustCompile(`^127\.0\.0\.1:\d{4,5}$`).MatchString(res.LocalAddr().String()) {
t.Fatalf("res LocalAddr addr match fail: %q, hope match: %q", res.LocalAddr().String(), "^127.0.0.1:[0-9]{4,5}$")
}
}
Expand Down Expand Up @@ -2258,7 +2258,7 @@ type writeErrorConn struct {
}

func (w *writeErrorConn) Write(p []byte) (int, error) {
return 1, fmt.Errorf("error")
return 1, errors.New("error")
}

func (w *writeErrorConn) Close() error {
Expand Down Expand Up @@ -2286,7 +2286,7 @@ type readErrorConn struct {
}

func (r *readErrorConn) Read(p []byte) (int, error) {
return 0, fmt.Errorf("error")
return 0, errors.New("error")
}

func (r *readErrorConn) Write(p []byte) (int, error) {
Expand Down Expand Up @@ -2323,7 +2323,7 @@ func (r *singleReadConn) Read(p []byte) (int, error) {
if len(r.s) == r.n {
return 0, io.EOF
}
n := copy(p, []byte(r.s[r.n:]))
n := copy(p, r.s[r.n:])
r.n += n
return n, nil
}
Expand Down Expand Up @@ -2849,7 +2849,7 @@ func TestClientConfigureClientFailed(t *testing.T) {

c := &Client{
ConfigureClient: func(hc *HostClient) error {
return fmt.Errorf("failed to configure")
return errors.New("failed to configure")
},
}

Expand Down
4 changes: 2 additions & 2 deletions client_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func BenchmarkNetHTTPClientDoFastServer(b *testing.B) {

nn := uint32(0)
b.RunParallel(func(pb *testing.PB) {
req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)), nil)
req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)), http.NoBody)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -550,7 +550,7 @@ func benchmarkNetHTTPClientEndToEndBigResponseInmemory(b *testing.B, parallelism
url := "http://unused.host" + requestURI
b.SetParallelism(parallelism)
b.RunParallel(func(pb *testing.PB) {
req, err := http.NewRequest(MethodGet, url, nil)
req, err := http.NewRequest(MethodGet, url, http.NoBody)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fasthttp

import (
"bytes"
"errors"
"fmt"
"io"
"testing"
Expand Down Expand Up @@ -225,7 +226,7 @@ func testConcurrent(concurrency int, f func() error) error {
return err
}
case <-time.After(time.Second):
return fmt.Errorf("timeout")
return errors.New("timeout")
}
}
return nil
Expand Down
50 changes: 48 additions & 2 deletions fasthttpadaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
package fasthttpadaptor

import (
"bufio"
"io"
"net"
"net/http"
"sync"

"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -53,8 +56,10 @@ func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler {
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return
}

w := netHTTPResponseWriter{w: ctx.Response.BodyWriter()}
w := netHTTPResponseWriter{
w: ctx.Response.BodyWriter(),
ctx: ctx,
}
h.ServeHTTP(&w, r.WithContext(ctx))

ctx.SetStatusCode(w.StatusCode())
Expand Down Expand Up @@ -86,6 +91,7 @@ type netHTTPResponseWriter struct {
statusCode int
h http.Header
w io.Writer
ctx *fasthttp.RequestCtx
}

func (w *netHTTPResponseWriter) StatusCode() int {
Expand All @@ -111,3 +117,43 @@ func (w *netHTTPResponseWriter) Write(p []byte) (int, error) {
}

func (w *netHTTPResponseWriter) Flush() {}

type wrappedConn struct {
net.Conn

wg sync.WaitGroup
once sync.Once
}

func (c *wrappedConn) Close() (err error) {
c.once.Do(func() {
err = c.Conn.Close()
c.wg.Done()
})
return
}

func (w *netHTTPResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
// Hijack assumes control of the connection, so we need to prevent fasthttp from closing it or
// doing anything else with it.
w.ctx.HijackSetNoResponse(true)

conn := &wrappedConn{Conn: w.ctx.Conn()}
conn.wg.Add(1)
w.ctx.Hijack(func(net.Conn) {
conn.wg.Wait()
})

bufW := bufio.NewWriter(conn)

// Write any unflushed body to the hijacked connection buffer.
unflushedBody := w.ctx.Response.Body()
if len(unflushedBody) > 0 {
if _, err := bufW.Write(unflushedBody); err != nil {
conn.Close()
return nil, nil, err
}
}

return conn, &bufio.ReadWriter{Reader: bufio.NewReader(conn), Writer: bufW}, nil
}
Loading
Loading