Changeset 9 in code for trunk


Ignore:
Timestamp:
Oct 11, 2021, 6:35:15 PM (4 years ago)
Author:
dev
Message:

Move PUT/GET handlers to their own functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/partage.go

    r8 r9  
    9393}
    9494
    95 func parse(w http.ResponseWriter, r *http.Request) {
    96 
     95func uploaderPut(w http.ResponseWriter, r *http.Request) {
    9796        // Max 15 Gb uploads
    9897        if r.ContentLength > conf.maxsize {
     
    10099                w.Write([]byte("File is too big"))
    101100        }
     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
     119func 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
     137func uploader(w http.ResponseWriter, r *http.Request) {
    102138
    103139        err := r.ParseForm()
     
    108144        switch r.Method {
    109145        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)
    125147
    126148        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)
    142150        }
    143151}
     
    151159        conf.filectx = "/f/"
    152160
    153         http.HandleFunc("/", parse)
     161        http.HandleFunc("/", uploader)
    154162        http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
    155163        http.ListenAndServe("0.0.0.0:8080", nil)
Note: See TracChangeset for help on using the changeset viewer.