-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-server.go
66 lines (58 loc) · 1.33 KB
/
go-server.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
package main
import (
"fmt"
"net"
"os"
"strings"
"io/ioutil"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
service := ":1200"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
var buf [512]byte
n, err := conn.Read(buf[0:])
fmt.Println(n)
if err != nil {
return
}
req := string(buf[0:])
fmt.Println(req)
header := strings.Split(req,"\n")
parts := strings.Split(header[0], " ")
method := parts[0]
// path := strings.TrimSpace(parts[1])
version := strings.TrimSpace(parts[2])
if version != "HTTP/1.1"{
fmt.Println("version is, ", version)
conn.Write([]byte("HTTP/1.1 505 HTTP Version Not Supported\n\r\n"))
fmt.Println("Response: HTTP/1.1 505 HTTP Version Not Supported")
} else if method != "GET"{
conn.Write([]byte("HTTP/1.1 405 Method Not Allowed\n\r\n"))
fmt.Println("Response: HTTP/1.1 405 Method Not Allowed")
} else{
conn.Write([]byte("HTTP/1.1 200 OK\n\r\n"))
dat, err := ioutil.ReadFile("./index.html")
check(err)
conn.Write([]byte(string(dat)))
}
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}