@@ -19,7 +19,7 @@ type DialOptions struct {
1919 // HTTPClient is the http client used for the handshake.
2020 // Its Transport must use HTTP/1.1 and must return writable bodies
2121 // for WebSocket handshakes. This was introduced in Go 1.12.
22- // http.Transport does this correctly.
22+ // http.Transport does this all correctly.
2323 HTTPClient * http.Client
2424
2525 // HTTPHeader specifies the HTTP headers included in the handshake request.
@@ -35,6 +35,9 @@ type DialOptions struct {
3535var secWebSocketKey = base64 .StdEncoding .EncodeToString (make ([]byte , 16 ))
3636
3737// Dial performs a WebSocket handshake on the given url with the given options.
38+ // The response is the WebSocket handshake response from the server.
39+ // If an error occurs, the returned response may be non nil. However, you can only
40+ // read the first 1024 bytes of its body.
3841func Dial (ctx context.Context , u string , opts DialOptions ) (* Conn , * http.Response , error ) {
3942 c , r , err := dial (ctx , u , opts )
4043 if err != nil {
@@ -48,7 +51,7 @@ func dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
4851 opts .HTTPClient = http .DefaultClient
4952 }
5053 if opts .HTTPClient .Timeout > 0 {
51- return nil , nil , xerrors .Errorf ("please use context for cancellation instead of http.Client.Timeout; see issue nhooyr.io/ websocket# 67" )
54+ return nil , nil , xerrors .Errorf ("please use context for cancellation instead of http.Client.Timeout; see https://github.com/nhooyr/ websocket/issues/ 67" )
5255 }
5356 if opts .HTTPHeader == nil {
5457 opts .HTTPHeader = http.Header {}
@@ -65,7 +68,7 @@ func dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
6568 case "wss" :
6669 parsedURL .Scheme = "https"
6770 default :
68- return nil , nil , xerrors .Errorf ("unexpected url scheme scheme : %q" , parsedURL .Scheme )
71+ return nil , nil , xerrors .Errorf ("unexpected url scheme: %q" , parsedURL .Scheme )
6972 }
7073
7174 req , _ := http .NewRequest ("GET" , parsedURL .String (), nil )
@@ -84,13 +87,12 @@ func dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
8487 return nil , nil , xerrors .Errorf ("failed to send handshake request: %w" , err )
8588 }
8689 defer func () {
87- respBody := resp .Body
8890 if err != nil {
89- // We read a bit of the body for better debugging.
91+ // We read a bit of the body for easier debugging.
9092 r := io .LimitReader (resp .Body , 1024 )
9193 b , _ := ioutil .ReadAll (r )
94+ resp .Body .Close ()
9295 resp .Body = ioutil .NopCloser (bytes .NewReader (b ))
93- respBody .Close ()
9496 }
9597 }()
9698
@@ -104,7 +106,6 @@ func dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
104106 return nil , resp , xerrors .Errorf ("response body is not a read write closer: %T" , rwc )
105107 }
106108
107- // TODO pool bufio
108109 c := & Conn {
109110 subprotocol : resp .Header .Get ("Sec-WebSocket-Protocol" ),
110111 br : bufio .NewReader (rwc ),
@@ -123,11 +124,11 @@ func verifyServerResponse(resp *http.Response) error {
123124 }
124125
125126 if ! headerValuesContainsToken (resp .Header , "Connection" , "Upgrade" ) {
126- return xerrors .Errorf ("websocket protocol violation: Connection header does not contain Upgrade: %q " , resp .Header .Get ("Connection" ))
127+ return xerrors .Errorf ("websocket protocol violation: Connection header %q does not contain Upgrade" , resp .Header .Get ("Connection" ))
127128 }
128129
129130 if ! headerValuesContainsToken (resp .Header , "Upgrade" , "WebSocket" ) {
130- return xerrors .Errorf ("websocket protocol violation: Upgrade header does not contain websocket: %q " , resp .Header .Get ("Upgrade" ))
131+ return xerrors .Errorf ("websocket protocol violation: Upgrade header %q does not contain websocket" , resp .Header .Get ("Upgrade" ))
131132 }
132133
133134 // We do not care about Sec-WebSocket-Accept because it does not matter.
0 commit comments