From 9a44e26da7c2c9026e22484460d1dab0893d343e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 11 Nov 2022 18:50:34 +0100 Subject: [PATCH] set header as own func --- server/upstream/upstream.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 5704583..3061a1d 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -172,21 +172,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin } // Set ETag & MIME - if eTag := header.Get(gitea.ETagHeader); eTag != "" { - ctx.RespWriter.Header().Set(gitea.ETagHeader, eTag) - } - if cacheIndicator := header.Get(gitea.PagesCacheIndicatorHeader); cacheIndicator != "" { - ctx.RespWriter.Header().Set(gitea.PagesCacheIndicatorHeader, cacheIndicator) - } - if length := header.Get(gitea.ContentLengthHeader); length != "" { - ctx.RespWriter.Header().Set(gitea.ContentLengthHeader, length) - } - if mime := header.Get(gitea.ContentTypeHeader); mime == "" || o.ServeRaw { - ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, rawMime) - } else { - ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, mime) - } - ctx.RespWriter.Header().Set(headerLastModified, o.BranchTimestamp.In(time.UTC).Format(time.RFC1123)) + setHeader(ctx, header, o.ServeRaw, o.BranchTimestamp) log.Debug().Msg("Prepare response") @@ -206,3 +192,22 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin return true } + +// setHeader set values to response header +func setHeader(ctx *context.Context, header http.Header, serveRaw bool, timestap time.Time) { + if eTag := header.Get(gitea.ETagHeader); eTag != "" { + ctx.RespWriter.Header().Set(gitea.ETagHeader, eTag) + } + if cacheIndicator := header.Get(gitea.PagesCacheIndicatorHeader); cacheIndicator != "" { + ctx.RespWriter.Header().Set(gitea.PagesCacheIndicatorHeader, cacheIndicator) + } + if length := header.Get(gitea.ContentLengthHeader); length != "" { + ctx.RespWriter.Header().Set(gitea.ContentLengthHeader, length) + } + if mime := header.Get(gitea.ContentTypeHeader); mime == "" || serveRaw { + ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, rawMime) + } else { + ctx.RespWriter.Header().Set(gitea.ContentTypeHeader, mime) + } + ctx.RespWriter.Header().Set(headerLastModified, timestap.In(time.UTC).Format(time.RFC1123)) +}