changeset 1:960887cc6641 default tip

stop corrupting binary files with text processing
author Atarwn Gard <a@qwa.su>
date Mon, 20 Oct 2025 00:54:02 +0500
parents 250e333a372e
children
files qtd.go
diffstat 1 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/qtd.go	Mon Oct 20 00:13:07 2025 +0500
+++ b/qtd.go	Mon Oct 20 00:54:02 2025 +0500
@@ -5,6 +5,7 @@
 	"flag"
 	"fmt"
 	"html"
+	"io"
 	"log"
 	"net"
 	"net/http"
@@ -156,14 +157,23 @@
 func handleHTTP(w http.ResponseWriter, r *http.Request) {
 	root := *dir
 	path := strings.TrimPrefix(r.URL.Path, "/")
-	title, content, isDir, err := resolvePath(root, path)
+	
+	cleanPath := filepath.Clean("/" + path)
+	target := filepath.Join(root, cleanPath)
 
+	info, err := os.Stat(target)
 	if err != nil {
 		http.NotFound(w, r)
 		return
 	}
 
-	if strings.HasSuffix(r.URL.Path, "/") || isDir || path == "" {
+	if info.IsDir() {
+		title, content, _, err := resolvePath(root, path)
+		if err != nil {
+			http.NotFound(w, r)
+			return
+		}
+		
 		w.Header().Set("Content-Type", "text/html; charset=utf-8")
 		content = linkify(content)
 		
@@ -209,8 +219,14 @@
 		return
 	}
 
-	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
-	w.Write([]byte(content))
+	file, err := os.Open(target)
+	if err != nil {
+		http.NotFound(w, r)
+		return
+	}
+	defer file.Close()
+
+	io.Copy(w, file)
 }
 
 func handleTCP(conn net.Conn, root string) {