Skip to content

Commit

Permalink
feat: add pass_thru mode to continue to next handler instead of reply…
Browse files Browse the repository at this point in the history
…ing directly (#23)

Thank you @adrienyhuel for contributing.
Merged now
  • Loading branch information
adrienyhuel authored Nov 21, 2024
1 parent 256822c commit 229dfcb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
6 changes: 6 additions & 0 deletions upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 229dfcb

Please sign in to comment.