fix unexpectedly formated http request

This commit is contained in:
xvzc 2022-03-06 17:44:11 +09:00
parent 5b5a081681
commit 7563f2b895
3 changed files with 22 additions and 14 deletions

View File

@ -58,7 +58,7 @@ func (conn *Conn) ReadBytes() ([]byte, error) {
ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE)
conn.conn.SetReadDeadline(time.Now().Add(3 * time.Second))
conn.conn.SetReadDeadline(time.Now().Add(5 * time.Second))
for {
n, err := conn.Read(buf)

View File

@ -53,8 +53,11 @@ func ParseUrl(raw []byte) {
}
func NewHttpPacket(raw []byte) HttpPacket {
method, domain, port, path, version := parse(raw)
func NewHttpPacket(raw []byte) (HttpPacket, error){
method, domain, port, path, version, err := parse(raw)
if err != nil {
return HttpPacket{}, err
}
return HttpPacket{
raw: raw,
@ -63,7 +66,7 @@ func NewHttpPacket(raw []byte) HttpPacket {
port: port,
path: path,
version: version,
}
}, nil
}
func (p *HttpPacket) Raw() []byte {
@ -125,10 +128,10 @@ func (p *HttpPacket) Tidy() {
p.raw = []byte(result)
}
func parse(raw []byte) (string, string, string, string, string) {
func parse(raw []byte) (string, string, string, string, string, error) {
var firstLine string
for i := 0; i < len(raw); i++ {
if (raw)[i] == '\n' {
if (raw)[i] == '\r' {
firstLine = string((raw)[:i])
break
}
@ -136,15 +139,15 @@ func parse(raw []byte) (string, string, string, string, string) {
tokens := strings.Split(firstLine, " ")
method := strings.TrimSpace(tokens[0])
url := strings.TrimSpace(tokens[1])
version := strings.TrimSpace(tokens[2])
method := tokens[0]
url := tokens[1]
version := tokens[2]
url = strings.Replace(url, "http://", "", 1)
url = strings.Replace(url, "https://", "", 1)
var domain string
var port string
domain := ""
port := ""
for i := 0; i < len(url); i++ {
if url[i] == ':' {
domain = url[:i]
@ -166,5 +169,5 @@ func parse(raw []byte) (string, string, string, string, string) {
}
}
return method, domain, port, path, version
return method, domain, port, path, version, nil
}

View File

@ -47,10 +47,15 @@ func (p *Proxy) Start() {
}
// log.Debug("[PROXY] Client sent a request")
pkt := packet.NewHttpPacket(b)
pkt, err := packet.NewHttpPacket(b)
if err != nil {
log.Debug("Error while parsing request")
log.Println(b)
return
}
if !pkt.IsValidMethod() {
log.Println("Unsupported method: ", pkt.Method())
log.Debug("Unsupported method: ", pkt.Method())
return
}