[server] Switch to using upstream fasthttp implementation.

This commit is contained in:
Ciprian Dorin Craciun 2021-11-17 21:35:35 +02:00
parent 6d24ad3252
commit 83bd67f14c
2 changed files with 44 additions and 59 deletions

View file

@ -270,50 +270,45 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
_responseStatus := http.StatusOK _responseStatus := http.StatusOK
_responseHeaders.AddRawLines (_dataMetaRaw) //! _responseHeaders.AddRawLines (_dataMetaRaw)
// FIXME: Re-enable this part! _handleHeader := func (_name []byte, _value []byte) {
// _handleHeader := func (_name []byte, _value []byte) { if _name[0] != '!' {
// if _name[0] != '_' { _responseHeaders.AddBytesKV (_name, _value)
// _responseHeaders.AddRawKv (_name, _value) } else {
// } else { switch BytesToString (_name) {
// switch BytesToString (_name) { case "!Status" :
// case "!Status" : if _value, _error := strconv.Atoi (BytesToString (_value)); _error == nil {
// if _value, _error := strconv.Atoi (BytesToString (_value)); _error == nil { if (_value >= 200) && (_value <= 599) {
// if (_value >= 200) && (_value <= 599) { _responseStatus = _value
// _responseStatus = _value } else {
// } else { log.Printf ("[c2f7ec36] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
// log.Printf ("[c2f7ec36] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) _responseStatus = http.StatusInternalServerError
// _responseStatus = http.StatusInternalServerError }
// } } else {
// } else { log.Printf ("[beedae55] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
// log.Printf ("[beedae55] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) _responseStatus = http.StatusInternalServerError
// _responseStatus = http.StatusInternalServerError }
// } default :
// default : log.Printf ("[7acc7d90] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
// log.Printf ("[7acc7d90] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) }
// } }
// } }
// } if _error := MetadataDecodeIterate (_dataMetaRaw, _handleHeader); _error != nil {
// if _error := MetadataDecodeIterate (_dataMetaRaw, _handleHeader); _error != nil { _server.ServeError (_context, http.StatusInternalServerError, _error, false)
// _server.ServeError (_context, http.StatusInternalServerError, _error, false) return
// return }
// }
if _server.securityHeadersEnabled { if _server.securityHeadersEnabled {
if _server.securityHeadersTls { if _server.securityHeadersTls {
const _lines = ( _responseHeaders.AddBytesKV (StringToBytes ("Strict-Transport-Security"), StringToBytes ("max-age=31536000"))
"Strict-Transport-Security: max-age=31536000" + "\r\n" + _responseHeaders.AddBytesKV (StringToBytes ("Content-Security-Policy"), StringToBytes ("upgrade-insecure-requests"))
"Content-Security-Policy: upgrade-insecure-requests" + "\r\n")
_responseHeaders.AddRawLines (StringToBytes (_lines))
} }
{ {
const _lines = ( _responseHeaders.AddBytesKV (StringToBytes ("Referrer-Policy"), StringToBytes ("strict-origin-when-cross-origin"))
"Referrer-Policy: strict-origin-when-cross-origin" + "\r\n" + _responseHeaders.AddBytesKV (StringToBytes ("X-Content-Type-Options"), StringToBytes ("nosniff"))
"X-Content-Type-Options: nosniff" + "\r\n" + _responseHeaders.AddBytesKV (StringToBytes ("X-XSS-Protection"), StringToBytes ("1; mode=block"))
"X-XSS-Protection: 1; mode=block" + "\r\n" + _responseHeaders.AddBytesKV (StringToBytes ("X-Frame-Options"), StringToBytes ("sameorigin"))
"X-Frame-Options: sameorigin" + "\r\n")
_responseHeaders.AddRawLines (StringToBytes (_lines))
} }
} }
@ -337,13 +332,13 @@ func (_server *server) ServeStatic (_context *fasthttp.RequestCtx, _status uint,
_response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response)))
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
_responseHeaders.AddRawKv (StringToBytes ("Content-Type"), StringToBytes (_contentType)) _responseHeaders.AddBytesKV (StringToBytes ("Content-Type"), StringToBytes (_contentType))
_responseHeaders.AddRawKv (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding)) _responseHeaders.AddBytesKV (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding))
if _cache { if _cache {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: public, immutable, max-age=3600\r\n")) _responseHeaders.AddBytesKV (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
} else { } else {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: private, no-cache, no-store\r\n")) _responseHeaders.AddBytesKV (StringToBytes ("Cache-Control"), StringToBytes ("private, no-cache, no-store"))
} }
_response.SetStatusCode (int (_status)) _response.SetStatusCode (int (_status))
@ -359,9 +354,9 @@ func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uin
_responseHeaders.SetCanonical (StringToBytes ("Location"), _path) _responseHeaders.SetCanonical (StringToBytes ("Location"), _path)
if _cache { if _cache {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: public, immutable, max-age=3600\r\n")) _responseHeaders.AddBytesKV (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
} else { } else {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: private, no-cache, no-store\r\n")) _responseHeaders.AddBytesKV (StringToBytes ("Cache-Control"), StringToBytes ("private, no-cache, no-store"))
} }
_response.SetStatusCode (int (_status)) _response.SetStatusCode (int (_status))
@ -373,13 +368,13 @@ func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint,
_response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response)))
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
_responseHeaders.AddRawKv (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType)) _responseHeaders.AddBytesKV (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType))
_responseHeaders.AddRawKv (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding)) _responseHeaders.AddBytesKV (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding))
if _cache { if _cache {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: public, immutable, max-age=3600\r\n")) _responseHeaders.AddBytesKV (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
} else { } else {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: private, no-cache, no-store\r\n")) _responseHeaders.AddBytesKV (StringToBytes ("Cache-Control"), StringToBytes ("private, no-cache, no-store"))
} }
if _banner, _bannerFound := ErrorBannersData[_status]; _bannerFound { if _banner, _bannerFound := ErrorBannersData[_status]; _bannerFound {
@ -393,16 +388,7 @@ func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint,
func (_server *server) ServeDummy (_context *fasthttp.RequestCtx) () { func (_server *server) ServeDummy (_context *fasthttp.RequestCtx) () {
if false {
_server.ServeStatic (_context, http.StatusOK, DummyData, DummyContentType, DummyContentEncoding, false) _server.ServeStatic (_context, http.StatusOK, DummyData, DummyContentType, DummyContentEncoding, false)
} else {
ServeDummyRaw (_context)
}
}
func ServeDummyRaw (_context *fasthttp.RequestCtx) () {
_context.Response.Header.SetRaw (DummyMeta)
_context.Response.SetBodyRaw (DummyData)
} }

View file

@ -17,7 +17,6 @@ var HeartbeatDataNok = []byte ("NOK\n")
var DummyContentType = MimeTypeText var DummyContentType = MimeTypeText
var DummyContentEncoding = "identity" var DummyContentEncoding = "identity"
var DummyMeta = []byte ("HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\n")
var DummyData = []byte ("hello world!\n") var DummyData = []byte ("hello world!\n")