Skip to content
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

feat(server): echo request headers in response over REST transport #1509

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions util/genrest/goviewcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func NewView(model *gomodel.Model) (*goview.View, error) {
source.P(` backend.StdLog.Printf(" urlPathParams (expect %d, have %%d): %%q", numUrlPathParams, urlPathParams)`, len(allURLVariables))
source.P(` backend.StdLog.Printf(" urlRequestHeaders:\n%%s", resttools.PrettyPrintHeaders(r, " "))`)
source.P("")
source.P(" resttools.IncludeRequestHeadersInResponse(w, r)")
source.P("")
source.P(" if numUrlPathParams!=%d {", len(allURLVariables))
source.P(` backend.Error(w, http.StatusBadRequest, "found unexpected number of URL variables: expected %d, have %%d: %%#v", numUrlPathParams, urlPathParams)`, len(allURLVariables))
source.P(" return")
Expand Down
13 changes: 13 additions & 0 deletions util/genrest/resttools/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,16 @@ func PrettyPrintHeaders(request *http.Request, indentation string) string {
}
return strings.Join(lines, "\n")
}

// IncludeRequestHeadersInResponse includes all headers in the request `r` and includes them in the response `w`,
// prefixing each of these header keys with a constant to reflect they came from the request.
func IncludeRequestHeadersInResponse(w http.ResponseWriter, r *http.Request) {
const prefix = "x-showcase-request-"

responseHeaders := w.Header()
for requestKey, valueList := range r.Header {
for _, value := range valueList {
responseHeaders.Add(prefix+requestKey, value)
}
}
}