From 2bb349612f8d53469a318e10a2e6188b22bffc24 Mon Sep 17 00:00:00 2001 From: dev Date: Sun, 10 Oct 2021 05:29:51 +0000 Subject: [PATCH] Provide basic GET/PUT HTTP methods git-svn-id: file:///srv/svn/repo/marisa/trunk@1 d6811dac-2434-b64a-9ddc-f563ab233461 --- config.mk | 2 + go.mod | 3 + mkfile | 21 +++++++ partage.go | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 config.mk create mode 100644 go.mod create mode 100644 mkfile create mode 100644 partage.go diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..530cf0d --- /dev/null +++ b/config.mk @@ -0,0 +1,2 @@ +GO = go +GOOS = `{uname -s | tr A-Z a-z} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..31ec353 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.z3bra.org/partage + +go 1.17 diff --git a/mkfile b/mkfile new file mode 100644 index 0000000..f05222e --- /dev/null +++ b/mkfile @@ -0,0 +1,21 @@ + conf.maxsize { + w.WriteHeader(http.StatusRequestEntityTooLarge) + w.Write([]byte("File is too big")) + } + + err := r.ParseForm() + if err != nil { + fmt.Printf("%s %s: %s", r.Method, r.URL.Path, err) + } + + switch r.Method { + case "PUT": + tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path)) + f, err := os.Create(tmp.Name()) + if err != nil { + fmt.Println(err) + return + } + defer f.Close() + + if writefile(f, r.Body) < 0 { + w.WriteHeader(http.StatusInternalServerError) + return + } + + resp := conf.baseuri + "/" + filepath.Base(tmp.Name()) + w.Write([]byte(resp)) + + case "GET": + // r.URL.Path is sanitized regarding "." and ".." + filename := r.URL.Path + if r.URL.Path == "/" { + filename = "/index.html" + } + + f, err := os.Open(conf.rootdir + filename) + if err != nil { + w.WriteHeader(http.StatusNotFound) + fmt.Println(err) + return + } + defer f.Close() + + servefile(f, w) + } +} + +func main() { + + conf.bind = "0.0.0.0:8080" + conf.maxsize = 28 * 1024 * 1024 + conf.filepath = "/tmp" + conf.rootdir = "./static" + conf.baseuri = "http://192.168.0.3:8080" + + http.HandleFunc("/", parse) + http.ListenAndServe("0.0.0.0:8080", nil) +}