-
Notifications
You must be signed in to change notification settings - Fork 23
/
construction.go
405 lines (344 loc) · 10.6 KB
/
construction.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package htmlhouse
import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/writeas/impart"
"github.com/writeas/nerds/store"
"github.com/writeas/web-core/bots"
)
func createHouse(app *app, w http.ResponseWriter, r *http.Request) error {
html := r.FormValue("html")
if strings.TrimSpace(html) == "" {
return impart.HTTPError{http.StatusBadRequest, "Supply something to publish."}
}
public := r.FormValue("public") == "true"
houseID := store.GenerateFriendlyRandomString(8)
_, err := app.db.Exec("INSERT INTO houses (id, html) VALUES (?, ?)", houseID, html)
if err != nil {
return err
}
if err = app.session.writeToken(w, houseID); err != nil {
return err
}
resUser := newSessionInfo(houseID)
if public && passesPublicFilter(app, html) {
go addPublicAccess(app, houseID, html)
}
return impart.WriteSuccess(w, resUser, http.StatusCreated)
}
func validTitle(title string) bool {
return title != "" && strings.TrimSpace(title) != "HTMLhouse"
}
func removePublicAccess(app *app, houseID string) error {
var approved sql.NullInt64
err := app.db.QueryRow("SELECT approved FROM publichouses WHERE house_id = ?", houseID).Scan(&approved)
switch {
case err == sql.ErrNoRows:
return nil
case err != nil:
fmt.Printf("Couldn't fetch for public removal: %v\n", err)
return nil
}
if approved.Valid && approved.Int64 == 0 {
// Page has been banned, so do nothing
} else {
_, err = app.db.Exec("DELETE FROM publichouses WHERE house_id = ?", houseID)
if err != nil {
return err
}
}
return nil
}
func addPublicAccess(app *app, houseID, html string) error {
// Parse title of page
title := titleReg.FindStringSubmatch(html)[1]
if !validTitle(title) {
// <title/> was invalid, so look for an <h1/>
header := headerReg.FindStringSubmatch(html)[1]
if validTitle(header) {
// <h1/> was valid, so use that instead of <title/>
title = header
}
}
title = strings.TrimSpace(title)
// Get thumbnail
data := url.Values{}
data.Set("url", fmt.Sprintf("%s/%s.html", app.cfg.HostName, houseID))
u, err := url.ParseRequestURI(app.cfg.PreviewsHost)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing request URI: %v\n", err)
return err
}
u.Path = "/"
urlStr := fmt.Sprintf("%v", u)
client := &http.Client{}
r, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
if err != nil {
fmt.Printf("Error creating request: %v", err)
}
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
var thumbURL string
resp, err := client.Do(r)
if err != nil {
fmt.Printf("Error requesting thumbnail: %v", err)
return impart.HTTPError{http.StatusInternalServerError, "Couldn't generate thumbnail"}
} else {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode == http.StatusOK {
thumbURL = string(body)
}
}
// Add to public houses table
approved := sql.NullInt64{Valid: false}
if app.cfg.AutoApprove {
approved.Int64 = 1
approved.Valid = true
}
_, err = app.db.Exec("INSERT INTO publichouses (house_id, title, thumb_url, added, updated, approved) VALUES (?, ?, ?, NOW(), NOW(), ?) ON DUPLICATE KEY UPDATE title = ?, updated = NOW()", houseID, title, thumbURL, approved, title)
if err != nil {
return err
}
// Tweet about it
tweet(app, houseID, title)
return nil
}
func renovateHouse(app *app, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
houseID := vars["house"]
html := r.FormValue("html")
if strings.TrimSpace(html) == "" {
return impart.HTTPError{http.StatusBadRequest, "Supply something to publish."}
}
public := r.FormValue("public") == "true"
authHouseID, err := app.session.readToken(r)
if err != nil {
return err
}
if authHouseID != houseID {
return impart.HTTPError{http.StatusUnauthorized, "Bad token for this ⌂ house ⌂."}
}
_, err = app.db.Exec("UPDATE houses SET html = ? WHERE id = ?", html, houseID)
if err != nil {
return err
}
if err = app.session.writeToken(w, houseID); err != nil {
return err
}
resUser := newSessionInfo(houseID)
if public {
go addPublicAccess(app, houseID, html)
} else {
go removePublicAccess(app, houseID)
}
return impart.WriteSuccess(w, resUser, http.StatusOK)
}
func getHouseStats(app *app, houseID string) (*time.Time, int64, error) {
var created time.Time
var views int64
err := app.db.QueryRow("SELECT created, view_count FROM houses WHERE id = ?", houseID).Scan(&created, &views)
switch {
case err == sql.ErrNoRows:
return nil, 0, impart.HTTPError{http.StatusNotFound, "Return to sender. Address unknown."}
case err != nil:
fmt.Printf("Couldn't fetch: %v\n", err)
return nil, 0, err
}
return &created, views, nil
}
func getHouseHTML(app *app, houseID string) (string, error) {
var html string
err := app.db.QueryRow("SELECT html FROM houses WHERE id = ?", houseID).Scan(&html)
switch {
case err == sql.ErrNoRows:
return "", impart.HTTPError{http.StatusNotFound, "Return to sender. Address unknown."}
case err != nil:
fmt.Printf("Couldn't fetch: %v\n", err)
return "", err
}
return html, nil
}
func getPublicHousesData(app *app, w http.ResponseWriter, r *http.Request) error {
houses, err := getPublicHouses(app, true)
if err != nil {
return err
}
for i := range *houses {
(*houses)[i].process(app)
}
return impart.WriteSuccess(w, houses, http.StatusOK)
}
// regular expressions for extracting data
var (
htmlReg = regexp.MustCompile("<html( ?.*)>")
titleReg = regexp.MustCompile("<title>(.+)</title>")
headerReg = regexp.MustCompile("<h1>(.+)</h1>")
)
func getHouse(app *app, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
houseID := vars["house"]
// Fetch HTML
html, err := getHouseHTML(app, houseID)
if err != nil {
if err, ok := err.(impart.HTTPError); ok {
if err.Status == http.StatusNotFound {
page, err := ioutil.ReadFile(app.cfg.StaticDir + "/404.html")
if err != nil {
page = []byte("<!DOCTYPE html><html><body>HTMLlot.</body></html>")
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s", page)
return err
}
}
return err
}
// Add nofollow meta tag
if strings.Index(html, "<head>") == -1 {
html = htmlReg.ReplaceAllString(html, "<html$1><head></head>")
}
html = strings.Replace(html, "<head>", "<head><meta name=\"robots\" content=\"nofollow\" />", 1)
// Add links back to HTMLhouse
homeLink := "<a href='/'><⌂/></a>"
watermark := fmt.Sprintf("<div style='position: absolute;top:16px;right:16px;'>%s · <a href='/stats/%s.html'>stats</a> · <a href='/edit/%s.html'>edit</a></div>", homeLink, houseID, houseID)
if strings.Index(html, "</body>") == -1 {
html = strings.Replace(html, "</html>", "</body></html>", 1)
}
html = strings.Replace(html, "</body>", fmt.Sprintf("%s</body>", watermark), 1)
// Print HTML, with sanity check in case someone did something crazy
if strings.Index(html, homeLink) == -1 {
fmt.Fprintf(w, "%s%s", html, watermark)
} else {
fmt.Fprintf(w, "%s", html)
}
if r.Method != "HEAD" && !bots.IsBot(r.UserAgent()) {
app.db.Exec("UPDATE houses SET view_count = view_count + 1 WHERE id = ?", houseID)
}
return nil
}
func viewHouseStats(app *app, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
houseID := vars["house"]
created, views, err := getHouseStats(app, houseID)
if err != nil {
if err, ok := err.(impart.HTTPError); ok {
if err.Status == http.StatusNotFound {
// TODO: put this logic in one place (shared with getHouse func)
page, err := ioutil.ReadFile(app.cfg.StaticDir + "/404.html")
if err != nil {
page = []byte("<!DOCTYPE html><html><body>HTMLlot.</body></html>")
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s", page)
return err
}
}
return err
}
viewsLbl := "view"
if views != 1 {
viewsLbl = "views"
}
app.templates["stats"].ExecuteTemplate(w, "stats", &HouseStats{
ID: houseID,
Stats: []Stat{
Stat{
Data: fmt.Sprintf("%d", views),
Label: viewsLbl,
},
Stat{
Data: created.Format(time.RFC1123),
Label: "created",
},
},
})
return nil
}
func viewHouses(app *app, w http.ResponseWriter, r *http.Request) error {
houses, err := getPublicHouses(app, false)
if err != nil {
fmt.Printf("Couln't load houses: %v", err)
return err
}
app.templates["browse"].ExecuteTemplate(w, "browse", struct{ Houses *[]PublicHouse }{houses})
return nil
}
func getPublicHouses(app *app, deepData bool) (*[]PublicHouse, error) {
houses := []PublicHouse{}
q := fmt.Sprintf("SELECT house_id, title, thumb_url FROM publichouses WHERE approved = 1 ORDER BY updated DESC LIMIT %d", app.cfg.BrowseItems)
if deepData {
q = fmt.Sprintf("SELECT house_id, title, thumb_url, created, updated, view_count FROM publichouses INNER JOIN houses ON house_id = id WHERE approved = 1 ORDER BY updated DESC LIMIT %d", app.cfg.BrowseItems)
}
rows, err := app.db.Query(q)
switch {
case err == sql.ErrNoRows:
return nil, impart.HTTPError{http.StatusNotFound, "Return to sender. Address unknown."}
case err != nil:
fmt.Printf("Couldn't fetch: %v\n", err)
return nil, err
}
defer rows.Close()
house := &PublicHouse{}
for rows.Next() {
if deepData {
err = rows.Scan(&house.ID, &house.Title, &house.ThumbURL, &house.Created, &house.Updated, &house.Views)
} else {
err = rows.Scan(&house.ID, &house.Title, &house.ThumbURL)
}
houses = append(houses, *house)
}
return &houses, nil
}
func isHousePublic(app *app, houseID string) bool {
var dummy int64
err := app.db.QueryRow("SELECT 1 FROM publichouses WHERE house_id = ?", houseID).Scan(&dummy)
switch {
case err == sql.ErrNoRows:
return false
case err != nil:
fmt.Printf("Couldn't fetch: %v\n", err)
return false
}
return true
}
func banHouse(app *app, w http.ResponseWriter, r *http.Request) error {
houseID := r.FormValue("house")
pass := r.FormValue("pass")
if app.cfg.AdminPass != pass {
w.WriteHeader(http.StatusNotFound)
return nil
}
_, err := app.db.Exec("UPDATE publichouses SET approved = 0 WHERE house_id = ?", houseID)
if err != nil {
fmt.Fprintf(w, "Couldn't ban house: %v", err)
return err
}
fmt.Fprintf(w, "BOOM! %s banned.", houseID)
return nil
}
func unbanHouse(app *app, w http.ResponseWriter, r *http.Request) error {
houseID := r.FormValue("house")
pass := r.FormValue("pass")
if app.cfg.AdminPass != pass {
w.WriteHeader(http.StatusNotFound)
return nil
}
_, err := app.db.Exec("UPDATE publichouses SET approved = 1 WHERE house_id = ?", houseID)
if err != nil {
fmt.Fprintf(w, "Couldn't ban house: %v", err)
return err
}
fmt.Fprintf(w, "boom. Ban on %s reversed.", houseID)
return nil
}