-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathmain.go
157 lines (127 loc) · 3.16 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
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"runtime"
"strconv"
"sync/atomic"
"time"
"github.com/evanphx/wildcat"
"github.com/panjf2000/gnet/v2"
)
type httpServer struct {
gnet.BuiltinEventEngine
addr string
multicore bool
eng gnet.Engine
}
type httpCodec struct {
parser *wildcat.HTTPParser
contentLength int
buf []byte
}
var CRLF = []byte("\r\n\r\n")
func (hc *httpCodec) parse(data []byte) (int, error) {
// Perform a legit HTTP request parsing.
bodyOffset, err := hc.parser.Parse(data)
if err != nil {
return 0, err
}
// First check if the Content-Length header is present.
contentLength := hc.getContentLength()
if contentLength > -1 {
return bodyOffset + contentLength, nil
}
// If the Content-Length header is not found,
// we need to find the end of the body section.
if idx := bytes.Index(data, CRLF); idx != -1 {
return idx + 4, nil
}
return 0, errors.New("invalid http request")
}
var contentLengthKey = []byte("Content-Length")
func (hc *httpCodec) getContentLength() int {
if hc.contentLength != -1 {
return hc.contentLength
}
val := hc.parser.FindHeader(contentLengthKey)
if val != nil {
i, err := strconv.ParseInt(string(val), 10, 0)
if err == nil {
hc.contentLength = int(i)
}
}
return hc.contentLength
}
func (hc *httpCodec) resetParser() {
hc.contentLength = -1
}
func (hc *httpCodec) reset() {
hc.resetParser()
hc.buf = hc.buf[:0]
}
func (hc *httpCodec) appendResponse() {
hc.buf = append(hc.buf, "HTTP/1.1 200 OK\r\nServer: gnet\r\nContent-Type: text/plain\r\nDate: "...)
hc.buf = append(hc.buf, NowTimeFormat()...)
hc.buf = append(hc.buf, "\r\nContent-Length: 13\r\n\r\nHello, World!"...)
}
func (hs *httpServer) OnBoot(eng gnet.Engine) gnet.Action {
hs.eng = eng
log.Printf("echo server with multi-core=%t is listening on %s\n", hs.multicore, hs.addr)
return gnet.None
}
func (hs *httpServer) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
c.SetContext(&httpCodec{parser: wildcat.NewHTTPParser()})
return nil, gnet.None
}
func (hs *httpServer) OnTraffic(c gnet.Conn) gnet.Action {
hc := c.Context().(*httpCodec)
buf, _ := c.Next(-1)
pipeline:
nextOffset, err := hc.parse(buf)
if err != nil {
goto response
}
hc.resetParser()
hc.appendResponse()
buf = buf[nextOffset:]
if len(buf) > 0 {
goto pipeline
}
response:
c.Write(hc.buf)
hc.reset()
return gnet.None
}
var now atomic.Value
func ticktock() {
now.Store(nowTimeFormat())
for range time.Tick(time.Second) {
now.Store(nowTimeFormat())
}
}
func NowTimeFormat() string {
return now.Load().(string)
}
func nowTimeFormat() string {
return time.Now().Format("Mon, 02 Jan 2006 15:04:05 GMT")
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
now.Store(nowTimeFormat())
go ticktock()
}
func main() {
var port int
var multicore bool
// Example command: go run main.go --port 8080 --multicore=true
flag.IntVar(&port, "port", 8080, "server port")
flag.BoolVar(&multicore, "multicore", true, "multicore")
flag.Parse()
hs := &httpServer{addr: fmt.Sprintf("tcp://:%d", port), multicore: multicore}
// Start serving!
log.Println("server exits:", gnet.Run(hs, hs.addr, gnet.WithMulticore(multicore)))
}