From 7bf705dd2dcf4f700b26d029a618267f5ebec8a7 Mon Sep 17 00:00:00 2001 From: Pacien Date: Wed, 17 Jul 2013 14:28:11 +0200 Subject: Add error handling --- main.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 16c5ca6..d9b2516 100755 --- a/main.go +++ b/main.go @@ -34,29 +34,35 @@ var params struct { func defaultHandler(w http.ResponseWriter, r *http.Request) { host := strings.Split(r.Host, ":") - + if host[0] == "" { log.Println("Undefined host") http.NotFound(w, r) return } - + request := r.URL.Path[1:] requestedFile := params.dir + "/" + host[0] + "/" + request log.Println(requestedFile) - + file, err := os.Stat(requestedFile) if err != nil { log.Println(err) - http.NotFound(w, r) + if os.IsNotExist(err) { + http.NotFound(w, r) + } else if os.IsPermission(err) { + http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) + } else { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + } return } - + if file.IsDir() && !strings.HasSuffix(requestedFile, "/") { http.Redirect(w, r, r.URL.Path+"/", http.StatusFound) return } - + http.ServeFile(w, r, requestedFile) } -- cgit v1.2.3