From 229dfcba832e3aebf1a41a49f969e233d12c1d6d Mon Sep 17 00:00:00 2001 From: Adrien YHUEL <14916484+adrienyhuel@users.noreply.github.com> Date: Thu, 21 Nov 2024 16:18:53 +0100 Subject: [PATCH] feat: add pass_thru mode to continue to next handler instead of replying directly (#23) Thank you @adrienyhuel for contributing. Merged now --- README.adoc | 3 +++ caddyfile.go | 10 ++++++++++ upload.go | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/README.adoc b/README.adoc index 85eabda..492697d 100644 --- a/README.adoc +++ b/README.adoc @@ -102,6 +102,9 @@ The default size is **1G**. |**dest_dir_field_name** |If set, the dest_dir will be set from the specified form value. +|**pass_thru** +|If set to `true`, enables pass-thru mode, which continues to the next HTTP handler in the route instead of responding directly. + |=== === Caddy Variables diff --git a/caddyfile.go b/caddyfile.go index 65539c9..71de883 100644 --- a/caddyfile.go +++ b/caddyfile.go @@ -107,6 +107,16 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { if !d.Args(&u.DestDirFieldName) { return d.ArgErr() } + case "pass_thru": + var passThruStr string + if !d.AllArgs(&passThruStr) { + return d.ArgErr() + } + passThruBool, err := strconv.ParseBool(passThruStr) + if err != nil { + return d.Errf("parsing pass_thru: %v", err) + } + u.PassThru = passThruBool default: return d.Errf("unrecognized servers option '%s'", d.Val()) } diff --git a/upload.go b/upload.go index e08f70c..25bde39 100644 --- a/upload.go +++ b/upload.go @@ -40,6 +40,7 @@ type Upload struct { NotifyMethod string `json:"notify_method,omitempty"` CreateUuidDir bool `json:"create_uuid_dir,omitempty"` DestDirFieldName string `json:"dest_dir_field_name,omitempty"` + PassThru bool `json:"pass_thru,omitempty"` MyTlsSetting struct { InsecureSkipVerify bool `json:"insecure,omitempty"` @@ -173,6 +174,7 @@ func (u *Upload) Provision(ctx caddy.Context) error { zap.String("capath", u.MyTlsSetting.CAPath), zap.Bool("insecure", u.MyTlsSetting.InsecureSkipVerify), zap.String("dest_dir_field_name", u.DestDirFieldName), + zap.Bool("pass_thru", u.PassThru), ) return nil @@ -326,6 +328,10 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp } } + if u.PassThru { + return next.ServeHTTP(w, r) + } + if u.ResponseTemplate != "" { fileRespTemplate, fRTErr := os.Open(caddyhttp.SanitizedPathJoin(u.RootDir, u.ResponseTemplate))