View Javadoc
1   package com.ericsson.research.transport.ws.spi;
2   
3   /*
4    * ##_BEGIN_LICENSE_##
5    * Transport Abstraction Package (trap)
6    * ----------
7    * Copyright (C) 2014 Ericsson AB
8    * ----------
9    * Redistribution and use in source and binary forms, with or without modification,
10   * are permitted provided that the following conditions are met:
11   * 
12   * 1. Redistributions of source code must retain the above copyright notice, this
13   *    list of conditions and the following disclaimer.
14   * 
15   * 2. Redistributions in binary form must reproduce the above copyright notice,
16   *    this list of conditions and the following disclaimer in the documentation
17   *    and/or other materials provided with the distribution.
18   * 
19   * 3. Neither the name of the Ericsson AB nor the names of its contributors
20   *    may be used to endorse or promote products derived from this software without
21   *    specific prior written permission.
22   * 
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32   * OF THE POSSIBILITY OF SUCH DAMAGE.
33   * ##_END_LICENSE_##
34   */
35  
36  import java.io.IOException;
37  import java.io.OutputStream;
38  import java.util.Arrays;
39  import java.util.Random;
40  
41  import com.ericsson.research.transport.ws.WSException;
42  import com.ericsson.research.trap.utils.Base64;
43  
44  public class WSHybi10Handshake extends WSAbstractHandshake implements WSConstants
45  {
46      
47      protected static final String SERVER_PREAMBLE = HTTP11 + " 101 Switching Protocols";
48      protected String              key;
49      protected static final Random random          = new Random();
50      
51      public WSHybi10Handshake(WSAbstractProtocol protocol)
52      {
53          super(protocol);
54      }
55      
56      public void sendHandshake(OutputStream os) throws IOException
57      {
58          StringBuffer h = new StringBuffer();
59          if (this.protocol.client)
60          {
61              h.append(GET);
62              h.append(" ");
63              h.append(this.protocol.resource);
64              h.append(" ");
65              h.append(HTTP11);
66              h.append(WSHixie75Handshake.COMMON_PART);
67              h.append(HOST_HEADER);
68              h.append(COLON_SPACE);
69              h.append(this.protocol.host);
70              if ((this.protocol.securityContext == null && this.protocol.port != 80) || (this.protocol.securityContext != null && this.protocol.port != 443))
71              {
72                  h.append(":");
73                  h.append(this.protocol.port);
74              }
75              h.append(CRLF);
76              h.append(SEC_WEBSOCKET_ORIGIN_HEADER);
77              h.append(COLON_SPACE);
78              h.append(this.protocol.origin);
79              h.append(CRLF);
80              h.append(SEC_WEBSOCKET_VERSION_HEADER);
81              h.append(COLON_SPACE);
82              h.append(8);
83              h.append(CRLF);
84              h.append(SEC_WEBSOCKET_KEY_HEADER);
85              h.append(COLON_SPACE);
86              byte[] randomBytes = new byte[16];
87              random.nextBytes(randomBytes);
88              this.key = new String(Base64.encode(randomBytes));
89              h.append(this.key);
90              h.append(CRLFCRLF);
91          }
92          else
93          {
94              h.append(SERVER_PREAMBLE);
95              h.append(WSHixie75Handshake.COMMON_PART);
96              h.append(SEC_WEBSOCKET_ACCEPT_HEADER);
97              h.append(COLON_SPACE);
98              String nounceAndGUID = this.key + GUID;
99              try
100             {
101                 byte[] sha1 = this.computeSHA1(nounceAndGUID);
102                 h.append(Base64.encode(sha1));
103                 h.append(CRLFCRLF);
104             }
105             catch (WSException e)
106             {
107                 throw new IOException(e);
108             }
109         }
110         synchronized (os)
111         {
112             os.write(h.toString().getBytes(ISO_8859_1));
113             os.flush();
114         }
115     }
116     
117     public void headersRead() throws WSException
118     {
119         if (this.protocol.client)
120         {
121             byte[] expected = this.computeSHA1(this.key + GUID);
122             if (this.getHeader(SEC_WEBSOCKET_ACCEPT_HEADER) == null)
123                 throw new WSException("Missing " + SEC_WEBSOCKET_ACCEPT_HEADER + " header");
124             byte[] actual = Base64.decode(this.getHeader(SEC_WEBSOCKET_ACCEPT_HEADER));
125             if (!Arrays.equals(expected, actual))
126                 throw new WSException("Failed to validate " + SEC_WEBSOCKET_ACCEPT_HEADER + " header value");
127             this.protocol.protocol = this.getHeader(SEC_WEBSOCKET_PROTOCOL_HEADER);
128         }
129         else
130         {
131             if (this.getHeader(HOST_HEADER) == null)
132                 throw new WSException("Missing " + HOST_HEADER + " header");
133             if (!"8".equals(this.getHeader(SEC_WEBSOCKET_VERSION_HEADER)))
134                 throw new WSException("Unsupported version " + this.getHeader(SEC_WEBSOCKET_VERSION_HEADER));
135             this.key = this.getHeader(SEC_WEBSOCKET_KEY_HEADER);
136             if (this.key == null)
137                 throw new WSException("Missing " + SEC_WEBSOCKET_KEY_HEADER + " header");
138         }
139     }
140     
141 }