[server] Minor refactoring (take 2).

This commit is contained in:
Ciprian Dorin Craciun 2022-08-26 17:18:52 +03:00
parent 89761ff44e
commit d8bbd477d5

View file

@ -127,9 +127,6 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
return
}
_pathIsRoot := _pathLen == 1
_pathHasSlash := !_pathIsRoot && (_path[_pathLen - 1] == '/')
if bytes.HasPrefix (_path, StringToBytes ("/__/")) {
if bytes.Equal (_path, StringToBytes ("/__/heartbeat")) || bytes.HasPrefix (_path, StringToBytes ("/__/heartbeat/")) {
_server.ServeStatic (_context, http.StatusOK, HeartbeatDataOk, HeartbeatContentType, HeartbeatContentEncoding, false)
@ -158,32 +155,15 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
var _referencesValues [2]uint64
var _referencesBuffer []byte
var _namespaceAndPathSuffixes [][2]string
if true {
_namespaceAndPathSuffixes = _namespaceAndPathSuffixes_a_static
} else {
_namespaceAndPathSuffixes = _namespaceAndPathSuffixes_b_static
}
if !_referencesFound {
for _hostIdx := 0; _hostIdx < 2; _hostIdx += 1 {
if (_hostIdx == 0) && (_server.hostsDisabled) {
if (_hostIdx == 0) && _server.hostsDisabled {
continue
}
for _, _namespaceAndPathSuffix := range _namespaceAndPathSuffixes {
_namespace := _namespaceAndPathSuffix[0]
_pathSuffix := _namespaceAndPathSuffix[1]
if !_pathIsRoot && !_pathHasSlash {
// NOP
} else if _pathSuffix == "" {
// NOP
} else if (_pathSuffix[0] == '/') && (_pathIsRoot || _pathHasSlash) {
_pathSuffix = _pathSuffix[1:]
}
for _, _namespace := range _namespaces_a_static {
if _server.cachedReferences != nil {
_key := _keyBufferLarge[:0]
@ -193,7 +173,6 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
_key = append (_key, _requestHost ...)
}
_key = append (_key, _path ...)
_key = append (_key, _pathSuffix ...)
_referencesValues, _referencesFound = _server.cachedReferences[BytesToString (*NoEscapeBytes (&_key))]
} else {
_key := _keyBufferLarge[:0]
@ -203,7 +182,6 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
_key = append (_key, _requestHost ...)
}
_key = append (_key, _path ...)
_key = append (_key, _pathSuffix ...)
if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
_referencesBuffer = _value
_referencesFound = _value != nil
@ -212,20 +190,6 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
return
}
}
if _referencesFound {
if _pathSuffix == "/*" {
_pathSuffix = "/"
} else if _pathSuffix == "*" {
_pathSuffix = ""
}
if _pathSuffix != "" {
_path = append (_path, _pathSuffix ...)
_server.ServeRedirect (_context, http.StatusTemporaryRedirect, _path, true)
return
}
break
}
}
if _referencesFound {
@ -245,7 +209,7 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
for _hostIdx := 0; _hostIdx < 2; _hostIdx += 1 {
if (_hostIdx == 0) && (_server.hostsDisabled) {
if (_hostIdx == 0) && _server.hostsDisabled {
continue
}
@ -254,7 +218,7 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
_pathLimit >= 0;
_pathLimit = bytes.LastIndexByte (_path[: _pathLimit], '/') {
for _, _namespace := range _namespace_c_static {
for _, _namespace := range _namespaces_b_static {
if _server.cachedReferences != nil {
_key := _keyBufferLarge[:0]
@ -442,24 +406,13 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
}
var _namespaceAndPathSuffixes_a_static = [][2]string {
{NamespaceFilesContent, ""},
{NamespaceRedirectsContent, ""},
{NamespaceFoldersContent, ""},
var _namespaces_a_static = []string {
NamespaceFilesContent,
NamespaceRedirectsContent,
NamespaceFoldersContent,
}
var _namespaceAndPathSuffixes_b_static = [][2]string {
{NamespaceFilesContent, ""},
{NamespaceRedirectsContent, ""},
{NamespaceFilesContent, "/"},
{NamespaceRedirectsContent, "/"},
{NamespaceFilesContent, "/*"},
{NamespaceRedirectsContent, "/*"},
{NamespaceFoldersContent, ""},
{NamespaceFoldersContent, "/"},
}
var _namespace_c_static = []string {
var _namespaces_b_static = []string {
NamespaceFilesContent,
NamespaceRedirectsContent,
}