/
html.go
202 lines (180 loc) · 4.38 KB
/
html.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
package server
import (
"database/sql"
"fmt"
"io"
"net/http"
"strconv"
"meguca/auth"
"meguca/cache"
"meguca/common"
"meguca/config"
"meguca/db"
"meguca/lang"
"meguca/templates"
)
// Apply headers and write HTML to client
func serveHTML(w http.ResponseWriter, r *http.Request, buf []byte) {
head := w.Header()
for key, val := range vanillaHeaders {
head.Set(key, val)
}
if assertCached(w, r, buf) {
return
}
head.Set("Content-Type", "text/html")
writeData(w, r, buf)
}
func serveLanding(w http.ResponseWriter, r *http.Request) {
ss, _ := getSession(r, "")
html := templates.Landing(ss, lang.FromReq(r))
serveHTML(w, r, html)
}
func serve404(w http.ResponseWriter, r *http.Request) {
html := templates.NotFound(lang.FromReq(r))
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(404)
io.WriteString(w, html)
}
// Serves board HTML to regular or noscript clients
func boardHTML(w http.ResponseWriter, r *http.Request, b string, catalog bool) {
if !assertBoard(w, r, b) {
return
}
ss, _ := getSession(r, b)
if !assertNotModOnly(w, r, b, ss) {
return
}
html, data, _, err := cache.GetHTML(boardCacheArgs(r, b, catalog))
switch err {
case nil:
// Do nothing.
case errPageOverflow:
serve404(w, r)
return
default:
text500(w, r, err)
return
}
var n, total int
if !catalog {
p := data.(boardPage)
n = p.pageN
total = p.pageTotal
}
l := lang.FromReq(r)
boardConf := config.GetBoardConfig(b)
title := boardConf.Title
if b == "all" {
title = lang.Get(l, "aggregator")
}
html = templates.Board(l, title, n, total, ss, catalog, html)
serveHTML(w, r, html)
}
// Asserts a thread exists on the specific board and renders the index template
func threadHTML(w http.ResponseWriter, r *http.Request) {
ss, id, ok := validateThread(w, r)
if !ok {
return
}
l := lang.FromReq(r)
lastN := detectLastN(r)
k := cache.ThreadKey(l, id, lastN)
html, data, _, err := cache.GetHTML(k, threadCache)
if err != nil {
respondToJSONError(w, r, err)
return
}
b := getParam(r, "board")
title := data.(common.Thread).Subject
html = templates.Thread(id, l, b, title, lastN != 0, ss, html)
serveHTML(w, r, html)
}
// Execute a simple template, that accepts no arguments
func staticTemplate(
w http.ResponseWriter,
r *http.Request,
fn func(string) string,
) {
l := lang.FromReq(r)
serveHTML(w, r, []byte(fn(l)))
}
// Renders a form for creating new boards
func boardCreationForm(w http.ResponseWriter, r *http.Request) {
staticTemplate(w, r, templates.CreateBoard)
}
// Render the form for configuring the server
func serverConfigurationForm(w http.ResponseWriter, r *http.Request) {
if !isAdmin(w, r) {
return
}
data := []byte(templates.ConfigureServer(lang.FromReq(r), *config.Get()))
serveHTML(w, r, data)
}
// Render a form to change an account password
func changePasswordForm(w http.ResponseWriter, r *http.Request) {
staticTemplate(w, r, templates.ChangePassword)
}
// Redirect the client to the appropriate board through a cross-board redirect
func crossRedirect(w http.ResponseWriter, r *http.Request) {
idStr := getParam(r, "id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
serve404(w, r)
return
}
board, op, err := db.GetPostParenthood(id)
switch err {
case nil:
// Don't allow cross-redirects to mod-only boards.
// TODO(Kagami): We shouldn't make links to mod-only clickable?
if !assertNotModOnly(w, r, board, nil) {
return
}
url := r.URL
url.Path = fmt.Sprintf("/%s/%d", board, op)
http.Redirect(w, r, url.String(), 301)
case sql.ErrNoRows:
serve404(w, r)
default:
text500(w, r, err)
}
}
func serveStickers(w http.ResponseWriter, r *http.Request) {
ss, _ := getSession(r, "")
stickHTML := []byte{}
html := templates.Stickers(ss, lang.FromReq(r), stickHTML)
serveHTML(w, r, html)
}
// Confirms a the thread exists on the board and returns its ID. If an error
// occurred and the calling function should return, ok = false.
func validateThread(w http.ResponseWriter, r *http.Request) (
ss *auth.Session,
id uint64,
ok bool,
) {
b := getParam(r, "board")
if !assertBoard(w, r, b) {
return
}
ss, _ = getSession(r, b)
if !assertNotModOnly(w, r, b, ss) {
return
}
id, err := strconv.ParseUint(getParam(r, "thread"), 10, 64)
if err != nil {
serve404(w, r)
return
}
valid, err := db.ValidateOP(id, b)
if err != nil {
text500(w, r, err)
return
}
if !valid {
serve404(w, r)
return
}
ok = true
return
}