[server][documentation] Add support for virtual hosts.

This commit is contained in:
Ciprian Dorin Craciun 2022-08-26 14:46:40 +03:00
parent 5482499027
commit ac505b1b65
6 changed files with 133 additions and 57 deletions

View file

@ -74,6 +74,8 @@ kawipiko -- blazingly fast static HTTP server \- kawipiko-server
\-\-index\-data\-meta \-\-index\-data\-meta
\-\-index\-data\-content \-\-index\-data\-content
\-\-hosts\-disable (ignore \(gaHost\(ga header)
\-\-security\-headers\-tls \-\-security\-headers\-tls
\-\-security\-headers\-disable \-\-security\-headers\-disable
@ -208,6 +210,13 @@ it is recommended to use either \fB\-\-archive\-mmap\fP or \fB\-\-archive\-inme
.UNINDENT .UNINDENT
.UNINDENT .UNINDENT
.sp .sp
\fB\-\-hosts\-disable\fP
.INDENT 0.0
.INDENT 3.5
Disables the virtual\-hosts feature by ignoring the \fIHost\fP header.
.UNINDENT
.UNINDENT
.sp
\fB\-\-security\-headers\-tls\fP \fB\-\-security\-headers\-tls\fP
.INDENT 0.0 .INDENT 0.0
.INDENT 3.5 .INDENT 3.5

View file

@ -45,6 +45,8 @@ kawipiko -- blazingly fast static HTTP server
--index-data-meta --index-data-meta
--index-data-content --index-data-content
--hosts-disable (ignore `Host` header)
--security-headers-tls --security-headers-tls
--security-headers-disable --security-headers-disable
@ -144,6 +146,10 @@ Flags
* it is recommended to use either ``--archive-mmap`` or ``--archive-inmem``, else (especially if data is indexed) the resulting effect is that of loading everything in RAM; * it is recommended to use either ``--archive-mmap`` or ``--archive-inmem``, else (especially if data is indexed) the resulting effect is that of loading everything in RAM;
``--hosts-disable``
Disables the virtual-hosts feature by ignoring the `Host` header.
``--security-headers-tls`` ``--security-headers-tls``
Enables adding the following TLS related headers to the response: :: Enables adding the following TLS related headers to the response: ::

View file

@ -36,6 +36,8 @@ NAME
--index-data-meta --index-data-meta
--index-data-content --index-data-content
--hosts-disable (ignore `Host` header)
--security-headers-tls --security-headers-tls
--security-headers-disable --security-headers-disable
@ -175,6 +177,9 @@ FLAGS
--archive-inmem, else (especially if data is indexed) the --archive-inmem, else (especially if data is indexed) the
resulting effect is that of loading everything in RAM; resulting effect is that of loading everything in RAM;
--hosts-disable
Disables the virtual-hosts feature by ignoring the Host header.
--security-headers-tls --security-headers-tls
Enables adding the following TLS related headers to the response: Enables adding the following TLS related headers to the response:

View file

@ -36,6 +36,8 @@ NAME
--index-data-meta --index-data-meta
--index-data-content --index-data-content
--hosts-disable (ignore `Host` header)
--security-headers-tls --security-headers-tls
--security-headers-disable --security-headers-disable
@ -175,6 +177,9 @@ FLAGS
--archive-inmem, else (especially if data is indexed) the --archive-inmem, else (especially if data is indexed) the
resulting effect is that of loading everything in RAM; resulting effect is that of loading everything in RAM;
--hosts-disable
Disables the virtual-hosts feature by ignoring the Host header.
--security-headers-tls --security-headers-tls
Enables adding the following TLS related headers to the response: Enables adding the following TLS related headers to the response:

View file

@ -52,6 +52,7 @@ type server struct {
cachedReferences map[string][2]uint64 cachedReferences map[string][2]uint64
cachedDataMeta map[uint64][]byte cachedDataMeta map[uint64][]byte
cachedDataContent map[uint64][]byte cachedDataContent map[uint64][]byte
hostsDisabled bool
securityHeadersEnabled bool securityHeadersEnabled bool
securityHeadersTls bool securityHeadersTls bool
http1Disabled bool http1Disabled bool
@ -89,6 +90,7 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_response.Header))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_response.Header)))
_requestMethod := _requestHeaders.Method () _requestMethod := _requestHeaders.Method ()
_requestHost := _requestHeaders.Host ()
_requestUri := _requestHeaders.RequestURI () _requestUri := _requestHeaders.RequestURI ()
_requestUriString_0 := BytesToString (_requestUri) _requestUriString_0 := BytesToString (_requestUri)
_requestUriString := NoEscapeString (&_requestUriString_0) _requestUriString := NoEscapeString (&_requestUriString_0)
@ -168,50 +170,70 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
} }
if !_referencesFound { if !_referencesFound {
for _namespaceAndPathSuffixIndex := range _namespaceAndPathSuffixes {
_namespaceAndPathSuffix := _namespaceAndPathSuffixes[_namespaceAndPathSuffixIndex] for _hostIdx := 0; _hostIdx < 2; _hostIdx += 1 {
_namespace := _namespaceAndPathSuffix[0]
_pathSuffix := _namespaceAndPathSuffix[1]
if !_pathIsRoot && !_pathHasSlash { if (_hostIdx == 0) && (_server.hostsDisabled) {
// NOP continue
} else if _pathSuffix == "" {
// NOP
} else if (_pathSuffix[0] == '/') && (_pathIsRoot || _pathHasSlash) {
_pathSuffix = _pathSuffix[1:]
} }
if _server.cachedReferences != nil { for _namespaceAndPathSuffixIndex := range _namespaceAndPathSuffixes {
_key := _keyBufferLarge[:0] _namespaceAndPathSuffix := _namespaceAndPathSuffixes[_namespaceAndPathSuffixIndex]
_key = append (_key, KeyNamespacePrefix (_namespace), ':') _namespace := _namespaceAndPathSuffix[0]
_key = append (_key, _path ...) _pathSuffix := _namespaceAndPathSuffix[1]
_key = append (_key, _pathSuffix ...)
_referencesValues, _referencesFound = _server.cachedReferences[BytesToString (*NoEscapeBytes (&_key))] if !_pathIsRoot && !_pathHasSlash {
} else { // NOP
_key := _keyBufferLarge[:0] } else if _pathSuffix == "" {
_key = append (_key, KeyNamespacePrefix (_namespace), ':') // NOP
_key = append (_key, _path ...) } else if (_pathSuffix[0] == '/') && (_pathIsRoot || _pathHasSlash) {
_key = append (_key, _pathSuffix ...) _pathSuffix = _pathSuffix[1:]
if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil { }
_referencesBuffer = _value
_referencesFound = _value != nil if _server.cachedReferences != nil {
_key := _keyBufferLarge[:0]
_key = append (_key, KeyNamespacePrefix (_namespace), ':')
if _hostIdx == 0 {
_key = append (_key, "://" ...)
_key = append (_key, _requestHost ...)
}
_key = append (_key, _path ...)
_key = append (_key, _pathSuffix ...)
_referencesValues, _referencesFound = _server.cachedReferences[BytesToString (*NoEscapeBytes (&_key))]
} else { } else {
_server.ServeError (_context, http.StatusInternalServerError, _error, false) _key := _keyBufferLarge[:0]
return _key = append (_key, KeyNamespacePrefix (_namespace), ':')
if _hostIdx == 0 {
_key = append (_key, "://" ...)
_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
} else {
_server.ServeError (_context, http.StatusInternalServerError, _error, false)
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 { if _referencesFound {
if _pathSuffix == "/*" {
_pathSuffix = "/"
} else if _pathSuffix == "*" {
_pathSuffix = ""
}
if _pathSuffix != "" {
_path = append (_path, _pathSuffix ...)
_server.ServeRedirect (_context, http.StatusTemporaryRedirect, _path, true)
return
}
break break
} }
} }
@ -225,30 +247,50 @@ func (_server *server) ServeUnwrapped (_context *fasthttp.RequestCtx) () {
} }
if !_referencesFound { if !_referencesFound {
for
_pathLimit := bytes.LastIndexByte (_path, '/'); for _hostIdx := 0; _hostIdx < 2; _hostIdx += 1 {
_pathLimit >= 0;
_pathLimit = bytes.LastIndexByte (_path[: _pathLimit], '/') {
for _, _namespace := range []string { NamespaceFilesContent, NamespaceRedirectsContent } { if (_hostIdx == 0) && (_server.hostsDisabled) {
continue
}
for
_pathLimit := bytes.LastIndexByte (_path, '/');
_pathLimit >= 0;
_pathLimit = bytes.LastIndexByte (_path[: _pathLimit], '/') {
if _server.cachedReferences != nil { for _, _namespace := range []string { NamespaceFilesContent, NamespaceRedirectsContent } {
_key := _keyBufferLarge[:0]
_key = append (_key, KeyNamespacePrefix (_namespace), ':') if _server.cachedReferences != nil {
_key = append (_key, _path[: _pathLimit] ...) _key := _keyBufferLarge[:0]
_key = append (_key, "/*" ...) _key = append (_key, KeyNamespacePrefix (_namespace), ':')
_referencesValues, _referencesFound = _server.cachedReferences[BytesToString (*NoEscapeBytes (&_key))] if _hostIdx == 0 {
} else { _key = append (_key, "://" ...)
_key := _keyBufferLarge[:0] _key = append (_key, _requestHost ...)
_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 { _referencesValues, _referencesFound = _server.cachedReferences[BytesToString (*NoEscapeBytes (&_key))]
_referencesBuffer = _value
_referencesFound = _value != nil
} else { } else {
_server.ServeError (_context, http.StatusInternalServerError, _error, false) _key := _keyBufferLarge[:0]
return _key = append (_key, KeyNamespacePrefix (_namespace), ':')
if _hostIdx == 0 {
_key = append (_key, "://" ...)
_key = append (_key, _requestHost ...)
}
_key = append (_key, _path[: _pathLimit] ...)
_key = append (_key, "/*" ...)
if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
_referencesBuffer = _value
_referencesFound = _value != nil
} else {
_server.ServeError (_context, http.StatusInternalServerError, _error, false)
return
}
}
if _referencesFound {
break
} }
} }
@ -689,6 +731,7 @@ func main_0 () (error) {
var _indexPaths bool var _indexPaths bool
var _indexDataMeta bool var _indexDataMeta bool
var _indexDataContent bool var _indexDataContent bool
var _hostsDisabled bool
var _securityHeadersEnabled bool var _securityHeadersEnabled bool
var _securityHeadersTls bool var _securityHeadersTls bool
var _timeoutDisabled bool var _timeoutDisabled bool
@ -733,6 +776,7 @@ func main_0 () (error) {
_indexDataMeta_0 := _flags.Bool ("index-data-meta", false, "") _indexDataMeta_0 := _flags.Bool ("index-data-meta", false, "")
_indexDataContent_0 := _flags.Bool ("index-data-content", false, "") _indexDataContent_0 := _flags.Bool ("index-data-content", false, "")
_timeoutDisabled_0 := _flags.Bool ("timeout-disable", false, "") _timeoutDisabled_0 := _flags.Bool ("timeout-disable", false, "")
_hostsDisabled_0 := _flags.Bool ("hosts-disable", false, "")
_securityHeadersTls_0 := _flags.Bool ("security-headers-tls", false, "") _securityHeadersTls_0 := _flags.Bool ("security-headers-tls", false, "")
_securityHeadersDisabled_0 := _flags.Bool ("security-headers-disable", false, "") _securityHeadersDisabled_0 := _flags.Bool ("security-headers-disable", false, "")
_tlsPrivate_0 := _flags.String ("tls-private", "", "") _tlsPrivate_0 := _flags.String ("tls-private", "", "")
@ -771,6 +815,7 @@ func main_0 () (error) {
_indexPaths = _indexAll || *_indexPaths_0 _indexPaths = _indexAll || *_indexPaths_0
_indexDataMeta = _indexAll || *_indexDataMeta_0 _indexDataMeta = _indexAll || *_indexDataMeta_0
_indexDataContent = _indexAll || *_indexDataContent_0 _indexDataContent = _indexAll || *_indexDataContent_0
_hostsDisabled = *_hostsDisabled_0
_securityHeadersTls = *_securityHeadersTls_0 _securityHeadersTls = *_securityHeadersTls_0
_securityHeadersEnabled = ! *_securityHeadersDisabled_0 _securityHeadersEnabled = ! *_securityHeadersDisabled_0
_timeoutDisabled = *_timeoutDisabled_0 _timeoutDisabled = *_timeoutDisabled_0
@ -997,6 +1042,9 @@ func main_0 () (error) {
if _indexDataContent { if _indexDataContent {
_processArguments = append (_processArguments, "--index-data-content") _processArguments = append (_processArguments, "--index-data-content")
} }
if _hostsDisabled {
_processArguments = append (_processArguments, "--hosts-disabled")
}
if _securityHeadersTls { if _securityHeadersTls {
_processArguments = append (_processArguments, "--security-headers-tls") _processArguments = append (_processArguments, "--security-headers-tls")
} }
@ -1476,6 +1524,7 @@ func main_0 () (error) {
cachedReferences : _cachedReferences, cachedReferences : _cachedReferences,
cachedDataMeta : _cachedDataMeta, cachedDataMeta : _cachedDataMeta,
cachedDataContent : _cachedDataContent, cachedDataContent : _cachedDataContent,
hostsDisabled : _hostsDisabled,
securityHeadersTls : _securityHeadersTls, securityHeadersTls : _securityHeadersTls,
securityHeadersEnabled : _securityHeadersEnabled, securityHeadersEnabled : _securityHeadersEnabled,
http1Disabled : _http1Disabled, http1Disabled : _http1Disabled,

View file

@ -30,6 +30,8 @@
--index-data-meta --index-data-meta
--index-data-content --index-data-content
--hosts-disable (ignore `Host` header)
--security-headers-tls --security-headers-tls
--security-headers-disable --security-headers-disable