Skip to content

Commit cf55a49

Browse files
committed
Example how to reject a handshake as a server
1 parent 0ce902c commit cf55a49

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
import org.java_websocket.WebSocket;
27+
import org.java_websocket.WebSocketImpl;
28+
import org.java_websocket.drafts.Draft;
29+
import org.java_websocket.exceptions.InvalidDataException;
30+
import org.java_websocket.framing.CloseFrame;
31+
import org.java_websocket.handshake.ClientHandshake;
32+
import org.java_websocket.handshake.ServerHandshakeBuilder;
33+
34+
import java.io.BufferedReader;
35+
import java.io.IOException;
36+
import java.io.InputStreamReader;
37+
import java.net.InetSocketAddress;
38+
39+
/**
40+
* This example shows how to reject a handshake as a server from a client.
41+
*
42+
* For this you have to override onWebsocketHandshakeReceivedAsServer in your WebSocketServer class
43+
*/
44+
public class ServerRejectHandshakeExample extends ChatServer {
45+
46+
public ServerRejectHandshakeExample( int port ) {
47+
super( new InetSocketAddress( port ));
48+
}
49+
50+
@Override
51+
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException {
52+
ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
53+
//In this example we don't allow any resource descriptor ( "ws://localhost:8887/?roomid=1 will be rejected but ws://localhost:8887 is fine)
54+
if (! request.getResourceDescriptor().equals( "/" )) {
55+
throw new InvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
56+
}
57+
//If there are no cookies set reject it as well.
58+
if (!request.hasFieldValue( "Cookie" )) {
59+
throw new InvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
60+
}
61+
//If the cookie does not contain a specific value
62+
if (!request.getFieldValue( "Cookie" ).equals( "username=nemo" )) {
63+
throw new InvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
64+
}
65+
return builder;
66+
}
67+
68+
69+
public static void main( String[] args ) throws InterruptedException , IOException {
70+
WebSocketImpl.DEBUG = true;
71+
int port = 8887; // 843 flash policy port
72+
try {
73+
port = Integer.parseInt( args[ 0 ] );
74+
} catch ( Exception ex ) {
75+
}
76+
ServerRejectHandshakeExample s = new ServerRejectHandshakeExample( port );
77+
s.start();
78+
System.out.println( "Server started on port: " + s.getPort() );
79+
80+
BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) );
81+
while ( true ) {
82+
String in = sysin.readLine();
83+
s.broadcast( in );
84+
if( in.equals( "exit" ) ) {
85+
s.stop();
86+
break;
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)