Skip to content
Permalink
Browse files

⚡️ Used Headers constants instead raw strings

  • Loading branch information...
toby3d authored and kirillDanshin committed May 27, 2019
1 parent a4505cb commit d3715c361c6d8d227117caa9a7e6872498bf7283
Showing with 192 additions and 192 deletions.
  1. +2 −2 client_timing_test.go
  2. +1 −1 fasthttpadaptor/adaptor_test.go
  3. +9 −9 fs_test.go
  4. +38 −38 header.go
  5. +82 −82 header_test.go
  6. +20 −20 http_test.go
  7. +18 −18 server_test.go
  8. +2 −2 server_timing_test.go
  9. +20 −20 strings.go
@@ -186,7 +186,7 @@ func fasthttpEchoHandler(ctx *RequestCtx) {
}

func nethttpEchoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set(HeaderContentType, "text/plain")
w.Write([]byte(r.RequestURI))
}

@@ -516,7 +516,7 @@ func BenchmarkNetHTTPClientEndToEndBigResponse10Inmemory(b *testing.B) {
func benchmarkNetHTTPClientEndToEndBigResponseInmemory(b *testing.B, parallelism int) {
bigResponse := createFixedBody(1024 * 1024)
h := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set(HeaderContentType, "text/plain")
w.Write(bigResponse)
}
ln := fasthttputil.NewInmemoryListener()
@@ -95,7 +95,7 @@ func TestNewFastHTTPHandler(t *testing.T) {
req.Header.SetMethod(expectedMethod)
req.SetRequestURI(expectedRequestURI)
req.Header.SetHost(expectedHost)
req.Header.Add("Transfer-Encoding", expectedTransferEncoding)
req.Header.Add(fasthttp.HeaderTransferEncoding, expectedTransferEncoding)
req.BodyWriter().Write([]byte(expectedBody))
for k, v := range expectedHeader {
req.Header.Set(k, v)
@@ -115,7 +115,7 @@ func TestServeFileHead(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}

ce := resp.Header.Peek("Content-Encoding")
ce := resp.Header.Peek(HeaderContentEncoding)
if len(ce) > 0 {
t.Fatalf("Unexpected 'Content-Encoding' %q", ce)
}
@@ -190,7 +190,7 @@ func TestServeFileCompressed(t *testing.T) {
var ctx RequestCtx
var req Request
req.SetRequestURI("http://foobar.com/baz")
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set(HeaderAcceptEncoding, "gzip")
ctx.Init(&req, nil, nil)

ServeFile(&ctx, "fs.go")
@@ -202,7 +202,7 @@ func TestServeFileCompressed(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}

ce := resp.Header.Peek("Content-Encoding")
ce := resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "gzip" {
t.Fatalf("Unexpected 'Content-Encoding' %q. Expecting %q", ce, "gzip")
}
@@ -224,7 +224,7 @@ func TestServeFileUncompressed(t *testing.T) {
var ctx RequestCtx
var req Request
req.SetRequestURI("http://foobar.com/baz")
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set(HeaderAcceptEncoding, "gzip")
ctx.Init(&req, nil, nil)

ServeFileUncompressed(&ctx, "fs.go")
@@ -236,7 +236,7 @@ func TestServeFileUncompressed(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}

ce := resp.Header.Peek("Content-Encoding")
ce := resp.Header.Peek(HeaderContentEncoding)
if len(ce) > 0 {
t.Fatalf("Unexpected 'Content-Encoding' %q", ce)
}
@@ -319,7 +319,7 @@ func testFSByteRange(t *testing.T, h RequestHandler, filePath string) {
if resp.StatusCode() != StatusPartialContent {
t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusPartialContent, filePath)
}
cr := resp.Header.Peek("Content-Range")
cr := resp.Header.Peek(HeaderContentRange)

expectedCR := fmt.Sprintf("bytes %d-%d/%d", startPos, endPos, fileSize)
if string(cr) != expectedCR {
@@ -473,7 +473,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
if resp.StatusCode() != StatusOK {
t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce := resp.Header.Peek("Content-Encoding")
ce := resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "" {
t.Fatalf("unexpected content-encoding %q. Expecting empty string. filePath=%q", ce, filePath)
}
@@ -482,7 +482,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
// request compressed file
ctx.Request.Reset()
ctx.Request.SetRequestURI(filePath)
ctx.Request.Header.Set("Accept-Encoding", "gzip")
ctx.Request.Header.Set(HeaderAcceptEncoding, "gzip")
h(&ctx)
s = ctx.Response.String()
br = bufio.NewReader(bytes.NewBufferString(s))
@@ -492,7 +492,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
if resp.StatusCode() != StatusOK {
t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce = resp.Header.Peek("Content-Encoding")
ce = resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "gzip" {
t.Fatalf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "gzip", filePath)
}
@@ -178,13 +178,13 @@ func (h *RequestHeader) ResetConnectionClose() {

// ConnectionUpgrade returns true if 'Connection: Upgrade' header is set.
func (h *ResponseHeader) ConnectionUpgrade() bool {
return hasHeaderValue(h.Peek("Connection"), strUpgrade)
return hasHeaderValue(h.Peek(HeaderConnection), strUpgrade)
}

// ConnectionUpgrade returns true if 'Connection: Upgrade' header is set.
func (h *RequestHeader) ConnectionUpgrade() bool {
h.parseRawHeaders()
return hasHeaderValue(h.Peek("Connection"), strUpgrade)
return hasHeaderValue(h.Peek(HeaderConnection), strUpgrade)
}

// PeekCookie is able to returns cookie by a given key from response.
@@ -845,16 +845,16 @@ func (h *ResponseHeader) DelBytes(key []byte) {

func (h *ResponseHeader) del(key []byte) {
switch string(key) {
case "Content-Type":
case HeaderContentType:
h.contentType = h.contentType[:0]
case "Server":
case HeaderServer:
h.server = h.server[:0]
case "Set-Cookie":
case HeaderSetCookie:
h.cookies = h.cookies[:0]
case "Content-Length":
case HeaderContentLength:
h.contentLength = 0
h.contentLengthBytes = h.contentLengthBytes[:0]
case "Connection":
case HeaderConnection:
h.connectionClose = false
}
h.h = delAllArgsBytes(h.h, key)
@@ -877,18 +877,18 @@ func (h *RequestHeader) DelBytes(key []byte) {

func (h *RequestHeader) del(key []byte) {
switch string(key) {
case "Host":
case HeaderHost:
h.host = h.host[:0]
case "Content-Type":
case HeaderContentType:
h.contentType = h.contentType[:0]
case "User-Agent":
case HeaderUserAgent:
h.userAgent = h.userAgent[:0]
case "Cookie":
case HeaderCookie:
h.cookies = h.cookies[:0]
case "Content-Length":
case HeaderContentLength:
h.contentLength = 0
h.contentLengthBytes = h.contentLengthBytes[:0]
case "Connection":
case HeaderConnection:
h.connectionClose = false
}
h.h = delAllArgsBytes(h.h, key)
@@ -964,30 +964,30 @@ func (h *ResponseHeader) SetBytesKV(key, value []byte) {
// key is in canonical form.
func (h *ResponseHeader) SetCanonical(key, value []byte) {
switch string(key) {
case "Content-Type":
case HeaderContentType:
h.SetContentTypeBytes(value)
case "Server":
case HeaderServer:
h.SetServerBytes(value)
case "Set-Cookie":
case HeaderSetCookie:
var kv *argsKV
h.cookies, kv = allocArg(h.cookies)
kv.key = getCookieKey(kv.key, value)
kv.value = append(kv.value[:0], value...)
case "Content-Length":
case HeaderContentLength:
if contentLength, err := parseContentLength(value); err == nil {
h.contentLength = contentLength
h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
}
case "Connection":
case HeaderConnection:
if bytes.Equal(strClose, value) {
h.SetConnectionClose()
} else {
h.ResetConnectionClose()
h.h = setArgBytes(h.h, key, value, argsHasValue)
}
case "Transfer-Encoding":
case HeaderTransferEncoding:
// Transfer-Encoding is managed automatically.
case "Date":
case HeaderDate:
// Date is managed automatically.
default:
h.h = setArgBytes(h.h, key, value, argsHasValue)
@@ -1149,28 +1149,28 @@ func (h *RequestHeader) SetBytesKV(key, value []byte) {
func (h *RequestHeader) SetCanonical(key, value []byte) {
h.parseRawHeaders()
switch string(key) {
case "Host":
case HeaderHost:
h.SetHostBytes(value)
case "Content-Type":
case HeaderContentType:
h.SetContentTypeBytes(value)
case "User-Agent":
case HeaderUserAgent:
h.SetUserAgentBytes(value)
case "Cookie":
case HeaderCookie:
h.collectCookies()
h.cookies = parseRequestCookies(h.cookies, value)
case "Content-Length":
case HeaderContentLength:
if contentLength, err := parseContentLength(value); err == nil {
h.contentLength = contentLength
h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
}
case "Connection":
case HeaderConnection:
if bytes.Equal(strClose, value) {
h.SetConnectionClose()
} else {
h.ResetConnectionClose()
h.h = setArgBytes(h.h, key, value, argsHasValue)
}
case "Transfer-Encoding":
case HeaderTransferEncoding:
// Transfer-Encoding is managed automatically.
default:
h.h = setArgBytes(h.h, key, value, argsHasValue)
@@ -1217,18 +1217,18 @@ func (h *RequestHeader) PeekBytes(key []byte) []byte {

func (h *ResponseHeader) peek(key []byte) []byte {
switch string(key) {
case "Content-Type":
case HeaderContentType:
return h.ContentType()
case "Server":
case HeaderServer:
return h.Server()
case "Connection":
case HeaderConnection:
if h.ConnectionClose() {
return strClose
}
return peekArgBytes(h.h, key)
case "Content-Length":
case HeaderContentLength:
return h.contentLengthBytes
case "Set-Cookie":
case HeaderSetCookie:
return appendResponseCookieBytes(nil, h.cookies)
default:
return peekArgBytes(h.h, key)
@@ -1238,20 +1238,20 @@ func (h *ResponseHeader) peek(key []byte) []byte {
func (h *RequestHeader) peek(key []byte) []byte {
h.parseRawHeaders()
switch string(key) {
case "Host":
case HeaderHost:
return h.Host()
case "Content-Type":
case HeaderContentType:
return h.ContentType()
case "User-Agent":
case HeaderUserAgent:
return h.UserAgent()
case "Connection":
case HeaderConnection:
if h.ConnectionClose() {
return strClose
}
return peekArgBytes(h.h, key)
case "Content-Length":
case HeaderContentLength:
return h.contentLengthBytes
case "Cookie":
case HeaderCookie:
if h.cookiesCollected {
return appendRequestCookieBytes(nil, h.cookies)
} else {

0 comments on commit d3715c3

Please sign in to comment.
You can’t perform that action at this time.