diff --git a/crypt.go b/crypt.go index 4f5a41c5..e7acdaaf 100644 --- a/crypt.go +++ b/crypt.go @@ -7,10 +7,15 @@ import ( "errors" ) +var ( + errInvalidToken = errors.New("Invalid token") + errInvalidURLEncoding = errors.New("Invalid token encoding") +) + func validatePath(token, path string) error { messageMAC, err := base64.RawURLEncoding.DecodeString(token) if err != nil { - return errors.New("Invalid token encoding") + return errInvalidURLEncoding } mac := hmac.New(sha256.New, conf.Key) @@ -19,7 +24,7 @@ func validatePath(token, path string) error { expectedMAC := mac.Sum(nil) if !hmac.Equal(messageMAC, expectedMAC) { - return errors.New("Invalid token") + return errInvalidToken } return nil diff --git a/download.go b/download.go index 6f683ab1..86c6fbf5 100644 --- a/download.go +++ b/download.go @@ -25,6 +25,10 @@ var ( downloadClient *http.Client imageTypeCtxKey = ctxKey("imageType") imageDataCtxKey = ctxKey("imageData") + + errSourceDimensionsTooBig = errors.New("Source image dimensions are too big") + errSourceResolutionTooBig = errors.New("Source image resolution are too big") + errSourceImageTypeNotSupported = errors.New("Source image type not supported") ) var downloadBufPool = sync.Pool{ @@ -95,13 +99,13 @@ func checkTypeAndDimensions(r io.Reader) (imageType, error) { return imageTypeUnknown, err } if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension { - return imageTypeUnknown, errors.New("Source image is too big") + return imageTypeUnknown, errSourceDimensionsTooBig } if imgconf.Width*imgconf.Height > conf.MaxSrcResolution { - return imageTypeUnknown, errors.New("Source image is too big") + return imageTypeUnknown, errSourceResolutionTooBig } if !imgtypeOk || !vipsTypeSupportLoad[imgtype] { - return imageTypeUnknown, errors.New("Source image type not supported") + return imageTypeUnknown, errSourceImageTypeNotSupported } return imgtype, nil diff --git a/errors.go b/errors.go index 62c605ba..0624622c 100644 --- a/errors.go +++ b/errors.go @@ -26,11 +26,6 @@ func newUnexpectedError(err error, skip int) imgproxyError { return imgproxyError{500, msg, "Internal error"} } -var ( - invalidSecretErr = newError(403, "Invalid secret", "Forbidden") - invalidMethodErr = newError(422, "Invalid request method", "Method doesn't allowed") -) - func stacktrace(skip int) string { callers := make([]uintptr, 10) n := runtime.Callers(skip+1, callers) diff --git a/etag.go b/etag.go index 02beed83..acb6aa0a 100644 --- a/etag.go +++ b/etag.go @@ -9,7 +9,7 @@ import ( "sync" ) -var notModifiedErr = newError(304, "Not modified", "Not modified") +var errNotModified = newError(304, "Not modified", "Not modified") type eTagCalc struct { hash hash.Hash diff --git a/process.go b/process.go index b04187b6..0a234c05 100644 --- a/process.go +++ b/process.go @@ -17,9 +17,13 @@ import ( "unsafe" ) -var vipsSupportSmartcrop bool -var vipsTypeSupportLoad = make(map[imageType]bool) -var vipsTypeSupportSave = make(map[imageType]bool) +var ( + vipsSupportSmartcrop bool + vipsTypeSupportLoad = make(map[imageType]bool) + vipsTypeSupportSave = make(map[imageType]bool) + + errSmartCropNotSupported = errors.New("Smart crop is not supported by used version of libvips") +) type cConfig struct { Quality C.int @@ -212,7 +216,7 @@ func processImage(ctx context.Context) ([]byte, error) { imgtype := getImageType(ctx) if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop { - return nil, errors.New("Smart crop is not supported by used version of libvips") + return nil, errSmartCropNotSupported } img, err := vipsLoadImage(data, imgtype, 1) diff --git a/server.go b/server.go index 2cd719d4..9fc99200 100644 --- a/server.go +++ b/server.go @@ -25,6 +25,9 @@ var ( healthRequestURI = []byte("/health") serverMutex mutex + + errInvalidMethod = newError(422, "Invalid request method", "Method doesn't allowed") + errInvalidSecret = newError(403, "Invalid secret", "Forbidden") ) func startServer() *fasthttp.Server { @@ -146,11 +149,11 @@ func serveHTTP(rctx *fasthttp.RequestCtx) { } if !rctx.IsGet() { - panic(invalidMethodErr) + panic(errInvalidMethod) } if !checkSecret(rctx) { - panic(invalidSecretErr) + panic(errInvalidSecret) } serverMutex.Lock() @@ -183,7 +186,7 @@ func serveHTTP(rctx *fasthttp.RequestCtx) { rctx.Response.Header.SetBytesV("ETag", eTag) if bytes.Equal(eTag, rctx.Request.Header.Peek("If-None-Match")) { - panic(notModifiedErr) + panic(errNotModified) } }