mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 19:23:48 +00:00
Merge pull request #1072 from Fenny/master
✏ removeNewLines is present in fh 1.18
This commit is contained in:
commit
07aa649a6f
2
app.go
2
app.go
@ -31,7 +31,7 @@ import (
|
||||
)
|
||||
|
||||
// Version of current fiber package
|
||||
const Version = "2.2.4"
|
||||
const Version = "2.2.5"
|
||||
|
||||
// Handler defines a function to serve HTTP requests.
|
||||
type Handler = func(*Ctx) error
|
||||
|
2
ctx.go
2
ctx.go
@ -1017,7 +1017,7 @@ func (c *Ctx) SendStream(stream io.Reader, size ...int) error {
|
||||
|
||||
// Set sets the response's HTTP header field to the specified key, value.
|
||||
func (c *Ctx) Set(key string, val string) {
|
||||
c.fasthttp.Response.Header.Set(key, removeNewLines(val))
|
||||
c.fasthttp.Response.Header.Set(key, val)
|
||||
}
|
||||
|
||||
func (c *Ctx) setCanonical(key string, val string) {
|
||||
|
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.14
|
||||
|
||||
require (
|
||||
github.com/valyala/fasthttp v1.18.0
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
|
||||
golang.org/x/sys v0.0.0-20201210223839-7e3030f88018
|
||||
)
|
||||
|
10
go.sum
10
go.sum
@ -1,15 +1,9 @@
|
||||
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
||||
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
|
||||
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
|
||||
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/klauspost/compress v1.11.3 h1:dB4Bn0tN3wdCzQxnS8r06kV74qN/TAfaIS0bVE8h3jc=
|
||||
github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.17.0 h1:P8/koH4aSnJ4xbd0cUUFEGQs3jQqIxoDDyRQrUiAkqg=
|
||||
github.com/valyala/fasthttp v1.17.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
|
||||
github.com/valyala/fasthttp v1.18.0 h1:IV0DdMlatq9QO1Cr6wGJPVW1sV1Q8HvZXAIcjorylyM=
|
||||
github.com/valyala/fasthttp v1.18.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
|
||||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
|
||||
@ -23,8 +17,8 @@ golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwY
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201210223839-7e3030f88018 h1:XKi8B/gRBuTZN1vU9gFsLMm6zVz5FSCDzm8JYACnjy8=
|
||||
golang.org/x/sys v0.0.0-20201210223839-7e3030f88018/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=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
|
23
helpers.go
23
helpers.go
@ -94,29 +94,6 @@ func quoteString(raw string) string {
|
||||
return quoted
|
||||
}
|
||||
|
||||
// removeNewLines will replace `\r` and `\n` with an empty space
|
||||
func removeNewLines(raw string) string {
|
||||
start := 0
|
||||
if start = strings.IndexByte(raw, '\r'); start == -1 {
|
||||
if start = strings.IndexByte(raw, '\n'); start == -1 {
|
||||
return raw
|
||||
}
|
||||
}
|
||||
bb := bytebufferpool.Get()
|
||||
buf := bb.Bytes()
|
||||
buf = append(buf, raw...)
|
||||
for i := start; i < len(buf); i++ {
|
||||
if buf[i] != '\r' && buf[i] != '\n' {
|
||||
continue
|
||||
}
|
||||
buf[i] = ' '
|
||||
}
|
||||
raw = utils.UnsafeString(buf)
|
||||
bytebufferpool.Put(bb)
|
||||
|
||||
return raw
|
||||
}
|
||||
|
||||
// Scan stack if other methods match the request
|
||||
func methodExist(ctx *Ctx) (exist bool) {
|
||||
for i := 0; i < len(intMethod); i++ {
|
||||
|
@ -16,70 +16,6 @@ import (
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_RemoveNewLines -benchmem -count=4
|
||||
func Benchmark_RemoveNewLines(b *testing.B) {
|
||||
withNL := "foo\r\nSet-Cookie:%20SESSIONID=MaliciousValue\r\n"
|
||||
withoutNL := "foo Set-Cookie:%20SESSIONID=MaliciousValue "
|
||||
expected := utils.SafeString(withoutNL)
|
||||
var res string
|
||||
|
||||
b.Run("withoutNL", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
res = removeNewLines(withoutNL)
|
||||
}
|
||||
utils.AssertEqual(b, expected, res)
|
||||
})
|
||||
b.Run("withNL", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
res = removeNewLines(withNL)
|
||||
}
|
||||
utils.AssertEqual(b, expected, res)
|
||||
})
|
||||
}
|
||||
|
||||
// go test -v -run=RemoveNewLines_Bytes -count=3
|
||||
func Test_RemoveNewLines_Bytes(t *testing.T) {
|
||||
app := New()
|
||||
t.Run("Not Status OK", func(t *testing.T) {
|
||||
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(c)
|
||||
c.SendString("Hello, World!")
|
||||
c.Status(201)
|
||||
setETag(c, false)
|
||||
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
|
||||
})
|
||||
|
||||
t.Run("No Body", func(t *testing.T) {
|
||||
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(c)
|
||||
setETag(c, false)
|
||||
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
|
||||
})
|
||||
|
||||
t.Run("Has HeaderIfNoneMatch", func(t *testing.T) {
|
||||
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(c)
|
||||
c.SendString("Hello, World!")
|
||||
c.Request().Header.Set(HeaderIfNoneMatch, `"13-1831710635"`)
|
||||
setETag(c, false)
|
||||
utils.AssertEqual(t, 304, c.Response().StatusCode())
|
||||
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
|
||||
utils.AssertEqual(t, "", string(c.Response().Body()))
|
||||
})
|
||||
|
||||
t.Run("No HeaderIfNoneMatch", func(t *testing.T) {
|
||||
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(c)
|
||||
c.SendString("Hello, World!")
|
||||
setETag(c, false)
|
||||
utils.AssertEqual(t, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
|
||||
})
|
||||
}
|
||||
|
||||
// go test -v -run=Test_Utils_ -count=3
|
||||
func Test_Utils_ETag(t *testing.T) {
|
||||
app := New()
|
||||
|
Loading…
x
Reference in New Issue
Block a user