1//
2//  ========================================================================
3//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4//  ------------------------------------------------------------------------
5//  All rights reserved. This program and the accompanying materials
6//  are made available under the terms of the Eclipse Public License v1.0
7//  and Apache License v2.0 which accompanies this distribution.
8//
9//      The Eclipse Public License is available at
10//      http://www.eclipse.org/legal/epl-v10.html
11//
12//      The Apache License v2.0 is available at
13//      http://www.opensource.org/licenses/apache2.0.php
14//
15//  You may elect to redistribute this code under either of these licenses.
16//  ========================================================================
17//
18
19package org.eclipse.jetty.websocket;
20
21import java.io.IOException;
22import javax.servlet.http.HttpServletRequest;
23import javax.servlet.http.HttpServletResponse;
24
25import org.eclipse.jetty.http.HttpURI;
26import org.eclipse.jetty.io.EndPoint;
27import org.eclipse.jetty.util.QuotedStringTokenizer;
28
29public class WebSocketServletConnectionD00 extends WebSocketConnectionD00 implements WebSocketServletConnection
30{
31    private final WebSocketFactory factory;
32
33    public WebSocketServletConnectionD00(WebSocketFactory factory, WebSocket websocket, EndPoint endpoint, WebSocketBuffers buffers, long timestamp, int maxIdleTime, String protocol)
34            throws IOException
35    {
36        super(websocket,endpoint,buffers,timestamp,maxIdleTime,protocol);
37        this.factory = factory;
38    }
39
40    public void handshake(HttpServletRequest request, HttpServletResponse response, String subprotocol) throws IOException
41    {
42        String uri = request.getRequestURI();
43        String query = request.getQueryString();
44        if (query != null && query.length() > 0)
45        {
46            uri += "?" + query;
47        }
48        uri = new HttpURI(uri).toString();
49        String host = request.getHeader("Host");
50
51        String origin = request.getHeader("Sec-WebSocket-Origin");
52        if (origin == null)
53        {
54            origin = request.getHeader("Origin");
55        }
56        if (origin != null)
57        {
58            origin = QuotedStringTokenizer.quoteIfNeeded(origin,"\r\n");
59        }
60
61        String key1 = request.getHeader("Sec-WebSocket-Key1");
62
63        if (key1 != null)
64        {
65            String key2 = request.getHeader("Sec-WebSocket-Key2");
66            setHixieKeys(key1,key2);
67
68            response.setHeader("Upgrade","WebSocket");
69            response.addHeader("Connection","Upgrade");
70            if (origin != null)
71            {
72                response.addHeader("Sec-WebSocket-Origin",origin);
73            }
74            response.addHeader("Sec-WebSocket-Location",(request.isSecure()?"wss://":"ws://") + host + uri);
75            if (subprotocol != null)
76            {
77                response.addHeader("Sec-WebSocket-Protocol",subprotocol);
78            }
79            response.sendError(101, "WebSocket Protocol Handshake");
80        }
81        else
82        {
83            response.setHeader("Upgrade","WebSocket");
84            response.addHeader("Connection","Upgrade");
85            response.addHeader("WebSocket-Origin",origin);
86            response.addHeader("WebSocket-Location",(request.isSecure()?"wss://":"ws://") + host + uri);
87            if (subprotocol != null)
88            {
89                response.addHeader("WebSocket-Protocol",subprotocol);
90            }
91            response.sendError(101,"Web Socket Protocol Handshake");
92            response.flushBuffer();
93
94            onFrameHandshake();
95            onWebsocketOpen();
96        }
97    }
98
99    @Override
100    public void onClose()
101    {
102        super.onClose();
103        factory.removeConnection(this);
104    }
105}
106