org.eclipse.jetty.websocket.server.ServletWebSocketRequest Maven / Gradle / Ivy
The newest version!
//
// ========================================================================
// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.server;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
import org.eclipse.jetty.websocket.api.util.QuoteUtil;
public class ServletWebSocketRequest extends UpgradeRequest
{
private Map cookieMap;
private HttpServletRequest req;
public ServletWebSocketRequest(HttpServletRequest request)
{
super(request.getRequestURI());
this.req = request;
// Copy Request Line Details
setMethod(request.getMethod());
setHttpVersion(request.getProtocol());
// Copy Cookies
cookieMap = new HashMap();
for (Cookie cookie : request.getCookies())
{
cookieMap.put(cookie.getName(),cookie.getValue());
}
// Copy Headers
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements())
{
String name = headerNames.nextElement();
Enumeration valuesEnum = request.getHeaders(name);
List values = new ArrayList<>();
while (valuesEnum.hasMoreElements())
{
values.add(valuesEnum.nextElement());
}
setHeader(name,values);
}
// Parse Sub Protocols
Enumeration protocols = request.getHeaders("Sec-WebSocket-Protocol");
List subProtocols = new ArrayList<>();
String protocol = null;
while ((protocol == null) && (protocols != null) && protocols.hasMoreElements())
{
String candidate = protocols.nextElement();
for (String p : parseProtocols(candidate))
{
subProtocols.add(p);
}
}
setSubProtocols(subProtocols);
// Parse Extension Configurations
Enumeration e = request.getHeaders("Sec-WebSocket-Extensions");
while (e.hasMoreElements())
{
Iterator extTokenIter = QuoteUtil.splitAt(e.nextElement(),",");
while (extTokenIter.hasNext())
{
String extToken = extTokenIter.next();
ExtensionConfig config = ExtensionConfig.parse(extToken);
addExtensions(config);
}
}
}
protected String[] parseProtocols(String protocol)
{
if (protocol == null)
{
return new String[]
{ null };
}
protocol = protocol.trim();
if ((protocol == null) || (protocol.length() == 0))
{
return new String[]
{ null };
}
String[] passed = protocol.split("\\s*,\\s*");
String[] protocols = new String[passed.length + 1];
System.arraycopy(passed,0,protocols,0,passed.length);
return protocols;
}
public void setAttribute(String name, Object o)
{
this.req.setAttribute(name,o);
}
}