summaryrefslogtreecommitdiffstats
path: root/reg-tests/http-messaging/websocket.vtc
blob: aed55fe5153d1742d838a08ab3a30a0c5c78254e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# This reg-test is uses to test respect of the websocket protocol according to
# rfc6455.
#
# In particular, a request/response without a websocket key must be rejected by
# haproxy. Note that in the tested case (h1 on both sides), haproxy does not
# validate the key of the server but only checks its presence.
#
# For the case h2 client/h1 server, haproxy would add the key and validates it.
# However, there is no way to check this case quickly at the moment using vtest.

varnishtest "WebSocket test"

feature ignore_unknown_macro

#REQUIRE_VERSION=2.4

# valid websocket server
server s1 {
	rxreq
	expect req.method == "GET"
	expect req.http.connection == "upgrade"
	expect req.http.upgrade == "websocket"
	expect req.http.sec-websocket-key == "dGhlIHNhbXBsZSBub25jZQ=="

	txresp \
	  -status 101 \
	  -hdr "connection: upgrade" \
	  -hdr "upgrade: websocket" \
	  -hdr "sec-websocket-accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo="

        recv 4
	send "PONG"
} -start

# non-conformant server: no websocket key
server s2 {
	rxreq
	expect req.method == "GET"
	expect req.http.connection == "upgrade"
	expect req.http.upgrade == "websocket"

	txresp \
	  -status 101 \
	  -hdr "connection: upgrade" \
	  -hdr "upgrade: websocket"
} -start

# haproxy instance used as a server
# generate a http/1.1 websocket response with the valid key
haproxy hap_srv -conf {
	defaults
	mode http
	timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
	timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
	timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"

	listen fe1
	bind "fd@${fe1}"

	# reject if the request does not contains a websocket key
	acl ws_handshake hdr(sec-websocket-key) -m found
	http-request reject unless ws_handshake

	# return a valid websocket handshake response
	capture request header sec-websocket-key len 128
	http-request return status 200 hdr connection upgrade hdr upgrade websocket hdr sec-websocket-accept "%[capture.req.hdr(0),concat(258EAFA5-E914-47DA-95CA-C5AB0DC85B11,,),sha1,base64]"
	http-after-response set-status 101 if { status eq 200 }
} -start

# haproxy instance used as a server
# generate a http/1.1 websocket response with an invalid key
haproxy hap_srv_bad_key -conf {
	defaults
	mode http
	timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
	timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
	timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"

	listen fe1
	bind "fd@${fe1}"

	# reject if the request does not contains a websocket key
	acl ws_handshake hdr(sec-websocket-key) -m found
	http-request reject unless ws_handshake

	# return an invalid websocket handshake response
	capture request header sec-websocket-key len 128
	http-request return status 200 hdr connection upgrade hdr upgrade websocket hdr sec-websocket-accept "invalid_key"
	http-after-response set-status 101 if { status eq 200 }
} -start

haproxy hap -conf {
	defaults
	mode http
	timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
	timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
	timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"

	listen fe1
	bind "fd@${fe1}"
	server s1 ${s1_addr}:${s1_port}

	listen fe2
	bind "fd@${fe2}"
	server s2 ${s2_addr}:${s2_port}

	listen fe3
	bind "fd@${fe3}" proto h2
	server hap_srv ${hap_srv_fe1_addr}:${hap_srv_fe1_port}

	listen fe4
	bind "fd@${fe4}" proto h2
	server hap_srv_bad_key ${hap_srv_bad_key_fe1_addr}:${hap_srv_bad_key_fe1_port}
} -start

# standard request
client c1 -connect ${hap_fe1_sock} {
	txreq \
	  -req "GET" \
	  -url "/" \
	  -hdr "host: 127.0.0.1" \
	  -hdr "connection: upgrade" \
	  -hdr "upgrade: websocket" \
	  -hdr "sec-websocket-key: dGhlIHNhbXBsZSBub25jZQ=="
	rxresp
	expect resp.status == 101
	expect resp.http.connection == "upgrade"
	expect resp.http.upgrade == "websocket"
	expect resp.http.sec-websocket-accept == "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="

	send "PING"
	recv 4
} -run

# missing websocket key
client c2 -connect ${hap_fe1_sock} {
	txreq \
	  -req "GET" \
	  -url "/" \
	  -hdr "host: 127.0.0.1" \
	  -hdr "connection: upgrade" \
	  -hdr "upgrade: websocket"

	rxresp
	expect resp.status == 400
} -run

# missing key on server side
client c3 -connect ${hap_fe2_sock} {
	txreq \
	  -req "GET" \
	  -url "/" \
	  -hdr "host: 127.0.0.1" \
	  -hdr "connection: upgrade" \
	  -hdr "upgrade: websocket" \
	  -hdr "sec-websocket-key: dGhlIHNhbXBsZSBub25jZQ=="

	rxresp
	expect resp.status == 502
} -run

# connect with http/2 on a http/1.1 websocket server
# the key must be provided by haproxy
client c4 -connect ${hap_fe3_sock} {
	txpri
	stream 0 {
		txsettings
		rxsettings
		txsettings -ack
		rxsettings
		expect settings.ack == true
	} -run

	stream 1 {
		txreq \
		  -req "CONNECT" \
		  -scheme "http" \
		  -url "/" \
		  -hdr ":authority" "127.0.0.1" \
		  -hdr ":protocol" "websocket"

		rxhdrs
		expect resp.status == 200
	} -run
} -run

# connect with http/2 on a http/1.1 websocket server
# however, the server will respond with an invalid key
# haproxy is responsible to reject the request and returning a 502 to the client
client c5 -connect ${hap_fe4_sock} {
	txpri
	stream 0 {
		txsettings
		rxsettings
		txsettings -ack
		rxsettings
		expect settings.ack == true
	} -run

	stream 1 {
		txreq \
		  -req "CONNECT" \
		  -scheme "http" \
		  -url "/" \
		  -hdr ":authority" "127.0.0.1" \
		  -hdr ":protocol" "websocket"

		rxhdrs
		expect resp.status == 502
	} -run
} -run