Skip to content

Commit

Permalink
Merge pull request valyala#1 from valyala/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
chhquan committed Sep 11, 2021
2 parents a6f9c8a + 46d9235 commit 1d670c3
Show file tree
Hide file tree
Showing 31 changed files with 206 additions and 390 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/lint.yml
Expand Up @@ -8,9 +8,12 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: GolangCI-Lint Action
uses: golangci/golangci-lint-action@v2
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.17.x
- run: go version
- run: diff -u <(echo -n) <(gofmt -d .)
- uses: golangci/golangci-lint-action@v2
with:
version: v1.28.3
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ tags
*.fasthttp.br
.idea
.DS_Store
vendor/
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
# fasthttp [![GoDoc](https://godoc.org/github.com/valyala/fasthttp?status.svg)](http://godoc.org/github.com/valyala/fasthttp) [![Go Report](https://goreportcard.com/badge/github.com/valyala/fasthttp)](https://goreportcard.com/report/github.com/valyala/fasthttp) [![Sourcegraph](https://sourcegraph.com/github.com/valyala/fasthttp/-/badge.svg)](https://sourcegraph.com/github.com/valyala/fasthttp?badge)
# fasthttp [![GoDoc](https://godoc.org/github.com/valyala/fasthttp?status.svg)](http://godoc.org/github.com/valyala/fasthttp) [![Go Report](https://goreportcard.com/badge/github.com/valyala/fasthttp)](https://goreportcard.com/report/github.com/valyala/fasthttp)

![FastHTTP – Fastest and reliable HTTP implementation in Go](https://github.com/fasthttp/docs-assets/raw/master/banner@0.5.png)

Expand Down
1 change: 1 addition & 0 deletions allocation_test.go
@@ -1,3 +1,4 @@
//go:build !race
// +build !race

package fasthttp
Expand Down
4 changes: 3 additions & 1 deletion bytesconv.go
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"net"
"reflect"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -345,8 +346,9 @@ func s2b(s string) (b []byte) {
/* #nosec G103 */
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
bh.Len = sh.Len
runtime.KeepAlive(&s)
return b
}

Expand Down
1 change: 1 addition & 0 deletions bytesconv_32.go
@@ -1,3 +1,4 @@
//go:build !amd64 && !arm64 && !ppc64 && !ppc64le
// +build !amd64,!arm64,!ppc64,!ppc64le

package fasthttp
Expand Down
1 change: 1 addition & 0 deletions bytesconv_32_test.go
@@ -1,3 +1,4 @@
//go:build !amd64 && !arm64 && !ppc64 && !ppc64le
// +build !amd64,!arm64,!ppc64,!ppc64le

package fasthttp
Expand Down
1 change: 1 addition & 0 deletions bytesconv_64.go
@@ -1,3 +1,4 @@
//go:build amd64 || arm64 || ppc64 || ppc64le
// +build amd64 arm64 ppc64 ppc64le

package fasthttp
Expand Down
1 change: 1 addition & 0 deletions bytesconv_64_test.go
@@ -1,3 +1,4 @@
//go:build amd64 || arm64 || ppc64 || ppc64le
// +build amd64 arm64 ppc64 ppc64le

package fasthttp
Expand Down
1 change: 1 addition & 0 deletions bytesconv_table_gen.go
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
2 changes: 1 addition & 1 deletion client.go
Expand Up @@ -950,7 +950,7 @@ var clientURLResponseChPool sync.Pool

func clientPostURL(dst []byte, url string, postArgs *Args, c clientDoer) (statusCode int, body []byte, err error) {
req := AcquireRequest()
req.Header.SetMethodBytes(strPost)
req.Header.SetMethod(MethodPost)
req.Header.SetContentTypeBytes(strPostArgsContentType)
if postArgs != nil {
if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Expand Up @@ -1282,7 +1282,7 @@ func TestHostClientMaxConnsWithDeadline(t *testing.T) {
var (
emptyBodyCount uint8
ln = fasthttputil.NewInmemoryListener()
timeout = 50 * time.Millisecond
timeout = 200 * time.Millisecond
wg sync.WaitGroup
)

Expand Down
1 change: 1 addition & 0 deletions client_timing_wait_test.go
@@ -1,3 +1,4 @@
//go:build go1.11
// +build go1.11

package fasthttp
Expand Down
16 changes: 8 additions & 8 deletions cookie.go
Expand Up @@ -85,12 +85,12 @@ type Cookie struct {
// CopyTo copies src cookie to c.
func (c *Cookie) CopyTo(src *Cookie) {
c.Reset()
c.key = append(c.key[:0], src.key...)
c.value = append(c.value[:0], src.value...)
c.key = append(c.key, src.key...)
c.value = append(c.value, src.value...)
c.expire = src.expire
c.maxAge = src.maxAge
c.domain = append(c.domain[:0], src.domain...)
c.path = append(c.path[:0], src.path...)
c.domain = append(c.domain, src.domain...)
c.path = append(c.path, src.path...)
c.httpOnly = src.httpOnly
c.secure = src.secure
c.sameSite = src.sameSite
Expand Down Expand Up @@ -345,8 +345,8 @@ func (c *Cookie) ParseBytes(src []byte) error {
return errNoCookies
}

c.key = append(c.key[:0], kv.key...)
c.value = append(c.value[:0], kv.value...)
c.key = append(c.key, kv.key...)
c.value = append(c.value, kv.value...)

for s.next(kv) {
if len(kv.key) != 0 {
Expand Down Expand Up @@ -378,12 +378,12 @@ func (c *Cookie) ParseBytes(src []byte) error {

case 'd': // "domain"
if caseInsensitiveCompare(strCookieDomain, kv.key) {
c.domain = append(c.domain[:0], kv.value...)
c.domain = append(c.domain, kv.value...)
}

case 'p': // "path"
if caseInsensitiveCompare(strCookiePath, kv.key) {
c.path = append(c.path[:0], kv.value...)
c.path = append(c.path, kv.value...)
}

case 's': // "samesite"
Expand Down
58 changes: 22 additions & 36 deletions fs_test.go
Expand Up @@ -112,7 +112,7 @@ func TestPathNotFoundFunc(t *testing.T) {
}

func TestServeFileHead(t *testing.T) {
t.Parallel()
// This test can't run parallel as files in / might by changed by other tests.

var ctx RequestCtx
var req Request
Expand Down Expand Up @@ -204,7 +204,7 @@ func (pw pureWriter) Write(p []byte) (nn int, err error) {
}

func TestServeFileCompressed(t *testing.T) {
t.Parallel()
// This test can't run parallel as files in / might by changed by other tests.

var ctx RequestCtx
ctx.Init(&Request{}, nil, nil)
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestServeFileCompressed(t *testing.T) {
}

func TestServeFileUncompressed(t *testing.T) {
t.Parallel()
// This test can't run parallel as files in / might by changed by other tests.

var ctx RequestCtx
var req Request
Expand Down Expand Up @@ -480,7 +480,7 @@ func testParseByteRangeError(t *testing.T, v string, contentLength int) {
}

func TestFSCompressConcurrent(t *testing.T) {
// This test can't run parallel as files in / might by changed by other tests.
// This test can't run parallel as files in / might be changed by other tests.

stop := make(chan struct{})
defer close(stop)
Expand Down Expand Up @@ -549,14 +549,14 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
s := ctx.Response.String()
br := bufio.NewReader(bytes.NewBufferString(s))
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %s. filePath=%q", err, filePath)
t.Errorf("unexpected error: %s. filePath=%q", err, filePath)
}
if resp.StatusCode() != StatusOK {
t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce := resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "" {
t.Fatalf("unexpected content-encoding %q. Expecting empty string. filePath=%q", ce, filePath)
t.Errorf("unexpected content-encoding %q. Expecting empty string. filePath=%q", ce, filePath)
}
body := string(resp.Body())

Expand All @@ -568,21 +568,21 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
s = ctx.Response.String()
br = bufio.NewReader(bytes.NewBufferString(s))
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %s. filePath=%q", err, filePath)
t.Errorf("unexpected error: %s. filePath=%q", err, filePath)
}
if resp.StatusCode() != StatusOK {
t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce = resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "gzip" {
t.Fatalf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "gzip", filePath)
t.Errorf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "gzip", filePath)
}
zbody, err := resp.BodyGunzip()
if err != nil {
t.Fatalf("unexpected error when gunzipping response body: %s. filePath=%q", err, filePath)
t.Errorf("unexpected error when gunzipping response body: %s. filePath=%q", err, filePath)
}
if string(zbody) != body {
t.Fatalf("unexpected body len=%d. Expected len=%d. FilePath=%q", len(zbody), len(body), filePath)
t.Errorf("unexpected body len=%d. Expected len=%d. FilePath=%q", len(zbody), len(body), filePath)
}

// request compressed brotli file
Expand All @@ -593,43 +593,27 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
s = ctx.Response.String()
br = bufio.NewReader(bytes.NewBufferString(s))
if err = resp.Read(br); err != nil {
t.Fatalf("unexpected error: %s. filePath=%q", err, filePath)
t.Errorf("unexpected error: %s. filePath=%q", err, filePath)
}
if resp.StatusCode() != StatusOK {
t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce = resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "br" {
t.Fatalf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "br", filePath)
t.Errorf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "br", filePath)
}
zbody, err = resp.BodyUnbrotli()
if err != nil {
t.Fatalf("unexpected error when unbrotling response body: %s. filePath=%q", err, filePath)
t.Errorf("unexpected error when unbrotling response body: %s. filePath=%q", err, filePath)
}
if string(zbody) != body {
t.Fatalf("unexpected body len=%d. Expected len=%d. FilePath=%q", len(zbody), len(body), filePath)
}
}

func TestFileLock(t *testing.T) {
t.Parallel()

for i := 0; i < 10; i++ {
filePath := fmt.Sprintf("foo/bar/%d.jpg", i)
lock := getFileLock(filePath)
lock.Lock()
lock.Unlock() // nolint:staticcheck
}

for i := 0; i < 10; i++ {
filePath := fmt.Sprintf("foo/bar/%d.jpg", i)
lock := getFileLock(filePath)
lock.Lock()
lock.Unlock() // nolint:staticcheck
t.Errorf("unexpected body len=%d. Expected len=%d. FilePath=%q", len(zbody), len(body), filePath)
}
}

func TestFSHandlerSingleThread(t *testing.T) {
// This test can't run parallel as files in / might by changed by other tests.

requestHandler := FSHandler(".", 0)

f, err := os.Open(".")
Expand All @@ -650,6 +634,8 @@ func TestFSHandlerSingleThread(t *testing.T) {
}

func TestFSHandlerConcurrent(t *testing.T) {
// This test can't run parallel as files in / might by changed by other tests.

requestHandler := FSHandler(".", 0)

f, err := os.Open(".")
Expand Down Expand Up @@ -796,7 +782,7 @@ func testFileExtension(t *testing.T, path string, compressed bool, compressedFil
}

func TestServeFileContentType(t *testing.T) {
t.Parallel()
// This test can't run parallel as files in / might by changed by other tests.

var ctx RequestCtx
var req Request
Expand Down
1 change: 1 addition & 0 deletions fuzzit/cookie/cookie_fuzz.go
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package fuzz
Expand Down
1 change: 1 addition & 0 deletions fuzzit/request/request_fuzz.go
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package fuzz
Expand Down
1 change: 1 addition & 0 deletions fuzzit/response/response_fuzz.go
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package fuzz
Expand Down
1 change: 1 addition & 0 deletions fuzzit/url/url_fuzz.go
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package fuzz
Expand Down
26 changes: 15 additions & 11 deletions header.go
Expand Up @@ -271,7 +271,11 @@ func (h *RequestHeader) SetContentLength(contentLength int) {
func (h *ResponseHeader) isCompressibleContentType() bool {
contentType := h.ContentType()
return bytes.HasPrefix(contentType, strTextSlash) ||
bytes.HasPrefix(contentType, strApplicationSlash)
bytes.HasPrefix(contentType, strApplicationSlash) ||
bytes.HasPrefix(contentType, strImageSVG) ||
bytes.HasPrefix(contentType, strImageIcon) ||
bytes.HasPrefix(contentType, strFontSlash) ||
bytes.HasPrefix(contentType, strMultipartSlash)
}

// ContentType returns Content-Type header value.
Expand Down Expand Up @@ -443,7 +447,7 @@ func (h *RequestHeader) SetRefererBytes(referer []byte) {
// Method returns HTTP request method.
func (h *RequestHeader) Method() []byte {
if len(h.method) == 0 {
return strGet
return []byte(MethodGet)
}
return h.method
}
Expand Down Expand Up @@ -503,47 +507,47 @@ func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) {

// IsGet returns true if request method is GET.
func (h *RequestHeader) IsGet() bool {
return bytes.Equal(h.Method(), strGet)
return string(h.Method()) == MethodGet
}

// IsPost returns true if request method is POST.
func (h *RequestHeader) IsPost() bool {
return bytes.Equal(h.Method(), strPost)
return string(h.Method()) == MethodPost
}

// IsPut returns true if request method is PUT.
func (h *RequestHeader) IsPut() bool {
return bytes.Equal(h.Method(), strPut)
return string(h.Method()) == MethodPut
}

// IsHead returns true if request method is HEAD.
func (h *RequestHeader) IsHead() bool {
return bytes.Equal(h.Method(), strHead)
return string(h.Method()) == MethodHead
}

// IsDelete returns true if request method is DELETE.
func (h *RequestHeader) IsDelete() bool {
return bytes.Equal(h.Method(), strDelete)
return string(h.Method()) == MethodDelete
}

// IsConnect returns true if request method is CONNECT.
func (h *RequestHeader) IsConnect() bool {
return bytes.Equal(h.Method(), strConnect)
return string(h.Method()) == MethodConnect
}

// IsOptions returns true if request method is OPTIONS.
func (h *RequestHeader) IsOptions() bool {
return bytes.Equal(h.Method(), strOptions)
return string(h.Method()) == MethodOptions
}

// IsTrace returns true if request method is TRACE.
func (h *RequestHeader) IsTrace() bool {
return bytes.Equal(h.Method(), strTrace)
return string(h.Method()) == MethodTrace
}

// IsPatch returns true if request method is PATCH.
func (h *RequestHeader) IsPatch() bool {
return bytes.Equal(h.Method(), strPatch)
return string(h.Method()) == MethodPatch
}

// IsHTTP11 returns true if the request is HTTP/1.1.
Expand Down

0 comments on commit 1d670c3

Please sign in to comment.