Move PUT/GET handlers to their own functions
git-svn-id: file:///srv/svn/repo/marisa/trunk@9 d6811dac-2434-b64a-9ddc-f563ab233461
This commit is contained in:
30
partage.go
30
partage.go
@@ -92,21 +92,13 @@ func servefile(f *os.File, w http.ResponseWriter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parse(w http.ResponseWriter, r *http.Request) {
|
func uploaderPut(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Max 15 Gb uploads
|
// Max 15 Gb uploads
|
||||||
if r.ContentLength > conf.maxsize {
|
if r.ContentLength > conf.maxsize {
|
||||||
w.WriteHeader(http.StatusRequestEntityTooLarge)
|
w.WriteHeader(http.StatusRequestEntityTooLarge)
|
||||||
w.Write([]byte("File is too big"))
|
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))
|
tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path))
|
||||||
f, err := os.Create(tmp.Name())
|
f, err := os.Create(tmp.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -122,8 +114,9 @@ func parse(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n"
|
resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n"
|
||||||
w.Write([]byte(resp))
|
w.Write([]byte(resp))
|
||||||
|
}
|
||||||
|
|
||||||
case "GET":
|
func uploaderGet(w http.ResponseWriter, r *http.Request) {
|
||||||
// r.URL.Path is sanitized regarding "." and ".."
|
// r.URL.Path is sanitized regarding "." and ".."
|
||||||
filename := r.URL.Path
|
filename := r.URL.Path
|
||||||
if r.URL.Path == "/" {
|
if r.URL.Path == "/" {
|
||||||
@@ -139,6 +132,21 @@ func parse(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
servefile(f, w)
|
servefile(f, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func uploader(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s %s: %s", r.Method, r.URL.Path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "PUT":
|
||||||
|
uploaderPut(w, r)
|
||||||
|
|
||||||
|
case "GET":
|
||||||
|
uploaderGet(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +158,7 @@ func main() {
|
|||||||
conf.baseuri = "http://192.168.0.3:8080"
|
conf.baseuri = "http://192.168.0.3:8080"
|
||||||
conf.filectx = "/f/"
|
conf.filectx = "/f/"
|
||||||
|
|
||||||
http.HandleFunc("/", parse)
|
http.HandleFunc("/", uploader)
|
||||||
http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
|
http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
|
||||||
http.ListenAndServe("0.0.0.0:8080", nil)
|
http.ListenAndServe("0.0.0.0:8080", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user