-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
118 lines (96 loc) · 2.38 KB
/
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
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
package http
import (
"fmt"
"io"
"strings"
)
var DefaultServer = &Server{
tcpClient: NewTCPClient(),
router: make(map[string]handler),
}
type Server struct {
tcpClient TCPClient
router
}
type handler func(w ResponseWriter, r *Request)
type router map[string]handler
func ListenAndServe(port int) error {
return DefaultServer.ListenAndServe(port)
}
func (s *Server) ListenAndServe(port int) error {
ip := [4]byte{127, 0, 0, 1}
listener, err := s.tcpClient.Listen(ip, port)
if err != nil {
return err
}
defer listener.Close()
ch := make(chan io.ReadWriteCloser)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
break
}
ch <- conn
}
}()
for {
conn := <-ch
go func() {
defer conn.Close()
req, err := parseRequest(conn)
if err != nil {
io.Copy(conn, strings.NewReader("HTTP/1.1 404 Not Found\n\n404 Not Found"))
}
handler, exists := s.router[req.Path]
if exists {
w := &responseWriter{
conn: conn,
header: make(Header),
}
handler(w, req)
} else {
io.Copy(conn, strings.NewReader("HTTP/1.1 404 Not Found\n\n404 Not Found"))
}
}()
}
}
func HandleFunc(path string, handler handler) {
DefaultServer.router[path] = handler
}
func (s *Server) HandleFunc(path string, handler handler) {
s.router[path] = handler
}
// A ResponseWriter interface is used by an HTTP handler to
// construct an HTTP response.
type ResponseWriter interface {
// Header returns the header map that will be sent by WriteHeader
Header() Header
// Write writes the data to the connection as part of an HTTP reply.
Write([]byte) (int, error)
// WriteHeader sends an HTTP response header with the provided
// status code.
//
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
WriteHeader(statusCode int)
}
type responseWriter struct {
conn io.Writer // TCP connection
header Header // Response header
wroteHeader bool // If it already wrote the HTTP header
}
func (w *responseWriter) Header() Header {
return w.header
}
func (w *responseWriter) Write(p []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(200)
}
return w.conn.Write(p)
}
func (w *responseWriter) WriteHeader(statusCode int) {
fmt.Fprintf(w.conn, "HTTP/1.1 %d %s\n", statusCode, statusText[statusCode])
w.header.writeTo(w.conn)
w.wroteHeader = true
}