Skip to content

Commit bd4953e

Browse files
committed
Add websocket support
1 parent 3961c9a commit bd4953e

File tree

8 files changed

+20
-8
lines changed

8 files changed

+20
-8
lines changed

internal/mode/static/nginx/config/maps_template.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ map $http_host $gw_api_compliant_host {
1717
'' $host;
1818
default $http_host;
1919
}
20+
21+
# Set $connection_header variable to upgrade when the $http_upgrade header is set, otherwise, set it to close. This
22+
# allows support for websocket connections. See https://nginx.org/en/docs/http/websocket.html.
23+
map $http_upgrade $connection_upgrade {
24+
default upgrade;
25+
'' close;
26+
}
2027
`

internal/mode/static/nginx/config/maps_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestExecuteMaps(t *testing.T) {
8585
"~.* ${http_my_second_add_header},;": 1,
8686
"map ${http_my_set_header} $my_set_header_header_var {": 0,
8787
"map $http_host $gw_api_compliant_host {": 1,
88+
"map $http_upgrade $connection_upgrade {": 1,
8889
}
8990

9091
maps := string(executeMaps(conf))

internal/mode/static/nginx/config/servers_template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ server {
5151
proxy_set_header {{ $h.Name }} "{{ $h.Value }}";
5252
{{- end }}
5353
proxy_set_header Host $gw_api_compliant_host;
54+
proxy_set_header Upgrade $http_upgrade;
55+
proxy_set_header Connection $connection_upgrade;
5456
proxy_pass {{ $l.ProxyPass }}$request_uri;
5557
{{- end }}
5658
}

internal/mode/static/nginx/config/validation/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func validateEscapedStringNoVarExpansion(value string, examples []string) error
5252
}
5353

5454
const (
55-
invalidHostHeaderErrMsg string = "redefining the Host request header is not supported"
56-
maxHeaderLength int = 256
55+
invalidHeadersErrMsg string = "redefining the Host, Connection, or Upgrade request headers is not supported"
56+
maxHeaderLength int = 256
5757
)
5858

5959
func validateHeaderName(name string) error {
@@ -63,8 +63,8 @@ func validateHeaderName(name string) error {
6363
if msg := k8svalidation.IsHTTPHeaderName(name); msg != nil {
6464
return errors.New(msg[0])
6565
}
66-
if strings.ToLower(name) == "host" {
67-
return errors.New(invalidHostHeaderErrMsg)
66+
if strings.ToLower(name) == "host" || strings.ToLower(name) == "connection" || strings.ToLower(name) == "upgrade" {
67+
return errors.New(invalidHeadersErrMsg)
6868
}
6969
return nil
7070
}

internal/mode/static/nginx/config/validation/common_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func TestValidateValidHeaderName(t *testing.T) {
6464
`$test`,
6565
"Host",
6666
"host",
67+
"connection",
68+
"upgrade",
6769
"my-header[]",
6870
"my-header&",
6971
strings.Repeat("very-long-header", 16)+"1",

internal/mode/static/nginx/config/validation/http_filters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestValidateRequestHeaderName(t *testing.T) {
7474
t,
7575
validator.ValidateRequestHeaderName,
7676
"Content-Encoding",
77-
"Connection",
77+
"MyBespokeHeader",
7878
)
7979

8080
testInvalidValuesForSimpleValidator(t, validator.ValidateRequestHeaderName, "$Content-Encoding")

internal/mode/static/state/dataplane/configuration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,8 +1609,8 @@ func TestCreateFilters(t *testing.T) {
16091609
RequestHeaderModifier: &v1beta1.HTTPHeaderFilter{
16101610
Set: []v1beta1.HTTPHeader{
16111611
{
1612-
Name: "Connection",
1613-
Value: "close",
1612+
Name: "MyBespokeHeader",
1613+
Value: "my-value",
16141614
},
16151615
},
16161616
},

internal/mode/static/state/graph/httproute_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ func TestValidateFilterRequestHeaderModifier(t *testing.T) {
18361836
Type: v1beta1.HTTPRouteFilterRequestHeaderModifier,
18371837
RequestHeaderModifier: &v1beta1.HTTPHeaderFilter{
18381838
Set: []v1beta1.HTTPHeader{
1839-
{Name: "Connection", Value: "close"},
1839+
{Name: "MyBespokeHeader", Value: "my-value"},
18401840
},
18411841
Add: []v1beta1.HTTPHeader{
18421842
{Name: "Accept-Encoding", Value: "gzip"},

0 commit comments

Comments
 (0)