mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-25 17:45:28 +03:00
Use %w to wrap errors (#1175)
This commit is contained in:
+4
-4
@@ -42,7 +42,7 @@ func testBrotliBytesSingleCase(s string) error {
|
||||
|
||||
unbrotliedS, err := AppendUnbrotliBytes(prefix, brotlipedS[len(prefix):])
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
|
||||
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
|
||||
}
|
||||
if !bytes.Equal(unbrotliedS[:len(prefix)], prefix) {
|
||||
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, unbrotliedS[:len(prefix)], prefix)
|
||||
@@ -83,17 +83,17 @@ func testBrotliCompressSingleCase(s string) error {
|
||||
var buf bytes.Buffer
|
||||
zw := acquireStacklessBrotliWriter(&buf, CompressDefaultCompression)
|
||||
if _, err := zw.Write([]byte(s)); err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
releaseStacklessBrotliWriter(zw, CompressDefaultCompression)
|
||||
|
||||
zr, err := acquireBrotliReader(&buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
body, err := ioutil.ReadAll(zr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
if string(body) != s {
|
||||
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
|
||||
|
||||
+2
-2
@@ -98,7 +98,7 @@ func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error) {
|
||||
}
|
||||
v, err := ParseUint(b[:n])
|
||||
if err != nil {
|
||||
return dst, fmt.Errorf("cannot parse ipStr %q: %s", ipStr, err)
|
||||
return dst, fmt.Errorf("cannot parse ipStr %q: %w", ipStr, err)
|
||||
}
|
||||
if v > 255 {
|
||||
return dst, fmt.Errorf("cannot parse ipStr %q: ip part cannot exceed 255: parsed %d", ipStr, v)
|
||||
@@ -108,7 +108,7 @@ func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error) {
|
||||
}
|
||||
v, err := ParseUint(b)
|
||||
if err != nil {
|
||||
return dst, fmt.Errorf("cannot parse ipStr %q: %s", ipStr, err)
|
||||
return dst, fmt.Errorf("cannot parse ipStr %q: %w", ipStr, err)
|
||||
}
|
||||
if v > 255 {
|
||||
return dst, fmt.Errorf("cannot parse ipStr %q: ip part cannot exceed 255: parsed %d", ipStr, v)
|
||||
|
||||
+9
-5
@@ -626,6 +626,10 @@ func TestClientHeaderCase(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClientReadTimeout(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
@@ -814,14 +818,14 @@ func TestClientDoWithCustomHeaders(t *testing.T) {
|
||||
go func() {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
ch <- fmt.Errorf("cannot accept client connection: %s", err)
|
||||
ch <- fmt.Errorf("cannot accept client connection: %w", err)
|
||||
return
|
||||
}
|
||||
br := bufio.NewReader(conn)
|
||||
|
||||
var req Request
|
||||
if err = req.Read(br); err != nil {
|
||||
ch <- fmt.Errorf("cannot read client request: %s", err)
|
||||
ch <- fmt.Errorf("cannot read client request: %w", err)
|
||||
return
|
||||
}
|
||||
if string(req.Header.Method()) != MethodPost {
|
||||
@@ -854,11 +858,11 @@ func TestClientDoWithCustomHeaders(t *testing.T) {
|
||||
var resp Response
|
||||
bw := bufio.NewWriter(conn)
|
||||
if err = resp.Write(bw); err != nil {
|
||||
ch <- fmt.Errorf("cannot send response: %s", err)
|
||||
ch <- fmt.Errorf("cannot send response: %w", err)
|
||||
return
|
||||
}
|
||||
if err = bw.Flush(); err != nil {
|
||||
ch <- fmt.Errorf("cannot flush response: %s", err)
|
||||
ch <- fmt.Errorf("cannot flush response: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1221,7 +1225,7 @@ func TestHostClientPendingRequests(t *testing.T) {
|
||||
resp := AcquireResponse()
|
||||
|
||||
if err := c.DoTimeout(req, resp, 10*time.Second); err != nil {
|
||||
resultCh <- fmt.Errorf("unexpected error: %s", err)
|
||||
resultCh <- fmt.Errorf("unexpected error: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+9
-9
@@ -78,7 +78,7 @@ func testGzipBytesSingleCase(s string) error {
|
||||
|
||||
gunzippedS, err := AppendGunzipBytes(prefix, gzippedS[len(prefix):])
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
|
||||
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
|
||||
}
|
||||
if !bytes.Equal(gunzippedS[:len(prefix)], prefix) {
|
||||
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, gunzippedS[:len(prefix)], prefix)
|
||||
@@ -99,7 +99,7 @@ func testDeflateBytesSingleCase(s string) error {
|
||||
|
||||
inflatedS, err := AppendInflateBytes(prefix, deflatedS[len(prefix):])
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
|
||||
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
|
||||
}
|
||||
if !bytes.Equal(inflatedS[:len(prefix)], prefix) {
|
||||
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, inflatedS[:len(prefix)], prefix)
|
||||
@@ -165,17 +165,17 @@ func testGzipCompressSingleCase(s string) error {
|
||||
var buf bytes.Buffer
|
||||
zw := acquireStacklessGzipWriter(&buf, CompressDefaultCompression)
|
||||
if _, err := zw.Write([]byte(s)); err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
releaseStacklessGzipWriter(zw, CompressDefaultCompression)
|
||||
|
||||
zr, err := acquireGzipReader(&buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
body, err := ioutil.ReadAll(zr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
if string(body) != s {
|
||||
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
|
||||
@@ -188,17 +188,17 @@ func testFlateCompressSingleCase(s string) error {
|
||||
var buf bytes.Buffer
|
||||
zw := acquireStacklessDeflateWriter(&buf, CompressDefaultCompression)
|
||||
if _, err := zw.Write([]byte(s)); err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
releaseStacklessDeflateWriter(zw, CompressDefaultCompression)
|
||||
|
||||
zr, err := acquireFlateReader(&buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
body, err := ioutil.ReadAll(zr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
|
||||
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
|
||||
}
|
||||
if string(body) != s {
|
||||
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
|
||||
@@ -213,7 +213,7 @@ func testConcurrent(concurrency int, f func() error) error {
|
||||
go func(idx int) {
|
||||
err := f()
|
||||
if err != nil {
|
||||
ch <- fmt.Errorf("error in goroutine %d: %s", idx, err)
|
||||
ch <- fmt.Errorf("error in goroutine %d: %w", idx, err)
|
||||
}
|
||||
ch <- nil
|
||||
}(i)
|
||||
|
||||
@@ -58,7 +58,7 @@ func getExpvarRegexp(ctx *fasthttp.RequestCtx) (*regexp.Regexp, error) {
|
||||
}
|
||||
rr, err := regexp.Compile(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse r=%q: %s", r, err)
|
||||
return nil, fmt.Errorf("cannot parse r=%q: %w", r, err)
|
||||
}
|
||||
return rr, nil
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {
|
||||
|
||||
port, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unexpected addr format: %v", err)
|
||||
return nil, fmt.Errorf("unexpected addr format: %w", err)
|
||||
}
|
||||
|
||||
reqURL := &url.URL{Host: addr, Scheme: httpScheme}
|
||||
@@ -108,14 +108,14 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {
|
||||
|
||||
if err := res.Read(bufio.NewReader(conn)); err != nil {
|
||||
if connErr := conn.Close(); connErr != nil {
|
||||
return nil, fmt.Errorf("conn close err %v followed by read conn err %v", connErr, err)
|
||||
return nil, fmt.Errorf("conn close err %v precede by read conn err %w", connErr, err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if res.Header.StatusCode() != 200 {
|
||||
if connErr := conn.Close(); connErr != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"conn close err %v followed by connect to proxy: code: %d body %s",
|
||||
"conn close err %w precede by connect to proxy: code: %d body %s",
|
||||
connErr, res.StatusCode(), string(res.Body()))
|
||||
}
|
||||
return nil, fmt.Errorf("could not connect to proxy: code: %d body %s", res.StatusCode(), string(res.Body()))
|
||||
|
||||
@@ -164,7 +164,7 @@ func testPipeConnsCloseWhileReadWrite(t *testing.T) {
|
||||
var err error
|
||||
if _, err = io.Copy(ioutil.Discard, c1); err != nil {
|
||||
if err != errConnectionClosed {
|
||||
err = fmt.Errorf("unexpected error: %s", err)
|
||||
err = fmt.Errorf("unexpected error: %w", err)
|
||||
} else {
|
||||
err = nil
|
||||
}
|
||||
@@ -178,7 +178,7 @@ func testPipeConnsCloseWhileReadWrite(t *testing.T) {
|
||||
for {
|
||||
if _, err = c2.Write([]byte("foobar")); err != nil {
|
||||
if err != errConnectionClosed {
|
||||
err = fmt.Errorf("unexpected error: %s", err)
|
||||
err = fmt.Errorf("unexpected error: %w", err)
|
||||
} else {
|
||||
err = nil
|
||||
}
|
||||
|
||||
@@ -524,7 +524,7 @@ func (ff *fsFile) bigFileReader() (io.Reader, error) {
|
||||
|
||||
f, err := os.Open(ff.f.Name())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot open already opened file: %s", err)
|
||||
return nil, fmt.Errorf("cannot open already opened file: %w", err)
|
||||
}
|
||||
return &bigFileReader{
|
||||
f: f,
|
||||
@@ -981,7 +981,7 @@ func (h *fsHandler) openIndexFile(ctx *RequestCtx, dirPath string, mustCompress
|
||||
return ff, nil
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("cannot open file %q: %s", indexFilePath, err)
|
||||
return nil, fmt.Errorf("cannot open file %q: %w", indexFilePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1100,7 +1100,7 @@ func (h *fsHandler) compressAndOpenFSFile(filePath string, fileEncoding string)
|
||||
fileInfo, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("cannot obtain info for file %q: %s", filePath, err)
|
||||
return nil, fmt.Errorf("cannot obtain info for file %q: %w", filePath, err)
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() {
|
||||
@@ -1146,7 +1146,7 @@ func (h *fsHandler) compressFileNolock(f *os.File, fileInfo os.FileInfo, filePat
|
||||
if err != nil {
|
||||
f.Close()
|
||||
if !os.IsPermission(err) {
|
||||
return nil, fmt.Errorf("cannot create temporary file %q: %s", tmpFilePath, err)
|
||||
return nil, fmt.Errorf("cannot create temporary file %q: %w", tmpFilePath, err)
|
||||
}
|
||||
return nil, errNoCreatePermission
|
||||
}
|
||||
@@ -1168,14 +1168,14 @@ func (h *fsHandler) compressFileNolock(f *os.File, fileInfo os.FileInfo, filePat
|
||||
zf.Close()
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error when compressing file %q to %q: %s", filePath, tmpFilePath, err)
|
||||
return nil, fmt.Errorf("error when compressing file %q to %q: %w", filePath, tmpFilePath, err)
|
||||
}
|
||||
if err = os.Chtimes(tmpFilePath, time.Now(), fileInfo.ModTime()); err != nil {
|
||||
return nil, fmt.Errorf("cannot change modification time to %s for tmp file %q: %s",
|
||||
fileInfo.ModTime(), tmpFilePath, err)
|
||||
}
|
||||
if err = os.Rename(tmpFilePath, compressedFilePath); err != nil {
|
||||
return nil, fmt.Errorf("cannot move compressed file from %q to %q: %s", tmpFilePath, compressedFilePath, err)
|
||||
return nil, fmt.Errorf("cannot move compressed file from %q to %q: %w", tmpFilePath, compressedFilePath, err)
|
||||
}
|
||||
return h.newCompressedFSFile(compressedFilePath, fileEncoding)
|
||||
}
|
||||
@@ -1183,12 +1183,12 @@ func (h *fsHandler) compressFileNolock(f *os.File, fileInfo os.FileInfo, filePat
|
||||
func (h *fsHandler) newCompressedFSFile(filePath string, fileEncoding string) (*fsFile, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot open compressed file %q: %s", filePath, err)
|
||||
return nil, fmt.Errorf("cannot open compressed file %q: %w", filePath, err)
|
||||
}
|
||||
fileInfo, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("cannot obtain info for compressed file %q: %s", filePath, err)
|
||||
return nil, fmt.Errorf("cannot obtain info for compressed file %q: %w", filePath, err)
|
||||
}
|
||||
return h.newFSFile(f, fileInfo, true, fileEncoding)
|
||||
}
|
||||
@@ -1210,7 +1210,7 @@ func (h *fsHandler) openFSFile(filePath string, mustCompress bool, fileEncoding
|
||||
fileInfo, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("cannot obtain info for file %q: %s", filePath, err)
|
||||
return nil, fmt.Errorf("cannot obtain info for file %q: %w", filePath, err)
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() {
|
||||
@@ -1226,7 +1226,7 @@ func (h *fsHandler) openFSFile(filePath string, mustCompress bool, fileEncoding
|
||||
fileInfoOriginal, err := os.Stat(filePathOriginal)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("cannot obtain info for original file %q: %s", filePathOriginal, err)
|
||||
return nil, fmt.Errorf("cannot obtain info for original file %q: %w", filePathOriginal, err)
|
||||
}
|
||||
|
||||
// Only re-create the compressed file if there was more than a second between the mod times.
|
||||
@@ -1257,7 +1257,7 @@ func (h *fsHandler) newFSFile(f *os.File, fileInfo os.FileInfo, compressed bool,
|
||||
if len(contentType) == 0 {
|
||||
data, err := readFileHeader(f, compressed, fileEncoding)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read header of the file %q: %s", f.Name(), err)
|
||||
return nil, fmt.Errorf("cannot read header of the file %q: %w", f.Name(), err)
|
||||
}
|
||||
contentType = http.DetectContentType(data)
|
||||
}
|
||||
|
||||
@@ -1797,11 +1797,11 @@ func (h *ResponseHeader) tryRead(r *bufio.Reader, n int) error {
|
||||
}
|
||||
}
|
||||
return &ErrSmallBuffer{
|
||||
error: fmt.Errorf("error when reading response headers: %s", errSmallBuffer),
|
||||
error: fmt.Errorf("error when reading response headers: %w", errSmallBuffer),
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("error when reading response headers: %s", err)
|
||||
return fmt.Errorf("error when reading response headers: %w", err)
|
||||
}
|
||||
b = mustPeekBuffered(r)
|
||||
headersLen, errParse := h.parse(b)
|
||||
@@ -1849,11 +1849,11 @@ func (h *ResponseHeader) tryReadTrailer(r *bufio.Reader, n int) error {
|
||||
}
|
||||
}
|
||||
return &ErrSmallBuffer{
|
||||
error: fmt.Errorf("error when reading response trailer: %s", errSmallBuffer),
|
||||
error: fmt.Errorf("error when reading response trailer: %w", errSmallBuffer),
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("error when reading response trailer: %s", err)
|
||||
return fmt.Errorf("error when reading response trailer: %w", err)
|
||||
}
|
||||
b = mustPeekBuffered(r)
|
||||
headersLen, errParse := h.parseTrailer(b)
|
||||
@@ -1891,9 +1891,9 @@ func headerError(typ string, err, errParse error, b []byte, secureErrorLogMessag
|
||||
|
||||
func headerErrorMsg(typ string, err error, b []byte, secureErrorLogMessage bool) error {
|
||||
if secureErrorLogMessage {
|
||||
return fmt.Errorf("error when reading %s headers: %s. Buffer size=%d", typ, err, len(b))
|
||||
return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d", typ, err, len(b))
|
||||
}
|
||||
return fmt.Errorf("error when reading %s headers: %s. Buffer size=%d, contents: %s", typ, err, len(b), bufferSnippet(b))
|
||||
return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d, contents: %s", typ, err, len(b), bufferSnippet(b))
|
||||
}
|
||||
|
||||
// Read reads request header from r.
|
||||
@@ -1958,11 +1958,11 @@ func (h *RequestHeader) tryReadTrailer(r *bufio.Reader, n int) error {
|
||||
}
|
||||
}
|
||||
return &ErrSmallBuffer{
|
||||
error: fmt.Errorf("error when reading request trailer: %s", errSmallBuffer),
|
||||
error: fmt.Errorf("error when reading request trailer: %w", errSmallBuffer),
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("error when reading request trailer: %s", err)
|
||||
return fmt.Errorf("error when reading request trailer: %w", err)
|
||||
}
|
||||
b = mustPeekBuffered(r)
|
||||
headersLen, errParse := h.parseTrailer(b)
|
||||
@@ -1991,7 +1991,7 @@ func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
|
||||
// This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
|
||||
if err == bufio.ErrBufferFull {
|
||||
return &ErrSmallBuffer{
|
||||
error: fmt.Errorf("error when reading request headers: %s", errSmallBuffer),
|
||||
error: fmt.Errorf("error when reading request headers: %w", errSmallBuffer),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2001,7 +2001,7 @@ func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
|
||||
return ErrNothingRead{err}
|
||||
}
|
||||
|
||||
return fmt.Errorf("error when reading request headers: %s", err)
|
||||
return fmt.Errorf("error when reading request headers: %w", err)
|
||||
}
|
||||
b = mustPeekBuffered(r)
|
||||
headersLen, errParse := h.parse(b)
|
||||
@@ -2499,9 +2499,9 @@ func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) {
|
||||
h.statusCode, n, err = parseUintBuf(b)
|
||||
if err != nil {
|
||||
if h.secureErrorLogMessage {
|
||||
return 0, fmt.Errorf("cannot parse response status code: %s", err)
|
||||
return 0, fmt.Errorf("cannot parse response status code: %w", err)
|
||||
}
|
||||
return 0, fmt.Errorf("cannot parse response status code: %s. Response %q", err, buf)
|
||||
return 0, fmt.Errorf("cannot parse response status code: %w. Response %q", err, buf)
|
||||
}
|
||||
if len(b) > n && b[n] != ' ' {
|
||||
if h.secureErrorLogMessage {
|
||||
|
||||
@@ -844,7 +844,7 @@ func (req *Request) MultipartForm() (*multipart.Form, error) {
|
||||
if bytes.Equal(ce, strGzip) {
|
||||
// Do not care about memory usage here.
|
||||
if bodyStream, err = gzip.NewReader(bodyStream); err != nil {
|
||||
return nil, fmt.Errorf("cannot gunzip request body: %s", err)
|
||||
return nil, fmt.Errorf("cannot gunzip request body: %w", err)
|
||||
}
|
||||
} else if len(ce) > 0 {
|
||||
return nil, fmt.Errorf("unsupported Content-Encoding: %q", ce)
|
||||
@@ -853,14 +853,14 @@ func (req *Request) MultipartForm() (*multipart.Form, error) {
|
||||
mr := multipart.NewReader(bodyStream, req.multipartFormBoundary)
|
||||
req.multipartForm, err = mr.ReadForm(8 * 1024)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read multipart/form-data body: %s", err)
|
||||
return nil, fmt.Errorf("cannot read multipart/form-data body: %w", err)
|
||||
}
|
||||
} else {
|
||||
body := req.bodyBytes()
|
||||
if bytes.Equal(ce, strGzip) {
|
||||
// Do not care about memory usage here.
|
||||
if body, err = AppendGunzipBytes(nil, body); err != nil {
|
||||
return nil, fmt.Errorf("cannot gunzip request body: %s", err)
|
||||
return nil, fmt.Errorf("cannot gunzip request body: %w", err)
|
||||
}
|
||||
} else if len(ce) > 0 {
|
||||
return nil, fmt.Errorf("unsupported Content-Encoding: %q", ce)
|
||||
@@ -894,14 +894,14 @@ func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
|
||||
|
||||
mw := multipart.NewWriter(w)
|
||||
if err := mw.SetBoundary(boundary); err != nil {
|
||||
return fmt.Errorf("cannot use form boundary %q: %s", boundary, err)
|
||||
return fmt.Errorf("cannot use form boundary %q: %w", boundary, err)
|
||||
}
|
||||
|
||||
// marshal values
|
||||
for k, vv := range f.Value {
|
||||
for _, v := range vv {
|
||||
if err := mw.WriteField(k, v); err != nil {
|
||||
return fmt.Errorf("cannot write form field %q value %q: %s", k, v, err)
|
||||
return fmt.Errorf("cannot write form field %q value %q: %w", k, v, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -911,23 +911,23 @@ func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
|
||||
for _, fv := range fvv {
|
||||
vw, err := mw.CreatePart(fv.Header)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create form file %q (%q): %s", k, fv.Filename, err)
|
||||
return fmt.Errorf("cannot create form file %q (%q): %w", k, fv.Filename, err)
|
||||
}
|
||||
fh, err := fv.Open()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot open form file %q (%q): %s", k, fv.Filename, err)
|
||||
}
|
||||
if _, err = copyZeroAlloc(vw, fh); err != nil {
|
||||
return fmt.Errorf("error when copying form file %q (%q): %s", k, fv.Filename, err)
|
||||
return fmt.Errorf("error when copying form file %q (%q): %w", k, fv.Filename, err)
|
||||
}
|
||||
if err = fh.Close(); err != nil {
|
||||
return fmt.Errorf("cannot close form file %q (%q): %s", k, fv.Filename, err)
|
||||
return fmt.Errorf("cannot close form file %q (%q): %w", k, fv.Filename, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := mw.Close(); err != nil {
|
||||
return fmt.Errorf("error when closing multipart form writer: %s", err)
|
||||
return fmt.Errorf("error when closing multipart form writer: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -945,7 +945,7 @@ func readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize i
|
||||
mr := multipart.NewReader(lr, boundary)
|
||||
f, err := mr.ReadForm(int64(maxInMemoryFileSize))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read multipart/form-data body: %s", err)
|
||||
return nil, fmt.Errorf("cannot read multipart/form-data body: %w", err)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
@@ -1429,7 +1429,7 @@ func (req *Request) Write(w *bufio.Writer) error {
|
||||
if req.onlyMultipartForm() {
|
||||
body, err = marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error when marshaling multipart form: %s", err)
|
||||
return fmt.Errorf("error when marshaling multipart form: %w", err)
|
||||
}
|
||||
req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
|
||||
}
|
||||
@@ -2143,7 +2143,7 @@ func parseChunkSize(r *bufio.Reader) (int, error) {
|
||||
}
|
||||
if err := r.UnreadByte(); err != nil {
|
||||
return -1, ErrBrokenChunk{
|
||||
error: fmt.Errorf("cannot unread '\r' char at the end of chunk size: %s", err),
|
||||
error: fmt.Errorf("cannot unread '\r' char at the end of chunk size: %w", err),
|
||||
}
|
||||
}
|
||||
break
|
||||
@@ -2160,7 +2160,7 @@ func readCrLf(r *bufio.Reader) error {
|
||||
c, err := r.ReadByte()
|
||||
if err != nil {
|
||||
return ErrBrokenChunk{
|
||||
error: fmt.Errorf("cannot read %q char at the end of chunk size: %s", exp, err),
|
||||
error: fmt.Errorf("cannot read %q char at the end of chunk size: %w", exp, err),
|
||||
}
|
||||
}
|
||||
if c != exp {
|
||||
|
||||
+8
-3
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -20,9 +21,13 @@ import (
|
||||
func TestInvalidTrailers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
br := bufio.NewReader(bytes.NewReader([]byte{0x20, 0x30, 0x0a, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0xff, 0x0a, 0x0a, 0x30, 0x0d, 0x0a, 0x30}))
|
||||
err := (&Response{}).Read(br)
|
||||
if err == io.EOF {
|
||||
if err := (&Response{}).Read(bufio.NewReader(bytes.NewReader([]byte{0x20, 0x30, 0x0a, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0xff, 0x0a, 0x0a, 0x30, 0x0d, 0x0a, 0x30}))); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("%#v", err)
|
||||
}
|
||||
if err := (&Response{}).Read(bufio.NewReader(bytes.NewReader([]byte{0xff, 0x20, 0x0a, 0x54, 0x52, 0x61, 0x49, 0x4c, 0x65, 0x52, 0x3a, 0x2c, 0x0a, 0x0a}))); !errors.Is(err, errEmptyInt) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := (&Response{}).Read(bufio.NewReader(bytes.NewReader([]byte{0x54, 0x52, 0x61, 0x49, 0x4c, 0x65, 0x52, 0x3a, 0x2c, 0x0a, 0x0a}))); !strings.Contains(err.Error(), "cannot find whitespace in the first line of response") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -1561,14 +1561,14 @@ func (s *Server) ListenAndServe(addr string) error {
|
||||
// The server sets the given file mode for the UNIX addr.
|
||||
func (s *Server) ListenAndServeUNIX(addr string, mode os.FileMode) error {
|
||||
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("unexpected error when trying to remove unix socket file %q: %s", addr, err)
|
||||
return fmt.Errorf("unexpected error when trying to remove unix socket file %q: %w", addr, err)
|
||||
}
|
||||
ln, err := net.Listen("unix", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = os.Chmod(addr, mode); err != nil {
|
||||
return fmt.Errorf("cannot chmod %#o for %q: %s", mode, addr, err)
|
||||
return fmt.Errorf("cannot chmod %#o for %q: %w", mode, addr, err)
|
||||
}
|
||||
return s.Serve(ln)
|
||||
}
|
||||
@@ -1695,7 +1695,7 @@ func (s *Server) AppendCert(certFile, keyFile string) error {
|
||||
|
||||
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load TLS key pair from certFile=%q and keyFile=%q: %s", certFile, keyFile, err)
|
||||
return fmt.Errorf("cannot load TLS key pair from certFile=%q and keyFile=%q: %w", certFile, keyFile, err)
|
||||
}
|
||||
|
||||
s.configTLS()
|
||||
|
||||
@@ -73,7 +73,7 @@ func testWriter(newWriter NewWriterFunc, newReader func(io.Reader) io.Reader) er
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if err := testWriterReuse(w, dstW, newReader); err != nil {
|
||||
return fmt.Errorf("unexpected error when re-using writer on iteration %d: %s", i, err)
|
||||
return fmt.Errorf("unexpected error when re-using writer on iteration %d: %w", i, err)
|
||||
}
|
||||
dstW = &bytes.Buffer{}
|
||||
w.Reset(dstW)
|
||||
@@ -89,7 +89,7 @@ func testWriterReuse(w Writer, r io.Reader, newReader func(io.Reader) io.Reader)
|
||||
fmt.Fprintf(mw, "foobar %d\n", i)
|
||||
if i%13 == 0 {
|
||||
if err := w.Flush(); err != nil {
|
||||
return fmt.Errorf("error on flush: %s", err)
|
||||
return fmt.Errorf("error on flush: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func testWriterReuse(w Writer, r io.Reader, newReader func(io.Reader) io.Reader)
|
||||
zr := newReader(r)
|
||||
data, err := ioutil.ReadAll(zr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error: %s, data=%q", err, data)
|
||||
return fmt.Errorf("unexpected error: %w, data=%q", err, data)
|
||||
}
|
||||
|
||||
wantData := wantW.Bytes()
|
||||
@@ -120,7 +120,7 @@ func testConcurrent(testFunc func() error, concurrency int) error {
|
||||
select {
|
||||
case err := <-ch:
|
||||
if err != nil {
|
||||
return fmt.Errorf("unexpected error on goroutine %d: %s", i, err)
|
||||
return fmt.Errorf("unexpected error on goroutine %d: %w", i, err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
return fmt.Errorf("timeout on goroutine %d", i)
|
||||
|
||||
+2
-2
@@ -47,7 +47,7 @@ func TestStreamReaderClose(t *testing.T) {
|
||||
r := NewStreamReader(func(w *bufio.Writer) {
|
||||
fmt.Fprintf(w, "%s", firstLine)
|
||||
if err := w.Flush(); err != nil {
|
||||
ch <- fmt.Errorf("unexpected error on first flush: %s", err)
|
||||
ch <- fmt.Errorf("unexpected error on first flush: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ func TestStreamReaderClose(t *testing.T) {
|
||||
// read trailing data
|
||||
go func() {
|
||||
if _, err := ioutil.ReadAll(r); err != nil {
|
||||
ch <- fmt.Errorf("unexpected error when reading trailing data: %s", err)
|
||||
ch <- fmt.Errorf("unexpected error when reading trailing data: %w", err)
|
||||
return
|
||||
}
|
||||
ch <- nil
|
||||
|
||||
Reference in New Issue
Block a user