Skip to content

Commit

Permalink
feat: HTTP CONNECT client (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaikabanenkov committed Jan 6, 2025
1 parent 474cf4a commit 2e46ac4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
32 changes: 32 additions & 0 deletions x/httpconnect/connect_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,35 @@ func TestConnectClientOk(t *testing.T) {

require.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestConnectClientFail(t *testing.T) {
t.Parallel()

targetURL := "somehost:1234"

proxySrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodConnect, r.Method, "Method")
require.Equal(t, targetURL, r.Host, "Host")

w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("HTTP/1.1 400 Bad request\r\n\r\n"))
require.NoError(t, err, "Write")
}))
defer proxySrv.Close()

proxyURL, err := url.Parse(proxySrv.URL)
require.NoError(t, err, "Parse")

dialer := &transport.TCPDialer{
Dialer: net.Dialer{},
}

connClient, err := NewConnectClient(
dialer,
proxyURL.Host,
)
require.NoError(t, err, "NewConnectClient")

_, err = connClient.DialStream(context.Background(), targetURL)
require.Error(t, err, "unexpected status code: 400")
}
19 changes: 16 additions & 3 deletions x/httpconnect/pipe_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package httpconnect

import (
"errors"
"github.com/Jigsaw-Code/outline-sdk/transport"
"io"
)

// PipeConn is a [transport.StreamConn] that overrides [Read] and [Write] functions with the given [reader] and [writer]
// PipeConn is a [transport.StreamConn] that overrides [Read], [Write] (and corresponding [Close]) functions with the given [reader] and [writer]
type PipeConn struct {
reader io.Reader
writer io.Writer
reader io.ReadCloser
writer io.WriteCloser
transport.StreamConn
}

Expand All @@ -33,3 +34,15 @@ func (p *PipeConn) Read(b []byte) (n int, err error) {
func (p *PipeConn) Write(b []byte) (n int, err error) {
return p.writer.Write(b)
}

func (p *PipeConn) CloseRead() error {
return errors.Join(p.reader.Close(), p.StreamConn.CloseRead())
}

func (p *PipeConn) CloseWrite() error {
return errors.Join(p.writer.Close(), p.StreamConn.CloseWrite())
}

func (p *PipeConn) Close() error {
return errors.Join(p.reader.Close(), p.writer.Close(), p.StreamConn.Close())
}

0 comments on commit 2e46ac4

Please sign in to comment.