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  
39  import com.ericsson.research.transport.ws.WSException;
40  
41  public class WSHixie75Handshake extends WSAbstractHandshake implements WSConstants {
42  
43  	protected static final String SERVER_PREAMBLE = HTTP11 + " 101 Web Socket Protocol Handshake";
44  	protected static final String COMMON_PART = CRLF+UPGRADE_HEADER+COLON_SPACE+WEBSOCKET_VALUE+CRLF+CONNECTION_HEADER+COLON_SPACE+UPGRADE_VALUE+CRLF;
45  	
46  	public WSHixie75Handshake(WSAbstractProtocol protocol) {
47  		super(protocol);
48  	}
49  	
50  	public void sendHandshake(OutputStream os) throws IOException {
51  		StringBuffer h = new StringBuffer();
52  		if(protocol.client) {
53  			h.append(GET);
54  			h.append(" ");
55  			h.append(protocol.resource);
56  			h.append(" ");
57  			h.append(HTTP11);
58  			h.append(COMMON_PART);
59  			h.append(HOST_HEADER);
60  			h.append(COLON_SPACE);
61  			h.append(protocol.host);
62  			if ((protocol.securityContext==null && protocol.port!=80) || (protocol.securityContext!=null && protocol.port!=443)) {
63  				h.append(":");
64  				h.append(protocol.port);
65  			}
66  			h.append(CRLF);
67  			h.append(ORIGIN_HEADER);
68  			h.append(COLON_SPACE);
69  			h.append(protocol.origin);
70  			h.append(CRLFCRLF);
71  		} else {
72  			h.append(SERVER_PREAMBLE);
73  			h.append(COMMON_PART);
74  			h.append(WEBSOCKET_ORIGIN_HEADER);
75  			h.append(COLON_SPACE);
76  			h.append(protocol.origin);
77  			h.append(CRLF);
78  			h.append(WEBSOCKET_LOCATION_HEADER);
79  			h.append(COLON_SPACE);
80  			h.append(protocol.location);
81  			h.append(CRLFCRLF);
82  		}
83  		synchronized(os) {
84  			os.write(h.toString().getBytes(ISO_8859_1));
85  			os.flush();
86  		}
87  	}
88  
89  	public void headersRead() throws WSException {
90  		if(protocol.client) {
91  			if(!protocol.origin.equals(getHeader(WEBSOCKET_ORIGIN_HEADER)))
92  				throw new WSException("Failed to match origin ("+protocol.origin+" != "+getHeader(WEBSOCKET_ORIGIN_HEADER)+")");
93  			StringBuffer location = new StringBuffer();
94  			if(protocol.securityContext!=null)
95  				location.append(WSS_SCHEMA);
96  			else
97  				location.append(WS_SCHEMA);
98  			location.append(protocol.host);
99  			if((protocol.securityContext==null && protocol.port != 80) || (protocol.securityContext!=null && protocol.port != 443)) {
100 				location.append(":");
101 				location.append(protocol.port);
102 			}
103 			location.append(protocol.resource);
104 			if (!location.toString().equals(getHeader(WEBSOCKET_LOCATION_HEADER)))
105 				throw new WSException("Failed to match location ("+location+" != "+getHeader(WEBSOCKET_LOCATION_HEADER)+")");
106 		} else {
107 			protocol.origin = getHeader(ORIGIN_HEADER);
108 			StringBuffer loc = new StringBuffer();
109 			if(protocol.securityContext!=null)
110 				loc.append(WSS_SCHEMA);
111 			else
112 				loc.append(WS_SCHEMA);
113 			loc.append(getHeader(HOST_HEADER));
114 			loc.append(protocol.resource);
115 			protocol.location = loc.toString();
116 		}
117 	}
118 
119 }