summaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorache <ache@ache.one>2019-02-22 06:08:35 +0100
committerache <ache@ache.one>2019-02-22 06:08:35 +0100
commit485f2a8dd961c88f46163c0f818d0ce358870656 (patch)
treea38dad55839600b09bff67e065788311e806d3b3 /server.go
parentDesign improove (diff)
Format pipeline
Diffstat (limited to 'server.go')
-rw-r--r--server.go67
1 files changed, 62 insertions, 5 deletions
diff --git a/server.go b/server.go
index ad32199..7e6d54d 100644
--- a/server.go
+++ b/server.go
@@ -10,11 +10,34 @@ import (
"flag"
"path"
"syscall"
+ "net/url"
+ "errors"
"./config"
)
-var tmpl *template.Template
+var tmpl *template.Template;
+func verifyEmptyBody( w http.ResponseWriter, r *http.Request) error {
+ // 8.4 RFC4918 - Must return 415 (Unsupported Media Type) if body and should not.
+ if r.Body != nil {
+ p := make([]byte, 1)
+ _,er := r.Body.Read(p)
+
+ if er == nil {
+ w.WriteHeader(http.StatusUnsupportedMediaType)
+ fmt.Println("The body of the request isn't empty. It should be.")
+ return errors.New("Body not empty")
+ }
+ }
+ return nil
+}
+func encodeURL( urlStr string ) string {
+ url,err := url.Parse("http://localhost" + urlStr)
+ if err == nil {
+ return url.RequestURI()
+ }
+ return urlStr
+}
func normaliseName( name string ) string {
if len(name) > 27 {
name = name[:25] + "..."
@@ -43,9 +66,9 @@ func getPath( r *http.Request ) string {
}
return r.URL.Path[1:]
}
+
func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
filename := getPath(r)
-
fmt.Printf("Trying to get [%s]\n", filename)
stat, err := os.Stat(filename)
@@ -125,31 +148,40 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
default:
}
}
+
func handleDelete( w http.ResponseWriter, r *http.Request) {
filename := getPath(r)
+ fmt.Printf("Trying to get [%s]\n", filename)
+
+ if verifyEmptyBody(w, r) != nil { return }
+
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
- w.WriteHeader(http.StatusBadRequest)
+ w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Bro, they is nothing here")
} else {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Shit happen")
}
}
+
err = os.Remove(filename)
if err != nil {
w.WriteHeader(http.StatusConflict)
fmt.Fprintf(w, "Error : %s\n", err.Error())
+ return
}
+ w.WriteHeader(http.StatusNoContent) // Default success code ¯\_(ツ)_/¯ cf : 9.6.1.8
}
func handlePut( w http.ResponseWriter, r *http.Request) {
filename := r.URL.Path[1:]
if filename == "" {
filename = "."
}
+
stat, err := os.Stat(path.Dir(filename))
if err == syscall.ENOENT {
@@ -194,6 +226,8 @@ func handlePut( w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, err.Error())
return
}
+
+ w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "%d bytes written\n", s)
}
func handleMkcol( w http.ResponseWriter, r *http.Request) {
@@ -203,6 +237,9 @@ func handleMkcol( w http.ResponseWriter, r *http.Request) {
}
fmt.Printf("Trying to Mkcol [%s]\n", filename)
+ // 8.4 RFC4918 - Must return 415 (Unsupported Media Type) if body and should not.
+ verifyEmptyBody(w, r)
+
fmt.Printf(" Base [%s]\n", path.Base(filename))
fmt.Printf(" Dir [%s]\n", path.Dir(filename))
@@ -211,10 +248,22 @@ func handleMkcol( w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Sry for the error : %s", err.Error())
}
+
+ w.WriteHeader(http.StatusCreated)
+ fmt.Fprintf(w, "Collection created")
}
func handleOptions( w http.ResponseWriter, r *http.Request) {
+
+ // 8.4 RFC4918 - Must return 415 (Unsupported Media Type) if body and should not.
+ if r.Body != nil {
+ w.WriteHeader(http.StatusUnsupportedMediaType)
+ fmt.Println("The body of the request isn't empty. It should be.")
+ return
+ }
}
func handleCopy( w http.ResponseWriter, r *http.Request) {
+
+ verifyEmptyBody(w, r)
}
func handleMove( w http.ResponseWriter, r *http.Request) {
filename := r.URL.Path[1:]
@@ -224,6 +273,11 @@ func handleMove( w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Overwrite") != "" {
overwrite = r.Header.Get("Overwrite")
}
+ verifyEmptyBody(w, r)
+
+ fmt.Println(dest)
+
+ return
/* 5 cases
src doesn't exist => error
@@ -317,7 +371,6 @@ func handleMethod( w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Here's some tea. 🍵")
}
}
-
func main() {
var dir string
var help, version bool
@@ -357,7 +410,11 @@ func main() {
return
}
- tmpl = template.Must(template.ParseFiles("dir.html"))
+ fmap := template.FuncMap{
+ "formatURL": encodeURL,
+ "formatSize": normaliseSize,
+ }
+ tmpl = template.Must(template.New("dir.html").Funcs(fmap).ParseFiles("dir.html"))
if err := os.Chdir(dir) ; err != nil {
fmt.Println("Error with directory [" + dir + "]")