Browse Source

read full http proxy request, fix UnexpectedEOF error

ffdfgdfg 5 năm trước cách đây
mục cha
commit
e2d249a576
1 tập tin đã thay đổi với 20 bổ sung1 xóa
  1. 20 1
      lib/conn/conn.go

+ 20 - 1
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]