Skip to content

Commit

Permalink
Merge branch 'master' into staging-client
Browse files Browse the repository at this point in the history
  • Loading branch information
rod-hynes committed Mar 2, 2023
2 parents cf7bc4f + 8ae97c7 commit 33011f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
19 changes: 16 additions & 3 deletions psiphon/common/transforms/httpTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,26 @@ func (t *HTTPTransformer) Write(b []byte) (int, error) {
// Do not need to check return value. It is guaranteed that
// n == len(newHeader) because t.b.Len() >= n if the header
// size has not changed.
copy(t.b.Bytes()[:len(header)], newHeader)
copy(t.b.Bytes()[:headerLen], newHeader)
} else {
b := t.b.Bytes()

// Copy any request body bytes received before resetting the
// buffer.
var reqBody []byte
reqBodyLen := t.b.Len() - headerLen // number of request body bytes received
if reqBodyLen > 0 {
reqBody = make([]byte, reqBodyLen)
copy(reqBody, t.b.Bytes()[headerLen:])
}

// Reset the buffer and write transformed header and any
// request body bytes received into it.
t.b.Reset()
// Do not need to check return value of bytes.Buffer.Write() https://github.com/golang/go/blob/1e9ff255a130200fcc4ec5e911d28181fce947d5/src/bytes/buffer.go#L164
t.b.Write(newHeader)
t.b.Write(b[len(header):])
if len(reqBody) > 0 {
t.b.Write(reqBody)
}
}

header = newHeader
Expand Down
14 changes: 14 additions & 0 deletions psiphon/common/transforms/httpTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ func TestHTTPTransformerHTTPRequest(t *testing.T) {
chunkSize: 1,
transform: Spec{[2]string{"4", "100"}},
},
{
name: "transform with separate write for header and body",
input: "POST / HTTP/1.1\r\nContent-Length: 4\r\n\r\nabcd",
wantOutput: "POST / HTTP/1.1\r\nContent-Length: 100\r\n\r\nabcd",
chunkSize: 38, // length of header
transform: Spec{[2]string{"4", "100"}},
},
{
name: "transform with single write",
input: "POST / HTTP/1.1\r\nContent-Length: 4\r\n\r\nabcd",
wantOutput: "POST / HTTP/1.1\r\nContent-Length: 100\r\n\r\nabcd",
chunkSize: 999,
transform: Spec{[2]string{"4", "100"}},
},
{
name: "transform with partial write and errors in header write",
input: "POST / HTTP/1.1\r\nContent-Length: 4\r\n\r\nabcd",
Expand Down

0 comments on commit 33011f1

Please sign in to comment.