* Send at least a generic error reply when we are unable to satisfy the request.
authorUrban Wallasch <urban.wallasch@freenet.de>
Fri, 5 Apr 2019 21:38:48 +0000 (23:38 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Fri, 5 Apr 2019 21:38:48 +0000 (23:38 +0200)
gogopherd.go

index 4890480293400842e2c4616628d10a7708752acd..a4e95ce9fe10ece7e5f88726f017352ccfe8ea04 100644 (file)
@@ -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()