This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (145 loc) · 2.92 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strings"
)
func main() {
l, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalln(err)
}
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
log.Println(err)
continue
}
// now handles multiple connections
go serve(c)
}
}
func serve(c net.Conn) {
defer c.Close()
var i int
var rMethod, rURI string
scanner := bufio.NewScanner(c)
for scanner.Scan() {
ln := scanner.Text()
fmt.Println(ln)
if i == 0 {
// we're in REQUEST LINE
xs := strings.Fields(ln)
rMethod = xs[0]
rURI = xs[1]
fmt.Println("METHOD:", rMethod)
fmt.Println("URI:", rURI)
}
if ln == "" {
// when ln is empty, header is done
fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS")
break
}
i++
}
switch {
case rMethod == "GET" && rURI == "/":
handleIndex(c)
case rMethod == "GET" && rURI == "/apply":
handleApply(c)
case rMethod == "POST" && rURI == "/apply":
handleApplyPost(c)
default:
handleDefault(c)
}
}
func handleIndex(c net.Conn) {
body := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GET INDEX</title>
</head>
<body>
<h1>"GET INDEX"</h1>
<a href="/">index</a><br>
<a href="/apply">apply</a><br>
</body>
</html>
`
io.WriteString(c, "HTTP/1.1 200 OK\r\n")
fmt.Fprintf(c, "Content-Length: %d\r\n", len(body))
fmt.Fprint(c, "Content-Type: text/html\r\n")
io.WriteString(c, "\r\n")
io.WriteString(c, body)
}
func handleApply(c net.Conn) {
body := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GET DOG</title>
</head>
<body>
<h1>"GET APPLY"</h1>
<a href="/">index</a><br>
<a href="/apply">apply</a><br>
<form action="/apply" method="POST">
<input type="hidden" value="In my good death">
<input type="submit" value="submit">
</form>
</body>
</html>
`
io.WriteString(c, "HTTP/1.1 200 OK\r\n")
fmt.Fprintf(c, "Content-Length: %d\r\n", len(body))
fmt.Fprint(c, "Content-Type: text/html\r\n")
io.WriteString(c, "\r\n")
io.WriteString(c, body)
}
func handleApplyPost(c net.Conn) {
body := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>POST APPLY</title>
</head>
<body>
<h1>"POST APPLY"</h1>
<a href="/">index</a><br>
<a href="/apply">apply</a><br>
</body>
</html>
`
io.WriteString(c, "HTTP/1.1 200 OK\r\n")
fmt.Fprintf(c, "Content-Length: %d\r\n", len(body))
fmt.Fprint(c, "Content-Type: text/html\r\n")
io.WriteString(c, "\r\n")
io.WriteString(c, body)
}
func handleDefault(c net.Conn) {
body := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>default</title>
</head>
<body>
<h1>"default"</h1>
</body>
</html>
`
io.WriteString(c, "HTTP/1.1 200 OK\r\n")
fmt.Fprintf(c, "Content-Length: %d\r\n", len(body))
fmt.Fprint(c, "Content-Type: text/html\r\n")
io.WriteString(c, "\r\n")
io.WriteString(c, body)
}