-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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?
requestbody: Add replace
for optional body replacement
#5795
Conversation
618ce28
to
7aad531
Compare
replace
for optional body replacement
This is interesting, but a bit strange of a feature. I think we'll need to do a bit of thinking about how we want to approach this. It might take us a bit longer to decide on this one. Thanks for the contribution and idea! |
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.
Oh, so this actually completely drops the upstream response body and replaces it with a new one entirely, not performing replacements in the response body.
Should r.Body
be closed before it is set to a new value?
Like Francis said, this is a bit unusual and I'm not sure what the implications will be.
I guess it's a very simple change. (We're on feature freeze until after 2.8, so even if we merge this in, it won't be released until 2.9.)
Why is this needed instead of a simple respond
directive?
@@ -31,6 +32,7 @@ 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"` | |||
Replace string `json:"replace,omitempty"` |
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.
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.
Actually, can we rename this to Set
? I keep thinking of "replacing content within the body" whereas "setting the body" is more precise IMO.
Hi @mholt
I think it's only about the Request body. Today, I have been playing with Oracle REST Data Services / ORDS where you can POST sql statements to a service endpoint of ORDS such as:
Having a
I thought I just add this use case here where this |
Yeah - FWIW I thought this was about the response body at first as well, but this is definitely about the request body @mholt. Not the same as https://github.com/caddyserver/replace-response, entirely unrelated in fact. In terms of Caddyfile API, I'm considering whether we should make it the first argument to
|
Oh, I see -- thanks for pointing that out. I like Francis' suggestion to make it work inline, i.e.: |
7aad531
to
a449c16
Compare
a449c16
to
4a1cba4
Compare
Looking good, but my remaining concern is closing the request body. The std lib will close the original request body, I guess, and we don't need to close the new one since it's just a string buffer? Have you verified that there is not a memory leak? |
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.
So, I don't know when things changed (maybe I was looking at an old diff) but apparently the body is closed now. That's good.
Sorry for my severe delay. But what do you think about renaming Replace
to Set
?
I also wonder... I have an idea... should it be modular? i.e. are there multiple ways to get what the request body should be set to (e.g. the response to an HTTP request)?
@@ -31,6 +32,7 @@ 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"` | |||
Replace string `json:"replace,omitempty"` |
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.
Actually, can we rename this to Set
? I keep thinking of "replacing content within the body" whereas "setting the body" is more precise IMO.
Hi @mholt, is there any chance this PR could be merged in time for v2.9.0? It would enable some practical use cases, including one where a simple GET request to Caddy (e.g., Here’s an example Caddyfile configuration snipping illustrating this: handle /vat-check {
# Define a matcher to check if the `no` query parameter is missing
@noQueryParamNo {
vars {http.request.uri.query.no} ""
}
# Respond with an error message if the `no` query parameter is missing
respond @noQueryParamNo "Missing query-parameter ?no=VAT-NO" 400
# Extract the first two characters as the country code and the rest as the VAT number
# If no match is found, use default values "XX" and "1234567890" to trigger an error
map {http.request.uri.query.no} {country} {vatNumber} {
~^(.{2})(.+)$ "${1}" "${2}"
default "XX" "1234567890"
}
# Set the request body for the reverse proxy, including the extracted country code and VAT number
request_body {
set `{"countryCode": "{country}","vatNumber": "{vatNumber}"}`
}
# Forward the request to the external VAT-checking API with the modified request body
reverse_proxy {
to https://ec.europa.eu
header_up Host {upstream_hostport}
method POST
rewrite /taxation_customs/vies/rest-api/check-vat-number
}
} |
fb24b69
to
03175f0
Compare
Hello,
here is a need I have, I use caddy as a reverse proxy to EdgeDB endpoint, and declare custom routes to query Edgedb.
The endpoint act as an alias for my query, so no query appears in the javascript on client side.
Tell if it's a good idea or if the name of the directive should be something else.