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