forked from emgram769/livechan
/
http.go
107 lines (92 loc) · 2.62 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
package main
import (
"github.com/dchest/captcha"
"github.com/gorilla/websocket"
"net/http"
"fmt"
"strings"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true }, // TODO: fix
}
func wsServer(w http.ResponseWriter, req *http.Request) {
channelName := req.URL.Path[4:] // Slice off "/ws/"
if req.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
if (storage.getChatChannelId(channelName) == 0) {
http.Error(w, "Method not allowed", 405)
return
}
ws, err := upgrader.Upgrade(w, req, nil)
if err != nil {
fmt.Println(err)
return
}
c := &Connection{
send: make(chan []byte, 256),
ws: ws,
channelName: channelName,
ipAddr: req.RemoteAddr,
}
h.register <- c
/* Start a reader/writer pair for the new connection. */
go c.writer()
/* Nature of go treats this handler as a goroutine.
Small optimization to not spawn a new one. */
c.reader()
}
func channelServer(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "%+v", storage.getChannels());
}
func convoServer(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "%+v %s", storage.getConvos(req.URL.Path[8:]), req.URL.Path[8:]);
}
func htmlServer(w http.ResponseWriter, req *http.Request) {
channelName := req.URL.Path[1:] // Omit the leading "/"
/* Disallow / in the name. */
if strings.Contains(channelName, "/") {
http.Error(w, "Method not allowed", 405)
return
}
if req.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
if channelName == "" {
channelName = "General"
}
if (storage.getChatChannelId(channelName) == 0) {
http.Error(w, "No registration pages, yet!", 405)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
http.ServeFile(w, req, "index.html")
}
func captchaServer(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "%s", captcha.New());
return
} else if req.Method == "POST" {
if captcha.VerifyString(req.FormValue("captchaId"), req.FormValue("captchaSolution")) {
} else {
}
}
}
func staticServer(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, req.URL.Path[1:])
}