From 5d087230a9a7c5d75341a6b525059c78bd35ac92 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 28 Mar 2024 15:19:46 +0100 Subject: [PATCH] Improved performance --- Response.go | 15 --------------- Server.go | 11 ++++++++++- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Response.go b/Response.go index a544f01..f361eb9 100644 --- a/Response.go +++ b/Response.go @@ -2,7 +2,6 @@ package web import ( "io" - "strings" ) // Response is the interface for an HTTP response. @@ -78,17 +77,3 @@ func (res *response) WriteString(body string) (int, error) { res.body = append(res.body, body...) return len(body), nil } - -// headerText combines all HTTP headers into a single string. -func (res *response) headerText() string { - combined := strings.Builder{} - - for _, header := range res.headers { - combined.WriteString(header.Key) - combined.WriteString(": ") - combined.WriteString(header.Value) - combined.WriteString("\r\n") - } - - return combined.String() -} diff --git a/Server.go b/Server.go index 873f130..8f35347 100644 --- a/Server.go +++ b/Server.go @@ -216,7 +216,16 @@ func (s *server) handleRequest(ctx *context, method string, url string, writer i tmp.WriteString(strconv.Itoa(int(ctx.status))) tmp.WriteString("\r\nContent-Length: ") tmp.WriteString(strconv.Itoa(len(ctx.response.body))) - tmp.WriteString("\r\n\r\n") + tmp.WriteString("\r\n") + + for _, header := range ctx.response.headers { + tmp.WriteString(header.Key) + tmp.WriteString(": ") + tmp.WriteString(header.Value) + tmp.WriteString("\r\n") + } + + tmp.WriteString("\r\n") tmp.Write(ctx.response.body) writer.Write(tmp.Bytes()) }