From a62b22af69e06cd4a3cfe83d8b1031b867aecf4e Mon Sep 17 00:00:00 2001
From: Huy Le Viet <huylvt.vn@gmail.com>
Date: Fri, 20 Nov 2020 11:52:16 +0700
Subject: [PATCH] Add IMGPROXY_ENABLE_DEBUG_HEADERS config for debugging
 infomation (#509)

---
 config.go             | 3 +++
 docs/configuration.md | 1 +
 processing_handler.go | 5 +++++
 3 files changed, 9 insertions(+)

diff --git a/config.go b/config.go
index 4214cb6f..f95b402d 100644
--- a/config.go
+++ b/config.go
@@ -267,6 +267,8 @@ type config struct {
 
 	ReportDownloadingErrors bool
 
+	EnableDebugHeaders bool
+
 	FreeMemoryInterval             int
 	DownloadBufferSize             int
 	GZipBufferSize                 int
@@ -430,6 +432,7 @@ func configure() error {
 	strEnvConfig(&conf.SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT")
 	strEnvConfig(&conf.SentryRelease, "IMGPROXY_SENTRY_RELEASE")
 	boolEnvConfig(&conf.ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS")
+	boolEnvConfig(&conf.EnableDebugHeaders, "IMGPROXY_ENABLE_DEBUG_HEADERS")
 
 	intEnvConfig(&conf.FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL")
 	intEnvConfig(&conf.DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE")
diff --git a/docs/configuration.md b/docs/configuration.md
index 5e056bfc..516de476 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -43,6 +43,7 @@ echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
 * `IMGPROXY_CUSTOM_REQUEST_HEADERS`: <img class='pro-badge' src='assets/pro.svg' alt='pro' /> list of custom headers that imgproxy will send while requesting the source image, divided by `\;` (can be redefined by `IMGPROXY_CUSTOM_HEADERS_SEPARATOR`). Example: `X-MyHeader1=Lorem\;X-MyHeader2=Ipsum`;
 * `IMGPROXY_CUSTOM_RESPONSE_HEADERS`: <img class='pro-badge' src='assets/pro.svg' alt='pro' /> list of custom response headers, divided by `\;` (can be redefined by `IMGPROXY_CUSTOM_HEADERS_SEPARATOR`). Example: `X-MyHeader1=Lorem\;X-MyHeader2=Ipsum`;
 * `IMGPROXY_CUSTOM_HEADERS_SEPARATOR`: <img class='pro-badge' src='assets/pro.svg' alt='pro' /> string that will be used as a custom headers separator. Default: `\;`;
+* `IMGPROXY_ENABLE_DEBUG_HEADERS`: when `true`, imgproxy will add `X-Origin-Content-Length` header with the value is size of the source image. Default: `false`.
 
 ## Security
 
diff --git a/processing_handler.go b/processing_handler.go
index f85c89dc..e118e135 100644
--- a/processing_handler.go
+++ b/processing_handler.go
@@ -111,6 +111,11 @@ func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw htt
 		rw.Write(data)
 	}
 
+	if conf.EnableDebugHeaders {
+		imgdata := getImageData(ctx)
+		rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(imgdata.Data)))
+	}
+
 	imageURL := getImageURL(ctx)
 
 	logResponse(reqID, r, 200, nil, &imageURL, po)