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