From 63204868eb1d6d428061cda7876ebbb6f47a028a Mon Sep 17 00:00:00 2001 From: Palapeli <26661008+mkwcat@users.noreply.github.com> Date: Tue, 7 Apr 2026 08:12:28 -0400 Subject: [PATCH] NAS: Run HTTP header filters in a detached goroutine --- nas/listener.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/nas/listener.go b/nas/listener.go index 15c4c80..dd08acc 100644 --- a/nas/listener.go +++ b/nas/listener.go @@ -29,9 +29,16 @@ func (l *nasListener) Accept() (net.Conn, error) { return nil, err } + pr, pw := io.Pipe() + go func() { + r := bufio.NewReader(conn) + filterDuplicateHost(r, pw) + _, err := io.Copy(pw, r) + _ = pw.CloseWithError(err) + }() return &nasConn{ Conn: conn, - reader: filterDuplicateHost(conn), + reader: pr, }, nil } @@ -41,17 +48,17 @@ func (c *nasConn) Read(b []byte) (int, error) { // filterDuplicateHost wraps a net.Conn and filters out duplicate Host headers from the HTTP request, // making the invalid requests sent by DWC acceptable to the standard library's HTTP server. -func filterDuplicateHost(c net.Conn) io.Reader { - r := bufio.NewReader(c) - +func filterDuplicateHost(r *bufio.Reader, p *io.PipeWriter) { // Read the first line of the HTTP request line, err := r.ReadString('\n') if err != nil { - return io.MultiReader(strings.NewReader(line), r) + _, _ = p.Write([]byte(line)) + return } // Is this an HTTP request? if !strings.HasSuffix(line, "HTTP/1.1\r\n") { - return io.MultiReader(strings.NewReader(line), r) + _, _ = p.Write([]byte(line)) + return } // Iterate through the HTTP headers and remove any duplicate Host headers @@ -72,7 +79,7 @@ func filterDuplicateHost(c net.Conn) io.Reader { headers.WriteString(headerLine) } - return io.MultiReader(strings.NewReader(line+headers.String()), r) + _, _ = p.Write([]byte(line + headers.String())) } func listenAndServe(address string) {