2017-06-27 11:00:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2017-07-01 23:25:08 +02:00
|
|
|
"crypto/subtle"
|
2017-06-27 11:00:33 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-07-03 06:08:47 +02:00
|
|
|
"time"
|
2017-06-27 11:00:33 +02:00
|
|
|
)
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
var mimes = map[imageType]string{
|
|
|
|
JPEG: "image/jpeg",
|
|
|
|
PNG: "image/png",
|
|
|
|
WEBP: "image/webp",
|
2017-07-05 23:09:41 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 16:05:53 +02:00
|
|
|
type httpHandler struct {
|
|
|
|
sem chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:57:41 +02:00
|
|
|
func newHTTPHandler() *httpHandler {
|
|
|
|
return &httpHandler{make(chan struct{}, conf.Concurrency)}
|
2017-07-04 16:05:53 +02:00
|
|
|
}
|
2017-06-27 11:00:33 +02:00
|
|
|
|
|
|
|
func parsePath(r *http.Request) (string, processingOptions, error) {
|
|
|
|
var po processingOptions
|
|
|
|
var err error
|
|
|
|
|
|
|
|
path := r.URL.Path
|
|
|
|
parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
|
|
|
|
|
|
|
if len(parts) < 7 {
|
|
|
|
return "", po, errors.New("Invalid path")
|
|
|
|
}
|
|
|
|
|
|
|
|
token := parts[0]
|
|
|
|
|
|
|
|
if err = validatePath(token, strings.TrimPrefix(path, fmt.Sprintf("/%s", token))); err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if r, ok := resizeTypes[parts[1]]; ok {
|
2018-02-26 10:45:52 +02:00
|
|
|
po.Resize = r
|
2017-09-27 10:42:49 +02:00
|
|
|
} else {
|
|
|
|
return "", po, fmt.Errorf("Invalid resize type: %s", parts[1])
|
|
|
|
}
|
2017-06-27 11:00:33 +02:00
|
|
|
|
2018-02-26 10:45:52 +02:00
|
|
|
if po.Width, err = strconv.Atoi(parts[2]); err != nil {
|
2017-06-27 11:00:33 +02:00
|
|
|
return "", po, fmt.Errorf("Invalid width: %s", parts[2])
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:45:52 +02:00
|
|
|
if po.Height, err = strconv.Atoi(parts[3]); err != nil {
|
2017-06-27 11:00:33 +02:00
|
|
|
return "", po, fmt.Errorf("Invalid height: %s", parts[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
if g, ok := gravityTypes[parts[4]]; ok {
|
2018-02-26 10:45:52 +02:00
|
|
|
po.Gravity = g
|
2017-06-27 11:00:33 +02:00
|
|
|
} else {
|
|
|
|
return "", po, fmt.Errorf("Invalid gravity: %s", parts[4])
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:45:52 +02:00
|
|
|
po.Enlarge = parts[5] != "0"
|
2017-06-27 11:00:33 +02:00
|
|
|
|
|
|
|
filenameParts := strings.Split(strings.Join(parts[6:], ""), ".")
|
|
|
|
|
|
|
|
if len(filenameParts) < 2 {
|
2018-02-26 10:45:52 +02:00
|
|
|
po.Format = imageTypes["jpg"]
|
2017-06-27 11:00:33 +02:00
|
|
|
} else if f, ok := imageTypes[filenameParts[1]]; ok {
|
2018-02-26 10:45:52 +02:00
|
|
|
po.Format = f
|
2017-06-27 11:00:33 +02:00
|
|
|
} else {
|
|
|
|
return "", po, fmt.Errorf("Invalid image format: %s", filenameParts[1])
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:45:52 +02:00
|
|
|
if !vipsTypeSupportSave[po.Format] {
|
2017-09-27 10:42:49 +02:00
|
|
|
return "", po, errors.New("Resulting image type not supported")
|
|
|
|
}
|
|
|
|
|
2017-06-27 11:00:33 +02:00
|
|
|
filename, err := base64.RawURLEncoding.DecodeString(filenameParts[0])
|
|
|
|
if err != nil {
|
|
|
|
return "", po, errors.New("Invalid filename encoding")
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(filename), po, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func logResponse(status int, msg string) {
|
|
|
|
var color int
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
if status >= 500 {
|
2017-06-27 11:00:33 +02:00
|
|
|
color = 31
|
2017-10-04 21:44:58 +02:00
|
|
|
} else if status >= 400 {
|
2017-06-27 11:00:33 +02:00
|
|
|
color = 33
|
|
|
|
} else {
|
|
|
|
color = 32
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("|\033[7;%dm %d \033[0m| %s\n", color, status, msg)
|
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
func respondWithImage(r *http.Request, rw http.ResponseWriter, data []byte, imgURL string, po processingOptions, duration time.Duration) {
|
2017-06-27 11:00:33 +02:00
|
|
|
gzipped := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && conf.GZipCompression > 0
|
|
|
|
|
2017-07-03 12:10:44 +02:00
|
|
|
rw.Header().Set("Expires", time.Now().Add(time.Second*time.Duration(conf.TTL)).Format(http.TimeFormat))
|
2017-10-30 11:53:51 +02:00
|
|
|
rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", conf.TTL))
|
2018-02-26 10:45:52 +02:00
|
|
|
rw.Header().Set("Content-Type", mimes[po.Format])
|
|
|
|
|
2017-06-27 11:00:33 +02:00
|
|
|
if gzipped {
|
|
|
|
rw.Header().Set("Content-Encoding", "gzip")
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(200)
|
|
|
|
|
|
|
|
if gzipped {
|
|
|
|
gz, _ := gzip.NewWriterLevel(rw, conf.GZipCompression)
|
|
|
|
gz.Write(data)
|
|
|
|
gz.Close()
|
|
|
|
} else {
|
|
|
|
rw.Write(data)
|
|
|
|
}
|
2017-07-03 06:08:47 +02:00
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
logResponse(200, fmt.Sprintf("Processed in %s: %s; %+v", duration, imgURL, po))
|
2017-06-27 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
func respondWithError(rw http.ResponseWriter, err imgproxyError) {
|
|
|
|
logResponse(err.StatusCode, err.Message)
|
2017-06-27 11:00:33 +02:00
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
rw.WriteHeader(err.StatusCode)
|
|
|
|
rw.Write([]byte(err.PublicMessage))
|
2017-07-01 23:25:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkSecret(s string) bool {
|
2017-07-03 11:36:37 +02:00
|
|
|
if len(conf.Secret) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.HasPrefix(s, "Bearer ") && subtle.ConstantTimeCompare([]byte(strings.TrimPrefix(s, "Bearer ")), []byte(conf.Secret)) == 1
|
2017-07-01 23:25:08 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:53:39 +02:00
|
|
|
func (h *httpHandler) lock() {
|
|
|
|
t := startTimer(time.Duration(conf.WaitTimeout) * time.Second)
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
select {
|
|
|
|
case h.sem <- struct{}{}:
|
|
|
|
// Go ahead
|
|
|
|
case <-t.Timer:
|
|
|
|
panic(t.TimeoutErr())
|
|
|
|
}
|
2017-07-04 16:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) unlock() {
|
2017-10-04 21:44:58 +02:00
|
|
|
<-h.sem
|
2017-07-04 16:05:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:57:41 +02:00
|
|
|
func (h *httpHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2017-06-27 11:00:33 +02:00
|
|
|
log.Printf("GET: %s\n", r.URL.RequestURI())
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if err, ok := r.(imgproxyError); ok {
|
|
|
|
respondWithError(rw, err)
|
|
|
|
} else {
|
|
|
|
respondWithError(rw, newUnexpectedError(r.(error), 4))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-07-04 16:05:53 +02:00
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
t := startTimer(time.Duration(conf.WriteTimeout) * time.Second)
|
|
|
|
|
2018-03-15 16:53:39 +02:00
|
|
|
h.lock()
|
2017-10-04 21:44:58 +02:00
|
|
|
defer h.unlock()
|
2017-07-03 06:08:47 +02:00
|
|
|
|
2017-07-03 11:36:37 +02:00
|
|
|
if !checkSecret(r.Header.Get("Authorization")) {
|
2017-10-04 21:44:58 +02:00
|
|
|
panic(invalidSecretErr)
|
2017-07-01 23:25:08 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 11:57:19 +02:00
|
|
|
if r.URL.Path == "/health" {
|
2018-02-26 10:45:52 +02:00
|
|
|
rw.WriteHeader(200)
|
|
|
|
rw.Write([]byte("imgproxy is running"))
|
2017-10-25 11:57:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-27 11:00:33 +02:00
|
|
|
imgURL, procOpt, err := parsePath(r)
|
|
|
|
if err != nil {
|
2017-10-04 21:44:58 +02:00
|
|
|
panic(newError(404, err.Error(), "Invalid image url"))
|
2017-06-27 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = url.ParseRequestURI(imgURL); err != nil {
|
2017-10-04 21:44:58 +02:00
|
|
|
panic(newError(404, err.Error(), "Invalid image url"))
|
2017-06-27 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
b, imgtype, err := downloadImage(imgURL)
|
2017-06-27 11:00:33 +02:00
|
|
|
if err != nil {
|
2017-10-04 21:44:58 +02:00
|
|
|
panic(newError(404, err.Error(), "Image is unreachable"))
|
2017-06-27 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
t.Check()
|
|
|
|
|
2018-02-26 11:41:37 +02:00
|
|
|
if conf.ETagEnabled {
|
|
|
|
eTag := calcETag(b, &procOpt)
|
|
|
|
rw.Header().Set("ETag", eTag)
|
|
|
|
|
|
|
|
if eTag == r.Header.Get("If-None-Match") {
|
|
|
|
panic(notModifiedErr)
|
|
|
|
}
|
2018-02-26 10:45:52 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 11:41:37 +02:00
|
|
|
t.Check()
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
b, err = processImage(b, imgtype, procOpt, t)
|
2017-06-27 11:00:33 +02:00
|
|
|
if err != nil {
|
2017-10-04 21:44:58 +02:00
|
|
|
panic(newError(500, err.Error(), "Error occurred while processing image"))
|
2017-06-27 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
t.Check()
|
|
|
|
|
|
|
|
respondWithImage(r, rw, b, imgURL, procOpt, t.Since())
|
2017-06-27 11:00:33 +02:00
|
|
|
}
|