From 21d157e4726c43dfe80609939186deb150a9178e Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Mon, 12 Aug 2019 17:22:29 +0300 Subject: [PATCH] [server] Use a more performant header configuration method // do not include `Content-Length` in metadata --- examples/hello-world.cdb | Bin 3254 -> 3234 bytes sources/cmd/archiver.go | 2 +- sources/cmd/server.go | 79 ++++++++++++++++----------------- sources/lib/common/metadata.go | 6 +++ 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/examples/hello-world.cdb b/examples/hello-world.cdb index 425a63fa40ce8a5ac60548d1cc029bd90bad6cca..61f2e4044597bc23d080ab9a113612150c35d8af 100644 GIT binary patch literal 3234 zcmeHG&1%&^7`?jEwEipvU8da##WeGmJDC(WYQc>PQgq{H{^AX!NxgT}+l@uHQV?H2 z@F84?;MRArT?#&c3zvSACRn5hC6-dT10QE5GjryA=a9wuK{FO-cxWF^!wu5W~EIzr-~!Z6H2L3%vh3BsaYaZX_zreVnrCqk)k@+SO_Yx zF=;kLNkW8XI2FSoS0cee4V6R|HjUw2Dg1DKs;FShfcfNf4d=`%WG1!ysH$Od1J09J8@OmNW tR)fLl)YsjQ`_?bseJUTq`K|E#wVgA+&wWVi-CO(C-Oi7CYiPeM?;M-==``u!{redFn`7hO z|Ez=edq4R7uknJbz3O5l2YOp%yLvb(>IwnKVhCddViCtEm6QX(rGOeRfk>JIr6EEb zOO8@{(Vc(k!W5+*WwNfUu56ZoTyQ|N&@4@Ho&uRtjIqjz(#T*T2^CBzqEs$mjtDX! z$I?j1Ip$C)mNCW^1R|w<&KM#op&}R?!Zc(6Xel(5k{T+FF*#)n^#K z(zQuv$1l$&R#ZxlPn_JE}HLnB3cq9D^W6;2JlRy#ZM2{4$itq z!?=F%<;S;k?dk~0lQ(ad!=KA%A3fN7O{1gz!I|%eU#Hg3-+wM2I~x0}KD;$mzdiA@ KezCOR^1lEYsM3o7 diff --git a/sources/cmd/archiver.go b/sources/cmd/archiver.go index 7759dcb..bef587a 100644 --- a/sources/cmd/archiver.go +++ b/sources/cmd/archiver.go @@ -410,7 +410,7 @@ func prepareDataContent (_context *context, _pathResolved string, _pathInArchive } _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-Encoding"] = _dataEncoding if _context.includeEtag { diff --git a/sources/cmd/server.go b/sources/cmd/server.go index 2a06b1b..1c2a7a6 100644 --- a/sources/cmd/server.go +++ b/sources/cmd/server.go @@ -107,14 +107,14 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () { if _server.securityHeadersEnabled { if _server.securityHeadersTls { - _responseHeaders.SetCanonical (StringToBytes ("Strict-Transport-Security"), StringToBytes ("max-age=31536000")) - _responseHeaders.SetCanonical (StringToBytes ("Content-Security-Policy"), StringToBytes ("upgrade-insecure-requests")) + _responseHeaders.AddRaw (StringToBytes ("Strict-Transport-Security"), StringToBytes ("max-age=31536000")) + _responseHeaders.AddRaw (StringToBytes ("Content-Security-Policy"), StringToBytes ("upgrade-insecure-requests")) } { - _responseHeaders.SetCanonical (StringToBytes ("Referrer-Policy"), StringToBytes ("strict-origin-when-cross-origin")) - _responseHeaders.SetCanonical (StringToBytes ("X-Content-Type-Options"), StringToBytes ("nosniff")) - _responseHeaders.SetCanonical (StringToBytes ("X-XSS-Protection"), StringToBytes ("1; mode=block")) - _responseHeaders.SetCanonical (StringToBytes ("X-Frame-Options"), StringToBytes ("sameorigin")) + _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")) } } @@ -226,7 +226,7 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () { _fingerprintContent := _fingerprints[0:64] _fingerprintMeta := _fingerprints[65:129] - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) var _data []byte if _server.cachedDataContent != nil { @@ -272,26 +272,25 @@ func (_server *server) Serve (_context *fasthttp.RequestCtx) () { _responseStatus := http.StatusOK _handleHeader := func (_name []byte, _value []byte) { - switch { - case len (_name) == 0 : - log.Printf ("[90009821] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) - _responseStatus = http.StatusInternalServerError - case _name[0] != '_' : - _responseHeaders.SetCanonical (_name, _value) - case bytes.Equal (_name, StringToBytes ("_Status")) : - if _value, _error := strconv.Atoi (BytesToString (_value)); _error == nil { - if (_value >= 200) && (_value <= 599) { - _responseStatus = _value - } else { - log.Printf ("[c2f7ec36] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) - _responseStatus = http.StatusInternalServerError + if _name[0] != '_' { + _responseHeaders.AddRaw (_name, _value) + } else { + switch BytesToString (_name) { + case "_Status" : + if _value, _error := strconv.Atoi (BytesToString (_value)); _error == nil { + 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 } - } 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 ()) + default : + log.Printf ("[7acc7d90] invalid data metadata for `%s`!\n", _requestHeaders.RequestURI ()) + } } } if _error := MetadataDecodeIterate (_dataMetaRaw, _handleHeader); _error != nil { @@ -315,13 +314,13 @@ func (_server *server) ServeStatic (_context *fasthttp.RequestCtx, _status uint, _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) - _responseHeaders.SetCanonical (StringToBytes ("Content-Type"), StringToBytes (_contentType)) - _responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding)) + _responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (_contentType)) + _responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes (_contentEncoding)) if _cache { - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) } else { - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) } _response.SetStatusCode (int (_status)) @@ -334,17 +333,17 @@ func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uin _response := (*fasthttp.Response) (NoEscape (unsafe.Pointer (&_context.Response))) _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) - _responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes ("identity")) - _responseHeaders.SetCanonical (StringToBytes ("Location"), _path) + _responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes ("identity")) + _responseHeaders.AddRaw (StringToBytes ("Location"), _path) if _cache { - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) } else { - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) } - _responseHeaders.SetCanonical (StringToBytes ("Content-Type"), StringToBytes (MimeTypeText)) - _responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes ("identity")) + _responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (MimeTypeText)) + _responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes ("identity")) _response.SetStatusCode (int (_status)) } @@ -356,13 +355,13 @@ func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint, _responseHeaders := (*fasthttp.ResponseHeader) (NoEscape (unsafe.Pointer (&_context.Response.Header))) if _cache { - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("public, immutable, max-age=3600")) } else { - _responseHeaders.SetCanonical (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) + _responseHeaders.AddRaw (StringToBytes ("Cache-Control"), StringToBytes ("no-cache")) } - _responseHeaders.SetCanonical (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType)) - _responseHeaders.SetCanonical (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding)) + _responseHeaders.AddRaw (StringToBytes ("Content-Type"), StringToBytes (ErrorBannerContentType)) + _responseHeaders.AddRaw (StringToBytes ("Content-Encoding"), StringToBytes (ErrorBannerContentEncoding)) if _banner, _bannerFound := ErrorBannersData[_status]; _bannerFound { _response.SetBodyRaw (_banner) diff --git a/sources/lib/common/metadata.go b/sources/lib/common/metadata.go index ba28169..bdae360 100644 --- a/sources/lib/common/metadata.go +++ b/sources/lib/common/metadata.go @@ -86,6 +86,12 @@ func MetadataDecodeIterate (_data []byte, _callback func ([]byte, []byte) ()) (e } _key := _data[: _separator] _value := _data[_separator + 3 :] + if len (_key) == 0 { + return fmt.Errorf ("[c3f5e8f3] invalid metadata encoding (empty key)") + } + if len (_value) == 0 { + return fmt.Errorf ("[d6a923b6] invalid metadata encoding (empty value)") + } _callback (_key, _value) }