From: Urban Wallasch Date: Fri, 5 Apr 2019 21:38:48 +0000 (+0200) Subject: * Send at least a generic error reply when we are unable to satisfy the request. X-Git-Tag: v0.2~4 X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=9e8d4e511b8aa1f91dccc22c7aad9eef7e2ce92b;p=gogopherd.git * Send at least a generic error reply when we are unable to satisfy the request. --- diff --git a/gogopherd.go b/gogopherd.go index 4890480..a4e95ce 100644 --- a/gogopherd.go +++ b/gogopherd.go @@ -78,6 +78,17 @@ func createIndex(selector string) (string, error) { return list, error(nil) } +func replyErr(conn net.Conn, msg string) { + tracer.Print("sending error reply:" + msg) + s := "iGopher Meditation: " + msg + "\tErr\t" + cfg.fqdn + "\t" + cfg.port + "\r\n" + nb, err := conn.Write([]byte(s)) + if check(err, "replyErr Write ") != nil { + return + } + tracer.Printf("%d bytes sent.", nb) + return +} + func handleRequest(conn net.Conn) { defer conn.Close() // get request @@ -90,26 +101,31 @@ func handleRequest(conn net.Conn) { // canonicalize, and validate referenced path path, err := validatePath(cfg.docRoot, cfg.docRoot+pathSep+req) if check(err, "validatePath "+path) != nil { + replyErr(conn, "404") return } tracer.Print("request path: '", path, "'") if !cfg.showdot && isDotfile(path) { tracer.Print("skip dotfile") + replyErr(conn, "404") return } // check for symbolic link if cfg.fsymln == false { fi, err := os.Lstat(path) if check(err, "request path Lstat "+path) != nil { + replyErr(conn, "404") return } if fi.Mode()&os.ModeSymlink != 0 { + replyErr(conn, "404") return } } // stat the file fi, err := os.Stat(path) if check(err, "request path Stat "+path) != nil { + replyErr(conn, "404") return } fmode := fi.Mode() @@ -120,6 +136,7 @@ func handleRequest(conn net.Conn) { if fmode.IsDir() { diridx, err := createIndex(selector) if check(err, "makeIndex "+path) != nil { + replyErr(conn, "404") return } nb, err := conn.Write([]byte(cfg.message + diridx)) @@ -130,6 +147,7 @@ func handleRequest(conn net.Conn) { } else if fmode.IsRegular() { file, err := os.Open(path) if check(err, "Open "+path) != nil { + replyErr(conn, "404") return } defer file.Close()