summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2019-02-13 10:24:22 +0100
committerache <ache@ache.one>2019-02-13 10:24:22 +0100
commita231d24ab9915dc0ffa6e89923d7bbb9c2a1bb28 (patch)
tree14de092d72b7c49e68c59e6c0923bb04facbe0b8
parentSupport HEAD (diff)
Support PUT method
-rw-r--r--server.go99
1 files changed, 91 insertions, 8 deletions
diff --git a/server.go b/server.go
index 99b5d29..6274934 100644
--- a/server.go
+++ b/server.go
@@ -5,21 +5,24 @@ import (
"log"
"net/http"
"os"
+ "io"
"html/template"
"flag"
+ "path"
+ "syscall"
)
var tmpl *template.Template
func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
- path := r.URL.Path[1:]
- if path == "" {
- path = "."
+ filename := r.URL.Path[1:]
+ if filename == "" {
+ filename = "."
}
- fmt.Printf("Trying to get [%s]\n", r.URL.Path[1:])
+ fmt.Printf("Trying to get [%s]\n", filename)
- stat, err := os.Stat(path)
+ stat, err := os.Stat(filename)
switch {
case err != nil || stat == nil :
@@ -41,7 +44,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
}
var files []Entry
- f, err := os.Open(path)
+ f, err := os.Open(filename)
if err != nil {
w.WriteHeader(http.StatusNotFound)
@@ -78,7 +81,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
case stat.Mode().IsRegular():
if !headOnly {
- http.ServeFile(w, r, r.URL.Path[1:])
+ http.ServeFile(w, r, filename)
}
default:
}
@@ -86,13 +89,86 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) {
func handleDelete( w http.ResponseWriter, r *http.Request) {
}
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 {
+ w.WriteHeader(http.StatusConflict)
+ fmt.Fprintf(w, "Can't find [%s]\n)", path.Dir(filename))
+ return
+ } else if err != nil{
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprintln(w, err.Error())
+ return
+ } else if ! stat.IsDir(){
+ w.WriteHeader(http.StatusConflict)
+ fmt.Fprintln(w, "¯\\_(ツ)_/¯")
+ return
+ }
+
+ stat, err = os.Stat(filename)
+
+ if err != nil {
+ if !os.IsNotExist(err) {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprintln(w, err.Error())
+ return
+ }
+ } else if stat.IsDir() {
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ fmt.Fprintln(w, "Cannot write a file over a existing collection")
+ return
+ }
+
+ file, err := os.Create(filename)
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprintln(w, err.Error())
+ return
+ }
+ defer file.Close()
+
+ s, err := io.Copy(file, r.Body)
+ if err == io.EOF {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprintln(w, err.Error())
+ return
+ }
+ fmt.Fprintf(w, "%d bytes written\n", s)
}
func handleMkcol( w http.ResponseWriter, r *http.Request) {
+ filename := r.URL.Path[1:]
+ if filename == "" {
+ filename = "."
+ }
+ fmt.Printf("Trying to Mkcol [%s]\n", filename)
+
+ fmt.Printf(" Base [%s]\n", path.Base(filename))
+ fmt.Printf(" Dir [%s]\n", path.Dir(filename))
+
+ err := os.Mkdir(filename, 0755)
+ if err != nil {
+ w.WriteHeader(http.StatusNotFound)
+ fmt.Fprintf(w, "Sry for the error : %s", err.Error())
+ }
}
func handleOptions( w http.ResponseWriter, r *http.Request) {
}
func handleCopy( w http.ResponseWriter, r *http.Request) {
}
+func handleMove( w http.ResponseWriter, r *http.Request) {
+ filename := r.URL.Path[1:]
+ oups := ""
+ if filename == "" || filename == "." {
+ w.WriteHeader(http.StatusBadRequest)
+ fmt.Fprintln(w, "Cannot move root directory")
+ return
+ }
+ fmt.Printf("Trying to move [%s] to [%s]\n", filename, oups)
+}
func handleMethod( w http.ResponseWriter, r *http.Request) {
switch r.Method {
@@ -104,6 +180,10 @@ func handleMethod( w http.ResponseWriter, r *http.Request) {
handlePut(w, r)
case http.MethodDelete:
handleDelete(w, r)
+ case http.MethodOptions:
+ handleOptions(w, r)
+ case "MOVE":
+ handleDelete(w, r)
case "MKCOL":
handleMkcol(w, r)
case "LOCK":
@@ -111,7 +191,10 @@ func handleMethod( w http.ResponseWriter, r *http.Request) {
case "UNLOCK":
fallthrough
case http.MethodPost:
- fallthrough
+ w.WriteHeader(http.StatusTeapot)
+ fmt.Fprintln(w, "Sry, will be implemented \"soon\"")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "Here's a unicorn. 🦄")
default:
w.WriteHeader(http.StatusTeapot)
fmt.Fprintln(w, "Sry, i'm a teapot >///<")