-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (189 loc) · 6.53 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
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
package main
import (
"alexartwww/goHttpServer/file"
"alexartwww/goHttpServer/http"
"fmt"
"io"
"net"
"strconv"
"time"
)
// Config
// =======================================================
var address = "localhost:4321"
var home = "./www"
var indexes = []string{"", "index.html", "index.htm"}
// =======================================================
func main() {
fmt.Println("Listen http://" + address)
listener, _ := net.Listen("tcp", address)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleClient(conn)
}
}
func log(currentTime time.Time, conn net.Conn, iri string, code uint16) {
fmt.Println(currentTime.Format(time.RFC3339), conn.RemoteAddr().String(), code, iri)
}
func handleClient(conn net.Conn) {
defer conn.Close()
var currentTime = time.Now()
geoLocation, _ := time.LoadLocation("GMT")
frameLen := 32
frame := make([]byte, frameLen)
buff := make([]byte, 0)
for {
readLen, err := conn.Read(frame)
if err != nil {
if err != io.EOF {
fmt.Println("read error:", err)
}
break
}
buff = append(buff, frame[:readLen]...)
if readLen < frameLen {
break
}
}
request := http.Request{}
request.Parse(buff)
reader := file.File{}
for _, index := range indexes {
fileLocation := home + request.Path
if fileLocation[len(fileLocation)-1:] != "/" && index != "" {
fileLocation = fileLocation + "/"
}
fileLocation = fileLocation + index
reader.Info(fileLocation)
if reader.Exist {
if reader.Directory {
continue
}
if reader.Readable {
response200 := http.Response{
Protocol: request.Protocol,
Version: request.Version,
Code: 200,
Status: "OK",
Headers: []http.Header {
{Name: "Server", Value: "goHttpServer/0.0.1"},
{Name: "Date", Value: currentTime.In(geoLocation).Format(time.RFC1123)},
},
}
if !reader.DateTime.IsZero() {
response200.Headers = append(response200.Headers, http.Header{Name: "Last-Modified", Value: reader.DateTime.In(geoLocation).Format(time.RFC1123)})
}
if reader.ETag != "" {
response200.Headers = append(response200.Headers, http.Header{Name: "ETag", Value: reader.ETag})
}
if reader.Size > 0 {
response200.Headers = append(response200.Headers, http.Header{Name: "Content-Length", Value: strconv.Itoa(int(reader.Size))})
}
if reader.Mimetype != "" {
response200.Headers = append(response200.Headers, http.Header{Name: "Content-Type", Value: reader.Mimetype})
}
response200.Body = reader.Read()
log(currentTime, conn, request.Iri, response200.Code)
conn.Write(response200.Build())
} else {
response403 := http.Response{
Protocol: request.Protocol,
Version: request.Version,
Code: 403,
Status: "Forbidden",
Headers: []http.Header {
{Name: "Server", Value: "goHttpServer/0.0.1"},
{Name: "Date", Value: currentTime.In(geoLocation).Format(time.RFC1123)},
},
Body: []byte("<html><head><title>403 Forbidden</title></head><body><h1 style=\"text-align: center;\">404 Not Found</h1><hr><p style=\"text-align: center;\">goHttpServer/0.0.1 " + currentTime.In(geoLocation).Format(time.RFC1123) + "</p></body></html>"),
}
log(currentTime, conn, request.Iri, response403.Code)
conn.Write(response403.Build())
}
return
}
}
response404 := http.Response{
Protocol: request.Protocol,
Version: request.Version,
Code: 404,
Status: "Not Found",
Headers: []http.Header {
{Name: "Server", Value: "goHttpServer/0.0.1"},
{Name: "Date", Value: currentTime.In(geoLocation).Format(time.RFC1123)},
},
Body: []byte("<html><head><title>404 Not Found</title></head><body><h1 style=\"text-align: center;\">404 Not Found</h1><hr><p style=\"text-align: center;\">goHttpServer/0.0.1 " + currentTime.In(geoLocation).Format(time.RFC1123) + "</p></body></html>"),
}
log(currentTime, conn, request.Iri, response404.Code)
conn.Write(response404.Build())
return
}
// This will goes to tests
// =======================================================================
func testRequest() {
buff := []byte("GET /?a=1&b=2&FFSDF=asdasd HTTP/1.1\nHost: artem-aleksashkin\nConnection: keep-alive\nCache-Control: max-age=0\nDNT: 1\nUpgrade-Insecure-Requests: 1\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\nReferer: http://artem-aleksashkin/\nAccept-Encoding: gzip, deflate\nAccept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7\nCookie: id=ca2a568d-cdf0-4658-b98c-320a9f1b0eb6; geography=1; timezone=Europe%2FMoscow; language=ru; language-data=ru%2Cen; currency=rub; user=c4ca4238a0b923820dcc509a6f75849b\n\ntest")
request := http.Request{}
request.Parse(buff)
fmt.Println(request.Method)
fmt.Println(request.Protocol)
fmt.Println(request.Version)
fmt.Println(request.Iri)
fmt.Println(request.Path)
for _, param := range request.Params {
fmt.Printf("Param Name: \"%s\" = Value: \"%s\"\n", param.Name, param.Value)
}
for _, header := range request.Headers {
fmt.Printf("Header Name: \"%s\" = Value: \"%s\"\n", header.Name, header.Value)
}
for _, cookie := range request.Cookies {
fmt.Printf("Cookie Name: \"%s\" = Value: \"%s\"\n", cookie.Name, cookie.Value)
}
fmt.Println(string(request.Body))
}
func testResponse() {
response := http.Response{
Protocol: "HTTP",
Version: "1.1",
Code: 200,
Status: "OK",
Headers: []http.Header {
{Name: "Server", Value: "goHttpServer/0.0.1"},
},
Body: []byte("<html><head><title>goHttpServer works!</title></head><body><h1>goHttpServer works!</h1></body></html>"),
}
fmt.Println(string(response.Build()))
}
func testFile() {
home := "./www"
path := "/"
reader := file.File{}
indexes := []string{"", "index.html", "index.htm"}
for _, index := range indexes {
fileLocation := home + path
if fileLocation[len(fileLocation)-1:] != "/" && index != "" {
fileLocation = fileLocation + "/"
}
fileLocation = fileLocation + index
reader.Info(fileLocation)
fmt.Println("Name", reader.Name)
fmt.Println("Exist", reader.Exist)
if reader.Exist {
fmt.Println("Directory", reader.Directory)
if reader.Directory {
continue
}
fmt.Println("Readable", reader.Readable)
if reader.Readable {
fmt.Println("Size", reader.Size)
fmt.Println("Mimetype", reader.Mimetype)
geoLocation, _ := time.LoadLocation("GMT")
fmt.Println("DateTime", reader.DateTime.In(geoLocation).Format(time.RFC1123))
fmt.Println(string(reader.Read()))
break
}
}
}
}