Skip to content

Commit 7f2b1c4

Browse files
committed
refactor: rename Protocol to HTTPProtocol
Rename the Protocol type to HTTPProtocol for consistency with existing HTTPClient and HTTPHeader field naming in DialOptions. Constants are also renamed: - ProtocolHTTP1 → HTTPProtocol1 - ProtocolHTTP2 → HTTPProtocol2 - ProtocolAcceptAny → HTTPProtocolAny
1 parent c7ab904 commit 7f2b1c4

File tree

8 files changed

+92
-92
lines changed

8 files changed

+92
-92
lines changed

accept.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ type AcceptOptions struct {
2727
// reject it, close the connection when c.Subprotocol() == "".
2828
Subprotocols []string
2929

30-
// Protocol selects which HTTP version to accept. Zero value defaults to
31-
// ProtocolHTTP1. ProtocolAcceptAny allows accepting either HTTP/1.1 or
30+
// HTTPProtocol selects which HTTP version to accept. Zero value defaults to
31+
// HTTPProtocol1. HTTPProtocolAny allows accepting either HTTP/1.1 or
3232
// HTTP/2.
3333
//
3434
// Experimental: This feature is experimental and may change in the future.
35-
Protocol Protocol
35+
HTTPProtocol HTTPProtocol
3636

3737
// InsecureSkipVerify is used to disable Accept's origin verification behavior.
3838
//
@@ -95,10 +95,10 @@ func (opts *AcceptOptions) cloneWithDefaults() (*AcceptOptions, error) {
9595
}
9696

9797
// Defaults to HTTP/1.1 only to preserve existing behavior (zero value).
98-
switch o.Protocol {
99-
case ProtocolAcceptAny, ProtocolHTTP1, ProtocolHTTP2:
98+
switch o.HTTPProtocol {
99+
case HTTPProtocolAny, HTTPProtocol1, HTTPProtocol2:
100100
default:
101-
return nil, fmt.Errorf("websocket: invalid protocol for accept options: %s", o.Protocol)
101+
return nil, fmt.Errorf("websocket: invalid protocol for accept options: %s", o.HTTPProtocol)
102102
}
103103

104104
return &o, nil
@@ -148,7 +148,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
148148
}
149149

150150
switch proto {
151-
case ProtocolHTTP2:
151+
case HTTPProtocol2:
152152
// Prepare response headers for H2 (no Connection/Upgrade).
153153
w.Header().Set("Sec-WebSocket-Accept", secWebSocketAccept(key))
154154

@@ -184,7 +184,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
184184
bw: getBufioWriter(stream),
185185
}), nil
186186

187-
case ProtocolHTTP1:
187+
case HTTPProtocol1:
188188
hj, ok := hijacker(w)
189189
if !ok {
190190
err = errors.New("http.ResponseWriter does not implement http.Hijacker")
@@ -243,32 +243,32 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
243243
}
244244
}
245245

246-
func verifyClientRequest(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (proto Protocol, key string, errCode int, err error) {
246+
func verifyClientRequest(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (proto HTTPProtocol, key string, errCode int, err error) {
247247
if r.ProtoMajor == 2 {
248-
switch opts.Protocol {
249-
case ProtocolHTTP1:
250-
return ProtocolHTTP2, "", http.StatusBadRequest, errors.New("HTTP/2 extended CONNECT refused: server only accepts HTTP/1.1 Upgrade")
248+
switch opts.HTTPProtocol {
249+
case HTTPProtocol1:
250+
return HTTPProtocol2, "", http.StatusBadRequest, errors.New("HTTP/2 extended CONNECT refused: server only accepts HTTP/1.1 Upgrade")
251251
}
252252

253253
// HTTP/2 extended CONNECT (RFC 8441) path.
254254
key, errCode, err = verifyClientRequestH2(w, r)
255255
if err != nil {
256-
return ProtocolHTTP2, "", errCode, err
256+
return HTTPProtocol2, "", errCode, err
257257
}
258-
return ProtocolHTTP2, key, 0, nil
258+
return HTTPProtocol2, key, 0, nil
259259
}
260260

261-
switch opts.Protocol {
262-
case ProtocolHTTP2:
263-
return ProtocolHTTP1, "", http.StatusBadRequest, errors.New("HTTP/1.1 Upgrade refused: server requires HTTP/2 extended CONNECT")
261+
switch opts.HTTPProtocol {
262+
case HTTPProtocol2:
263+
return HTTPProtocol1, "", http.StatusBadRequest, errors.New("HTTP/1.1 Upgrade refused: server requires HTTP/2 extended CONNECT")
264264
}
265265

266266
// HTTP/1.1 GET/Upgrade handshake validation.
267267
key, errCode, err = verifyClientRequestH1(w, r)
268268
if err != nil {
269-
return ProtocolHTTP1, "", errCode, err
269+
return HTTPProtocol1, "", errCode, err
270270
}
271-
return ProtocolHTTP1, key, 0, nil
271+
return HTTPProtocol1, key, 0, nil
272272
}
273273

274274
// verifyClientRequestH1 validates an HTTP/1.1 WebSocket GET/Upgrade request.

accept_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func Test_verifyClientHandshake(t *testing.T) {
342342
r.Header.Add(k, v)
343343
}
344344

345-
_, _, _, err := verifyClientRequest(httptest.NewRecorder(), r, &AcceptOptions{Protocol: ProtocolHTTP1})
345+
_, _, _, err := verifyClientRequest(httptest.NewRecorder(), r, &AcceptOptions{HTTPProtocol: HTTPProtocol1})
346346
if tc.success {
347347
assert.Success(t, err)
348348
} else {

dial.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ type DialOptions struct {
2929
// HTTPHeader specifies the HTTP headers included in the handshake request.
3030
HTTPHeader http.Header
3131

32-
// Protocol selects the HTTP version for the handshake. Zero value defaults
33-
// to ProtocolHTTP1. ProtocolAcceptAny is not supported by Dial.
32+
// HTTPProtocol selects the HTTP version for the handshake. Zero value defaults
33+
// to HTTPProtocol1. HTTPProtocolAny is not supported by Dial.
3434
//
3535
// Experimental: This feature is experimental and may change in the future.
36-
Protocol Protocol
36+
HTTPProtocol HTTPProtocol
3737

3838
// Host optionally overrides the Host HTTP header to send. If empty, the value
3939
// of URL.Host will be used.
@@ -80,10 +80,10 @@ func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context
8080
}
8181

8282
// Defaults to HTTP/1.1 only to preserve existing behavior (zero value).
83-
switch o.Protocol {
84-
case ProtocolHTTP1, ProtocolHTTP2:
83+
switch o.HTTPProtocol {
84+
case HTTPProtocol1, HTTPProtocol2:
8585
default:
86-
return nil, nil, nil, fmt.Errorf("websocket: invalid protocol for dial options: %s", o.Protocol)
86+
return nil, nil, nil, fmt.Errorf("websocket: invalid protocol for dial options: %s", o.HTTPProtocol)
8787
}
8888

8989
if o.HTTPClient == nil {
@@ -218,13 +218,13 @@ func handshakeRequest(ctx context.Context, urls string, opts *DialOptions, copts
218218
return nil, fmt.Errorf("unexpected url scheme: %q", u.Scheme)
219219
}
220220

221-
switch opts.Protocol {
222-
case ProtocolHTTP2:
221+
switch opts.HTTPProtocol {
222+
case HTTPProtocol2:
223223
return handshakeRequestH2(ctx, u, opts, copts, secWebSocketKey)
224-
case ProtocolHTTP1:
224+
case HTTPProtocol1:
225225
return handshakeRequestH1(ctx, u, opts, copts, secWebSocketKey)
226226
default:
227-
return nil, fmt.Errorf("unknown protocol: %s", opts.Protocol)
227+
return nil, fmt.Errorf("unknown protocol: %s", opts.HTTPProtocol)
228228
}
229229
}
230230

@@ -317,13 +317,13 @@ func secWebSocketKey(rr io.Reader) (string, error) {
317317
}
318318

319319
func verifyServerResponse(opts *DialOptions, copts *compressionOptions, secWebSocketKey string, resp *http.Response) (*compressionOptions, error) {
320-
switch opts.Protocol {
321-
case ProtocolHTTP2:
320+
switch opts.HTTPProtocol {
321+
case HTTPProtocol2:
322322
return verifyServerResponseH2(opts, copts, secWebSocketKey, resp)
323-
case ProtocolHTTP1:
323+
case HTTPProtocol1:
324324
return verifyServerResponseH1(opts, copts, secWebSocketKey, resp)
325325
default:
326-
return nil, fmt.Errorf("unknown protocol: %s", opts.Protocol)
326+
return nil, fmt.Errorf("unknown protocol: %s", opts.HTTPProtocol)
327327
}
328328
}
329329

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
// Some important caveats to be aware of:
4242
//
4343
// - Accept always errors out
44-
// - Protocol in DialOptions and AcceptOptions is no-op
44+
// - HTTPProtocol in DialOptions and AcceptOptions is no-op
4545
// - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op
4646
// - *http.Response from Dial is &http.Response{} with a 101 status code on success
4747
// - Conn.Ping is no-op

internal/examples/http2/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Examples:
8181

8282
conn, resp, err := websocket.Dial(ctx, rawURL, &websocket.DialOptions{
8383
HTTPClient: hc,
84-
Protocol: websocket.ProtocolHTTP2,
84+
HTTPProtocol: websocket.HTTPProtocol2,
8585
})
8686
if err != nil {
8787
if resp != nil {

internal/thirdparty/http2/http2_test.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ type runTableTestCase struct {
148148
name string
149149
scheme string // "wss" or "ws"
150150
client func(*testing.T) *http.Client
151-
clientProto websocket.Protocol
152-
serverProto websocket.Protocol
151+
clientProto websocket.HTTPProtocol
152+
serverProto websocket.HTTPProtocol
153153
wantProto int // 1 or 2
154154
wantStatus int // Wanted status code (e.g., 200 or 100).
155155
wantErr bool // Want a Dial error.
@@ -159,7 +159,7 @@ type runTableTestCase struct {
159159
func runTable(t *testing.T, cases []runTableTestCase) {
160160
for _, tc := range cases {
161161
echoHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
162-
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{Protocol: tc.serverProto})
162+
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{HTTPProtocol: tc.serverProto})
163163
if err != nil {
164164
return
165165
}
@@ -193,8 +193,8 @@ func runTable(t *testing.T, cases []runTableTestCase) {
193193
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
194194
defer cancel()
195195
conn, resp, err := websocket.Dial(ctx, srvURL, &websocket.DialOptions{
196-
HTTPClient: tc.client(t),
197-
Protocol: tc.clientProto,
196+
HTTPClient: tc.client(t),
197+
HTTPProtocol: tc.clientProto,
198198
})
199199
if conn != nil {
200200
defer conn.CloseNow()
@@ -228,32 +228,32 @@ var sharedTestCases = []runTableTestCase{
228228
name: "Error TLS ClientHTTP1 RequestHTTP2 AcceptAny",
229229
scheme: "wss",
230230
client: func(t *testing.T) *http.Client { return newH1TLSClient() },
231-
clientProto: websocket.ProtocolHTTP2,
232-
serverProto: websocket.ProtocolAcceptAny,
231+
clientProto: websocket.HTTPProtocol2,
232+
serverProto: websocket.HTTPProtocolAny,
233233
wantErr: true,
234234
},
235235
{
236236
name: "Error H2C ClientHTTP1 RequestHTTP2 AcceptAny",
237237
scheme: "ws",
238238
client: func(t *testing.T) *http.Client { return newH1TLSClient() },
239-
clientProto: websocket.ProtocolHTTP2,
240-
serverProto: websocket.ProtocolAcceptAny,
239+
clientProto: websocket.HTTPProtocol2,
240+
serverProto: websocket.HTTPProtocolAny,
241241
wantErr: true,
242242
},
243243
{
244244
name: "Error TLS ClientHTTP2 RequestHTTP2 AcceptHTTP1",
245245
scheme: "wss",
246246
client: func(t *testing.T) *http.Client { return newH2TLSClient() },
247-
clientProto: websocket.ProtocolHTTP2,
248-
serverProto: websocket.ProtocolHTTP1,
247+
clientProto: websocket.HTTPProtocol2,
248+
serverProto: websocket.HTTPProtocol1,
249249
wantErr: true,
250250
},
251251
{
252252
name: "Error TLS ClientHTTP1 RequestHTTP1 AcceptHTTP2",
253253
scheme: "wss",
254254
client: func(t *testing.T) *http.Client { return newH1TLSClient() },
255-
clientProto: websocket.ProtocolHTTP1,
256-
serverProto: websocket.ProtocolHTTP2,
255+
clientProto: websocket.HTTPProtocol1,
256+
serverProto: websocket.HTTPProtocol2,
257257
wantErr: true,
258258
},
259259
}
@@ -267,24 +267,24 @@ func TestHTTP2Suite_XCONNECT_Enabled(t *testing.T) {
267267
name: "OK TLS ClientHTTP2 RequestHTTP2 AcceptHTTP2",
268268
scheme: "wss",
269269
client: func(t *testing.T) *http.Client { return newH2TLSClient() },
270-
clientProto: websocket.ProtocolHTTP2,
271-
serverProto: websocket.ProtocolHTTP2,
270+
clientProto: websocket.HTTPProtocol2,
271+
serverProto: websocket.HTTPProtocol2,
272272
wantProto: 2,
273273
},
274274
{
275275
name: "OK TLS ClientHTTP2 RequestHTTP2 AcceptAny",
276276
scheme: "wss",
277277
client: func(t *testing.T) *http.Client { return newH2TLSClient() },
278-
clientProto: websocket.ProtocolHTTP2,
279-
serverProto: websocket.ProtocolAcceptAny,
278+
clientProto: websocket.HTTPProtocol2,
279+
serverProto: websocket.HTTPProtocolAny,
280280
wantProto: 2,
281281
},
282282
{
283283
name: "OK H2C ClientHTTP2 RequestHTTP2 AcceptAny",
284284
scheme: "ws",
285285
client: func(t *testing.T) *http.Client { return newH2CClient() },
286-
clientProto: websocket.ProtocolHTTP2,
287-
serverProto: websocket.ProtocolAcceptAny,
286+
clientProto: websocket.HTTPProtocol2,
287+
serverProto: websocket.HTTPProtocolAny,
288288
wantProto: 2,
289289
},
290290
{
@@ -295,8 +295,8 @@ func TestHTTP2Suite_XCONNECT_Enabled(t *testing.T) {
295295
req.Header.Del(":protocol")
296296
}, nil)
297297
},
298-
clientProto: websocket.ProtocolHTTP2,
299-
serverProto: websocket.ProtocolHTTP2,
298+
clientProto: websocket.HTTPProtocol2,
299+
serverProto: websocket.HTTPProtocol2,
300300
wantErr: true,
301301
},
302302
{
@@ -311,8 +311,8 @@ func TestHTTP2Suite_XCONNECT_Enabled(t *testing.T) {
311311
req.Method = http.MethodGet
312312
}, nil)
313313
},
314-
clientProto: websocket.ProtocolHTTP2,
315-
serverProto: websocket.ProtocolHTTP2,
314+
clientProto: websocket.HTTPProtocol2,
315+
serverProto: websocket.HTTPProtocol2,
316316
wantErr: true,
317317
},
318318
{
@@ -326,8 +326,8 @@ func TestHTTP2Suite_XCONNECT_Enabled(t *testing.T) {
326326
_ = resp.Body.Close()
327327
})
328328
},
329-
clientProto: websocket.ProtocolHTTP2,
330-
serverProto: websocket.ProtocolHTTP2,
329+
clientProto: websocket.HTTPProtocol2,
330+
serverProto: websocket.HTTPProtocol2,
331331
wantErr: true,
332332
},
333333
}...))
@@ -342,16 +342,16 @@ func TestHTTP2Suite_XCONNECT_Disabled(t *testing.T) {
342342
name: "Error TLS ClientHTTP2 RequestHTTP2 AcceptAny NoExtendedConnect",
343343
scheme: "wss",
344344
client: func(t *testing.T) *http.Client { return newH2TLSClient() },
345-
clientProto: websocket.ProtocolHTTP2,
346-
serverProto: websocket.ProtocolAcceptAny,
345+
clientProto: websocket.HTTPProtocol2,
346+
serverProto: websocket.HTTPProtocolAny,
347347
wantErr: true,
348348
},
349349
{
350350
name: "Error H2C ClientHTTP2 RequestHTTP2 AcceptAny NoExtendedConnect",
351351
scheme: "ws",
352352
client: func(t *testing.T) *http.Client { return newH2CClient() },
353-
clientProto: websocket.ProtocolHTTP2,
354-
serverProto: websocket.ProtocolAcceptAny,
353+
clientProto: websocket.HTTPProtocol2,
354+
serverProto: websocket.HTTPProtocolAny,
355355
wantErr: true,
356356
},
357357
}...))

0 commit comments

Comments
 (0)