Skip to content

Commit 58cf95e

Browse files
authored
Merge pull request TooTallNate#556 from marci4/master
Example how to reject a handshake as a server
2 parents 4d9f9fe + cf55a49 commit 58cf95e

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
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+
}

src/main/java/org/java_websocket/drafts/Draft_6455.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Draft_6455 extends Draft {
5050
/**
5151
* Attribute for the used extension in this draft
5252
*/
53-
private IExtension extension;
53+
private IExtension extension = new DefaultExtension();
5454

5555
/**
5656
* Attribute for all available extension in this draft
@@ -104,8 +104,7 @@ public Draft_6455( List<IExtension> inputExtensions ) {
104104
knownExtensions.addAll( inputExtensions );
105105
//We always add the DefaultExtension to implement the normal RFC 6455 specification
106106
if( !hasDefault ) {
107-
DefaultExtension defaultExtension = new DefaultExtension();
108-
knownExtensions.add( this.knownExtensions.size(), defaultExtension );
107+
knownExtensions.add( this.knownExtensions.size(), extension );
109108
}
110109
}
111110

@@ -331,8 +330,8 @@ public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteExce
331330
frame.setRSV3( rsv3 );
332331
payload.flip();
333332
frame.setPayload( payload );
334-
getExtension().isFrameValid( frame );
335-
getExtension().decodeFrame( frame );
333+
getExtension().isFrameValid(frame);
334+
getExtension().decodeFrame(frame);
336335
if( WebSocketImpl.DEBUG )
337336
System.out.println( "afterDecoding(" + frame.getPayloadData().remaining() + "): {" + ( frame.getPayloadData().remaining() > 1000 ? "too big to display" : new String( frame.getPayloadData().array() ) ) + "}" );
338337
frame.isValid();
@@ -425,7 +424,7 @@ public void reset() {
425424
if( extension != null ) {
426425
extension.reset();
427426
}
428-
extension = null;
427+
extension = new DefaultExtension();
429428
}
430429

431430
/**

0 commit comments

Comments
 (0)