From e2d249a57666d3282915e66eec1e50d5d0c09ebf Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Tue, 11 Feb 2020 00:38:24 +0800 Subject: [PATCH] read full http proxy request, fix UnexpectedEOF error --- lib/conn/conn.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/conn/conn.go b/lib/conn/conn.go index 4e511d1..2bd5ea2 100755 --- a/lib/conn/conn.go +++ b/lib/conn/conn.go @@ -35,11 +35,30 @@ func NewConn(conn net.Conn) *Conn { return &Conn{Conn: conn} } +func (s *Conn) readRequest(buf []byte) (n int, err error) { + var rd int + for { + rd, err = s.Read(buf[n:]) + if err != nil { + return + } + n += rd + if string(buf[n-4:n]) == "\r\n\r\n" { + return + } + // buf is full, can't contain the request + if n == cap(buf) { + err = io.ErrUnexpectedEOF + return + } + } +} + //get host 、connection type、method...from connection func (s *Conn) GetHost() (method, address string, rb []byte, err error, r *http.Request) { var b [32 * 1024]byte var n int - if n, err = s.Read(b[:]); err != nil { + if n, err = s.readRequest(b[:]); err != nil { return } rb = b[:n]