Skip to content

Commit fed2f7e

Browse files
authored
Merge pull request TooTallNate#497 from marci4/master
Added new AutobahnServerTest for SSL and fixed errors in closeframe
2 parents ed37bb3 + 2c72264 commit fed2f7e

File tree

2 files changed

+148
-29
lines changed

2 files changed

+148
-29
lines changed

src/main/java/org/java_websocket/framing/CloseFrame.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ public String toString() {
176176
@Override
177177
public void isValid() throws InvalidDataException {
178178
super.isValid();
179+
if (code == CloseFrame.NO_UTF8 && reason == null) {
180+
throw new InvalidDataException( CloseFrame.NO_UTF8 );
181+
}
179182
if (code == CloseFrame.NOCODE && 0 < reason.length()) {
180183
throw new InvalidDataException(PROTOCOL_ERROR, "A close frame must have a closecode if it has a reason");
181184
}
@@ -190,35 +193,38 @@ public void isValid() throws InvalidDataException {
190193

191194
@Override
192195
public void setPayload(ByteBuffer payload) {
193-
code = CloseFrame.NOCODE;
194-
payload.mark();
195-
if( payload.remaining() >= 2 ) {
196-
ByteBuffer bb = ByteBuffer.allocate( 4 );
197-
bb.position( 2 );
198-
bb.putShort( payload.getShort() );
199-
bb.position( 0 );
200-
code = bb.getInt();
201-
}
202-
payload.reset();
203-
try {
204-
if (code == CloseFrame.NOCODE) {
205-
reason = Charsetfunctions.stringUtf8(payload);
206-
} else {
207-
ByteBuffer b = super.getPayloadData();
208-
int mark = b.position();// because stringUtf8 also creates a mark
209-
try {
210-
b.position(b.position() + 2);
211-
reason = Charsetfunctions.stringUtf8(b);
212-
} catch (IllegalArgumentException e) {
213-
throw new InvalidFrameException(e);
214-
} finally {
215-
b.position(mark);
216-
}
217-
}
218-
} catch (InvalidDataException e) {
219-
reason = "";
220-
}
221-
}
196+
code = CloseFrame.NOCODE;
197+
reason = "";
198+
payload.mark();
199+
if( payload.remaining() == 0 ) {
200+
code = CloseFrame.NORMAL;
201+
} else if( payload.remaining() == 1 ) {
202+
code = CloseFrame.PROTOCOL_ERROR;
203+
} else {
204+
if( payload.remaining() >= 2 ) {
205+
ByteBuffer bb = ByteBuffer.allocate( 4 );
206+
bb.position( 2 );
207+
bb.putShort( payload.getShort() );
208+
bb.position( 0 );
209+
code = bb.getInt();
210+
}
211+
payload.reset();
212+
try {
213+
int mark = payload.position();// because stringUtf8 also creates a mark
214+
try {
215+
payload.position( payload.position() + 2 );
216+
reason = Charsetfunctions.stringUtf8( payload );
217+
} catch ( IllegalArgumentException e ) {
218+
throw new InvalidDataException( CloseFrame.NO_UTF8 );
219+
} finally {
220+
payload.position( mark );
221+
}
222+
} catch ( InvalidDataException e ) {
223+
code = CloseFrame.NO_UTF8;
224+
reason = null;
225+
}
226+
}
227+
}
222228

223229
/**
224230
* Update the payload to represent the close code and the reason
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.java_websocket.example;
2+
3+
import org.java_websocket.WebSocket;
4+
import org.java_websocket.WebSocketImpl;
5+
import org.java_websocket.drafts.Draft;
6+
import org.java_websocket.drafts.Draft_6455;
7+
import org.java_websocket.framing.Framedata;
8+
import org.java_websocket.framing.FramedataImpl1;
9+
import org.java_websocket.handshake.ClientHandshake;
10+
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
11+
import org.java_websocket.server.WebSocketServer;
12+
13+
import javax.net.ssl.KeyManagerFactory;
14+
import javax.net.ssl.SSLContext;
15+
import javax.net.ssl.TrustManagerFactory;
16+
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.net.InetSocketAddress;
19+
import java.net.UnknownHostException;
20+
import java.nio.ByteBuffer;
21+
import java.security.KeyStore;
22+
import java.security.spec.ECField;
23+
import java.util.Collections;
24+
25+
public class AutobahnSSLServerTest extends WebSocketServer {
26+
private static int counter = 0;
27+
28+
public AutobahnSSLServerTest(int port, Draft d ) throws UnknownHostException {
29+
super( new InetSocketAddress( port ), Collections.singletonList( d ) );
30+
}
31+
32+
public AutobahnSSLServerTest(InetSocketAddress address, Draft d ) {
33+
super( address, Collections.singletonList( d ) );
34+
}
35+
36+
@Override
37+
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
38+
counter++;
39+
System.out.println( "///////////Opened connection number" + counter );
40+
}
41+
42+
@Override
43+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
44+
System.out.println( "closed" );
45+
}
46+
47+
@Override
48+
public void onError( WebSocket conn, Exception ex ) {
49+
System.out.println( "Error:" );
50+
ex.printStackTrace();
51+
}
52+
53+
@Override
54+
public void onStart() {
55+
System.out.println( "Server started!" );
56+
}
57+
58+
@Override
59+
public void onMessage( WebSocket conn, String message ) {
60+
conn.send( message );
61+
}
62+
63+
@Override
64+
public void onMessage( WebSocket conn, ByteBuffer blob ) {
65+
conn.send( blob );
66+
}
67+
68+
@Override
69+
public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) {
70+
FramedataImpl1 builder = ( FramedataImpl1 ) frame;
71+
builder.setTransferemasked( false );
72+
conn.sendFrame( frame );
73+
}
74+
75+
public static void main( String[] args ) throws UnknownHostException {
76+
WebSocketImpl.DEBUG = false;
77+
int port;
78+
try {
79+
port = new Integer( args[0] );
80+
} catch ( Exception e ) {
81+
System.out.println( "No port specified. Defaulting to 9003" );
82+
port = 9003;
83+
}
84+
AutobahnSSLServerTest test = new AutobahnSSLServerTest( port, new Draft_6455() );
85+
try {
86+
// load up the key store
87+
String STORETYPE = "JKS";
88+
String KEYSTORE = "keystore.jks";
89+
String STOREPASSWORD = "storepassword";
90+
String KEYPASSWORD = "keypassword";
91+
92+
KeyStore ks = KeyStore.getInstance(STORETYPE);
93+
File kf = new File(KEYSTORE);
94+
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());
95+
96+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
97+
kmf.init(ks, KEYPASSWORD.toCharArray());
98+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
99+
tmf.init(ks);
100+
101+
SSLContext sslContext = null;
102+
sslContext = SSLContext.getInstance("TLS");
103+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
104+
105+
test.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
106+
} catch (Exception e) {
107+
e.printStackTrace();
108+
}
109+
test.setConnectionLostTimeout( 0 );
110+
test.start();
111+
}
112+
113+
}

0 commit comments

Comments
 (0)