Changeset 9 in code
- Timestamp:
- Oct 11, 2021, 6:35:15 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/partage.go
r8 r9 93 93 } 94 94 95 func parse(w http.ResponseWriter, r *http.Request) { 96 95 func uploaderPut(w http.ResponseWriter, r *http.Request) { 97 96 // Max 15 Gb uploads 98 97 if r.ContentLength > conf.maxsize { … … 100 99 w.Write([]byte("File is too big")) 101 100 } 101 102 tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path)) 103 f, err := os.Create(tmp.Name()) 104 if err != nil { 105 fmt.Println(err) 106 return 107 } 108 defer f.Close() 109 110 if writefile(f, r.Body, r.ContentLength) < 0 { 111 w.WriteHeader(http.StatusInternalServerError) 112 return 113 } 114 115 resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n" 116 w.Write([]byte(resp)) 117 } 118 119 func uploaderGet(w http.ResponseWriter, r *http.Request) { 120 // r.URL.Path is sanitized regarding "." and ".." 121 filename := r.URL.Path 122 if r.URL.Path == "/" { 123 filename = "/index.html" 124 } 125 126 f, err := os.Open(conf.rootdir + filename) 127 if err != nil { 128 w.WriteHeader(http.StatusNotFound) 129 fmt.Println(err) 130 return 131 } 132 defer f.Close() 133 134 servefile(f, w) 135 } 136 137 func uploader(w http.ResponseWriter, r *http.Request) { 102 138 103 139 err := r.ParseForm() … … 108 144 switch r.Method { 109 145 case "PUT": 110 tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path)) 111 f, err := os.Create(tmp.Name()) 112 if err != nil { 113 fmt.Println(err) 114 return 115 } 116 defer f.Close() 117 118 if writefile(f, r.Body, r.ContentLength) < 0 { 119 w.WriteHeader(http.StatusInternalServerError) 120 return 121 } 122 123 resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n" 124 w.Write([]byte(resp)) 146 uploaderPut(w, r) 125 147 126 148 case "GET": 127 // r.URL.Path is sanitized regarding "." and ".." 128 filename := r.URL.Path 129 if r.URL.Path == "/" { 130 filename = "/index.html" 131 } 132 133 f, err := os.Open(conf.rootdir + filename) 134 if err != nil { 135 w.WriteHeader(http.StatusNotFound) 136 fmt.Println(err) 137 return 138 } 139 defer f.Close() 140 141 servefile(f, w) 149 uploaderGet(w, r) 142 150 } 143 151 } … … 151 159 conf.filectx = "/f/" 152 160 153 http.HandleFunc("/", parse)161 http.HandleFunc("/", uploader) 154 162 http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath)))) 155 163 http.ListenAndServe("0.0.0.0:8080", nil)
Note:
See TracChangeset
for help on using the changeset viewer.