Skip to content

Commit

Permalink
opt: improve http example
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Mar 13, 2020
1 parent 742f7f2 commit 7155bed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ import (
"strconv"
"strings"
"time"
"unsafe"

"github.com/panjf2000/gnet"
)
Expand Down Expand Up @@ -554,11 +555,15 @@ func appendResp(b []byte, status, head, body string) []byte {
return b
}

func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// parseReq is a very simple http request parser. This operation
// waits for the entire payload to be buffered before returning a
// valid request.
func parseReq(data []byte, req *request) (leftover []byte, err error) {
sdata := string(data)
sdata := b2s(data)
var i, s int
var head string
var clen int
Expand Down
7 changes: 6 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ import (
"strconv"
"strings"
"time"
"unsafe"

"github.com/panjf2000/gnet"
)
Expand Down Expand Up @@ -552,11 +553,15 @@ func appendResp(b []byte, status, head, body string) []byte {
return b
}

func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// parseReq is a very simple http request parser. This operation
// waits for the entire payload to be buffered before returning a
// valid request.
func parseReq(data []byte, req *request) (leftover []byte, err error) {
sdata := string(data)
sdata := b2s(data)
var i, s int
var head string
var clen int
Expand Down
7 changes: 6 additions & 1 deletion examples/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"time"
"unsafe"

"github.com/panjf2000/gnet"
)
Expand Down Expand Up @@ -131,11 +132,15 @@ func appendResp(b []byte, status, head, body string) []byte {
return b
}

func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// parseReq is a very simple http request parser. This operation
// waits for the entire payload to be buffered before returning a
// valid request.
func parseReq(data []byte, req *request) (leftover []byte, err error) {
sdata := string(data)
sdata := b2s(data)
var i, s int
var head string
var clen int
Expand Down

0 comments on commit 7155bed

Please sign in to comment.