/
ClientUpgradeRequest.java
425 lines (385 loc) · 10.9 KB
/
ClientUpgradeRequest.java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.client;
import java.net.HttpCookie;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.websocket.api.ExtensionConfig;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
/**
* Client based UpgradeRequest API
*/
public final class ClientUpgradeRequest implements UpgradeRequest
{
private final List<String> subProtocols = new ArrayList<>(1);
private final List<ExtensionConfig> extensions = new ArrayList<>(1);
private final List<HttpCookie> cookies = new ArrayList<>(1);
private final Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private final URI requestURI;
private final String host;
private long timeout;
public ClientUpgradeRequest()
{
/* anonymous, no requestURI, upgrade request */
this.requestURI = null;
this.host = null;
}
@Deprecated
public ClientUpgradeRequest(URI uri)
{
this.requestURI = uri;
String scheme = uri.getScheme();
if (!HttpScheme.WS.is(scheme) && !HttpScheme.WSS.is(scheme))
throw new IllegalArgumentException("URI scheme must be 'ws' or 'wss'");
this.host = this.requestURI.getHost();
}
@Override
public List<HttpCookie> getCookies()
{
return cookies;
}
@Override
public List<ExtensionConfig> getExtensions()
{
return extensions;
}
@Override
public String getHeader(String name)
{
List<String> values = headers.get(name);
return joinValues(values);
}
@Override
public int getHeaderInt(String name)
{
List<String> values = headers.get(name);
// no value list
if (values == null)
{
return -1;
}
int size = values.size();
// empty value list
if (size <= 0)
{
return -1;
}
// simple return
if (size == 1)
{
return Integer.parseInt(values.get(0));
}
throw new NumberFormatException("Cannot convert multi-value header into int");
}
@Override
public Map<String, List<String>> getHeaders()
{
return headers;
}
@Override
public List<String> getHeaders(String name)
{
return headers.get(name);
}
@Override
public String getHost()
{
return host;
}
@Override
public String getHttpVersion()
{
throw new UnsupportedOperationException("HttpVersion not available on ClientUpgradeRequest");
}
@Override
public String getMethod()
{
throw new UnsupportedOperationException("Method not available on ClientUpgradeRequest");
}
@Override
public String getOrigin()
{
return getHeader(HttpHeader.ORIGIN.name());
}
@Override
public Map<String, List<String>> getParameterMap()
{
return Collections.emptyMap();
}
@Override
public String getProtocolVersion()
{
String version = getHeader("Sec-WebSocket-Version");
return Objects.requireNonNullElse(version, "13");
}
@Override
public String getQueryString()
{
return requestURI == null ? null : requestURI.getQuery();
}
@Override
public URI getRequestURI()
{
return requestURI;
}
@Override
public List<String> getSubProtocols()
{
return subProtocols;
}
@Override
public Principal getUserPrincipal()
{
throw new UnsupportedOperationException("User Principal not available on Client request");
}
@Override
public boolean hasSubProtocol(String test)
{
for (String protocol : subProtocols)
{
if (protocol.equalsIgnoreCase(test))
{
return true;
}
}
return false;
}
@Override
public boolean isSecure()
{
throw new UnsupportedOperationException("Request.isSecure not available on Client request");
}
/**
* Add WebSocket Extension Configuration(s) to Upgrade Request.
* <p>
* This is merely the list of requested Extensions to use, see {@link UpgradeResponse#getExtensions()} for what was
* negotiated
*
* @param configs the configuration(s) to add
*/
public void addExtensions(ExtensionConfig... configs)
{
Collections.addAll(extensions, configs);
}
/**
* Add WebSocket Extension Configuration(s) to request
* <p>
* This is merely the list of requested Extensions to use, see {@link UpgradeResponse#getExtensions()} for what was
* negotiated
*
* @param configs the configuration(s) to add
*/
public void addExtensions(String... configs)
{
for (String config : configs)
{
extensions.add(ExtensionConfig.parse(config));
}
}
/**
* Set the list of Cookies on the request
*
* @param cookies the cookies to use
*/
public void setCookies(List<HttpCookie> cookies)
{
this.cookies.clear();
if (cookies != null && !cookies.isEmpty())
{
this.cookies.addAll(cookies);
}
}
/**
* Set the list of WebSocket Extension configurations on the request.
*
* @param configs the list of extension configurations
*/
public void setExtensions(List<ExtensionConfig> configs)
{
this.extensions.clear();
if (configs != null)
{
this.extensions.addAll(configs);
}
}
/**
* Set a specific header with multi-value field
* <p>
* Overrides any previous value for this named header
*
* @param name the name of the header
* @param values the multi-value field
*/
public void setHeader(String name, List<String> values)
{
headers.put(name, values);
}
/**
* Set a specific header value
* <p>
* Overrides any previous value for this named header
*
* @param name the header to set
* @param value the value to set it to
*/
public void setHeader(String name, String value)
{
List<String> values = new ArrayList<>();
values.add(value);
setHeader(name, values);
}
/**
* Sets multiple headers on the request.
* <p>
* Only sets those headers provided, does not remove
* headers that exist on request and are not provided in the
* parameter for this method.
* <p>
* Convenience method vs calling {@link #setHeader(String, List)} multiple times.
*
* @param headers the headers to set
*/
public void setHeaders(Map<String, List<String>> headers)
{
this.headers.clear();
for (Map.Entry<String, List<String>> entry : headers.entrySet())
{
String name = entry.getKey();
List<String> values = entry.getValue();
setHeader(name, values);
}
}
/**
* Set the offered WebSocket Sub-Protocol list.
*
* @param protocols the offered sub-protocol list
*/
public void setSubProtocols(List<String> protocols)
{
this.subProtocols.clear();
if (protocols != null)
{
this.subProtocols.addAll(protocols);
}
}
/**
* Set the offered WebSocket Sub-Protocol list.
*
* @param protocols the offered sub-protocol list
*/
public void setSubProtocols(String... protocols)
{
subProtocols.clear();
Collections.addAll(subProtocols, protocols);
}
/**
* @param timeout the total timeout for the request/response conversation of the WebSocket handshake;
* use zero or a negative value to disable the timeout
* @param unit the timeout unit
*/
public void setTimeout(long timeout, TimeUnit unit)
{
this.timeout = unit.toMillis(timeout);
}
/**
* @return the total timeout for this request, in milliseconds;
* zero or negative if the timeout is disabled
*/
public long getTimeout()
{
return timeout;
}
/**
* ABNF from RFC 2616, RFC 822, and RFC 6455 specified characters requiring quoting.
*/
public static final String ABNF_REQUIRED_QUOTING = "\"'\\\n\r\t\f\b%+ ;=";
public static String joinValues(List<String> values)
{
// no value list
if (values == null)
{
return null;
}
int size = values.size();
// empty value list
if (size <= 0)
{
return null;
}
// simple return
if (size == 1)
{
return values.get(0);
}
// join it with commas
boolean needsDelim = false;
StringBuilder ret = new StringBuilder();
for (String value : values)
{
if (needsDelim)
{
ret.append(", ");
}
quoteIfNeeded(ret, value, ABNF_REQUIRED_QUOTING);
needsDelim = true;
}
return ret.toString();
}
/**
* Append into buf the provided string, adding quotes if needed.
* <p>
* Quoting is determined if any of the characters in the {@code delim} are found in the input {@code str}.
*
* @param buf the buffer to append to
* @param str the string to possibly quote
* @param delim the delimiter characters that will trigger automatic quoting
*/
private static void quoteIfNeeded(StringBuilder buf, String str, String delim)
{
if (str == null)
{
return;
}
// check for delimiters in input string
int len = str.length();
if (len == 0)
{
return;
}
int ch;
for (int i = 0; i < len; i++)
{
ch = str.codePointAt(i);
if (delim.indexOf(ch) >= 0)
{
// found a delimiter codepoint. we need to quote it.
buf.append('"');
buf.append(str);
buf.append('"');
return;
}
}
// no special delimiters used, no quote needed.
buf.append(str);
}
}