[server] Add support for redirects and folders content.

This commit is contained in:
Ciprian Dorin Craciun 2022-08-26 14:21:25 +03:00
parent 53445228b7
commit 5482499027

View file

@ -158,27 +158,28 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
var _namespaceAndPathSuffixes = [][2]string {
{NamespaceFilesContent, ""},
{NamespaceRedirectsContent, ""},
{NamespaceFilesContent, "/"},
{NamespaceRedirectsContent, "/"},
{NamespaceFilesContent, "/*"},
{NamespaceRedirectsContent, "/*"},
{NamespaceFoldersContent, ""},
{NamespaceFoldersContent, "/"},
}
if !_referencesFound {
_loop_1 : for _namespaceAndPathSuffixIndex := range _namespaceAndPathSuffixes {
for _namespaceAndPathSuffixIndex := range _namespaceAndPathSuffixes {
_namespaceAndPathSuffix := _namespaceAndPathSuffixes[_namespaceAndPathSuffixIndex]
_namespace := _namespaceAndPathSuffix[0]
_pathSuffix := _namespaceAndPathSuffix[1]
switch {
case !_pathIsRoot && !_pathHasSlash :
if !_pathIsRoot && !_pathHasSlash {
// NOP
case _pathSuffix == "/" :
continue _loop_1
case _pathSuffix == "" :
} else if _pathSuffix == "" {
// NOP
case _pathSuffix[0] == '/' :
} else if (_pathSuffix[0] == '/') && (_pathIsRoot || _pathHasSlash) {
_pathSuffix = _pathSuffix[1:]
}
_pathSuffixHasSlash := (len (_pathSuffix) != 0) && (_pathSuffix[0] == '/')
if _server.cachedReferences != nil {
_key := _keyBufferLarge[:0]
@ -201,12 +202,17 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
}
if _referencesFound {
if ((_namespace == NamespaceFoldersContent) || _pathSuffixHasSlash) && (!_pathIsRoot && !_pathHasSlash) {
_path = append (_path, '/')
if _pathSuffix == "/*" {
_pathSuffix = "/"
} else if _pathSuffix == "*" {
_pathSuffix = ""
}
if _pathSuffix != "" {
_path = append (_path, _pathSuffix ...)
_server.ServeRedirect (_context, http.StatusTemporaryRedirect, _path, true)
return
}
break _loop_1
break
}
}
}
@ -219,20 +225,22 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
}
if !_referencesFound {
_loop_2 : for
for
_pathLimit := bytes.LastIndexByte (_path, '/');
_pathLimit >= 0;
_pathLimit = bytes.LastIndexByte (_path[: _pathLimit], '/') {
for _, _namespace := range []string { NamespaceFilesContent, NamespaceRedirectsContent } {
if _server.cachedReferences != nil {
_key := _keyBufferLarge[:0]
_key = append (_key, NamespaceFilesContentPrefix, ':')
_key = append (_key, KeyNamespacePrefix (_namespace), ':')
_key = append (_key, _path[: _pathLimit] ...)
_key = append (_key, "/*" ...)
_referencesValues, _referencesFound = _server.cachedReferences[BytesToString (*NoEscapeBytes (&_key))]
} else {
_key := _keyBufferLarge[:0]
_key = append (_key, NamespaceFilesContentPrefix, ':')
_key = append (_key, KeyNamespacePrefix (_namespace), ':')
_key = append (_key, _path[: _pathLimit] ...)
_key = append (_key, "/*" ...)
if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
@ -245,7 +253,12 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
}
if _referencesFound {
break _loop_2
break
}
}
if _referencesFound {
break
}
}
}
@ -1260,36 +1273,58 @@ func main_0 () (error) {
}
if _indexPaths || _indexDataMeta || _indexDataContent {
if !_quiet {
log.Printf ("[ii] [fa5338fd] [cdb.....] indexing archive...\n")
}
var _filesIndexKey string
if _key_0, _error := PrepareKeyToString (NamespaceFilesIndex, 1); _error == nil {
_filesIndexKey = _key_0
for _, _namespaceContents := range []string { NamespaceFilesContent, NamespaceFoldersContent, NamespaceRedirectsContent } {
_namespaceIndex := ""
switch _namespaceContents {
case NamespaceFilesContent : _namespaceIndex = NamespaceFilesIndex
case NamespaceFoldersContent : _namespaceIndex = NamespaceFoldersIndex
case NamespaceRedirectsContent : _namespaceIndex = NamespaceRedirectsIndex
}
var _indexKey string
if _key_0, _error := PrepareKeyToString (_namespaceIndex, 1); _error == nil {
_indexKey = _key_0
} else {
AbortError (_error, "[5289ee67] [cdb.....] failed indexing archive!")
}
if _filesIndex, _error := _cdbReader.GetWithCdbHash (StringToBytes (_filesIndexKey)); _error == nil {
if _filesIndex != nil {
var _index []byte
if _index_0, _error := _cdbReader.GetWithCdbHash (StringToBytes (_indexKey)); _error == nil {
_index = _index_0
} else {
AbortError (_error, "[82299b3d] [cdb.....] failed indexing arcdive!")
}
if _index == nil {
log.Printf ("[ww] [30314f31] [cdb.....] missing archive index `%s`; ignoring!\n", _namespaceIndex)
continue
}
_keyBuffer := [1024]byte {}
_keysCount := 0
for {
_offset := bytes.IndexByte (_filesIndex, '\n')
_offset := bytes.IndexByte (_index, '\n')
if _offset == 0 {
continue
}
if _offset == -1 {
break
}
_filePath := _filesIndex[: _offset]
_filesIndex = _filesIndex[_offset + 1 :]
var _fileReferences []byte
_path := _index[: _offset]
_index = _index[_offset + 1 :]
var _references []byte
{
_key := _keyBuffer[:0]
_key = append (_key, NamespaceFilesContentPrefix, ':')
_key = append (_key, _filePath ...)
_key = append (_key, KeyNamespacePrefix (_namespaceContents), ':')
_key = append (_key, _path ...)
if _references_0, _error := _cdbReader.GetWithCdbHash (_key); _error == nil {
if _references_0 != nil {
_fileReferences = _references_0
_references = _references_0
} else {
AbortError (_error, "[460b3cf1] [cdb.....] failed indexing archive!")
}
@ -1298,7 +1333,7 @@ func main_0 () (error) {
}
}
var _keyDataMeta, _keyDataContent uint64
if _keyDataMeta_0, _keyDataContent_0, _error := DecodeKeysPair (_fileReferences); _error == nil {
if _keyDataMeta_0, _keyDataContent_0, _error := DecodeKeysPair (_references); _error == nil {
_keyDataMeta = _keyDataMeta_0
_keyDataContent = _keyDataContent_0
} else {
@ -1306,8 +1341,8 @@ func main_0 () (error) {
}
if _indexPaths {
_key := _keyBuffer[:0]
_key = append (_key, NamespaceFilesContentPrefix, ':')
_key = append (_key, _filePath ...)
_key = append (_key, KeyNamespacePrefix (_namespaceContents), ':')
_key = append (_key, _path ...)
_cachedReferences[string (_key)] = [2]uint64 { _keyDataMeta, _keyDataContent }
}
if _indexDataMeta {
@ -1344,10 +1379,17 @@ func main_0 () (error) {
}
}
}
_keysCount += 1
}
if !_quiet {
log.Printf ("[ii] [0c2abafe] [cdb.....] indexed %d references in `%s`;\n", _keysCount, _namespaceIndex)
}
}
if !_quiet {
if _indexPaths {
log.Printf ("[ii] [6b7ec5d9] [cdb.....] cached %d file references;\n", len (_cachedReferences))
log.Printf ("[ii] [6b7ec5d9] [cdb.....] cached %d references;\n", len (_cachedReferences))
}
if _indexDataMeta {
log.Printf ("[ii] [5ec4f113] [cdb.....] cached %d meta-data blocks;\n", len (_cachedDataMeta))
@ -1356,17 +1398,18 @@ func main_0 () (error) {
log.Printf ("[ii] [d9680a2f] [cdb.....] cached %d content blocks;\n", len (_cachedDataContent))
}
}
} else {
log.Printf ("[ww] [30314f31] [cdb.....] missing archive files index; ignoring!\n")
if len (_cachedReferences) == 0 {
_indexPaths = false
_indexDataMeta = false
_indexDataContent = false
_cachedReferences = nil
_cachedDataMeta = nil
_cachedDataContent = nil
}
} else {
AbortError (_error, "[82299b3d] [cdb.....] failed indexing arcdive!")
if len (_cachedDataMeta) == 0 {
_indexDataMeta = false
_cachedDataMeta = nil
}
if len (_cachedDataContent) == 0 {
_indexDataContent = false
_cachedDataContent = nil
}
}