diff --git a/http_handle.go b/http_handle.go index 80a6d76..9f8fcfa 100644 --- a/http_handle.go +++ b/http_handle.go @@ -56,8 +56,6 @@ func HandleHTTPRequest(headers Headers, conn net.Conn) { defer file.Close() sendFile(file, conn) - - conn.Close() } func sendFile(file *os.File, conn net.Conn) { diff --git a/main.go b/main.go index 5000769..922a308 100644 --- a/main.go +++ b/main.go @@ -38,11 +38,15 @@ func main() { func handleConnection(conn net.Conn) { startTime := time.Now() - defer func() { - slog.Info("Request handled", "duration", time.Since(startTime).String()) - }() - defer conn.Close() + + durr, err := time.ParseDuration("5m") + if err != nil { + slog.Error("Could not parse duration", "err", err.Error()) + return + } + plus5min := startTime.Add(durr) + conn.SetDeadline(plus5min) reader := bufio.NewReader(conn) headers := readHeaders(reader) if headers == nil { @@ -52,5 +56,10 @@ func handleConnection(conn net.Conn) { slog.Info("Received request", "method", headers.Method, "uri", headers.Uri, "proto", headers.Proto, "client", conn.RemoteAddr().String()) + conn.SetDeadline(time.Now().Add(durr)) HandleHTTPRequest(*headers, conn) + + if headers.KV["Connection"] == "keep-alive" { + handleConnection(conn) + } }