From d5b9c5c2936d128dcdca006d64b99141b89ddcd4 Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Fri, 17 Dec 2021 22:18:47 +0200 Subject: [PATCH] [server] Refactor Go HTTP headers magic into adapter. --- sources/cmd/server/server.go | 20 ++++------- sources/lib/server/headers-magic.go | 56 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/sources/cmd/server/server.go b/sources/cmd/server/server.go index 2e64757..bf52cb5 100644 --- a/sources/cmd/server/server.go +++ b/sources/cmd/server/server.go @@ -503,27 +503,19 @@ func (_server *server) ServeHTTP (_response http.ResponseWriter, _request *http. _server.Serve (_context) - _responseHeaders := HttpResponseWriterHeaderDoMagic (_response) + _responseHeaders := NewHttpResponseWriterHeadersBuffer (_context.Response.Header.StatusCode ()) + _context.Response.Header.VisitAll ( - func (_key_0 []byte, _value_0 []byte) () { - switch string (_key_0) { + func (_key []byte, _value []byte) () { + switch BytesToString (_key) { case "Connection", "Content-Length", "Date" : // NOP default : - _key := CanonicalHeaderNameFromBytes (_key_0) - if _values, _ := _responseHeaders[_key]; _values == nil { - _values = CanonicalHeaderValueArrayFromBytes (_value_0) - _responseHeaders[_key] = _values - } else { - _value := CanonicalHeaderValueFromBytes (_value_0) - _values = append (_values, _value) - _responseHeaders[_key] = _values - } + _responseHeaders.Include (_key, _value) } }) - _responseHeaders["Date"] = nil - _response.WriteHeader (_context.Response.Header.StatusCode ()) + _responseHeaders.WriteTo (_response) _response.Write (_context.Response.Body ()) } diff --git a/sources/lib/server/headers-magic.go b/sources/lib/server/headers-magic.go index 7ee0c26..157cd02 100644 --- a/sources/lib/server/headers-magic.go +++ b/sources/lib/server/headers-magic.go @@ -13,6 +13,62 @@ import "unsafe" +type HttpResponseWriterHeadersBuffer struct { + status int + headers [128][2][]byte + headersCount int +} + + +func NewHttpResponseWriterHeadersBuffer (_status int) (HttpResponseWriterHeadersBuffer) { + + return HttpResponseWriterHeadersBuffer { + status : _status, + headersCount : 0, + } +} + + +func (_buffer *HttpResponseWriterHeadersBuffer) Include (_name []byte, _value []byte) () { + + if _buffer.headersCount == 128 { + panic ("[ca806ede]") + } + + _buffer.headers[_buffer.headersCount] = [2][]byte {_name, _value} + _buffer.headersCount += 1 +} + + +func (_buffer *HttpResponseWriterHeadersBuffer) WriteTo (_response http.ResponseWriter) () { + + _headers := HttpResponseWriterHeaderDoMagic (_response) + + for _index := 0; _index < _buffer.headersCount; _index += 1 { + + _nameAndValue_0 := _buffer.headers[_index] + _name_0 := _nameAndValue_0[0] + _value_0 := _nameAndValue_0[1] + + _name := CanonicalHeaderNameFromBytes (_name_0) + if _values, _ := _headers[_name]; _values == nil { + _values = CanonicalHeaderValueArrayFromBytes (_value_0) + _headers[_name] = _values + } else { + _value := CanonicalHeaderValueFromBytes (_value_0) + _values = append (_values, _value) + _headers[_name] = _values + } + } + + _headers["Date"] = nil + + _response.WriteHeader (_buffer.status) +} + + + + func HttpResponseWriterHeaderDoMagic (_response http.ResponseWriter) (http.Header) {