Skip to content

Commit ca86db0

Browse files
committed
A little bit of organization in the source code.
1 parent 940f114 commit ca86db0

File tree

5 files changed

+78
-79
lines changed

5 files changed

+78
-79
lines changed

dist/WebSocket.jar

-6 Bytes
Binary file not shown.

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -249,48 +249,48 @@ private void recieveHandshake() throws IOException, NoSuchAlgorithmException {
249249
// If the ByteBuffer contains 16 random bytes, and ends with
250250
// 0x0D 0x0A 0x0D 0x0A (or two CRLFs), then the client
251251
// handshake is complete for Draft 76 Client.
252-
if((h.length>=20 && h[h.length-20] == CR
252+
if((h.length >= 20 && h[h.length-20] == CR
253253
&& h[h.length-19] == LF
254254
&& h[h.length-18] == CR
255-
&& h[h.length-17] == LF)){
256-
byte[] handShakeBody = new byte[16];
257-
handShakeBody[0]=h[h.length-16];
258-
handShakeBody[1]=h[h.length-15];
259-
handShakeBody[2]=h[h.length-14];
260-
handShakeBody[3]=h[h.length-13];
261-
handShakeBody[4]=h[h.length-12];
262-
handShakeBody[5]=h[h.length-11];
263-
handShakeBody[6]=h[h.length-10];
264-
handShakeBody[7]=h[h.length-9];
265-
handShakeBody[8]=h[h.length-8];
266-
handShakeBody[9]=h[h.length-7];
267-
handShakeBody[10]=h[h.length-6];
268-
handShakeBody[11]=h[h.length-5];
269-
handShakeBody[12]=h[h.length-4];
270-
handShakeBody[13]=h[h.length-3];
271-
handShakeBody[14]=h[h.length-2];
272-
handShakeBody[15]=h[h.length-1];
273-
completeHandshake(handShakeBody);
274-
}
255+
&& h[h.length-17] == LF)) {
256+
completeHandshake(new byte[] {
257+
h[h.length-16],
258+
h[h.length-15],
259+
h[h.length-14],
260+
h[h.length-13],
261+
h[h.length-12],
262+
h[h.length-11],
263+
h[h.length-10],
264+
h[h.length-9],
265+
h[h.length-8],
266+
h[h.length-7],
267+
h[h.length-6],
268+
h[h.length-5],
269+
h[h.length-4],
270+
h[h.length-3],
271+
h[h.length-2],
272+
h[h.length-1]
273+
});
274+
275275
// If the ByteBuffer contains 8 random bytes,ends with
276276
// 0x0D 0x0A 0x0D 0x0A (or two CRLFs), and the response
277277
// contains Sec-WebSocket-Key1 then the client
278278
// handshake is complete for Draft 76 Server.
279-
else if ((h.length>=12 && h[h.length-12] == CR
279+
} else if ((h.length>=12 && h[h.length-12] == CR
280280
&& h[h.length-11] == LF
281281
&& h[h.length-10] == CR
282282
&& h[h.length-9] == LF) && new String(this.remoteHandshake.array(), UTF8_CHARSET).contains("Sec-WebSocket-Key1")) {
283-
byte[] handShakeBody = new byte[8];
284-
handShakeBody[0]=h[h.length-8];
285-
handShakeBody[1]=h[h.length-7];
286-
handShakeBody[2]=h[h.length-6];
287-
handShakeBody[3]=h[h.length-5];
288-
handShakeBody[4]=h[h.length-4];
289-
handShakeBody[5]=h[h.length-3];
290-
handShakeBody[6]=h[h.length-2];
291-
handShakeBody[7]=h[h.length-1];
292-
completeHandshake(handShakeBody);
293-
283+
completeHandshake(new byte[] {
284+
h[h.length-8],
285+
h[h.length-7],
286+
h[h.length-6],
287+
h[h.length-5],
288+
h[h.length-4],
289+
h[h.length-3],
290+
h[h.length-2],
291+
h[h.length-1]
292+
});
293+
294294
// Consider Draft 75, and the Flash Security Policy
295295
// Request edge-case.
296296
} else if ((h.length>=4 && h[h.length-4] == CR

src/net/tootallnate/websocket/WebSocketClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,13 @@ private String generateKey() {
244244
* @return <var>true</var> if <var>handshake</var> is a valid WebSocket server
245245
* handshake, <var>false</var> otherwise.
246246
* @throws IOException When socket related I/O errors occur.
247-
* @throws NoSuchAlgorithmException
247+
* @throws NoSuchAlgorithmException
248248
*/
249-
public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] reply) throws IOException, NoSuchAlgorithmException {
249+
public boolean onHandshakeRecieved(WebSocket conn, String handshake, byte[] reply) throws IOException, NoSuchAlgorithmException {
250250
// TODO: Do some parsing of the returned handshake, and close connection
251251
// (return false) if we recieved anything unexpected.
252252
if(this.draft == Draft.DRAFT76) {
253-
if (reply == null){
253+
if (reply == null) {
254254
return false;
255255
}
256256
byte[] challenge = new byte[] {

src/net/tootallnate/websocket/WebSocketListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static enum Draft {
3030
* handshake was invalid, and the connection should be terminated.
3131
* @throws NoSuchAlgorithmException
3232
*/
33-
public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] handShakeBody) throws IOException, NoSuchAlgorithmException;
33+
public boolean onHandshakeRecieved(WebSocket conn, String handshake, byte[] handShakeBody) throws IOException, NoSuchAlgorithmException;
3434

3535
/**
3636
* Called when an entire text frame has been recieved. Do whatever you want

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ public void run() {
260260
* http://github.com/gimite/web-socket-js
261261
*
262262
* @return An XML String that comforms to Flash's security policy. You MUST
263-
* not need to include the null char at the end, it is appended
264-
* automatically.
263+
* not include the null char at the end, it is appended automatically.
265264
*/
266265
protected String getFlashSecurityPolicy() {
267266
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\""
@@ -282,7 +281,7 @@ protected String getFlashSecurityPolicy() {
282281
* @throws IOException When socket related I/O errors occur.
283282
* @throws NoSuchAlgorithmException
284283
*/
285-
public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] key3) throws IOException, NoSuchAlgorithmException {
284+
public boolean onHandshakeRecieved(WebSocket conn, String handshake, byte[] key3) throws IOException, NoSuchAlgorithmException {
286285

287286
// If a Flash client requested the Policy File...
288287
if (FLASH_POLICY_REQUEST.equals(handshake)) {
@@ -319,11 +318,11 @@ public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] key3)
319318
if (prop == null || !prop.equals("Upgrade")) {
320319
isWebSocketRequest = false;
321320
}
322-
String key1=p.getProperty("Sec-WebSocket-Key1");
323-
String key2=p.getProperty("Sec-WebSocket-Key2");
324-
String headerPrefix="";
325-
byte[] responseChallenge=null;
326-
switch (this.draft){
321+
String key1 = p.getProperty("Sec-WebSocket-Key1");
322+
String key2 = p.getProperty("Sec-WebSocket-Key2");
323+
String headerPrefix = "";
324+
byte[] responseChallenge = null;
325+
switch (this.draft) {
327326
case DRAFT75:
328327
if (key1 != null || key2 != null || key3 != null) {
329328
isWebSocketRequest = false;
@@ -337,28 +336,28 @@ public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] key3)
337336
}
338337
if (isWebSocketRequest) {
339338
if (key1 != null && key2 != null && key3 != null) {
340-
headerPrefix="Sec-";
341-
byte[] part1=this.getPart(key1);
342-
byte[] part2=this.getPart(key2);
343-
byte[] challenge=new byte[16];
344-
challenge[0]=part1[0];
345-
challenge[1]=part1[1];
346-
challenge[2]=part1[2];
347-
challenge[3]=part1[3];
348-
challenge[4]=part2[0];
349-
challenge[5]=part2[1];
350-
challenge[6]=part2[2];
351-
challenge[7]=part2[3];
352-
challenge[8]=key3[0];
353-
challenge[9]=key3[1];
354-
challenge[10]=key3[2];
355-
challenge[11]=key3[3];
356-
challenge[12]=key3[4];
357-
challenge[13]=key3[5];
358-
challenge[14]=key3[6];
359-
challenge[15]=key3[7];
360-
MessageDigest md5=MessageDigest.getInstance("MD5");
361-
responseChallenge=md5.digest(challenge);
339+
headerPrefix = "Sec-";
340+
byte[] part1 = this.getPart(key1);
341+
byte[] part2 = this.getPart(key2);
342+
byte[] challenge = new byte[16];
343+
challenge[0] = part1[0];
344+
challenge[1] = part1[1];
345+
challenge[2] = part1[2];
346+
challenge[3] = part1[3];
347+
challenge[4] = part2[0];
348+
challenge[5] = part2[1];
349+
challenge[6] = part2[2];
350+
challenge[7] = part2[3];
351+
challenge[8] = key3[0];
352+
challenge[9] = key3[1];
353+
challenge[10] = key3[2];
354+
challenge[11] = key3[3];
355+
challenge[12] = key3[4];
356+
challenge[13] = key3[5];
357+
challenge[14] = key3[6];
358+
challenge[15] = key3[7];
359+
MessageDigest md5 = MessageDigest.getInstance("MD5");
360+
responseChallenge = md5.digest(challenge);
362361
}
363362

364363
String responseHandshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
@@ -397,25 +396,25 @@ public void onOpen(WebSocket conn) {
397396
onClientOpen(conn);
398397
}
399398
}
400-
399+
401400
public void onClose(WebSocket conn) {
402401
if (this.connections.remove(conn)) {
403402
onClientClose(conn);
404403
}
405404
}
406405

407-
private byte[] getPart(String key){
408-
long keyNumber=Long.parseLong(key.replaceAll("[^0-9]",""));
409-
long keySpace=key.split("\u0020").length-1;
410-
long part=new Long(keyNumber/keySpace);
411-
byte[] bytes=new byte[4];
412-
bytes[0] =(byte)( part >> 24 );
413-
bytes[1] =(byte)( (part << 8) >> 24 );
414-
bytes[2] =(byte)( (part << 16) >> 24 );
415-
bytes[3] =(byte)( (part << 24) >> 24 );
416-
return bytes;
406+
private byte[] getPart(String key) {
407+
long keyNumber = Long.parseLong(key.replaceAll("[^0-9]",""));
408+
long keySpace = key.split("\u0020").length - 1;
409+
long part = new Long(keyNumber / keySpace);
410+
return new byte[] {
411+
(byte)( part >> 24 ),
412+
(byte)( (part << 8) >> 24 ),
413+
(byte)( (part << 16) >> 24 ),
414+
(byte)( (part << 24) >> 24 )
415+
};
417416
}
418-
417+
419418
// ABTRACT METHODS /////////////////////////////////////////////////////////
420419
public abstract void onClientOpen(WebSocket conn);
421420
public abstract void onClientClose(WebSocket conn);

0 commit comments

Comments
 (0)