diff options
author | kiyon <kiyon@gofiber.io> | 2020-08-03 01:18:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-02 19:18:57 +0200 |
commit | 2509c12e7794859d16e359be0fe62a08a5d382d3 (patch) | |
tree | ab6611d1f3e70f8b745df5464d1a6d5538553a4a /status.go | |
parent | Fix comment typo (diff) | |
download | fasthttp-2509c12e7794859d16e359be0fe62a08a5d382d3.tar.gz fasthttp-2509c12e7794859d16e359be0fe62a08a5d382d3.tar.bz2 fasthttp-2509c12e7794859d16e359be0fe62a08a5d382d3.zip |
improve statusLine and StatusMessage by using slice instead of map (#855)
* 🚀 improve statusLine and StatusMessage by using slice instead of map
Co-authored-by: Fenny <fenny@gofiber.io>
Co-authored-by: ReneWerner87 <rene@gofiber.io>
* for cli stuff
* make invalidStatusLine() just return the formatted line
* for ci stuff
Co-authored-by: Fenny <fenny@gofiber.io>
Co-authored-by: ReneWerner87 <rene@gofiber.io>
Diffstat (limited to 'status.go')
-rw-r--r-- | status.go | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -2,7 +2,11 @@ package fasthttp import ( "fmt" - "sync/atomic" +) + +const ( + statusMessageMin = 100 + statusMessageMax = 511 ) // HTTP status codes were stolen from net/http. @@ -10,6 +14,7 @@ const ( StatusContinue = 100 // RFC 7231, 6.2.1 StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2 StatusProcessing = 102 // RFC 2518, 10.1 + StatusEarlyHints = 103 // RFC 8297 StatusOK = 200 // RFC 7231, 6.3.1 StatusCreated = 201 // RFC 7231, 6.3.2 @@ -51,6 +56,7 @@ const ( StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4 StatusExpectationFailed = 417 // RFC 7231, 6.5.14 StatusTeapot = 418 // RFC 7168, 2.3.3 + StatusMisdirectedRequest = 421 // RFC 7540, 9.1.2 StatusUnprocessableEntity = 422 // RFC 4918, 11.2 StatusLocked = 423 // RFC 4918, 11.3 StatusFailedDependency = 424 // RFC 4918, 11.4 @@ -74,12 +80,13 @@ const ( ) var ( - statusLines atomic.Value + statusLines = make([][]byte, statusMessageMax+1) - statusMessages = map[int]string{ + statusMessages = []string{ StatusContinue: "Continue", StatusSwitchingProtocols: "Switching Protocols", StatusProcessing: "Processing", + StatusEarlyHints: "Early Hints", StatusOK: "OK", StatusCreated: "Created", @@ -120,6 +127,7 @@ var ( StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable", StatusExpectationFailed: "Expectation Failed", StatusTeapot: "I'm a teapot", + StatusMisdirectedRequest: "Misdirected Request", StatusUnprocessableEntity: "Unprocessable Entity", StatusLocked: "Locked", StatusFailedDependency: "Failed Dependency", @@ -145,6 +153,10 @@ var ( // StatusMessage returns HTTP status message for the given status code. func StatusMessage(statusCode int) string { + if statusCode < statusMessageMin || statusCode > statusMessageMax { + return "Unknown Status Code" + } + s := statusMessages[statusCode] if s == "" { s = "Unknown Status Code" @@ -153,24 +165,21 @@ func StatusMessage(statusCode int) string { } func init() { - statusLines.Store(make(map[int][]byte)) + // Fill all valid status lines + for i := 0; i < len(statusLines); i++ { + statusLines[i] = []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", i, StatusMessage(i))) + } } func statusLine(statusCode int) []byte { - m := statusLines.Load().(map[int][]byte) - h := m[statusCode] - if h != nil { - return h + if statusCode < 0 || statusCode > statusMessageMax { + return invalidStatusLine(statusCode) } - statusText := StatusMessage(statusCode) + return statusLines[statusCode] +} - h = []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", statusCode, statusText)) - newM := make(map[int][]byte, len(m)+1) - for k, v := range m { - newM[k] = v - } - newM[statusCode] = h - statusLines.Store(newM) - return h +func invalidStatusLine(statusCode int) []byte { + statusText := StatusMessage(statusCode) + return []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", statusCode, statusText)) } |