|
@@ -5,8 +5,6 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"log"
|
|
"log"
|
|
"runtime"
|
|
"runtime"
|
|
- "strconv"
|
|
|
|
- "strings"
|
|
|
|
"time"
|
|
"time"
|
|
"unsafe"
|
|
"unsafe"
|
|
|
|
|
|
@@ -24,8 +22,6 @@ type httpServer struct {
|
|
*gnet.EventServer
|
|
*gnet.EventServer
|
|
}
|
|
}
|
|
|
|
|
|
-var res string
|
|
|
|
-
|
|
|
|
type httpCodec struct {
|
|
type httpCodec struct {
|
|
req request
|
|
req request
|
|
}
|
|
}
|
|
@@ -45,7 +41,7 @@ pipeline:
|
|
// request not ready, yet
|
|
// request not ready, yet
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- out = appendHandle(out, res)
|
|
|
|
|
|
+ out = appendResp(out)
|
|
buf = leftover
|
|
buf = leftover
|
|
goto pipeline
|
|
goto pipeline
|
|
}
|
|
}
|
|
@@ -75,8 +71,6 @@ func main() {
|
|
flag.BoolVar(&multicore, "multicore", true, "multicore")
|
|
flag.BoolVar(&multicore, "multicore", true, "multicore")
|
|
flag.Parse()
|
|
flag.Parse()
|
|
|
|
|
|
- res = "Hello, World!"
|
|
|
|
-
|
|
|
|
http := new(httpServer)
|
|
http := new(httpServer)
|
|
hc := new(httpCodec)
|
|
hc := new(httpCodec)
|
|
|
|
|
|
@@ -84,31 +78,16 @@ func main() {
|
|
log.Fatal(gnet.Serve(http, fmt.Sprintf("tcp://:%d", port), gnet.WithMulticore(multicore), gnet.WithCodec(hc)))
|
|
log.Fatal(gnet.Serve(http, fmt.Sprintf("tcp://:%d", port), gnet.WithMulticore(multicore), gnet.WithCodec(hc)))
|
|
}
|
|
}
|
|
|
|
|
|
-// appendHandle handles the incoming request and appends the response to
|
|
|
|
-// the provided bytes, which is then returned to the caller.
|
|
|
|
-func appendHandle(b []byte, res string) []byte {
|
|
|
|
- return appendResp(b, "200 OK", res)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// appendResp will append a valid http response to the provide bytes.
|
|
// appendResp will append a valid http response to the provide bytes.
|
|
// The status param should be the code plus text such as "200 OK".
|
|
// The status param should be the code plus text such as "200 OK".
|
|
// The head parameter should be a series of lines ending with "\r\n" or empty.
|
|
// The head parameter should be a series of lines ending with "\r\n" or empty.
|
|
-func appendResp(b []byte, status, body string) []byte {
|
|
|
|
- b = append(b, "HTTP/1.1"...)
|
|
|
|
- b = append(b, ' ')
|
|
|
|
- b = append(b, status...)
|
|
|
|
- b = append(b, '\r', '\n')
|
|
|
|
- b = append(b, "Server: gnet\r\n"...)
|
|
|
|
- b = append(b, "Content-Type: text/plain\r\n"...)
|
|
|
|
- b = append(b, "Date: "...)
|
|
|
|
|
|
+func appendResp(b []byte) []byte {
|
|
|
|
+ b = append(b, "HTTP/1.1 200 OK\r\nServer: gnet\r\nContent-Type: text/plain\r\nDate: "...)
|
|
|
|
+
|
|
b = time.Now().AppendFormat(b, "Mon, 02 Jan 2006 15:04:05 GMT")
|
|
b = time.Now().AppendFormat(b, "Mon, 02 Jan 2006 15:04:05 GMT")
|
|
- b = append(b, '\r', '\n')
|
|
|
|
- if len(body) > 0 {
|
|
|
|
- b = append(b, "Content-Length: "...)
|
|
|
|
- b = strconv.AppendInt(b, int64(len(body)), 10)
|
|
|
|
- b = append(b, '\r', '\n', '\r', '\n')
|
|
|
|
- b = append(b, body...)
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ b = append(b, "\r\nContent-Length: 13\r\n\r\nHello, World!"...)
|
|
|
|
+
|
|
return b
|
|
return b
|
|
}
|
|
}
|
|
|
|
|
|
@@ -116,11 +95,6 @@ func b2s(b []byte) string {
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
}
|
|
}
|
|
|
|
|
|
-const (
|
|
|
|
- contentLength = "Content-Length:"
|
|
|
|
- contentLen = len(contentLength)
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
// parseReq is a very simple http request parser. This operation
|
|
// parseReq is a very simple http request parser. This operation
|
|
// waits for the entire payload to be buffered before returning a
|
|
// waits for the entire payload to be buffered before returning a
|
|
// valid request.
|
|
// valid request.
|
|
@@ -130,7 +104,6 @@ func parseReq(data []byte, req *request) (leftover []byte, err error) {
|
|
var (
|
|
var (
|
|
i, s int
|
|
i, s int
|
|
head string
|
|
head string
|
|
- clen int
|
|
|
|
q = -1
|
|
q = -1
|
|
)
|
|
)
|
|
// method, path, proto line
|
|
// method, path, proto line
|
|
@@ -171,21 +144,8 @@ func parseReq(data []byte, req *request) (leftover []byte, err error) {
|
|
if line == "" {
|
|
if line == "" {
|
|
req.head = sdata[len(head)+2 : i+1]
|
|
req.head = sdata[len(head)+2 : i+1]
|
|
i++
|
|
i++
|
|
- if clen > 0 {
|
|
|
|
- if len(sdata[i:]) < clen {
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- req.body = sdata[i : i+clen]
|
|
|
|
- i += clen
|
|
|
|
- }
|
|
|
|
return data[i:], nil
|
|
return data[i:], nil
|
|
}
|
|
}
|
|
- if strings.HasPrefix(line, contentLength) {
|
|
|
|
- n, err := strconv.ParseInt(strings.TrimSpace(line[contentLen:]), 10, 64)
|
|
|
|
- if err == nil {
|
|
|
|
- clen = int(n)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// not enough data
|
|
// not enough data
|