[sources] Move Cache-Control header into archive // Add support for custom fasthttp fork AddRaw* features

This commit is contained in:
Ciprian Dorin Craciun 2019-08-12 22:06:00 +03:00
parent f1937e52bc
commit bf0d448e04
3 changed files with 78 additions and 62 deletions

View file

@ -37,6 +37,7 @@ type context struct {
compress string compress string
includeIndex bool includeIndex bool
includeStripped bool includeStripped bool
includeCache bool
includeEtag bool includeEtag bool
includeFileListing bool includeFileListing bool
includeFolderListing bool includeFolderListing bool
@ -410,9 +411,14 @@ func prepareDataContent (_context *context, _pathResolved string, _pathInArchive
} }
_dataMeta := make (map[string]string, 16) _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-Type"] = _dataType
_dataMeta["Content-Encoding"] = _dataEncoding _dataMeta["Content-Encoding"] = _dataEncoding
if _context.includeCache {
_dataMeta["Cache-Control"] = "public, immutable, max-age=3600"
}
if _context.includeEtag { if _context.includeEtag {
_dataMeta["ETag"] = _fingerprintContent _dataMeta["ETag"] = _fingerprintContent
} }
@ -631,6 +637,7 @@ func main_0 () (error) {
var _compress string var _compress string
var _includeIndex bool var _includeIndex bool
var _includeStripped bool var _includeStripped bool
var _includeCache bool
var _includeEtag bool var _includeEtag bool
var _includeFileListing bool var _includeFileListing bool
var _includeFolderListing bool var _includeFolderListing bool
@ -662,6 +669,7 @@ func main_0 () (error) {
--exclude-index --exclude-index
--exclude-strip --exclude-strip
--exclude-cache
--exclude-etag --exclude-etag
--exclude-file-listing --exclude-file-listing
@ -680,6 +688,7 @@ func main_0 () (error) {
_compress_0 := _flags.String ("compress", "", "") _compress_0 := _flags.String ("compress", "", "")
_excludeIndex_0 := _flags.Bool ("exclude-index", false, "") _excludeIndex_0 := _flags.Bool ("exclude-index", false, "")
_excludeStripped_0 := _flags.Bool ("exclude-strip", false, "") _excludeStripped_0 := _flags.Bool ("exclude-strip", false, "")
_excludeCache_0 := _flags.Bool ("exclude-cache", false, "")
_excludeEtag_0 := _flags.Bool ("exclude-etag", false, "") _excludeEtag_0 := _flags.Bool ("exclude-etag", false, "")
_excludeFileListing_0 := _flags.Bool ("exclude-file-listing", false, "") _excludeFileListing_0 := _flags.Bool ("exclude-file-listing", false, "")
_includeFolderListing_0 := _flags.Bool ("include-folder-listing", false, "") _includeFolderListing_0 := _flags.Bool ("include-folder-listing", false, "")
@ -692,6 +701,7 @@ func main_0 () (error) {
_compress = *_compress_0 _compress = *_compress_0
_includeIndex = ! *_excludeIndex_0 _includeIndex = ! *_excludeIndex_0
_includeStripped = ! *_excludeStripped_0 _includeStripped = ! *_excludeStripped_0
_includeCache = ! *_excludeCache_0
_includeEtag = ! *_excludeEtag_0 _includeEtag = ! *_excludeEtag_0
_includeFileListing = ! *_excludeFileListing_0 _includeFileListing = ! *_excludeFileListing_0
_includeFolderListing = *_includeFolderListing_0 _includeFolderListing = *_includeFolderListing_0
@ -728,6 +738,7 @@ func main_0 () (error) {
compress : _compress, compress : _compress,
includeIndex : _includeIndex, includeIndex : _includeIndex,
includeStripped : _includeStripped, includeStripped : _includeStripped,
includeCache : _includeCache,
includeEtag : _includeEtag, includeEtag : _includeEtag,
includeFileListing : _includeFileListing, includeFileListing : _includeFileListing,
includeFolderListing : _includeFolderListing, includeFolderListing : _includeFolderListing,

View file

@ -105,19 +105,6 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
} }
} }
if _server.securityHeadersEnabled {
if _server.securityHeadersTls {
_responseHeaders.AddRaw (StringToBytes ("Strict-Transport-Security"), StringToBytes ("max-age=31536000"))
_responseHeaders.AddRaw (StringToBytes ("Content-Security-Policy"), StringToBytes ("upgrade-insecure-requests"))
}
{
_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"))
}
}
var _fingerprints []byte var _fingerprints []byte
var _namespaceAndPathSuffixes = [][2]string { var _namespaceAndPathSuffixes = [][2]string {
@ -226,8 +213,6 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
_fingerprintContent := _fingerprints[0:64] _fingerprintContent := _fingerprints[0:64]
_fingerprintMeta := _fingerprints[65:129] _fingerprintMeta := _fingerprints[65:129]
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600"))
var _data []byte var _data []byte
if _server.cachedDataContent != nil { if _server.cachedDataContent != nil {
_data, _ = _server.cachedDataContent[BytesToString (_fingerprintContent)] _data, _ = _server.cachedDataContent[BytesToString (_fingerprintContent)]
@ -271,31 +256,52 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () {
} }
_responseStatus := http.StatusOK _responseStatus := http.StatusOK
_handleHeader := func (_name []byte, _value []byte) {
if _name[0] != '_' { _responseHeaders.AddRawLines (_dataMetaRaw)
_responseHeaders.AddRaw (_name, _value)
} else { // FIXME: Re-enable this part!
switch BytesToString (_name) { // _handleHeader := func (_name []byte, _value []byte) {
case "_Status" : // if _name[0] != '_' {
if _value, _error := strconv.Atoi (BytesToString (_value)); _error == nil { // _responseHeaders.AddRawKv (_name, _value)
if (_value >= 200) && (_value <= 599) { // } else {
_responseStatus = _value // switch BytesToString (_name) {
} else { // case "!Status" :
log.Printf ("[c2f7ec36] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) // if _value, _error := strconv.Atoi (BytesToString (_value)); _error == nil {
_responseStatus = http.StatusInternalServerError // 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
// }
// default :
// log.Printf ("[7acc7d90] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
// }
// }
// }
// if _error := MetadataDecodeIterate (_dataMetaRaw, _handleHeader); _error != nil {
// _server.ServeError (_context, http.StatusInternalServerError, _error, false)
// return
// }
if _server.securityHeadersEnabled {
if _server.securityHeadersTls {
const _lines = (
"Strict-Transport-Security: max-age=31536000" + "\r\n" +
"Content-Security-Policy: upgrade-insecure-requests" + "\r\n")
_responseHeaders.AddRawLines (StringToBytes (_lines))
} }
} else { {
log.Printf ("[beedae55] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) const _lines = (
_responseStatus = http.StatusInternalServerError "Referrer-Policy: strict-origin-when-cross-origin" + "\r\n" +
"X-Content-Type-Options: nosniff" + "\r\n" +
"X-XSS-Protection: 1; mode=block" + "\r\n" +
"X-Frame-Options: sameorigin" + "\r\n")
_responseHeaders.AddRawLines (StringToBytes (_lines))
} }
default :
log.Printf ("[7acc7d90] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ())
}
}
}
if _error := MetadataDecodeIterate (_dataMetaRaw, _handleHeader); _error != nil {
_server.ServeError (_context, http.StatusInternalServerError, _error, false)
return
} }
if _server.debug { if _server.debug {
@ -314,13 +320,13 @@ func (_server *server) ServeStatic (_context *fasthttp.RequestCtx, _status uint,
_response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response)))
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
_responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (_contentType)) _responseHeaders.AddRawKv (StringToBytes ("Content-Type"), StringToBytes (_contentType))
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding)) _responseHeaders.AddRawKv (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding))
if _cache { if _cache {
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) _responseHeaders.AddRawLines (StringToBytes ("Cache-Control: public, immutable, max-age=3600\r\n"))
} else { } else {
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) _responseHeaders.AddRawLines (StringToBytes ("Cache-Control: private, no-cache, no-store\r\n"))
} }
_response.SetStatusCode (int (_status)) _response.SetStatusCode (int (_status))
@ -333,18 +339,14 @@ func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uin
_response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response)))
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes ("identity")) _responseHeaders.SetCanonical (StringToBytes ("Location"), _path)
_responseHeaders.AddRaw (StringToBytes ("Location"), _path)
if _cache { if _cache {
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) _responseHeaders.AddRawLines (StringToBytes ("Cache-Control: public, immutable, max-age=3600\r\n"))
} else { } else {
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) _responseHeaders.AddRawLines (StringToBytes ("Cache-Control: private, no-cache, no-store\r\n"))
} }
_responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (MimeTypeText))
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes ("identity"))
_response.SetStatusCode (int (_status)) _response.SetStatusCode (int (_status))
} }
@ -354,14 +356,14 @@ func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint,
_response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response)))
_responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header)))
if _cache { _responseHeaders.AddRawKv (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType))
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) _responseHeaders.AddRawKv (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding))
} else {
_responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache"))
}
_responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType)) if _cache {
_responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding)) _responseHeaders.AddRawLines (StringToBytes ("Cache-Control: public, immutable, max-age=3600\r\n"))
} else {
_responseHeaders.AddRawLines (StringToBytes ("Cache-Control: private, no-cache, no-store\r\n"))
}
if _banner, _bannerFound := ErrorBannersData[_status]; _bannerFound { if _banner, _bannerFound := ErrorBannersData[_status]; _bannerFound {
_response.SetBodyRaw (_banner) _response.SetBodyRaw (_banner)

View file

@ -39,7 +39,7 @@ func MetadataEncode (_metadata map[string]string) ([]byte, error) {
_buffer.Write ([]byte (_key)) _buffer.Write ([]byte (_key))
_buffer.Write ([]byte (": ")) _buffer.Write ([]byte (": "))
_buffer.Write ([]byte (_value)) _buffer.Write ([]byte (_value))
_buffer.Write ([]byte ("\n")) _buffer.Write ([]byte ("\r\n"))
} }
_data := _buffer.Bytes () _data := _buffer.Bytes ()
@ -73,11 +73,14 @@ func MetadataDecodeIterate (_data []byte, _callback func ([]byte, []byte) ()) (e
} }
_data := _data[_headerOffset :] _data := _data[_headerOffset :]
_headerLimit := bytes.IndexByte (_data, '\n') _headerLimit := bytes.IndexByte (_data, '\r')
if (_headerLimit == -1) { if (_headerLimit == -1) {
return fmt.Errorf ("[2d0d442a] invalid metadata encoding") return fmt.Errorf ("[2d0d442a] invalid metadata encoding")
} }
_headerOffset += _headerLimit + 1 if ((_headerOffset + _headerLimit) == (_dataSize - 1)) || (_data[_headerLimit + 1] != '\n') {
return fmt.Errorf ("[0e319685] invalid metadata encoding")
}
_headerOffset += _headerLimit + 2
_data = _data[: _headerLimit] _data = _data[: _headerLimit]
_separator := bytes.Index (_data, []byte (": ")) _separator := bytes.Index (_data, []byte (": "))