Skip to content

Commit edeb6fe

Browse files
committed
Make Protocols less strict
1 parent bba4914 commit edeb6fe

File tree

8 files changed

+262
-18
lines changed

8 files changed

+262
-18
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
@@ -913,6 +914,9 @@ public SSLSession getSSLSession() {
913914
return engine.getSSLSession();
914915
}
915916

917+
@Override
918+
public IProtocol getProtocol() { return engine.getProtocol();}
919+
916920
/**
917921
* Method to give some additional info for specific IOExceptions
918922
* @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 ) {

0 commit comments

Comments
 (0)