|
|
|
@ -2,7 +2,6 @@ package core
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
|
"time"
|
|
|
|
@ -50,7 +49,7 @@ func wrapConn(c net.Conn) *conn {
|
|
|
|
|
wd: &connWriteDeadline,
|
|
|
|
|
}
|
|
|
|
|
return &conn{
|
|
|
|
|
b: connRequestBufferPool.Get(),
|
|
|
|
|
b: connRequestBufferPool.Get()[:0], // reset the buffer
|
|
|
|
|
br: connBufferedReaderPool.Get(deadlineConn),
|
|
|
|
|
bw: connBufferedWriterPool.Get(deadlineConn),
|
|
|
|
|
c: c,
|
|
|
|
@ -64,24 +63,28 @@ func (c *conn) Conn() net.Conn {
|
|
|
|
|
|
|
|
|
|
// ReadLine reads a single line and returns the result, or nil and error
|
|
|
|
|
func (c *conn) ReadLine() ([]byte, errors.Error) {
|
|
|
|
|
// Perform single read into buffer slice
|
|
|
|
|
count, err := c.br.Read(c.b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, ErrConnRead.Wrap(err)
|
|
|
|
|
for {
|
|
|
|
|
// Attempt to read next line
|
|
|
|
|
b, isPrefix, err := c.br.ReadLine()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, ErrConnRead.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we hit the max request size (i.e. capacity
|
|
|
|
|
// of the request buffer), return error
|
|
|
|
|
if len(c.b)+len(b) > cap(c.b) {
|
|
|
|
|
return nil, ErrInvalidRequest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add current bytes to request buffer
|
|
|
|
|
c.b = append(c.b, b...)
|
|
|
|
|
|
|
|
|
|
// If we hit the end, break out
|
|
|
|
|
if !isPrefix {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get up to first '\r\n' (or just '\n')
|
|
|
|
|
end := bytes.IndexByte(c.b[:count], '\n')
|
|
|
|
|
if end > 0 && c.b[end-1] == '\r' {
|
|
|
|
|
end--
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If end never reached return error,
|
|
|
|
|
// else return slice up to the end
|
|
|
|
|
if end == -1 {
|
|
|
|
|
return nil, ErrInvalidRequest
|
|
|
|
|
}
|
|
|
|
|
return c.b[:end], nil
|
|
|
|
|
return c.b, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteBytes writes a byte slice to the buffer and returns error status
|
|
|
|
@ -113,7 +116,6 @@ func (c *conn) Close() errors.Error {
|
|
|
|
|
err := c.c.Close()
|
|
|
|
|
|
|
|
|
|
// Put buffers back
|
|
|
|
|
connBufferedReaderPool.Put(c.br)
|
|
|
|
|
connBufferedWriterPool.Put(c.bw)
|
|
|
|
|
connRequestBufferPool.Put(c.b)
|
|
|
|
|
|
|
|
|
|