Skip to content

Commit

Permalink
Merge pull request #3 from gofiber/master
Browse files Browse the repository at this point in the history
merge in changes
  • Loading branch information
codemicro committed Oct 26, 2020
2 parents 12260c7 + 4dd33f1 commit f9fac80
Show file tree
Hide file tree
Showing 34 changed files with 1,725 additions and 1,370 deletions.
166 changes: 84 additions & 82 deletions .github/README.md

Large diffs are not rendered by default.

169 changes: 86 additions & 83 deletions .github/README_de.md

Large diffs are not rendered by default.

169 changes: 86 additions & 83 deletions .github/README_es.md

Large diffs are not rendered by default.

169 changes: 86 additions & 83 deletions .github/README_fr.md

Large diffs are not rendered by default.

216 changes: 124 additions & 92 deletions .github/README_he.md

Large diffs are not rendered by default.

175 changes: 91 additions & 84 deletions .github/README_id.md

Large diffs are not rendered by default.

184 changes: 94 additions & 90 deletions .github/README_ja.md

Large diffs are not rendered by default.

168 changes: 85 additions & 83 deletions .github/README_ko.md

Large diffs are not rendered by default.

167 changes: 85 additions & 82 deletions .github/README_nl.md

Large diffs are not rendered by default.

173 changes: 88 additions & 85 deletions .github/README_pt.md

Large diffs are not rendered by default.

168 changes: 85 additions & 83 deletions .github/README_ru.md

Large diffs are not rendered by default.

185 changes: 91 additions & 94 deletions .github/README_sa.md

Large diffs are not rendered by default.

170 changes: 87 additions & 83 deletions .github/README_tr.md

Large diffs are not rendered by default.

188 changes: 96 additions & 92 deletions .github/README_zh-CN.md

Large diffs are not rendered by default.

164 changes: 83 additions & 81 deletions .github/README_zh-TW.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// Version of current fiber package
const Version = "2.1.0"
const Version = "2.1.1"

// Map is a shortcut for map[string]interface{}, useful for JSON returns
type Map map[string]interface{}
Expand Down Expand Up @@ -278,7 +278,7 @@ const (
DefaultCompressedFileSuffix = ".fiber.gz"
)

// Default ErrorHandler that process return errors from handlers
// DefaultErrorHandler that process return errors from handlers
var DefaultErrorHandler = func(c *Ctx, err error) error {
code := StatusInternalServerError
if e, ok := err.(*Error); ok {
Expand Down
34 changes: 32 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ func Test_App_Static_Direct(t *testing.T) {
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
utils.AssertEqual(t, "", resp.Header.Get(HeaderCacheControl), "CacheControl Control")


body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(body), "gofiber.io/support"))
Expand Down Expand Up @@ -877,6 +876,15 @@ func Test_App_Listen(t *testing.T) {
utils.AssertEqual(t, nil, app.Listen(":4003"))
}

// go test -run Test_App_Listen_Prefork
func Test_App_Listen_Prefork(t *testing.T) {
testPreforkMaster = true

app := New(Config{DisableStartupMessage: true, Prefork: true})

utils.AssertEqual(t, nil, app.Listen(":99999"))
}

// go test -run Test_App_Listener
func Test_App_Listener(t *testing.T) {
app := New()
Expand All @@ -890,6 +898,16 @@ func Test_App_Listener(t *testing.T) {
utils.AssertEqual(t, nil, app.Listener(ln))
}

// go test -run Test_App_Listener_Prefork
func Test_App_Listener_Prefork(t *testing.T) {
testPreforkMaster = true

app := New(Config{DisableStartupMessage: true, Prefork: true})

ln := fasthttputil.NewInmemoryListener()
utils.AssertEqual(t, nil, app.Listener(ln))
}

// go test -v -run=^$ -bench=Benchmark_AcquireCtx -benchmem -count=4
func Benchmark_AcquireCtx(b *testing.B) {
app := New()
Expand Down Expand Up @@ -1103,11 +1121,23 @@ func Test_App_SmallReadBuffer(t *testing.T) {

func Test_App_Master_Process_Show_Startup_Message(t *testing.T) {
New(Config{Prefork: true}).
startupMessage(":3000", true, "")
startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10))
}

func Test_App_Server(t *testing.T) {
app := New()

utils.AssertEqual(t, false, app.Server() == nil)
}

func Test_App_Error_In_Fasthttp_Server(t *testing.T) {
app := New()
app.config.ErrorHandler = func(ctx *Ctx, err error) error {
return errors.New("fake error")
}
app.server.GetOnly = true

resp, err := app.Test(httptest.NewRequest(MethodPost, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 500, resp.StatusCode)
}
32 changes: 23 additions & 9 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,35 @@ func (c *Ctx) Accepts(offers ...string) string {
if commaPos != -1 {
spec = utils.Trim(header[:commaPos], ' ')
} else {
spec = header
spec = utils.TrimLeft(header, ' ')
}
if factorSign := strings.IndexByte(spec, ';'); factorSign != -1 {
spec = spec[:factorSign]
}

var mimetype string
for _, offer := range offers {
mimetype := utils.GetMIME(offer)
if len(spec) > 2 && spec[len(spec)-2:] == "/*" {
if strings.HasPrefix(spec[:len(spec)-2], strings.Split(mimetype, "/")[0]) {
return offer
} else if spec == "*/*" {
return offer
}
} else if strings.HasPrefix(spec, mimetype) {
if len(offer) == 0 {
continue
// Accept: */*
} else if spec == "*/*" {
return offer
}

if strings.IndexByte(offer, '/') != -1 {
mimetype = offer // MIME type
} else {
mimetype = utils.GetMIME(offer) // extension
}

if spec == mimetype {
// Accept: <MIME_type>/<MIME_subtype>
return offer
}

s := strings.IndexByte(mimetype, '/')
// Accept: <MIME_type>/*
if strings.HasPrefix(spec, mimetype[:s]) && (spec[s:] == "/*" || mimetype[s:] == "/*") {
return offer
}
}
Expand Down
31 changes: 31 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"mime/multipart"
"net/http/httptest"
"os"
"reflect"
"strconv"
"strings"
"sync"
Expand All @@ -40,6 +41,20 @@ func Test_Ctx_Accepts(t *testing.T) {
utils.AssertEqual(t, "", c.Accepts())
utils.AssertEqual(t, ".xml", c.Accepts(".xml"))
utils.AssertEqual(t, "", c.Accepts(".john"))

c.Request().Header.Set(HeaderAccept, "text/*, application/json")
utils.AssertEqual(t, "html", c.Accepts("html"))
utils.AssertEqual(t, "text/html", c.Accepts("text/html"))
utils.AssertEqual(t, "json", c.Accepts("json", "text"))
utils.AssertEqual(t, "application/json", c.Accepts("application/json"))
utils.AssertEqual(t, "", c.Accepts("image/png"))
utils.AssertEqual(t, "", c.Accepts("png"))

c.Request().Header.Set(HeaderAccept, "text/html, application/json")
utils.AssertEqual(t, "text/*", c.Accepts("text/*"))

c.Request().Header.Set(HeaderAccept, "*/*")
utils.AssertEqual(t, "html", c.Accepts("html"))
}

// go test -v -run=^$ -bench=Benchmark_Ctx_Accepts -benchmem -count=4
Expand Down Expand Up @@ -257,6 +272,8 @@ func Test_Ctx_BaseURL(t *testing.T) {
defer app.ReleaseCtx(c)
c.Request().SetRequestURI("http://google.com/test")
utils.AssertEqual(t, "http://google.com", c.BaseURL())
// Check cache
utils.AssertEqual(t, "http://google.com", c.BaseURL())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_BaseURL -benchmem
Expand Down Expand Up @@ -504,6 +521,9 @@ func Test_Ctx_Format(t *testing.T) {
c.Format("Hello, World!")
utils.AssertEqual(t, `<string>Hello, World!</string>`, string(c.Response().Body()))

err := c.Format(complex(1, 1))
utils.AssertEqual(t, true, err != nil)

c.Request().Header.Set(HeaderAccept, MIMETextPlain)
c.Format(Map{})
utils.AssertEqual(t, "map[]", string(c.Response().Body()))
Expand Down Expand Up @@ -1256,6 +1276,9 @@ func Test_Ctx_Download(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, expect, c.Response().Body())
utils.AssertEqual(t, `attachment; filename="Awesome+File%21"`, string(c.Response().Header.Peek(HeaderContentDisposition)))

c.Download("ctx.go")
utils.AssertEqual(t, `attachment; filename="ctx.go"`, string(c.Response().Header.Peek(HeaderContentDisposition)))
}

// go test -race -run Test_Ctx_SendFile
Expand Down Expand Up @@ -1957,6 +1980,14 @@ func Test_Ctx_QueryParser(t *testing.T) {

}

func Test_Ctx_EqualFieldType(t *testing.T) {
var out int
utils.AssertEqual(t, false, equalFieldType(&out, reflect.Int, "key"))

var dummy struct{ f string }
utils.AssertEqual(t, false, equalFieldType(&dummy, reflect.String, "key"))
}

// go test -v -run=^$ -bench=Benchmark_Ctx_QueryParser -benchmem -count=4
func Benchmark_Ctx_QueryParser(b *testing.B) {
app := New()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.14

require (
github.com/valyala/fasthttp v1.16.0
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20u
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a h1:e3IU37lwO4aq3uoRKINC7JikojFmE5gO7xhfxs8VC34=
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
12 changes: 6 additions & 6 deletions internal/encoding/ascii/valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func ValidString(s string) bool {
return valid(unsafe.Pointer(&s), uintptr(len(s)))
}

// ValidBytes returns true if b is an ASCII character.
// ValidByte returns true if b is an ASCII character.
func ValidByte(b byte) bool {
return b <= 0x7f
}

// ValidBytes returns true if b is an ASCII character.
// ValidRune returns true if r is an ASCII character.
func ValidRune(r rune) bool {
return r <= 0x7f
}
Expand Down Expand Up @@ -59,22 +59,22 @@ func valid(s unsafe.Pointer, n uintptr) bool {
return (x & 0x80808080) == 0
}

// Valid returns true if b contains only printable ASCII characters.
// ValidPrint returns true if b contains only printable ASCII characters.
func ValidPrint(b []byte) bool {
return validPrint(unsafe.Pointer(&b), uintptr(len(b)))
}

// ValidString returns true if s contains only printable ASCII characters.
// ValidPrintString returns true if s contains only printable ASCII characters.
func ValidPrintString(s string) bool {
return validPrint(unsafe.Pointer(&s), uintptr(len(s)))
}

// ValidBytes returns true if b is an ASCII character.
// ValidPrintByte returns true if b is an ASCII character.
func ValidPrintByte(b byte) bool {
return 0x20 <= b && b <= 0x7e
}

// ValidBytes returns true if b is an ASCII character.
// ValidPrintRune returns true if r is an ASCII character.
func ValidPrintRune(r rune) bool {
return 0x20 <= r && r <= 0x7e
}
Expand Down
2 changes: 2 additions & 0 deletions middleware/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type Config struct {
Level Level
}

// Level is numeric representation of compression level
type Level int

// Represents compression level that will be used in the middleware
const (
LevelDisabled Level = -1
LevelDefault Level = 0
Expand Down
7 changes: 7 additions & 0 deletions middleware/compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,17 @@ func Test_Compress_Different_Level(t *testing.T) {
})

req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Accept-Encoding", "gzip")

resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding))

// Validate that the file size has shrunk
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(body) < len(filedata))
})
}
}
Expand Down
13 changes: 5 additions & 8 deletions middleware/csrf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ app.Use(csrf.New(csrf.Config{
Cookie: &fiber.Cookie{
Name: "_csrf",
},
CookieExpires: 24 * time.Hour,
Expiration: 24 * time.Hour,
}))
```

Expand Down Expand Up @@ -63,10 +63,10 @@ type Config struct {
// Optional.
Cookie *fiber.Cookie

// CookieExpires is the duration before the cookie will expire
// Expiration is the duration before csrf token will expire
//
// Optional. Default: 24 * time.Hour
CookieExpires time.Duration
Expiration time.Duration

// Context key to store generated CSRF token into context.
//
Expand All @@ -83,11 +83,8 @@ var ConfigDefault = Config{
ContextKey: "csrf",
Cookie: &fiber.Cookie{
Name: "_csrf",
Domain: "",
Path: "",
Secure: false,
HTTPOnly: false,
SameSite: "Strict",
},
CookieExpires: 24 * time.Hour,
Expiration: 24 * time.Hour,
}
```

0 comments on commit f9fac80

Please sign in to comment.