From 5609528a18762db83d787af59b4e14b2bf2abafb Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Sat, 18 Dec 2021 02:02:46 +0200 Subject: [PATCH] [server] Use "magic" also for Go HTTP handler. --- sources/cmd/server/server.go | 15 +++++++-------- sources/lib/server/headers-magic.go | 10 +++++++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sources/cmd/server/server.go b/sources/cmd/server/server.go index ad03f49..f754070 100644 --- a/sources/cmd/server/server.go +++ b/sources/cmd/server/server.go @@ -483,12 +483,11 @@ func (_server *server) ServeHTTP (_response http.ResponseWriter, _request *http. if _server.dummy { if !_server.dummyEmpty { - _responseHeaders := _response.Header () - _responseHeaders["Content-Type"] = []string { DummyContentType } - _responseHeaders["Content-Encoding"] = []string { DummyContentEncoding } - _responseHeaders["Cache-Control"] = []string { "no-store, max-age=0" } - _responseHeaders["Date"] = nil - _response.WriteHeader (http.StatusOK) + _responseHeaders := NewHttpResponseWriterHeadersBuffer (http.StatusOK) + _responseHeaders.IncludeString ("Content-Type", DummyContentType) + _responseHeaders.IncludeString ("Content-Encoding", DummyContentEncoding) + _responseHeaders.IncludeString ("Cache-Control", "no-store, max-age=0") + _responseHeaders.WriteTo (_response) _response.Write (DummyData) } if _server.dummyDelay != 0 { @@ -523,7 +522,7 @@ func (_server *server) ServeHTTP (_response http.ResponseWriter, _request *http. case "Connection", "Content-Length", "Date" : // NOP default : - _responseHeaders.Include (_key, _value) + _responseHeaders.IncludeBytes (_key, _value) } }) @@ -812,7 +811,7 @@ func main_0 () (error) { debug.SetGCPercent (50) debug.SetMaxThreads (int (128 * (_threads / 64 + 1))) - debug.SetMaxStack (16 * 1024) + debug.SetMaxStack (32 * 1024) _httpReduceMemory := false diff --git a/sources/lib/server/headers-magic.go b/sources/lib/server/headers-magic.go index 817ed24..a8ac090 100644 --- a/sources/lib/server/headers-magic.go +++ b/sources/lib/server/headers-magic.go @@ -11,6 +11,9 @@ import "sync/atomic" import "unsafe" +import . "github.com/volution/kawipiko/lib/common" + + type HttpResponseWriterHeadersBuffer struct { @@ -29,7 +32,7 @@ func NewHttpResponseWriterHeadersBuffer (_status int) (HttpResponseWriterHeaders } -func (_buffer *HttpResponseWriterHeadersBuffer) Include (_name []byte, _value []byte) () { +func (_buffer *HttpResponseWriterHeadersBuffer) IncludeBytes (_name []byte, _value []byte) () { if _buffer.headersCount == 128 { panic ("[ca806ede]") @@ -40,6 +43,11 @@ func (_buffer *HttpResponseWriterHeadersBuffer) Include (_name []byte, _value [] } +func (_buffer *HttpResponseWriterHeadersBuffer) IncludeString (_name string, _value string) () { + _buffer.IncludeBytes (StringToBytes (_name), StringToBytes (_value)) +} + + func (_buffer *HttpResponseWriterHeadersBuffer) WriteToGenericResponse (_response http.ResponseWriter) () {