1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-07 02:32:13 +00:00

✏ removeNewLines is present in fh 1.18

Co-Authored-By: RW <7063188+ReneWerner87@users.noreply.github.com>
Co-Authored-By: kiyon <kiyon@gofiber.io>
This commit is contained in:
Fenny 2020-12-11 00:49:57 +01:00
parent 3eb8735794
commit 1468a049c4
3 changed files with 1 additions and 88 deletions

2
ctx.go
View File

@ -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) {

View File

@ -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++ {

View File

@ -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()