From 0752508469d81786bf8c5a7b0adb42125906f613 Mon Sep 17 00:00:00 2001 From: fatedier Date: Sat, 1 Jul 2017 12:13:44 +0800 Subject: [PATCH] vhost: a bug fix of reading request --- utils/vhost/http.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/utils/vhost/http.go b/utils/vhost/http.go index 5ecce03c..5ecee78c 100644 --- a/utils/vhost/http.go +++ b/utils/vhost/http.go @@ -76,8 +76,12 @@ func hostNameRewrite(request io.Reader, rewriteHost string, remoteIP string) (_ buf := pool.GetBuf(1024) defer pool.PutBuf(buf) - request.Read(buf) - retBuffer, err := parseRequest(buf, rewriteHost, remoteIP) + var n int + n, err = request.Read(buf) + if err != nil { + return + } + retBuffer, err := parseRequest(buf[:n], rewriteHost, remoteIP) return retBuffer, err } @@ -113,8 +117,8 @@ func parseRequest(org []byte, rewriteHost string, remoteIP string) (ret []byte, } buf := new(bytes.Buffer) buf.Write(b) - buf.WriteString(fmt.Sprintf("X-Forwarded-For: %s\n", remoteIP)) - buf.WriteString(fmt.Sprintf("X-Real-IP: %s\n", remoteIP)) + buf.WriteString(fmt.Sprintf("X-Forwarded-For: %s\r\n", remoteIP)) + buf.WriteString(fmt.Sprintf("X-Real-IP: %s\r\n", remoteIP)) if len(changedBuf) == 0 { tp.WriteTo(buf) } else { @@ -138,8 +142,8 @@ func parseRequest(org []byte, rewriteHost string, remoteIP string) (ret []byte, firstLine := req.Method + " " + req.URL.String() + " " + req.Proto buf := new(bytes.Buffer) buf.WriteString(firstLine) - buf.WriteString(fmt.Sprintf("X-Forwarded-For: %s\n", remoteIP)) - buf.WriteString(fmt.Sprintf("X-Real-IP: %s\n", remoteIP)) + buf.WriteString(fmt.Sprintf("X-Forwarded-For: %s\r\n", remoteIP)) + buf.WriteString(fmt.Sprintf("X-Real-IP: %s\r\n", remoteIP)) tp.WriteTo(buf) return buf.Bytes(), err } @@ -175,9 +179,9 @@ func changeHostName(buff *bytes.Buffer, rewriteHost string) (_ []byte, err error var hostHeader string portPos := bytes.IndexByte(kv[j+1:], ':') if portPos == -1 { - hostHeader = fmt.Sprintf("Host: %s\n", rewriteHost) + hostHeader = fmt.Sprintf("Host: %s\r\n", rewriteHost) } else { - hostHeader = fmt.Sprintf("Host: %s:%s\n", rewriteHost, kv[j+portPos+2:]) + hostHeader = fmt.Sprintf("Host: %s:%s\r\n", rewriteHost, kv[j+portPos+2:]) } retBuf.WriteString(hostHeader) peek = peek[i+1:]