[server] Refactor Go HTTP headers magic into adapter.

This commit is contained in:
Ciprian Dorin Craciun 2021-12-17 22:18:47 +02:00
parent 01586f506d
commit d5b9c5c293
2 changed files with 62 additions and 14 deletions

View file

@ -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 ())
}

View file

@ -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) {