Commit 6b798e5
authored
refactor(proxy): extract writeJSONResponse and forwardAndReadBody helpers in handler.go (#2646)
`internal/proxy/handler.go` repeated the same 3-line JSON response
pattern at 4 sites and the `forwardToGitHub` + `io.ReadAll` sequence at
3 sites, with no shared helpers unlike the `server` package.
## Changes
### `writeJSONResponse` helper (issue #2629)
Mirrors the existing helper in `internal/server/http_helpers.go`.
Replaces inline `Content-Type → WriteHeader → json.Encode` blocks at:
- Health check (200)
- Unknown GraphQL query block (403)
- DIFC write violation (403)
- Strict-mode filter violation (403)
### `forwardAndReadBody` method on `proxyHandler` (issue #2630)
Encapsulates the forward + read + dual error-check pattern. Returns
`(*http.Response, []byte)`; callers check `resp == nil` on failure.
Replaces inline sequences in `ServeHTTP` (GraphQL introspection),
`handleWithDIFC` (Phase 3), and `passthrough`.
Also fixes a latent bug: the GraphQL introspection site was silently
discarding `io.ReadAll` errors (`respBody, _ := io.ReadAll(...)`),
inconsistent with the other two sites.
```go
// Before — repeated verbatim in 3 places:
resp, err := h.server.forwardToGitHub(ctx, method, path, body, ct, auth)
if err != nil {
http.Error(w, "upstream request failed", http.StatusBadGateway)
return
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, "failed to read upstream response", http.StatusBadGateway)
return
}
// After:
resp, respBody := h.forwardAndReadBody(w, ctx, method, path, body, ct, auth)
if resp == nil {
return
}
```
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build1901554330/b334/launcher.test
/tmp/go-build1901554330/b334/launcher.test
-test.testlogfile=/tmp/go-build1901554330/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s 8469��
ache/go/1.25.8/x64/src/runtime/cgo
ache/go/1.25.8/x64/src/encoding/encoding.go x_amd64/vet -nxv` (dns
block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build1901554330/b319/config.test
/tmp/go-build1901554330/b319/config.test
-test.testlogfile=/tmp/go-build1901554330/b319/testlog.txt
-test.paniconexit0 -test.timeout=10m0s abis��
ternal/engine/interpreter/compiler.go
ternal/engine/interpreter/format.go x_amd64/compile` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build1901554330/b334/launcher.test
/tmp/go-build1901554330/b334/launcher.test
-test.testlogfile=/tmp/go-build1901554330/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s 8469��
ache/go/1.25.8/x64/src/runtime/cgo
ache/go/1.25.8/x64/src/encoding/encoding.go x_amd64/vet -nxv` (dns
block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build1901554330/b334/launcher.test
/tmp/go-build1901554330/b334/launcher.test
-test.testlogfile=/tmp/go-build1901554330/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s 8469��
ache/go/1.25.8/x64/src/runtime/cgo
ache/go/1.25.8/x64/src/encoding/encoding.go x_amd64/vet -nxv` (dns
block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build1901554330/b343/mcp.test
/tmp/go-build1901554330/b343/mcp.test
-test.testlogfile=/tmp/go-build1901554330/b343/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -o /mcp/connection.go
/mcp/dockerenv.go x_amd64/vet -p ions =0 x_amd64/vet ortc�� g_.a
64/src/compress/gzip/gunzip.go x_amd64/vet --gdwarf-5 ernal/launcher
lcache/go/1.25.8/x64=/_/GOROOT x_amd64/vet` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>
<!-- START COPILOT CODING AGENT TIPS -->
---
⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS
or Windows machine with [Raycast](https://gh.io/cca-raycast-docs).1 file changed
Lines changed: 42 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | | - | |
38 | | - | |
39 | | - | |
| 38 | + | |
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
| |||
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
71 | | - | |
72 | | - | |
73 | | - | |
| 70 | + | |
74 | 71 | | |
75 | 72 | | |
76 | 73 | | |
| |||
80 | 77 | | |
81 | 78 | | |
82 | 79 | | |
83 | | - | |
84 | | - | |
85 | | - | |
| 80 | + | |
| 81 | + | |
86 | 82 | | |
87 | 83 | | |
88 | | - | |
89 | | - | |
90 | 84 | | |
91 | 85 | | |
92 | 86 | | |
| |||
157 | 151 | | |
158 | 152 | | |
159 | 153 | | |
160 | | - | |
161 | | - | |
162 | | - | |
| 154 | + | |
163 | 155 | | |
164 | 156 | | |
165 | 157 | | |
| |||
169 | 161 | | |
170 | 162 | | |
171 | 163 | | |
| 164 | + | |
172 | 165 | | |
173 | | - | |
| 166 | + | |
174 | 167 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 168 | + | |
181 | 169 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
| 170 | + | |
188 | 171 | | |
189 | 172 | | |
190 | 173 | | |
| |||
241 | 224 | | |
242 | 225 | | |
243 | 226 | | |
244 | | - | |
245 | | - | |
246 | | - | |
| 227 | + | |
247 | 228 | | |
248 | 229 | | |
249 | 230 | | |
| |||
329 | 310 | | |
330 | 311 | | |
331 | 312 | | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
| 313 | + | |
| 314 | + | |
342 | 315 | | |
343 | 316 | | |
344 | 317 | | |
| |||
378 | 351 | | |
379 | 352 | | |
380 | 353 | | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
381 | 383 | | |
382 | 384 | | |
383 | 385 | | |
| |||
0 commit comments