Skip to content

Commit f4842fc

Browse files
committed
Merge branch 'master' of https://github.com/detro/Java-WebSocket
2 parents fb72389 + 3e05bf9 commit f4842fc

8 files changed

Lines changed: 112 additions & 31 deletions

File tree

src/org/java_websocket/Draft.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public abstract class Draft {
1818

1919
public enum HandshakeState {
2020
/** Handshake matched this Draft successfully */
21-
MATCHED ,
21+
MATCHED,
2222
/** Handshake is does not match this Draft */
23-
NOT_MATCHED ,
23+
NOT_MATCHED,
2424
/** Handshake matches this Draft but is not complete */
2525
MATCHING
2626
}
@@ -29,7 +29,7 @@ public enum HandshakeState {
2929

3030
/** In some cases the handshake will be parsed different depending on whether */
3131
protected Role role = null;
32-
32+
3333
public static ByteBuffer readLine( ByteBuffer buf ) {
3434
ByteBuffer sbuf = ByteBuffer.allocate( buf.remaining() );
3535
byte prev = '0';
@@ -45,7 +45,7 @@ public static ByteBuffer readLine( ByteBuffer buf ) {
4545

4646
}
4747
}
48-
//ensure that there wont be any bytes skipped
48+
// ensure that there wont be any bytes skipped
4949
buf.position( buf.position() - sbuf.position() );
5050
return null;
5151
}
@@ -55,18 +55,28 @@ public static String readStringLine( ByteBuffer buf ) {
5555
return b == null ? null : Charsetfunctions.stringAscii( b.array(), 0, b.limit() );
5656
}
5757

58-
public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf ) throws InvalidHandshakeException {
58+
public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf, Role role ) throws InvalidHandshakeException {
5959
HandshakedataImpl1 draft = new HandshakedataImpl1();
6060

6161
String line = readStringLine( buf );
6262
if( line == null )
6363
throw new InvalidHandshakeException( "could not match http status line" );
6464

6565
String[] firstLineTokens = line.split( " " );// eg. GET / HTTP/1.1
66-
if( firstLineTokens.length < 3 ) {
66+
67+
if( role == Role.CLIENT && firstLineTokens.length == 4 ) {
68+
// translating/parsing the response from the SERVER
69+
draft.setHttpVersion( firstLineTokens[ 0 ] );
70+
draft.setHttpStatus( Short.parseShort( firstLineTokens[ 1 ] ) );
71+
draft.setHttpStatusMessage( firstLineTokens[ 2 ] + ' ' + firstLineTokens[ 3 ] );
72+
} else if( role == Role.SERVER && firstLineTokens.length == 3 ) {
73+
// translating/parsing the request from the CLIENT
74+
draft.setMethod( firstLineTokens[ 0 ] );
75+
draft.setResourceDescriptor( firstLineTokens[ 1 ] );
76+
draft.setHttpVersion( firstLineTokens[ 2 ] );
77+
} else {
6778
throw new InvalidHandshakeException( "could not match http status line" );
6879
}
69-
draft.setResourceDescriptor( firstLineTokens[ 1 ] );
7080

7181
line = readStringLine( buf );
7282
while ( line != null && line.length() > 0 ) {
@@ -139,19 +149,19 @@ public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownro
139149
public abstract List<Framedata> translateFrame( ByteBuffer buffer ) throws InvalidDataException;
140150

141151
public Handshakedata translateHandshake( ByteBuffer buf ) throws InvalidHandshakeException {
142-
return translateHandshakeHttp( buf );
152+
return translateHandshakeHttp( buf, role );
143153
}
144154

145155
public int checkAlloc( int bytecount ) throws LimitExedeedException , InvalidDataException {
146156
if( bytecount < 0 )
147157
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "Negative count" );
148158
return bytecount;
149159
}
150-
151-
public void setParseMode( Role role ){
160+
161+
public void setParseMode( Role role ) {
152162
this.role = role;
153163
}
154164

155165
public abstract boolean hasCloseHandshake();
156166

157-
}
167+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.java_websocket;
22

33
public interface HandshakeBuilder extends Handshakedata {
4-
4+
public abstract void setHttpVersion( String version );
5+
public abstract void setMethod( String method );
56
public abstract void setContent( byte[] content );
6-
77
public abstract void setResourceDescriptor( String resourcedescriptor );
8-
8+
public abstract void setHttpStatus( short status );
99
public abstract void setHttpStatusMessage( String message );
10-
1110
public abstract void put( String name, String value );
12-
13-
}
11+
}

src/org/java_websocket/Handshakedata.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.Iterator;
44

55
public interface Handshakedata {
6+
public String getHttpVersion();
7+
public String getMethod();
8+
public short getHttpStatus();
69
public String getHttpStatusMessage();
710
public String getResourceDescriptor();
811
public Iterator<String> iterateHttpFields();

src/org/java_websocket/HandshakedataImpl1.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
public class HandshakedataImpl1 implements HandshakeBuilder {
88

9+
private String httpversion;
10+
private String method;
11+
private short httpstatus;
912
private String httpstatusmessage;
1013
private String resourcedescriptor;
1114
private byte[] content;
@@ -83,4 +86,31 @@ public void setHttpStatusMessage( String message ) {
8386

8487
}
8588

89+
@Override
90+
public short getHttpStatus() {
91+
return httpstatus;
92+
}
93+
94+
@Override
95+
public void setHttpStatus( short status ) {
96+
httpstatus = status;
97+
}
98+
99+
@Override
100+
public String getHttpVersion() {
101+
return httpversion;
102+
}
103+
104+
public void setHttpVersion( String version ) {
105+
httpversion = version;
106+
}
107+
108+
public String getMethod() {
109+
return method;
110+
}
111+
112+
public void setMethod( String method ) {
113+
this.method = method;
114+
}
115+
86116
}

src/org/java_websocket/WebSocket.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
181181
if( handshakestate == HandshakeState.MATCHED ) {
182182
HandshakeBuilder response;
183183
try {
184-
response = wsl.onWebsocketHandshakeRecievedAsServer( this, d, handshake );
184+
response = wsl.onWebsocketHandshakeReceivedAsServer( this, d, handshake );
185185
} catch ( InvalidDataException e ) {
186186
closeConnection( e.getCloseCode(), e.getMessage(), false );
187187
return;
@@ -232,7 +232,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
232232
handshakestate = draft.acceptHandshakeAsClient( handshakerequest, handshake );
233233
if( handshakestate == HandshakeState.MATCHED ) {
234234
try {
235-
wsl.onWebsocketHandshakeRecievedAsClient( this, handshakerequest, handshake );
235+
wsl.onWebsocketHandshakeReceivedAsClient( this, handshakerequest, handshake );
236236
} catch ( InvalidDataException e ) {
237237
closeConnection( e.getCloseCode(), e.getMessage(), false );
238238
return;
@@ -483,9 +483,21 @@ else if( request.limit() < Draft.FLASH_POLICY_REQUEST.length ) {
483483

484484
public void startHandshake( HandshakeBuilder handshakedata ) throws InvalidHandshakeException , InterruptedException {
485485
if( handshakeComplete )
486-
throw new IllegalStateException( "Handshake has allready been sent." );
487-
this.handshakerequest = handshakedata;
488-
channelWrite( draft.createHandshake( draft.postProcessHandshakeRequestAsClient( handshakedata ), role ) );
486+
throw new IllegalStateException( "Handshake has already been sent." );
487+
488+
// Store the Handshake Request we are about to send
489+
this.handshakerequest = draft.postProcessHandshakeRequestAsClient( handshakedata );
490+
491+
// Notify Listener
492+
try {
493+
wsl.onWebsocketHandshakeSentAsClient( this, this.handshakerequest );
494+
} catch ( InvalidDataException e ) {
495+
// Stop if the client code throws an exception
496+
throw new InvalidHandshakeException( "Handshake data rejected by client." );
497+
}
498+
499+
// Send
500+
channelWrite( draft.createHandshake( this.handshakerequest, role ) );
489501
}
490502

491503
private void channelWrite( ByteBuffer buf ) throws InterruptedException {

src/org/java_websocket/WebSocketAdapter.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public abstract class WebSocketAdapter implements WebSocketListener {
1111
* @see org.java_websocket.WebSocketListener#onWebsocketHandshakeRecievedAsServer(org.java_websocket.WebSocket, org.java_websocket.Draft, org.java_websocket.Handshakedata)
1212
*/
1313
@Override
14-
public HandshakeBuilder onWebsocketHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException {
14+
public HandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException {
1515
return new HandshakedataImpl1();
1616
}
1717

@@ -21,7 +21,16 @@ public HandshakeBuilder onWebsocketHandshakeRecievedAsServer( WebSocket conn, Dr
2121
* @see org.java_websocket.WebSocketListener#onWebsocketHandshakeRecievedAsClient(org.java_websocket.WebSocket, org.java_websocket.Handshakedata, org.java_websocket.Handshakedata)
2222
*/
2323
@Override
24-
public void onWebsocketHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException {
24+
public void onWebsocketHandshakeReceivedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException {
25+
}
26+
27+
/**
28+
* This default implementation does not do anything which will cause the connections to always progress.
29+
*
30+
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeSentAsClient(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Handshakedata)
31+
*/
32+
@Override
33+
public void onWebsocketHandshakeSentAsClient( WebSocket conn, Handshakedata request ) throws InvalidDataException {
2534
}
2635

2736
/**
@@ -68,10 +77,10 @@ public void onWebsocketMessage( WebSocket conn, byte[] blob ) {
6877
*/
6978
@Override
7079
public void onWebsocketPing( WebSocket conn, Framedata f ) {
71-
FramedataImpl1 resp = new FramedataImpl1 ( f );
80+
FramedataImpl1 resp = new FramedataImpl1( f );
7281
resp.setOptcode( Opcode.PONG );
7382
try {
74-
conn.sendFrame ( resp );
83+
conn.sendFrame( resp );
7584
} catch ( InterruptedException e ) {
7685
e.printStackTrace();
7786
}
@@ -106,6 +115,7 @@ public String getFlashPolicy( WebSocket conn ) {
106115

107116
/**
108117
* This default implementation does not do anything. Go ahead and overwrite it.
118+
*
109119
* @see org.java_websocket.WebSocketListener#onWebsocketError(org.java_websocket.WebSocket, java.lang.Exception)
110120
*/
111121
@Override

src/org/java_websocket/WebSocketListener.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface WebSocketListener {
1313
* Called on the server side when the socket connection is first established, and the WebSocket
1414
* handshake has been received.
1515
*
16+
* @param conn
17+
* The WebSocket related to this event
1618
* @param draft
1719
* The protocol draft the client uses to connect
1820
* @param request
@@ -21,19 +23,35 @@ public interface WebSocketListener {
2123
* @throws InvalidDataException
2224
* Throwing this exception will cause this handshake to be rejected
2325
*/
24-
public HandshakeBuilder onWebsocketHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException;
26+
public HandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException;
27+
2528
/**
2629
* Called on the client side when the socket connection is first established, and the WebSocket
2730
* handshake response has been received.
2831
*
32+
* @param conn
33+
* The WebSocket related to this event
2934
* @param request
3035
* The handshake initially send out to the server by this websocket.
3136
* @param response
3237
* The handshake the server sent in response to the request.
3338
* @throws InvalidDataException
3439
* Allows the client to reject the connection with the server in respect of its handshake response.
3540
*/
36-
public void onWebsocketHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException;
41+
public void onWebsocketHandshakeReceivedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException;
42+
43+
/**
44+
* Called on the client side when the socket connection is first established, and the WebSocket
45+
* handshake has just been sent.
46+
*
47+
* @param conn
48+
* The WebSocket related to this event
49+
* @param request
50+
* The handshake sent to the server by this websocket
51+
* @throws InvalidDataException
52+
* Allows the client to stop the connection from progressing
53+
*/
54+
public void onWebsocketHandshakeSentAsClient( WebSocket conn, Handshakedata request ) throws InvalidDataException;
3755

3856
/**
3957
* Called when an entire text frame has been received. Do whatever you want
@@ -58,9 +76,9 @@ public interface WebSocketListener {
5876
public void onWebsocketMessage( WebSocket conn, byte[] blob );
5977

6078
/**
61-
* Called after <var>onHandshakeRecieved</var> returns <var>true</var>.
79+
* Called after <var>onHandshakeReceived</var> returns <var>true</var>.
6280
* Indicates that a complete WebSocket connection has been established,
63-
* and we are ready to send/recieve data.
81+
* and we are ready to send/receive data.
6482
*
6583
* @param conn
6684
* The <tt>WebSocket</tt> instance this event is occuring on.

src/org/java_websocket/drafts/Draft_76.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata requ
163163
@Override
164164
public Handshakedata translateHandshake( ByteBuffer buf ) throws InvalidHandshakeException {
165165

166-
HandshakeBuilder bui = translateHandshakeHttp( buf );
166+
HandshakeBuilder bui = translateHandshakeHttp( buf, role );
167167
// the first drafts are lacking a protocol number which makes them difficult to distinguish. Sec-WebSocket-Key1 is typical for draft76
168168
if( ( bui.hasFieldValue( "Sec-WebSocket-Key1" ) || role == Role.CLIENT ) && !bui.hasFieldValue( "Sec-WebSocket-Version" ) ) {
169169
byte[] key3 = new byte[ role == Role.SERVER ? 8 : 16 ];

0 commit comments

Comments
 (0)