-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
216 lines (187 loc) · 5.72 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
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
package main
import(
"fmt"
"syscall"
"log"
"strings"
"errors"
"strconv"
"os"
)
func send_img(conn_socket int, path string) error{
img_file, err := os.ReadFile(path)
if err != nil{
body :="Internal Sever error while loading the img file"
send_msg("500 Internal Server Error", body, "close", conn_socket)
return errors.New("ERROR: Internal Sever error while loading the img file")
}
response := fmt.Sprintf("HTTP/1.1 200 OK\r\n" +
"Host: www.monchaco.tech-ahmed.com\r\n" +
"Content-Type: img/jpg\r\n" +
"Content-Length: %d\r\n" +
"Connection: close\r\n" +
"\r\n%s",
len(img_file),string(img_file))
_, msg_err := syscall.Write(conn_socket, []byte(response))
if msg_err != nil {
if msg_err == syscall.EPIPE {
return errors.New("ERROR: Broken pipe: Client has disconnected.")
}else {
return msg_err
}
}
return nil
}
func send_html(conn_socket int, path string) error{
html_file, err := os.ReadFile(path)
if err != nil{
body :="Internal Sever error while loading the HTML file"
send_msg("500 Internal Server Error", body, "close", conn_socket)
return errors.New("ERROR: Internal Sever error while loading the HTML file")
}
response := fmt.Sprintf("HTTP/1.1 200 OK\r\n" +
"Host: www.monchaco.tech-ahmed.com\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: %d\r\n" +
"Connection: close\r\n" +
"\r\n%s",
len(html_file),string(html_file))
_, msg_err := syscall.Write(conn_socket, []byte(response))
if msg_err != nil {
if msg_err == syscall.EPIPE {
return errors.New("ERROR: Broken pipe: Client has disconnected.")
}else {
return msg_err
}
}
return nil
}
func send_msg(code string, body string, conn_state string, conn_socket int) error{
body_len := strconv.Itoa(len(body))
response := "HTTP/1.1 " + code + "\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: " + body_len + "\r\n" +
"Connection: " + conn_state + "\r\n" +
"\r\n" +
body + "\n"
_, msg_err := syscall.Write(conn_socket, []byte(response))
if msg_err != nil {
if msg_err == syscall.EPIPE {
return errors.New("ERROR: Broken pipe: Client has disconnected.")
}else {
return msg_err
}
}
return nil
}
func get_peer(conn_socket int) (Addr [4]byte, Port int, err error){
peer, err := syscall.Getpeername(conn_socket)
if err != nil{
log.Fatal(err)
}
peer_data, ok := peer.(*syscall.SockaddrInet4)
if !ok {
return [4]byte{}, 0, errors.New("ERROR: unexpected address type")
}
Addr = peer_data.Addr
Port = peer_data.Port
return Addr, Port, nil
}
func client_handler(conn_socket int){
var recv_buf = make([]byte, 1024)
for{
length, _, recv_err := syscall.Recvfrom(conn_socket, recv_buf, 0)
if recv_err != nil{
log.Print("ERROR: ", recv_err)
}
if length == 0{
body := "Connection closed by peer!"
send_msg("400", body, "close", conn_socket)
break
}else if length < 0{
body := "Connection reset by peer!"
send_msg("400", body, "close", conn_socket)
break
}else{
err := read_request(conn_socket, recv_buf, length)
if err != nil{
log.Print("ERROR: ", err)
body := "Detected an error, closing connection..."
send_msg("400", body, "close", conn_socket)
break
}
}
}
syscall.Close(conn_socket)
}
func read_request(conn_socket int, buf []byte, length int) error{
var builder strings.Builder
_, err := builder.Write(buf[:length])
if err != nil {
return errors.New("ERROR: Error writing to builder")
}
req_str := builder.String()
lines := strings.Split(req_str, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
switch line {
case "Connection: close":
log.Println("INFO:" + line)
return errors.New("ERROR: Connection close received!")
case "GET / HTTP/1.1":
log.Println("INFO:" + line)
return send_html(conn_socket, "pages/index.html")
case "GET /favicon.ico HTTP/1.1":
log.Println("INFO:" + line)
return send_img(conn_socket, "pages/img/favicon.ico")
case "GET /hey.jpg HTTP/1.1":
log.Println("INFO:" + line)
return send_img(conn_socket, "pages/img/hey.jpg")
case "GET /about HTTP/1.1":
log.Println("INFO:" + line)
return send_html(conn_socket, "pages/about.html")
case "GET /prime.jpg HTTP/1.1":
log.Println("INFO:" + line)
return send_img(conn_socket, "pages/img/prime.jpg")
default:
log.Println("ERROR:" + line)
send_msg("404 Not Found", "Page doesn't exist", "close", conn_socket)
return errors.New("ERROR: 404 Not Found, page doesn't exist")
}
}
if !strings.Contains(strings.TrimSpace(req_str), "/1.1"){
return errors.New("ERROR: This Server expects HTTP/1.1!")
}
return nil
}
func main() {
var sa syscall.SockaddrInet4
socket, sock_err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if sock_err != nil{
log.Fatal(sock_err)
}
sa.Port = 3232
sa.Addr = [4]byte{127,0,0,1}
bind_err := syscall.Bind(socket, &sa)
if bind_err != nil{
log.Fatal(bind_err)
}
listen_err := syscall.Listen(socket, 10)
if listen_err != nil{
log.Fatal(listen_err)
}
log.Println("INFO: Server is listening on 127.0.0.1")
for {
connection_socket, _, accept_err := syscall.Accept(socket)
if accept_err != nil{
errors.New("ERROR: Failed to accept the connection, please try again")
}
addr, port, getPeerErr := get_peer(connection_socket)
if getPeerErr != nil {
log.Println("ERROR: Getpeername error:", getPeerErr)
} else {
log.Println("INFO: Client connected: ", addr, port)
}
go client_handler(connection_socket)
}
}