1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.api;
20
21 import java.net.HttpCookie;
22 import java.net.URI;
23 import java.security.Principal;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.TreeMap;
30
31 import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
32 import org.eclipse.jetty.websocket.api.util.QuoteUtil;
33
34 public class UpgradeRequest
35 {
36 private URI requestURI;
37 private List<String> subProtocols = new ArrayList<>();
38 private List<ExtensionConfig> extensions = new ArrayList<>();
39 private List<HttpCookie> cookies = new ArrayList<>();
40 private Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
41 private Map<String, List<String>> parameters = new HashMap<>();
42 private Object session;
43 private String httpVersion;
44 private String method;
45 private String host;
46 private boolean secure = false;
47
48 protected UpgradeRequest()
49 {
50
51 }
52
53 public UpgradeRequest(String requestURI)
54 {
55 this(URI.create(requestURI));
56 }
57
58 public UpgradeRequest(URI requestURI)
59 {
60 this();
61 setRequestURI(requestURI);
62 }
63
64 public void addExtensions(ExtensionConfig... configs)
65 {
66 for (ExtensionConfig config : configs)
67 {
68 extensions.add(config);
69 }
70 }
71
72 public void addExtensions(String... configs)
73 {
74 for (String config : configs)
75 {
76 extensions.add(ExtensionConfig.parse(config));
77 }
78 }
79
80 public void clearHeaders()
81 {
82 headers.clear();
83 }
84
85 public List<HttpCookie> getCookies()
86 {
87 return cookies;
88 }
89
90 public List<ExtensionConfig> getExtensions()
91 {
92 return extensions;
93 }
94
95 public String getHeader(String name)
96 {
97 List<String> values = headers.get(name);
98
99 if (values == null)
100 {
101 return null;
102 }
103 int size = values.size();
104
105 if (size <= 0)
106 {
107 return null;
108 }
109
110 if (size == 1)
111 {
112 return values.get(0);
113 }
114
115 boolean needsDelim = false;
116 StringBuilder ret = new StringBuilder();
117 for (String value : values)
118 {
119 if (needsDelim)
120 {
121 ret.append(", ");
122 }
123 QuoteUtil.quoteIfNeeded(ret,value,QuoteUtil.ABNF_REQUIRED_QUOTING);
124 needsDelim = true;
125 }
126 return ret.toString();
127 }
128
129 public int getHeaderInt(String name)
130 {
131 List<String> values = headers.get(name);
132
133 if (values == null)
134 {
135 return -1;
136 }
137 int size = values.size();
138
139 if (size <= 0)
140 {
141 return -1;
142 }
143
144 if (size == 1)
145 {
146 return Integer.parseInt(values.get(0));
147 }
148 throw new NumberFormatException("Cannot convert multi-value header into int");
149 }
150
151 public Map<String, List<String>> getHeaders()
152 {
153 return headers;
154 }
155
156 public List<String> getHeaders(String name)
157 {
158 return headers.get(name);
159 }
160
161 public String getHost()
162 {
163 return host;
164 }
165
166 public String getHttpVersion()
167 {
168 return httpVersion;
169 }
170
171 public String getMethod()
172 {
173 return method;
174 }
175
176 public String getOrigin()
177 {
178 return getHeader("Origin");
179 }
180
181
182
183
184
185
186 public Map<String, List<String>> getParameterMap()
187 {
188 return Collections.unmodifiableMap(parameters);
189 }
190
191 public String getProtocolVersion()
192 {
193 String version = getHeader("Sec-WebSocket-Version");
194 if (version == null)
195 {
196 return "13";
197 }
198 return version;
199 }
200
201 public String getQueryString()
202 {
203 return requestURI.getQuery();
204 }
205
206 public URI getRequestURI()
207 {
208 return requestURI;
209 }
210
211
212
213
214
215
216
217
218 public Object getSession()
219 {
220 return session;
221 }
222
223 public List<String> getSubProtocols()
224 {
225 return subProtocols;
226 }
227
228
229
230
231
232
233
234
235 public Principal getUserPrincipal()
236 {
237
238 return null;
239 }
240
241 public boolean hasSubProtocol(String test)
242 {
243 for (String protocol : subProtocols)
244 {
245 if (protocol.equalsIgnoreCase(test))
246 {
247 return true;
248 }
249 }
250 return false;
251 }
252
253 public boolean isOrigin(String test)
254 {
255 return test.equalsIgnoreCase(getOrigin());
256 }
257
258 public boolean isSecure()
259 {
260 return secure;
261 }
262
263 public void setCookies(List<HttpCookie> cookies)
264 {
265 this.cookies = cookies;
266 }
267
268 public void setHeader(String name, List<String> values)
269 {
270 headers.put(name,values);
271 }
272
273 public void setHeader(String name, String value)
274 {
275 List<String> values = new ArrayList<>();
276 values.add(value);
277 setHeader(name,values);
278 }
279
280 public void setHeaders(Map<String, List<String>> headers)
281 {
282 clearHeaders();
283
284 for (Map.Entry<String, List<String>> entry : headers.entrySet())
285 {
286 String name = entry.getKey();
287 List<String> values = entry.getValue();
288 setHeader(name,values);
289 }
290 }
291
292 public void setHttpVersion(String httpVersion)
293 {
294 this.httpVersion = httpVersion;
295 }
296
297 public void setMethod(String method)
298 {
299 this.method = method;
300 }
301
302 protected void setParameterMap(Map<String, List<String>> parameters)
303 {
304 this.parameters.clear();
305 this.parameters.putAll(parameters);
306 }
307
308 public void setRequestURI(URI uri)
309 {
310 this.requestURI = uri;
311 String scheme = uri.getScheme();
312 if ("ws".equalsIgnoreCase(scheme))
313 {
314 secure = false;
315 }
316 else if ("wss".equalsIgnoreCase(scheme))
317 {
318 secure = true;
319 }
320 else
321 {
322 throw new IllegalArgumentException("URI scheme must be 'ws' or 'wss'");
323 }
324 this.host = this.requestURI.getHost();
325 this.parameters.clear();
326 }
327
328 public void setSession(Object session)
329 {
330 this.session = session;
331 }
332
333 public void setSubProtocols(List<String> subProtocols)
334 {
335 this.subProtocols.clear();
336 if (subProtocols != null)
337 {
338 this.subProtocols.addAll(subProtocols);
339 }
340 }
341
342
343
344
345
346
347
348 public void setSubProtocols(String... protocols)
349 {
350 this.subProtocols.clear();
351 for (String protocol : protocols)
352 {
353 this.subProtocols.add(protocol);
354 }
355 }
356 }