refactor: separate core components and improve session & server handling
This commit is contained in:
50
proto/proto.go
Normal file
50
proto/proto.go
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Package proto provides byte-level interaction with HTTP request payload.
|
||||
|
||||
Example of HTTP payload for future references, new line symbols escaped:
|
||||
|
||||
POST /upload HTTP/1.1\r\n
|
||||
User-Agent: Gor\r\n
|
||||
Content-Length: 11\r\n
|
||||
\r\n
|
||||
Hello world
|
||||
|
||||
GET /index.html HTTP/1.1\r\n
|
||||
User-Agent: Gor\r\n
|
||||
\r\n
|
||||
\r\n
|
||||
|
||||
https://github.com/buger/goreplay/blob/master/proto/proto.go
|
||||
*/
|
||||
package proto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var Methods = [...]string{
|
||||
http.MethodConnect, http.MethodDelete, http.MethodGet,
|
||||
http.MethodHead, http.MethodOptions, http.MethodPatch,
|
||||
http.MethodPost, http.MethodPut, http.MethodTrace,
|
||||
}
|
||||
|
||||
func Method(payload []byte) []byte {
|
||||
end := bytes.IndexByte(payload, ' ')
|
||||
if end == -1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return payload[:end]
|
||||
}
|
||||
|
||||
func IsHttpRequest(payload []byte) bool {
|
||||
method := string(Method(payload))
|
||||
var methodFound bool
|
||||
for _, m := range Methods {
|
||||
if methodFound = method == m; methodFound {
|
||||
break
|
||||
}
|
||||
}
|
||||
return methodFound
|
||||
}
|
||||
Reference in New Issue
Block a user