Skip to content

Commit 60320f0

Browse files
committed
Fixed an issue with OHTTP responses
1 parent 155a5d3 commit 60320f0

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ adheres to [Semantic Versioning][semver].
1515

1616
- Fixed using `--http3` together with `--connect-to`.
1717
- Fixed unexpected panic when the server stops responding unexpectedly.
18+
- Fixed OHTTP responses not being written to the output.
1819

1920
[unreleased]: https://github.com/ameshkov/gocurl/compare/v1.5.0...HEAD
2021

internal/client/ohttp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ func (t *obliviousHTTPTransport) RoundTrip(r *http.Request) (resp *http.Response
116116
return nil, fmt.Errorf("failed to parse decrypted response: %w", err)
117117
}
118118

119+
// bhttp does not set the status and proto fields, so we do it here.
120+
resp.Status = fmt.Sprintf("%d", resp.StatusCode)
121+
resp.Proto = "OHTTP/1.0"
122+
119123
return resp, nil
120124
}
121125

internal/cmd/cmd.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,20 @@ func Run(cfg *config.Config, out *output.Output) error {
8181
_ = body.Close()
8282
}(resp.Body)
8383

84-
// Response body is only written when we're sure that it is there.
84+
// Response body is only written when we're sure that it is there and can
85+
// be fully read.
8586
var responseBody io.Reader
8687
if resp.ProtoMajor >= 2 ||
88+
// Content length guarantees that the response can be fully read.
8789
resp.ContentLength > 0 ||
90+
// Transfer-Encoding also guarantees that it's clear when body is
91+
// finished.
8892
len(resp.TransferEncoding) > 0 ||
93+
// OHTTP responses may not have any headers apart from content-type,
94+
// but they contain the full body right away.
95+
cfg.OHTTPGatewayURL != nil ||
96+
// When Connection: close we must read the body until the connection is
97+
// closed.
8998
resp.Header.Get("Connection") == "close" {
9099
responseBody = resp.Body
91100
}

internal/cmd/cmd_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,12 @@ func TestRunOHTTP(t *testing.T) {
432432
err = cmd.Run(cfg, out)
433433
require.NoError(t, err)
434434

435-
// Verify the output
435+
// Verify the output contains the actual response body
436436
data := dataBuffer.String()
437437

438-
// httpbin /get returns JSON with the request details
439-
// The response should contain JSON content
440-
assert.Contains(t, data, "application/json")
438+
// httpbin /get returns JSON with the request details including the URL
439+
// This verifies that the response body is actually written to output
440+
assert.Contains(t, data, "httpbin.agrd.workers.dev/get")
441441
}
442442

443443
// TestOHTTPInvalidOptions tests that invalid OHTTP option combinations are rejected.
@@ -567,9 +567,9 @@ func TestRunOHTTPWithProxy(t *testing.T) {
567567
// Verify the request went through the proxy
568568
assert.True(t, *proxyReceived, "OHTTP request should have gone through the proxy")
569569

570-
// Verify the output contains JSON content (OHTTP response)
570+
// Verify the output contains the actual response body
571571
data := dataBuffer.String()
572-
assert.Contains(t, data, "application/json")
572+
assert.Contains(t, data, "httpbin.agrd.workers.dev/get")
573573
}
574574

575575
// TestRunWithConnectTo tests the --connect-to flag functionality.

0 commit comments

Comments
 (0)