Skip to content

Commit 7c84fb6

Browse files
committed
Make "Draft" class populate Req/Res HandshaeData with Http Version, Http Status (Message) and Http Method, as needed.
1 parent 257acfe commit 7c84fb6

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎src/net/tootallnate/websocket/Draft.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,28 @@ public static String readStringLine( ByteBuffer buf ) {
5353
return b == null ? null : Charsetfunctions.stringAscii( b.array(), 0, b.limit() );
5454
}
5555

56-
public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf ) throws InvalidHandshakeException {
56+
public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf , Role role) throws InvalidHandshakeException {
5757
HandshakedataImpl1 draft = new HandshakedataImpl1();
5858

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

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

6979
line = readStringLine( buf );
7080
while ( line != null && line.length() > 0 ) {
@@ -136,8 +146,8 @@ public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownro
136146

137147
public abstract List<Framedata> translateFrame( ByteBuffer buffer ) throws InvalidDataException;
138148

139-
public Handshakedata translateHandshake( ByteBuffer buf ) throws InvalidHandshakeException {
140-
return translateHandshakeHttp( buf );
149+
public Handshakedata translateHandshake( ByteBuffer buf , Role role) throws InvalidHandshakeException {
150+
return translateHandshakeHttp( buf , role );
141151
}
142152

143153
public int checkAlloc( int bytecount ) throws LimitExedeedException , InvalidDataException {
@@ -152,4 +162,4 @@ public void setParseMode( Role role ){
152162

153163
public abstract boolean hasCloseHandshake();
154164

155-
}
165+
}

‎src/net/tootallnate/websocket/WebSocket.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
175175
try {
176176
d.setParseMode( role );
177177
socketBuffer.reset();
178-
handshake = d.translateHandshake( socketBuffer );
178+
handshake = d.translateHandshake( socketBuffer, role );
179179
handshakestate = d.acceptHandshakeAsServer( handshake );
180180
if( handshakestate == HandshakeState.MATCHED ) {
181181
HandshakeBuilder response;
182182
try {
183-
response = wsl.onHandshakeRecievedAsServer( this, d, handshake );
183+
response = wsl.onHandshakeReceivedAsServer(this, d, handshake);
184184
} catch ( InvalidDataException e ) {
185185
closeConnection( e.getCloseCode(), e.getMessage(), false );
186186
return;
@@ -214,7 +214,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
214214
return;
215215
} else {
216216
// special case for multiple step handshakes
217-
handshake = draft.translateHandshake( socketBuffer );
217+
handshake = draft.translateHandshake( socketBuffer, role );
218218
handshakestate = draft.acceptHandshakeAsServer( handshake );
219219

220220
if( handshakestate == HandshakeState.MATCHED ) {
@@ -227,11 +227,11 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
227227
}
228228
} else if( role == Role.CLIENT ) {
229229
draft.setParseMode( role );
230-
handshake = draft.translateHandshake( socketBuffer );
230+
handshake = draft.translateHandshake( socketBuffer, role );
231231
handshakestate = draft.acceptHandshakeAsClient( handshakerequest, handshake );
232232
if( handshakestate == HandshakeState.MATCHED ) {
233233
try {
234-
wsl.onHandshakeRecievedAsClient( this, handshakerequest, handshake );
234+
wsl.onHandshakeReceivedAsClient(this, handshakerequest, handshake);
235235
} catch ( InvalidDataException e ) {
236236
closeConnection( e.getCloseCode(), e.getMessage(), false );
237237
return;

‎src/net/tootallnate/websocket/drafts/Draft_76.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata requ
161161
}
162162

163163
@Override
164-
public Handshakedata translateHandshake( ByteBuffer buf ) throws InvalidHandshakeException {
164+
public Handshakedata translateHandshake( ByteBuffer buf , Role role ) 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)