Skip to content

Commit 7b238d1

Browse files
authored
Merge branch 'gofiber:main' into feature/add-buffered-streaming-support
2 parents 2282a35 + 44cd700 commit 7b238d1

File tree

8 files changed

+97
-67
lines changed

8 files changed

+97
-67
lines changed

ctx.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ func (c *DefaultCtx) Fresh() bool {
625625
if err != nil {
626626
return false
627627
}
628-
return lastModifiedTime.Before(modifiedSinceTime)
628+
return lastModifiedTime.Compare(modifiedSinceTime) != 1
629629
}
630630
}
631631
}
@@ -1849,21 +1849,9 @@ func (c *DefaultCtx) IsProxyTrusted() bool {
18491849
return false
18501850
}
18511851

1852-
var localHosts = [...]string{"127.0.0.1", "::1"}
1853-
1854-
// IsLocalHost will return true if address is a localhost address.
1855-
func (*DefaultCtx) isLocalHost(address string) bool {
1856-
for _, h := range localHosts {
1857-
if address == h {
1858-
return true
1859-
}
1860-
}
1861-
return false
1862-
}
1863-
18641852
// IsFromLocal will return true if request came from local.
18651853
func (c *DefaultCtx) IsFromLocal() bool {
1866-
return c.isLocalHost(c.fasthttp.RemoteIP().String())
1854+
return c.fasthttp.RemoteIP().IsLoopback()
18671855
}
18681856

18691857
// Bind You can bind body, cookie, headers etc. into the map, map slice, struct easily by using Binding method.

ctx_interface_gen.go

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ctx_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,10 @@ func Test_Ctx_Fresh(t *testing.T) {
13981398
require.False(t, c.Fresh())
13991399

14001400
c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT")
1401+
require.True(t, c.Fresh())
1402+
1403+
c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:27:59 GMT")
1404+
c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT")
14011405
require.False(t, c.Fresh())
14021406
}
14031407

@@ -1413,6 +1417,18 @@ func Benchmark_Ctx_Fresh_WithNoCache(b *testing.B) {
14131417
}
14141418
}
14151419

1420+
// go test -v -run=^$ -bench=Benchmark_Ctx_Fresh_LastModified -benchmem -count=4
1421+
func Benchmark_Ctx_Fresh_LastModified(b *testing.B) {
1422+
app := New()
1423+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
1424+
1425+
c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT")
1426+
c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT")
1427+
for n := 0; n < b.N; n++ {
1428+
c.Fresh()
1429+
}
1430+
}
1431+
14161432
// go test -run Test_Ctx_Binders -v
14171433
func Test_Ctx_Binders(t *testing.T) {
14181434
t.Parallel()
@@ -6420,3 +6436,61 @@ func Benchmark_Ctx_IsProxyTrusted(b *testing.B) {
64206436
})
64216437
})
64226438
}
6439+
6440+
func Benchmark_Ctx_IsFromLocalhost(b *testing.B) {
6441+
// Scenario without localhost check
6442+
b.Run("Non_Localhost", func(b *testing.B) {
6443+
app := New()
6444+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
6445+
c.Request().SetRequestURI("http://google.com:8080/test")
6446+
b.ReportAllocs()
6447+
b.ResetTimer()
6448+
for n := 0; n < b.N; n++ {
6449+
c.IsFromLocal()
6450+
}
6451+
app.ReleaseCtx(c)
6452+
})
6453+
6454+
// Scenario without localhost check in parallel
6455+
b.Run("Non_Localhost_Parallel", func(b *testing.B) {
6456+
app := New()
6457+
b.ReportAllocs()
6458+
b.ResetTimer()
6459+
b.RunParallel(func(pb *testing.PB) {
6460+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
6461+
c.Request().SetRequestURI("http://google.com:8080/test")
6462+
for pb.Next() {
6463+
c.IsFromLocal()
6464+
}
6465+
app.ReleaseCtx(c)
6466+
})
6467+
})
6468+
6469+
// Scenario with localhost check
6470+
b.Run("Localhost", func(b *testing.B) {
6471+
app := New()
6472+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
6473+
c.Request().SetRequestURI("http://localhost:8080/test")
6474+
b.ReportAllocs()
6475+
b.ResetTimer()
6476+
for n := 0; n < b.N; n++ {
6477+
c.IsFromLocal()
6478+
}
6479+
app.ReleaseCtx(c)
6480+
})
6481+
6482+
// Scenario with localhost check in parallel
6483+
b.Run("Localhost_Parallel", func(b *testing.B) {
6484+
app := New()
6485+
b.ReportAllocs()
6486+
b.ResetTimer()
6487+
b.RunParallel(func(pb *testing.PB) {
6488+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
6489+
c.Request().SetRequestURI("http://localhost:8080/test")
6490+
for pb.Next() {
6491+
c.IsFromLocal()
6492+
}
6493+
app.ReleaseCtx(c)
6494+
})
6495+
})
6496+
}

docs/api/ctx.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ Improper use of the X-Forwarded-For header can be a security risk. For details,
788788

789789
## Is
790790

791-
Returns the matching **content type**, if the incoming request’s [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) HTTP header field matches the [MIME type](https://developer.mozilla.org/ru/docs/Web/HTTP/Basics_of_HTTP/MIME_types) specified by the type parameter.
791+
Returns the matching **content type**, if the incoming request’s [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) HTTP header field matches the [MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) specified by the type parameter.
792792

793793
:::info
794794
If the request has **no** body, it returns **false**.

docs/middleware/logger.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ app.Use(logger.New(logger.Config{
4444
app.Use(requestid.New())
4545
app.Use(logger.New(logger.Config{
4646
// For more options, see the Config section
47-
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n",
47+
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n",
4848
}))
4949

5050
// Changing TimeZone & TimeFormat

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ require (
88
github.com/mattn/go-colorable v0.1.13
99
github.com/mattn/go-isatty v0.0.20
1010
github.com/stretchr/testify v1.9.0
11-
github.com/tinylib/msgp v1.1.8
11+
github.com/tinylib/msgp v1.2.1
1212
github.com/valyala/bytebufferpool v1.0.0
13-
github.com/valyala/fasthttp v1.55.0
13+
github.com/valyala/fasthttp v1.56.0
1414
)
1515

1616
require (
1717
github.com/andybalholm/brotli v1.1.0 // indirect
1818
github.com/davecgh/go-spew v1.1.1 // indirect
1919
github.com/klauspost/compress v1.17.9 // indirect
20-
github.com/philhofer/fwd v1.1.2 // indirect
20+
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect
2121
github.com/pmezard/go-difflib v1.0.0 // indirect
2222
github.com/valyala/tcplisten v1.0.0 // indirect
23-
golang.org/x/net v0.26.0 // indirect
24-
golang.org/x/sys v0.21.0 // indirect
25-
golang.org/x/text v0.16.0 // indirect
23+
golang.org/x/net v0.29.0 // indirect
24+
golang.org/x/sys v0.25.0 // indirect
25+
golang.org/x/text v0.18.0 // indirect
2626
gopkg.in/yaml.v3 v3.0.1 // indirect
2727
)

go.sum

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,28 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
1313
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
1414
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
1515
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
16-
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
17-
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
16+
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 h1:jYi87L8j62qkXzaYHAQAhEapgukhenIMZRBKTNRLHJ4=
17+
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
1818
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1919
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2020
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2121
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
22-
github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0=
23-
github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw=
22+
github.com/tinylib/msgp v1.2.1 h1:6ypy2qcCznxpP4hpORzhtXyTqrBs7cfM9MCCWY8zsmU=
23+
github.com/tinylib/msgp v1.2.1/go.mod h1:2vIGs3lcUo8izAATNobrCHevYZC/LMsJtw4JPiYPHro=
2424
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
2525
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
26-
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
27-
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
26+
github.com/valyala/fasthttp v1.56.0 h1:bEZdJev/6LCBlpdORfrLu/WOZXXxvrUQSiyniuaoW8U=
27+
github.com/valyala/fasthttp v1.56.0/go.mod h1:sReBt3XZVnudxuLOx4J/fMrJVorWRiWY2koQKgABiVI=
2828
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
2929
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
30-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
31-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
32-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
33-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
34-
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
35-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
36-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
37-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
38-
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
39-
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
40-
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
41-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
42-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
43-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
44-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
45-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
46-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30+
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
31+
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
4932
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
50-
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5133
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
52-
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
53-
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
54-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
55-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
56-
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
57-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
58-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
59-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
60-
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
61-
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
62-
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
63-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
64-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
65-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
66-
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
67-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
34+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
35+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36+
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
37+
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
6838
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6939
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7040
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

middleware/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func New(config ...Config) fiber.Handler {
117117
// Get timestamp
118118
ts := atomic.LoadUint64(&timestamp)
119119

120-
// Cache Entry not found
120+
// Cache Entry found
121121
if e != nil {
122122
// Invalidate cache if requested
123123
if cfg.CacheInvalidator != nil && cfg.CacheInvalidator(c) {

0 commit comments

Comments
 (0)