-
Notifications
You must be signed in to change notification settings - Fork 10
/
handlers.go
190 lines (178 loc) · 4.21 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
type wfmRequest struct {
fs afero.Fs
w http.ResponseWriter
userName string
remAddr string
rwAccess bool
modern bool
eSort string // escaped sort order
uDir string // unescaped directory name
uFbn string // unescaped file base name
}
func wfmMain(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(*formMaxMem)
uName, uAccess := auth(w, r)
if uName == "" {
return
}
log.Printf("req from=%q user=%q uri=%q form=%v", r.RemoteAddr, uName, r.RequestURI, noText(r.Form))
if *dumpHeader {
dump, err := httputil.DumpRequest(r, false)
if err == nil {
log.Printf("debug: %v", string(dump))
}
}
wfm := &wfmRequest{
userName: uName,
rwAccess: uAccess,
remAddr: r.RemoteAddr,
w: w,
eSort: r.FormValue("sort"),
modern: func() bool {
return strings.HasPrefix(r.UserAgent(), "Mozilla/5") && r.Header.Get("Accept-Charset") == ""
}(),
fs: wfmFs, // TODO(tenox): per user FS/homedir
uFbn: filepath.Base(unescapeOrEmpty(r.FormValue("file"))),
uDir: filepath.Clean(unescapeOrEmpty(r.FormValue("dir"))),
}
// directory can come either from form value or URI Path
if wfm.uDir == "" || wfm.uDir == "." {
// TODO(tenox): use url.Parse() instead
u, err := url.PathUnescape(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
wfm.uDir = filepath.Clean("/" + strings.TrimPrefix(u, wfmPfx))
}
if wfm.uDir == "" || wfm.uDir == "." {
wfm.uDir = "/"
}
// button clicked
switch {
case r.FormValue("mkd") != "":
wfm.prompt("mkdir", nil)
return
case r.FormValue("mkf") != "":
wfm.prompt("mkfile", nil)
return
case r.FormValue("mkb") != "":
wfm.prompt("mkurl", nil)
return
case r.FormValue("mdelp") != "":
wfm.prompt("multi_delete", r.Form["mulf"])
return
case r.FormValue("mmovp") != "":
wfm.prompt("multi_move", r.Form["mulf"])
return
case r.FormValue("upload") != "":
f, h, err := r.FormFile("filename")
if err != nil {
htErr(w, "upload", err)
return
}
wfm.uploadFile(h, f)
return
case r.FormValue("save") != "":
wfm.saveText(r.FormValue("text"), r.FormValue("crlf"))
return
case r.FormValue("up") != "":
up, err := url.JoinPath(wfmPfx, filepath.Dir(wfm.uDir))
if err != nil {
htErr(w, "up path build", err)
return
}
if wfm.eSort != "" {
up += "?sort=" + wfm.eSort
}
redirect(w, up)
return
case r.FormValue("refresh") != "":
re, err := url.JoinPath(wfmPfx, wfm.uDir)
if err != nil {
htErr(w, "up path build", err)
return
}
if wfm.eSort != "" {
re += "?sort=" + wfm.eSort
}
redirect(w, re)
return
case r.FormValue("home") != "":
wfm.uDir = "/"
wfm.listFiles(filepath.Base(r.FormValue("hi")))
return
case r.FormValue("cancel") != "":
wfm.listFiles(filepath.Base(r.FormValue("hi")))
return
}
// form action submitted
switch r.FormValue("fn") {
case "disp":
wfm.dispFile()
case "down":
wfm.downFile()
case "edit":
wfm.editText()
case "mkdir":
wfm.mkdir()
case "mkfile":
wfm.mkfile()
case "mkurl":
wfm.mkurl(r.FormValue("url"))
case "rename":
wfm.renFile(r.FormValue("dst"))
case "renp":
wfm.prompt("rename", nil)
case "movp":
wfm.prompt("move", nil)
case "delp":
wfm.prompt("delete", nil)
case "move":
wfm.moveFiles([]string{wfm.uFbn}, r.FormValue("dst"))
case "delete":
wfm.deleteFiles([]string{wfm.uFbn})
case "multi_delete":
wfm.deleteFiles(r.Form["mulf"])
case "multi_move":
wfm.moveFiles(r.Form["mulf"], r.FormValue("dst"))
case "logout":
logout(w)
case "about":
wfm.about(r.UserAgent())
default:
wfm.dispOrDir(filepath.Base(r.FormValue("hi")))
}
}
func dispFavIcon(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/x-icon")
w.Write(favIcn)
}
func dispRobots(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "User-agent: *")
if *robots {
fmt.Fprintln(w, "Allow: /")
return
}
fmt.Fprintln(w, "Disallow: /")
}
func unescapeOrEmpty(s string) string {
u, err := url.QueryUnescape(s)
if err != nil {
log.Printf("unescape: %q err=%v", s, err)
return ""
}
return u
}