diff --git a/http_client.go b/http_client.go index 6adbb76..59d78ac 100644 --- a/http_client.go +++ b/http_client.go @@ -123,9 +123,7 @@ func (c *HTTPClient) isAlive() bool { if err == nil { return true } else if err == io.EOF { - if c.config.Debug { - Debug("[HTTPClient] connection closed, reconnecting") - } + Debug("[HTTPClient] connection closed, reconnecting") return false } else if err == syscall.EPIPE { Debug("Detected broken pipe.", err) @@ -198,27 +196,22 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { readBytes += n chunks++ - if err != nil { - if err == io.EOF { - err = nil - } - break - } - // First chunk if chunked || contentLength != -1 { currentContentLength += n } else { // If headers are finished - if bytes.Contains(c.respBuf[:readBytes], proto.EmptyLine) { - if bytes.Equal(proto.Header(c.respBuf, []byte("Transfer-Encoding")), []byte("chunked")) { + + if bytes.Contains(c.respBuf[:readBytes], proto.EmptyLine) { + if bytes.Equal(proto.Header(c.respBuf[:readBytes], []byte("Transfer-Encoding")), []byte("chunked")) { chunked = true } else { - status, _ := strconv.Atoi(string(proto.Status(c.respBuf))) + status, _ := strconv.Atoi(string(proto.Status(c.respBuf[:readBytes]))) if (status >= 100 && status < 200) || status == 204 || status == 304 { - contentLength = 0 + contentLength = 0 + break } else { - l := proto.Header(c.respBuf, []byte("Content-Length")) + l := proto.Header(c.respBuf[:readBytes], []byte("Content-Length")) if len(l) > 0 { contentLength, _ = strconv.Atoi(string(l)) } @@ -243,6 +236,13 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { break } } + + if err != nil { + if err == io.EOF { + err = nil + } + break + } } else { if currentChunk == nil { currentChunk = make([]byte, readChunkSize) @@ -250,13 +250,6 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { n, err = c.conn.Read(currentChunk) - if err == io.EOF { - break - } else if err != nil { - Debug("[HTTPClient] Read the whole body error:", err, c.baseURL) - break - } - readBytes += int(n) chunks++ currentContentLength += n @@ -279,6 +272,15 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { c.Disconnect() break } + + if err == io.EOF { + break + } else if err != nil { + Debug("[HTTPClient] Read the whole body error:", err, c.baseURL) + break + } + + } if readBytes >= maxResponseSize { @@ -292,14 +294,14 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { } if err != nil || readBytes == 0 { - Debug("[HTTPClient] Response read error", err, c.conn, readBytes) + Debug("[HTTPClient] Response read timeout error", err, c.conn, readBytes, string(c.respBuf[:readBytes])) response = errorPayload(HTTP_TIMEOUT) c.Disconnect() return } if readBytes < 4 || string(c.respBuf[:4]) != "HTTP" { - Debug("[HTTPClient] Response read error", err, c.conn, readBytes) + Debug("[HTTPClient] Response read unknown error", err, c.conn, readBytes, string(c.respBuf[:readBytes])) response = errorPayload(HTTP_UNKNOWN_ERROR) c.Disconnect() return @@ -334,8 +336,8 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { } if bytes.Equal(proto.Status(payload), []byte("400")) { - c.Disconnect() Debug("[HTTPClient] Closed connection on 400 response") + c.Disconnect() } c.redirectsCount = 0