aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien2013-07-17 14:28:11 +0200
committerPacien2013-07-17 14:28:11 +0200
commit7bf705dd2dcf4f700b26d029a618267f5ebec8a7 (patch)
tree6f1ff8cc084df49048d63df6d8b23d94fa49a046
parent6bd13fa29cf03ab3e590e7e5b67fcab41f677032 (diff)
downloadstaticweb-master.tar.gz
Add error handlingHEADmaster
-rwxr-xr-xmain.go18
1 files 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 {
34 34
35func defaultHandler(w http.ResponseWriter, r *http.Request) { 35func defaultHandler(w http.ResponseWriter, r *http.Request) {
36 host := strings.Split(r.Host, ":") 36 host := strings.Split(r.Host, ":")
37 37
38 if host[0] == "" { 38 if host[0] == "" {
39 log.Println("Undefined host") 39 log.Println("Undefined host")
40 http.NotFound(w, r) 40 http.NotFound(w, r)
41 return 41 return
42 } 42 }
43 43
44 request := r.URL.Path[1:] 44 request := r.URL.Path[1:]
45 requestedFile := params.dir + "/" + host[0] + "/" + request 45 requestedFile := params.dir + "/" + host[0] + "/" + request
46 log.Println(requestedFile) 46 log.Println(requestedFile)
47 47
48 file, err := os.Stat(requestedFile) 48 file, err := os.Stat(requestedFile)
49 if err != nil { 49 if err != nil {
50 log.Println(err) 50 log.Println(err)
51 http.NotFound(w, r) 51 if os.IsNotExist(err) {
52 http.NotFound(w, r)
53 } else if os.IsPermission(err) {
54 http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
55 } else {
56 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
57 }
52 return 58 return
53 } 59 }
54 60
55 if file.IsDir() && !strings.HasSuffix(requestedFile, "/") { 61 if file.IsDir() && !strings.HasSuffix(requestedFile, "/") {
56 http.Redirect(w, r, r.URL.Path+"/", http.StatusFound) 62 http.Redirect(w, r, r.URL.Path+"/", http.StatusFound)
57 return 63 return
58 } 64 }
59 65
60 http.ServeFile(w, r, requestedFile) 66 http.ServeFile(w, r, requestedFile)
61} 67}
62 68