From 28fb840e87cdfb5fd2c319903ea9b2239335ebf1 Mon Sep 17 00:00:00 2001 From: ache Date: Tue, 12 Feb 2019 21:52:32 +0100 Subject: Support HEAD --- server.go | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/server.go b/server.go index c38d216..99b5d29 100644 --- a/server.go +++ b/server.go @@ -11,7 +11,7 @@ import ( var tmpl *template.Template -func handleGet( w http.ResponseWriter, r *http.Request) { +func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { path := r.URL.Path[1:] if path == "" { @@ -25,9 +25,11 @@ func handleGet( w http.ResponseWriter, r *http.Request) { case err != nil || stat == nil : if os.IsNotExist(err) { w.WriteHeader(http.StatusNotFound) - fmt.Fprintln(w, "File not found") - fmt.Fprintln(w, "") - fmt.Fprintln(w, "Sry bro") + if ! headOnly { + fmt.Fprintln(w, "File not found") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Sry bro") + } return } case stat.IsDir() : @@ -43,9 +45,11 @@ func handleGet( w http.ResponseWriter, r *http.Request) { if err != nil { w.WriteHeader(http.StatusNotFound) - fmt.Fprintln(w, "File not found") - fmt.Fprintln(w, "") - fmt.Fprintln(w, "Sry bro") + if !headOnly { + fmt.Fprintln(w, "File not found") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Sry bro") + } return } @@ -54,9 +58,11 @@ func handleGet( w http.ResponseWriter, r *http.Request) { if err != nil { w.WriteHeader(http.StatusNotFound) - fmt.Fprintln(w, "File not found") - fmt.Fprintln(w, "") - fmt.Fprintln(w, "Sry bro") + if !headOnly { + fmt.Fprintln(w, "File not found") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Sry bro") + } } for _, file := range fileInfo { @@ -71,28 +77,41 @@ func handleGet( w http.ResponseWriter, r *http.Request) { tmpl.Execute(w, Collection{ "Title" , files}) case stat.Mode().IsRegular(): - http.ServeFile(w, r, r.URL.Path[1:]) + if !headOnly { + http.ServeFile(w, r, r.URL.Path[1:]) + } default: } } -func handlePost( w http.ResponseWriter, r *http.Request) { -} func handleDelete( w http.ResponseWriter, r *http.Request) { } func handlePut( w http.ResponseWriter, r *http.Request) { } +func handleMkcol( w http.ResponseWriter, r *http.Request) { +} +func handleOptions( w http.ResponseWriter, r *http.Request) { +} +func handleCopy( w http.ResponseWriter, r *http.Request) { +} func handleMethod( w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - handleGet(w, r) - case http.MethodPost: - handlePost(w, r) + handleGet(w, r, false) + case http.MethodHead: + handleGet(w, r, true) case http.MethodPut: handlePut(w, r) case http.MethodDelete: - // Remove the record. handleDelete(w, r) + case "MKCOL": + handleMkcol(w, r) + case "LOCK": + fallthrough + case "UNLOCK": + fallthrough + case http.MethodPost: + fallthrough default: w.WriteHeader(http.StatusTeapot) fmt.Fprintln(w, "Sry, i'm a teapot >///<") -- cgit v1.2.3