[server] Use a more performant header configuration method // do not include Content-Length in metadata

This commit is contained in:
Ciprian Dorin Craciun 2019-08-12 17:22:29 +03:00
parent 2f76bed806
commit 21d157e472
4 changed files with 46 additions and 41 deletions

Binary file not shown.

View file

@ -410,7 +410,7 @@ func prepareDataContent (_context *context, _pathResolved string, _pathInArchive
}
_dataMeta := make (map[string]string, 16)
_dataMeta["Content-Length"] = fmt.Sprintf ("%d", _dataSize)
// _dataMeta["Content-Length"] = fmt.Sprintf ("%d", _dataSize)
_dataMeta["Content-Type"] = _dataType
_dataMeta["Content-Encoding"] = _dataEncoding
if _context.includeEtag {

View file

@ -107,14 +107,14 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
if _server.securityHeadersEnabled {
if _server.securityHeadersTls {
_responseHeaders.SetCanonical (StringToBytes ("Strict-Transport-Security"), StringToBytes ("max-age=31536000"))
_responseHeaders.SetCanonical (StringToBytes ("Content-Security-Policy"), StringToBytes ("upgrade-insecure-requests"))
_responseHeaders.AddRaw (StringToBytes ("Strict-Transport-Security"), StringToBytes ("max-age=31536000"))
_responseHeaders.AddRaw (StringToBytes ("Content-Security-Policy"), StringToBytes ("upgrade-insecure-requests"))
}
{
_responseHeaders.SetCanonical (StringToBytes ("Referrer-Policy"), StringToBytes ("strict-origin-when-cross-origin"))
_responseHeaders.SetCanonical (StringToBytes ("X-Content-Type-Options"), StringToBytes ("nosniff"))
_responseHeaders.SetCanonical (StringToBytes ("X-XSS-Protection"), StringToBytes ("1; mode=block"))
_responseHeaders.SetCanonical (StringToBytes ("X-Frame-Options"), StringToBytes ("sameorigin"))
_responseHeaders.AddRaw (StringToBytes ("Referrer-Policy"), StringToBytes ("strict-origin-when-cross-origin"))
_responseHeaders.AddRaw (StringToBytes ("X-Content-Type-Options"), StringToBytes ("nosniff"))
_responseHeaders.AddRaw (StringToBytes ("X-XSS-Protection"), StringToBytes ("1; mode=block"))
_responseHeaders.AddRaw (StringToBytes ("X-Frame-Options"), StringToBytes ("sameorigin"))
}
}
@ -226,7 +226,7 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
_fingerprintContent := _fingerprints[0:64]
_fingerprintMeta := _fingerprints[65:129]
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
var _data []byte
if _server.cachedDataContent != nil {
@ -272,26 +272,25 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
_responseStatus := http.StatusOK
_handleHeader := func (_name []byte, _value []byte) {
switch {
case len (_name) == 0 :
log.Printf ("[90009821] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
_responseStatus = http.StatusInternalServerError
case _name[0] != '_' :
_responseHeaders.SetCanonical (_name, _value)
case bytes.Equal (_name, StringToBytes ("_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
if _name[0] != '_' {
_responseHeaders.AddRaw (_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
}
} 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 ())
default :
log.Printf ("[7acc7d90] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
}
}
}
if _error := MetadataDecodeIterate (_dataMetaRaw, _handleHeader); _error != nil {
@ -315,13 +314,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.SetCanonical (StringToBytes ("Content-Type"), StringToBytes (_contentType))
_responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding))
_responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (_contentType))
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding))
if _cache {
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
} else {
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
}
_response.SetStatusCode (int (_status))
@ -334,17 +333,17 @@ func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uin
_response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response)))
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
_responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes ("identity"))
_responseHeaders.SetCanonical (StringToBytes ("Location"), _path)
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes ("identity"))
_responseHeaders.AddRaw (StringToBytes ("Location"), _path)
if _cache {
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
} else {
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
}
_responseHeaders.SetCanonical (StringToBytes ("Content-Type"), StringToBytes (MimeTypeText))
_responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes ("identity"))
_responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (MimeTypeText))
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes ("identity"))
_response.SetStatusCode (int (_status))
}
@ -356,13 +355,13 @@ func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint,
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
if _cache {
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
} else {
_responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
}
_responseHeaders.SetCanonical (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType))
_responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding))
_responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType))
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding))
if _banner, _bannerFound := ErrorBannersData[_status]; _bannerFound {
_response.SetBodyRaw (_banner)

View file

@ -86,6 +86,12 @@ func MetadataDecodeIterate (_data []byte, _callback func ([]byte, []byte) ()) (e
}
_key := _data[: _separator]
_value := _data[_separator + 3 :]
if len (_key) == 0 {
return fmt.Errorf ("[c3f5e8f3] invalid metadata encoding (empty key)")
}
if len (_value) == 0 {
return fmt.Errorf ("[d6a923b6] invalid metadata encoding (empty value)")
}
_callback (_key, _value)
}