Skip to content

Commit 866ec85

Browse files
authored
Merge pull request TooTallNate#1034 from marci4/Issue1008
2 parents 499b26f + f1802ae commit 866ec85

File tree

9 files changed

+272
-28
lines changed

9 files changed

+272
-28
lines changed

src/main/example/ChatServer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
import java.net.InetSocketAddress;
3030
import java.net.UnknownHostException;
3131
import java.nio.ByteBuffer;
32+
import java.util.Collections;
3233

3334
import org.java_websocket.WebSocket;
3435
import org.java_websocket.WebSocketImpl;
36+
import org.java_websocket.drafts.Draft;
37+
import org.java_websocket.drafts.Draft_6455;
3538
import org.java_websocket.framing.Framedata;
3639
import org.java_websocket.handshake.ClientHandshake;
40+
import org.java_websocket.protocols.IProtocol;
3741
import org.java_websocket.server.WebSocketServer;
3842

3943
/**
@@ -49,6 +53,10 @@ public ChatServer( InetSocketAddress address ) {
4953
super( address );
5054
}
5155

56+
public ChatServer(int port, Draft_6455 draft) {
57+
super( new InetSocketAddress( port ), Collections.<Draft>singletonList(draft));
58+
}
59+
5260
@Override
5361
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
5462
conn.send("Welcome to the server!"); //This method sends a message to the new client
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2010-2020 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.drafts.Draft_6455;
27+
import org.java_websocket.extensions.IExtension;
28+
import org.java_websocket.protocols.IProtocol;
29+
import org.java_websocket.protocols.Protocol;
30+
31+
import java.net.URI;
32+
import java.net.URISyntaxException;
33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
36+
/**
37+
* This example demonstrates how to use a specific Sec-WebSocket-Protocol for your connection.
38+
*/
39+
public class SecWebSocketProtocolServerExample {
40+
41+
public static void main( String[] args ) throws URISyntaxException {
42+
// This draft only allows you to use the specific Sec-WebSocket-Protocol without a fallback.
43+
Draft_6455 draft_ocppOnly = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList(new Protocol("ocpp2.0")));
44+
45+
// This draft allows the specific Sec-WebSocket-Protocol and also provides a fallback, if the other endpoint does not accept the specific Sec-WebSocket-Protocol
46+
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
47+
protocols.add(new Protocol("ocpp2.0"));
48+
protocols.add(new Protocol(""));
49+
Draft_6455 draft_ocppAndFallBack = new Draft_6455(Collections.<IExtension>emptyList(), protocols);
50+
51+
ChatServer chatServer = new ChatServer(8887, draft_ocppOnly);
52+
chatServer.start();
53+
}
54+
}

src/main/java/org/java_websocket/WebSocket.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.java_websocket.enums.ReadyState;
3535
import org.java_websocket.exceptions.WebsocketNotConnectedException;
3636
import org.java_websocket.framing.Framedata;
37+
import org.java_websocket.protocols.IProtocol;
3738

3839
import javax.net.ssl.SSLSession;
3940

@@ -224,4 +225,12 @@ public interface WebSocket {
224225
* @since 1.4.1
225226
*/
226227
SSLSession getSSLSession() throws IllegalArgumentException;
228+
229+
/**
230+
* Returns the used Sec-WebSocket-Protocol for this websocket connection
231+
* @return the Sec-WebSocket-Protocol or null, if no draft available
232+
* @throws IllegalArgumentException the underlying draft does not support a Sec-WebSocket-Protocol
233+
* @since 1.5.2
234+
*/
235+
IProtocol getProtocol();
227236
}

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.java_websocket.framing.Framedata;
3535
import org.java_websocket.framing.PingFrame;
3636
import org.java_websocket.handshake.*;
37+
import org.java_websocket.protocols.IProtocol;
3738
import org.java_websocket.server.WebSocketServer.WebSocketWorker;
3839
import org.java_websocket.util.Charsetfunctions;
3940

@@ -832,6 +833,15 @@ public SSLSession getSSLSession() {
832833
return ((ISSLChannel) channel).getSSLEngine().getSession();
833834
}
834835

836+
@Override
837+
public IProtocol getProtocol() {
838+
if (draft == null)
839+
return null;
840+
if (!(draft instanceof Draft_6455))
841+
throw new IllegalArgumentException("This draft does not support Sec-WebSocket-Protocol");
842+
return ((Draft_6455) draft).getProtocol();
843+
}
844+
835845
@Override
836846
public <T> void setAttachment(T attachment) {
837847
this.attachment = attachment;

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.java_websocket.handshake.HandshakeImpl1Client;
6060
import org.java_websocket.handshake.Handshakedata;
6161
import org.java_websocket.handshake.ServerHandshake;
62+
import org.java_websocket.protocols.IProtocol;
6263

6364
/**
6465
* A subclass must implement at least <var>onOpen</var>, <var>onClose</var>, and <var>onMessage</var> to be
@@ -910,6 +911,9 @@ public SSLSession getSSLSession() {
910911
return engine.getSSLSession();
911912
}
912913

914+
@Override
915+
public IProtocol getProtocol() { return engine.getProtocol();}
916+
913917
/**
914918
* Method to give some additional info for specific IOExceptions
915919
* @param e the IOException causing a eot.

src/main/java/org/java_websocket/protocols/Protocol.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public Protocol( String providedProtocol ) {
5656

5757
@Override
5858
public boolean acceptProvidedProtocol( String inputProtocolHeader ) {
59+
if ("".equals(providedProtocol)) {
60+
return true;
61+
}
5962
String protocolHeader = patternSpace.matcher(inputProtocolHeader).replaceAll("");
6063
String[] headers = patternComma.split(protocolHeader);
6164
for( String header : headers ) {

src/test/java/org/java_websocket/drafts/Draft_6455Test.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void testGetKnownExtensions() throws Exception {
132132

133133
@Test
134134
public void testGetProtocol() throws Exception {
135-
Draft_6455 draft_6455 = new Draft_6455();
135+
Draft_6455 draft_6455 = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>emptyList());
136136
assertNull( draft_6455.getProtocol() );
137137
draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension );
138138
assertNull( draft_6455.getProtocol() );
@@ -194,7 +194,7 @@ public void testToString() throws Exception {
194194
Draft_6455 draft_6455 = new Draft_6455();
195195
assertEquals( "Draft_6455 extension: DefaultExtension max frame size: 2147483647", draft_6455.toString() );
196196
draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension );
197-
assertEquals( "Draft_6455 extension: DefaultExtension max frame size: 2147483647", draft_6455.toString() );
197+
assertEquals( "Draft_6455 extension: DefaultExtension protocol: max frame size: 2147483647", draft_6455.toString() );
198198
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList( new Protocol( "chat" ) ) );
199199
assertEquals( "Draft_6455 extension: DefaultExtension max frame size: 2147483647", draft_6455.toString() );
200200
draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension );
@@ -223,15 +223,15 @@ public void testEquals() throws Exception {
223223
draft1.acceptHandshakeAsServer( handshakedataProtocolExtension );
224224
assertNotEquals( draft2, draft3 );
225225
assertNotEquals( draft0, draft2 );
226-
assertEquals( draft0, draft1 );
226+
assertNotEquals( draft0, draft1 );
227227
draft2 = draft2.copyInstance();
228228
draft1 = draft1.copyInstance();
229229
//unequal for draft draft2 due to a provided protocol
230230
draft2.acceptHandshakeAsServer( handshakedataProtocol );
231231
draft1.acceptHandshakeAsServer( handshakedataProtocol );
232232
assertNotEquals( draft2, draft3 );
233233
assertNotEquals( draft0, draft2 );
234-
assertEquals( draft0, draft1 );
234+
assertNotEquals( draft0, draft1 );
235235
draft2 = draft2.copyInstance();
236236
draft1 = draft1.copyInstance();
237237
//unequal for draft draft0 due to a provided protocol (no protocol)
@@ -297,14 +297,14 @@ public void testHashCode() throws Exception {
297297
public void acceptHandshakeAsServer() throws Exception {
298298
Draft_6455 draft_6455 = new Draft_6455();
299299
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
300-
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
300+
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
301301
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
302-
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
302+
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
303303
draft_6455 = new Draft_6455( new TestExtension() );
304304
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
305-
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
305+
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
306306
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
307-
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
307+
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
308308
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList( new Protocol( "chat" ) ) );
309309
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
310310
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
@@ -336,15 +336,15 @@ public void acceptHandshakeAsClient() throws Exception {
336336
response.put( "Sec-WebSocket-Accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" );
337337
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
338338
response.put( "Sec-WebSocket-Protocol", "chat" );
339-
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
339+
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
340340
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList( new Protocol( "chat" ) ) );
341341
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
342342
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
343343
protocols.add( new Protocol( "" ) );
344344
protocols.add( new Protocol( "chat" ) );
345345
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), protocols );
346346
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
347-
draft_6455 = new Draft_6455();
347+
draft_6455 =new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>emptyList());
348348
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
349349
protocols.clear();
350350
protocols.add( new Protocol( "chat3" ) );

0 commit comments

Comments
 (0)