From 1468a049c4b2ee3aa68d377f7226d3a7e3fb4e78 Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Fri, 11 Dec 2020 00:49:57 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=20removeNewLines=20is=20present=20in?= =?UTF-8?q?=20fh=201.18?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: RW <7063188+ReneWerner87@users.noreply.github.com> Co-Authored-By: kiyon --- ctx.go | 2 +- helpers.go | 23 ------------------ helpers_test.go | 64 ------------------------------------------------- 3 files changed, 1 insertion(+), 88 deletions(-) diff --git a/ctx.go b/ctx.go index 0791acbc..63573da5 100644 --- a/ctx.go +++ b/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) { diff --git a/helpers.go b/helpers.go index f4612e35..6d6e49be 100644 --- a/helpers.go +++ b/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++ { diff --git a/helpers_test.go b/helpers_test.go index 9fe4e6f6..b0d90aa6 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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()