Skip to content

Commit

Permalink
Merge branch 'main' into sayers/raw_response
Browse files Browse the repository at this point in the history
  • Loading branch information
smaye81 authored Jan 26, 2024
2 parents c570c23 + 48ef283 commit f51d677
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
21 changes: 7 additions & 14 deletions .github/workflows/add-to-project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ on:
types:
- opened
- reopened
issue_comment:
types:
- created

jobs:
add-to-project:
name: Add issue to project
runs-on: ubuntu-latest
steps:
- name: Get GitHub app token
uses: actions/create-github-app-token@v1
id: app_token
with:
app-id: ${{ secrets.CONNECT_EXPORT_APP_ID }}
private-key: ${{ secrets.CONNECT_EXPORT_APP_KEY }}
- uses: actions/[email protected]
with:
project-url: https://github.com/orgs/connectrpc/projects/1
github-token: ${{ steps.app_token.outputs.token }}
call-workflow-add-to-project:
name: Call workflow to add issue to project
uses: connectrpc/base-workflows/.github/workflows/add-to-project.yaml@main
secrets: inherit
28 changes: 22 additions & 6 deletions internal/tracer/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (t *tracingResponseWriter) WriteHeader(statusCode int) {
}
}
t.resp = &http.Response{
Header: t.respWriter.Header(),
Body: io.NopCloser(bytes.NewBuffer(nil)), // empty body
Status: fmt.Sprintf("%d %s", statusCode, http.StatusText(statusCode)),
StatusCode: statusCode,
Expand All @@ -126,9 +125,20 @@ func (t *tracingResponseWriter) WriteHeader(statusCode int) {
ProtoMinor: t.req.ProtoMinor,
ContentLength: contentLen,
TLS: t.req.TLS,
Trailer: http.Header{},
}
for _, trailerNames := range t.Header().Values("Trailer") {
// Create snapshot of the headers
t.resp.Header = make(http.Header, len(t.Header()))
for headerName, headerVals := range t.Header() {
if strings.HasPrefix(headerName, http.TrailerPrefix) {
continue
}
headerVals = append([]string(nil), headerVals...) // snapshot the slice
t.resp.Header[headerName] = headerVals
}
// And also seed the trailers with expected trailer keys
trailerHeaders := t.Header().Values("Trailer")
t.resp.Trailer = make(http.Header, len(trailerHeaders))
for _, trailerNames := range trailerHeaders {
for _, trailerName := range strings.Split(trailerNames, ",") {
trailerName = strings.TrimSpace(trailerName)
if trailerName == "" {
Expand Down Expand Up @@ -163,18 +173,24 @@ func (t *tracingResponseWriter) tryFinish(err error) {
}

func (t *tracingResponseWriter) setTrailers() {
headersAndTrailers := t.Header() // response writer's headers (counter-intuitively) include trailers, too
// First extract any known trailer keys (that were advertised in "Trailer" header).
for trailerName := range t.resp.Trailer {
t.resp.Trailer[trailerName] = t.resp.Header[trailerName]
t.resp.Trailer[trailerName] = headersAndTrailers[trailerName]
}
for key, vals := range t.resp.Header {
// Then get any others, identified by a special prefix in the name.
for key, vals := range headersAndTrailers {
trailerKey := strings.TrimPrefix(key, http.TrailerPrefix)
if trailerKey == key {
// no prefix trimmed, so not a trailer
continue
}
existing := t.resp.Trailer[trailerKey]
if len(existing) > 0 {
// defensive copy, so we don't accidentally mutate the slice in response writer's headers
existing = append([]string(nil), existing...)
}
t.resp.Trailer[trailerKey] = append(existing, vals...)
delete(t.resp.Header, key)
}
}

Expand Down

0 comments on commit f51d677

Please sign in to comment.