-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
221 lines (197 loc) · 5.76 KB
/
http.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
package main
import (
"github.com/jinzhu/gorm"
"io/ioutil"
"strconv"
"strings"
)
import "fmt"
import "net/http"
const maxUploadSize = 256 * 1024 // 256 KiB
func initHttp() {
http.HandleFunc("/", httpRoot)
http.HandleFunc("/register", httpRegister)
http.HandleFunc("/login", httpLogin)
http.HandleFunc("/logout", httpLogout)
http.HandleFunc("/account", httpAccount)
http.HandleFunc("/upload", httpUpload)
http.HandleFunc("/download", httpDownload)
http.HandleFunc("/static/", httpStatic)
}
func httpRoot(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
username := ""
if session := RequestSession(r); session != nil {
username = session.Username()
}
w.Header().Set("Content-Type", "text/html")
check(renderTemplate(w, "index.html", struct{ UserName string }{username}))
} else if r.URL.Path == "/index.html" {
http.Redirect(w, r, "/", http.StatusSeeOther)
} else {
http.NotFound(w, r)
}
}
func httpRegister(w http.ResponseWriter, r *http.Request) {
if RequestSession(r) != nil {
http.Redirect(w, r, "/account", http.StatusTemporaryRedirect)
}
errorMessage := ""
if r.Method == "POST" {
err := registerUser(r.FormValue("username"), r.FormValue("password"))
if err != nil {
errorMessage = err.Error()
} else {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
}
w.Header().Set("Content-Type", "text/html")
check(renderTemplate(w, "register.html", struct {
UserName string
ErrorMessage string
}{"", errorMessage}))
}
func httpLogin(w http.ResponseWriter, r *http.Request) {
if RequestSession(r) != nil {
http.Redirect(w, r, "/account", http.StatusTemporaryRedirect)
}
if r.Method == "GET" {
w.Header().Set("Content-Type", "text/html")
check(renderTemplate(w, "login.html", struct{ UserName string }{""}))
} else if r.Method == "POST" {
username := r.FormValue("username")
password := r.FormValue("password")
username = string(makeUpper([]byte(username)))
var user User
if result := db.Where(&User{Username: username}).First(&user); result.Error != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
session := NewSession()
copy(session.UsernameBytes(), username)
copy(session.PasswordBytes(), password)
session.SetUid(uint64(user.ID))
http.SetCookie(w, session.ToCookie())
http.Redirect(w, r, "/account", http.StatusSeeOther)
}
}
func httpLogout(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{Name: "session"})
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func httpAccount(w http.ResponseWriter, r *http.Request) {
session := RequestSession(r)
if session == nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
var files []File
db.Where(&File{UserId: uint(session.Uid())}).Find(&files)
userRecord := User{Model: gorm.Model{ID: uint(session.Uid())}}
db.Where(&userRecord).Find(&userRecord)
w.Header().Set("Content-Type", "text/html")
check(renderTemplate(w, "account.html", struct {
UserName string
UserRecord User
Files []File
}{session.Username(), userRecord, files}))
}
func httpUpload(w http.ResponseWriter, r *http.Request) {
session := RequestSession(r)
if session == nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
errorMessage := ""
successMessage := ""
if r.Method == "POST" {
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
if err.Error() == "http: request body too large" {
errorMessage = fmt.Sprintf("Uploaded file is too large.")
goto out
} else {
panic(err)
}
}
username := r.FormValue("user")
var user User
if userid, err := strconv.ParseUint(username, 10, 0); err == nil {
user.ID = uint(userid)
} else {
user.Username = string(makeUpper([]byte(username)))
}
if result := db.Where(&user).First(&user); result.Error != nil {
errorMessage = fmt.Sprintf("unknown user %q\n", username)
goto out
}
file, header, err := r.FormFile("data")
if err != nil {
errorMessage = "cannot read uploaded file"
goto out
}
data, err := ioutil.ReadAll(file)
if err != nil {
errorMessage = "cannot read uploaded file"
goto out
}
fileRecord := File{
UserId: user.ID,
Name: header.Filename,
MimeType: header.Header.Get("Content-Type"),
Data: data,
}
if result := db.Create(&fileRecord); result.Error != nil {
errorMessage = "Failed to save upload."
goto out
}
successMessage = "File uploaded successfully."
goto out
}
out:
w.Header().Set("Content-Type", "text/html")
check(renderTemplate(w, "upload.html", struct {
UserName string
ErrorMessage string
SuccessMessage string
}{session.Username(), errorMessage, successMessage}))
}
func httpDownload(w http.ResponseWriter, r *http.Request) {
session := RequestSession(r)
if session == nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
fileId, err := strconv.Atoi(r.URL.RawQuery)
if err != nil {
fileId = -1
}
var file File
if result := db.Where(&File{Model: gorm.Model{ID: uint(fileId)}, UserId: uint(session.Uid())}).First(&file); result.Error != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "file not found")
return
}
if file.MimeType == "" {
file.MimeType = "application/octet-stream"
}
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", file.Name))
w.Header().Set("Content-Type", file.MimeType)
w.Write(file.Data)
}
func httpStatic(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
data, ok := bindata[name]
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "file not found")
return
}
mimeType := "application/octet-stream"
if strings.HasSuffix(name, ".css") {
mimeType = "text/css"
}
w.Header().Set("Content-Type", mimeType)
w.Write([]byte(data))
}