-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
307 lines (268 loc) · 10.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"errors"
"labix.org/v2/mgo/bson"
"net/http"
"fmt"
"regexp"
"io"
"os"
"path/filepath"
)
func getBinaryFile(w http.ResponseWriter, req *http.Request, ctx *Context) error {
//this strategy is deliberately not dynamic
//for security reasons we don't want strings to directly access files
//on the web server to prevent
//vulnerability from relative paths or obfuscated exploit code.
fmt.Printf("getBinaryFilethe requested path: <<%s>>", req.URL.Path)
binaryFileName := req.URL.Path
switch binaryFileName {
case "/images/lmromanunsl10-regular.eot":
w.Header().Set("Content-Type", "application/vnd.ms-fontobject")
http.ServeFile(w, req, "images/lmromanunsl10-regular.eot")
case "/images/lmromanunsl10-regular.ttf":
w.Header().Set("Content-Type", "font/ttf")
http.ServeFile(w, req, "images/lmromanunsl10-regular.ttf")
case "/images/AdequaTechBusinessCard31May2013.png":
w.Header().Set("Content-Type", "image/png")
http.ServeFile(w, req, "images/AdequaTechBusinessCard31May2013.png")
}
// image/gif
// image/jpeg
// image/pjpeg
// image/png
// image/svg+xml
// image/tiff
// application/vnd.ms-fontobject //.eot
// application/octet-stream //.otf .ttf
// font/ttf //.ttf
// font/otf //.otf
// application/x-woff //.woff
//http.Redirect(w, req, reverse("gostbookhello"), http.StatusSeeOther)
return nil
}
func gostbookhello(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
fmt.Printf("gostbookhellothe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
//set up the collection and query
coll := ctx.C("entries")
query := coll.Find(nil).Sort("-timestamp")
//execute the query
//TODO: add pagination :)
var entries []Entry
if err = query.All(&entries); err != nil {
return
}
//execute the template
return T("gostbookhello.html").Execute(w, map[string]interface{}{
"entries": entries,
"ctx": ctx,
})
}
func gostbooksign(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
fmt.Printf("gostbooksignthe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
//we need a user to sign to
if ctx.User == nil {
err = errors.New("Can't sign without being logged in")
return
}
entry := NewEntry()
entry.Name = ctx.User.Username
entry.Message = req.FormValue("message")
if entry.Message == "" {
entry.Message = "Some dummy who forgot a message."
}
coll := ctx.C("entries")
if err = coll.Insert(entry); err != nil {
return
}
//ignore errors: it's ok if the post count is wrong. we can always look at
//the entries table to fix.
ctx.C("users").Update(bson.M{"_id": ctx.User.ID}, bson.M{
"$inc": bson.M{"posts": 1},
})
http.Redirect(w, req, reverse("gostbookhello"), http.StatusSeeOther)
return
}
func loginForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
fmt.Printf("loginFormthe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
return T("login.html").Execute(w, map[string]interface{}{
"ctx": ctx,
})
}
func login(w http.ResponseWriter, req *http.Request, ctx *Context) error {
fmt.Printf("loginthe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
username, password := req.FormValue("username"), req.FormValue("password")
user, e := Login(ctx, username, password)
if e != nil {
ctx.Session.AddFlash("Invalid Username/Password")
return loginForm(w, req, ctx)
}
//store the user id in the values and redirect to index
ctx.Session.Values["user"] = user.ID
http.Redirect(w, req, reverse("gostbookhello"), http.StatusSeeOther)
return nil
}
func logout(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
if ctx.User == nil {
err = errors.New("You may not logout without being logged in. Please log in.")
return err
}
fmt.Printf("logoutthe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
delete(ctx.Session.Values, "user")
http.Redirect(w, req, reverse("gostbookhello"), http.StatusSeeOther)
return nil
}
func registerForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
fmt.Printf("registerFormthe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
return T("register.html").Execute(w, map[string]interface{}{
"ctx": ctx,
})
}
func register(w http.ResponseWriter, req *http.Request, ctx *Context) error {
fmt.Printf("registerthe <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
username := req.FormValue("username")
password := req.FormValue("password")
password2 := req.FormValue("password2")
//double-check password was typed-in correctly twice
if (password != password2) {
ctx.Session.AddFlash("Register password must be typed in correctly twice.")
return registerForm(w, req, ctx)
}
if ( len(password) < 12 ) {
ctx.Session.AddFlash("Register password must be at least 12 characters long with lowercase, uppercase, digits and punctuation marks.")
return registerForm(w, req, ctx)
}
//enforce password contains lower, upper, digit, punct
lowerRx := regexp.MustCompile("[[:lower:]]")
upperRx := regexp.MustCompile("[[:upper:]]")
digitRx := regexp.MustCompile("[[:digit:]]")
punctRx := regexp.MustCompile("[[:punct:]]")
if lowerMatches := lowerRx.FindAllStringSubmatch(password, -1); lowerMatches == nil {
ctx.Session.AddFlash("Register password must have lowercase letters.")
return registerForm(w, req, ctx)
}
if upperMatches := upperRx.FindAllStringSubmatch(password, -1); upperMatches == nil {
ctx.Session.AddFlash("Register password must have uppercase letters.")
return registerForm(w, req, ctx)
}
if digitMatches := digitRx.FindAllStringSubmatch(password, -1); digitMatches == nil {
ctx.Session.AddFlash("Register password must have digits.")
return registerForm(w, req, ctx)
}
if punctMatches := punctRx.FindAllStringSubmatch(password, -1); punctMatches == nil {
ctx.Session.AddFlash("Register password must have punctuation marks.")
return registerForm(w, req, ctx)
}
u := &User{
Username: username,
ID: bson.NewObjectId(),
}
u.SetPassword(password)
if err := ctx.C("users").Insert(u); err != nil {
ctx.Session.AddFlash("Problem registering user.")
return registerForm(w, req, ctx)
}
//store the user id in the values and redirect to index
ctx.Session.Values["user"] = u.ID
http.Redirect(w, req, reverse("gostbookhello"), http.StatusSeeOther)
return nil
}
func uploadForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
fmt.Printf("uploadForm the <<%s>> requested path: <<%s>>", req.RemoteAddr, req.URL.Path)
return T("upload.html").Execute(w, map[string]interface{}{
"ctx": ctx,
})
}
func upload(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
//make sure the user is logged in
if ctx.User == nil {
err = errors.New("You may not upload files without being logged in. Please log in.")
return
}
err2 := req.ParseMultipartForm(10000000) //Parse using 10MB memory chunks
if err2 != nil {
ctx.Session.AddFlash("upload files error: %s", err2.Error())
return
}
//get a ref to the parsed multipart form
m := req.MultipartForm
//get the *fileheaders
files := m.File["myfiles"]
for i, _ := range files {
// for ensuring no direct access to local file system
// if stripped filename isn't valid, report it and do nothing with this upload callback.
myUncleanfullpath := files[i].Filename
fmt.Printf("unclean files[i].Filename: %s\n", files[i].Filename)
myCleanedPath := filepath.Clean(myUncleanfullpath) //remove . and .. characters
fmt.Printf("myCleanedPath:%s\n", myCleanedPath)
myBasename := filepath.Base(myCleanedPath) //just get basename of file, but it can return a "." character.
fmt.Printf("myBasename:%s\n", myBasename)
if (myBasename == ".") {
ctx.Session.AddFlash("upload files error has no valid filename", err2.Error())
return
}
myFilenameExtension := filepath.Ext(myBasename)
fmt.Printf("Ext:%s\n", myFilenameExtension)
//for each fileheader, get a handle to the actual file
file, err2 := files[i].Open()
defer file.Close()
if err2 != nil {
ctx.Session.AddFlash("upload files open error: %s", err2.Error())
return
}
//create destination file making sure the path is writeable.
dst, err2 := os.Create("/home/loongson/Code/go/src/github.com/omac777/t1/uploaded/" + myBasename)
defer dst.Close()
if err2 != nil {
ctx.Session.AddFlash("upload files destination error: %s", err2.Error())
return
}
//copy the uploaded file to the destination file
fmt.Printf("uploading %s\n", myBasename)
if _, err2 := io.Copy(dst, file); err2 != nil {
ctx.Session.AddFlash("upload files write to destination error: %s", err2.Error())
return
}
fmt.Printf("upload successful %s\n", myBasename)
// validate all the content to match the extension to ensure no file content pretending to be another content type with false filename extensions.
// if file contents does not match file extension, report it and do nothing with this upload callback.
//later on transfer all this file into the mongodb gridfs system for better scalability and performance.
//this web server works through i2p when assigned to an i2p specific port
//this means i2p is just as powerful as tor.
}
//display success message.
ctx.Session.AddFlash("Upload successful")
http.Redirect(w, req, reverse("upload"), http.StatusSeeOther)
return
// this way shows the progress as you upload parts of a file
// and the different mime parts...
// mr, err := req.MultipartReader()
// if err != nil {
// return
// }
// length := req.ContentLength
// for {
// part, err := mr.NextPart()
// if err == io.EOF {
// break
// }
// var read int64
// var p float32
// dst, err := os.OpenFile("dstfile", os.O_WRONLY|os.O_CREATE, 0644)
// if err != nil {
// return
// }
// for {
// buffer := make([]byte, 100000)
// cBytes, err := part.Read(buffer)
// if err == io.EOF {
// break
// }
// read = read + int64(cBytes)
// //fmt.Printf("read: %v \n",read )
// p = float32(read) / float32(length) *100
// fmt.Printf("progress: %v \n",p )
// dst.Write(buffer)
// }
// }
}