View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
19  package org.eclipse.jetty.websocket.server.mux;
20  
21  import java.io.IOException;
22  
23  import org.eclipse.jetty.http.HttpField;
24  import org.eclipse.jetty.http.HttpHeader;
25  import org.eclipse.jetty.http.HttpMethod;
26  import org.eclipse.jetty.http.HttpVersion;
27  import org.eclipse.jetty.io.EndPoint;
28  import org.eclipse.jetty.server.Connector;
29  import org.eclipse.jetty.server.HttpConfiguration;
30  import org.eclipse.jetty.util.BufferUtil;
31  import org.eclipse.jetty.websocket.api.UpgradeRequest;
32  import org.eclipse.jetty.websocket.api.UpgradeResponse;
33  import org.eclipse.jetty.websocket.common.extensions.mux.MuxChannel;
34  import org.eclipse.jetty.websocket.common.extensions.mux.MuxException;
35  import org.eclipse.jetty.websocket.common.extensions.mux.Muxer;
36  import org.eclipse.jetty.websocket.common.extensions.mux.add.MuxAddServer;
37  
38  /**
39   * Handler for incoming MuxAddChannel requests.
40   */
41  public class MuxAddHandler implements MuxAddServer
42  {
43      /** Represents physical connector */
44      private Connector connector;
45  
46      /** Used for local address */
47      private EndPoint endPoint;
48  
49      /** The original request handshake */
50      private UpgradeRequest baseHandshakeRequest;
51  
52      /** The original request handshake */
53      private UpgradeResponse baseHandshakeResponse;
54  
55      private int maximumHeaderSize = 32 * 1024;
56  
57      @Override
58      public UpgradeRequest getPhysicalHandshakeRequest()
59      {
60          // TODO Auto-generated method stub
61          return null;
62      }
63  
64      @Override
65      public UpgradeResponse getPhysicalHandshakeResponse()
66      {
67          // TODO Auto-generated method stub
68          return null;
69      }
70  
71      /**
72       * An incoming MuxAddChannel request.
73       * 
74       * @param the
75       *            channel this request should be bound to
76       * @param request
77       *            the incoming request headers (complete and merged if delta encoded)
78       * @return the outgoing response headers
79       */
80      @Override
81      public void handshake(Muxer muxer, MuxChannel channel, UpgradeRequest request) throws MuxException, IOException
82      {
83          // Need to call into HttpChannel to get the websocket properly setup.
84          HttpTransportOverMux transport = new HttpTransportOverMux(muxer,channel);
85          EmptyHttpInput input = new EmptyHttpInput();
86          HttpConfiguration configuration = new HttpConfiguration();
87  
88          HttpChannelOverMux httpChannel = new HttpChannelOverMux(//
89                  connector,configuration,endPoint,transport,input);
90  
91          HttpMethod method = HttpMethod.fromString(request.getMethod());
92          HttpVersion version = HttpVersion.fromString(request.getHttpVersion());
93          httpChannel.startRequest(method,request.getMethod(),BufferUtil.toBuffer(request.getRequestURI().toASCIIString()),version);
94  
95          for (String headerName : request.getHeaders().keySet())
96          {
97              HttpHeader header = HttpHeader.CACHE.getBest(headerName.getBytes(),0,headerName.length());
98              for (String value : request.getHeaders().get(headerName))
99              {
100                 httpChannel.parsedHeader(new HttpField(header,value));
101             }
102         }
103 
104         httpChannel.headerComplete();
105         httpChannel.messageComplete();
106         httpChannel.run(); // calls into server for appropriate resource
107 
108         // TODO: what's in request handshake is not enough to process the request.
109         // like a partial http request. (consider this a AddChannelRequest failure)
110         throw new MuxException("Not a valid request");
111     }
112 }