-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
requestbody: Add replace
for optional body replacement
#5795
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package requestbody | |
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
"github.com/caddyserver/caddy/v2/modules/caddyhttp" | ||
|
@@ -30,7 +31,8 @@ func init() { | |
type RequestBody struct { | ||
// The maximum number of bytes to allow reading from the body by a later handler. | ||
// If more bytes are read, an error with HTTP status 413 is returned. | ||
MaxSize int64 `json:"max_size,omitempty"` | ||
MaxSize int64 `json:"max_size,omitempty"` | ||
Replace string `json:"replace,omitempty"` | ||
} | ||
|
||
// CaddyModule returns the Caddy module information. | ||
|
@@ -42,6 +44,10 @@ func (RequestBody) CaddyModule() caddy.ModuleInfo { | |
} | ||
|
||
func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { | ||
if rb.Replace != "" { | ||
r.Body = io.NopCloser(strings.NewReader(rb.Replace)) | ||
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're completely replacing the request body, we miiiight want to call Also, I wonder if we should add placeholder support and expand those when doing the body replacement. |
||
r.ContentLength = int64(len(rb.Replace)) | ||
} | ||
if r.Body == nil { | ||
return next.ServeHTTP(w, r) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would need a helpful godoc comment on this field.