Skip to content

Commit 816e384

Browse files
committed
-sorted functions in all drafts the same way
1: static stuff 2: the functions alphabetical sorted
1 parent 825c9c7 commit 816e384

4 files changed

Lines changed: 382 additions & 388 deletions

File tree

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

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
public abstract class Draft{
1414

15-
/**
16-
* The WebSocket protocol expects UTF-8 encoded bytes.
17-
*/
18-
public static final Charset UTF8_CHARSET = Charset.forName ( "UTF-8" );
19-
private static final byte[] FLASH_POLICY_REQUEST = "<policy-file-request/>".getBytes( UTF8_CHARSET );
20-
2115
public enum HandshakeState{
2216
/**Handshake matched this Draft successfully*/
2317
MATCHED,
@@ -28,53 +22,30 @@ public enum HandshakeState{
2822
//,/**Can not yet say anything*/
2923
//PENDING not yet in use
3024
}
25+
/**
26+
* The WebSocket protocol expects UTF-8 encoded bytes.
27+
*/
28+
public static final Charset UTF8_CHARSET = Charset.forName ( "UTF-8" );
3129

32-
public abstract HandshakeState acceptHandshakeAsServer( Handshakedata handshakedata ) throws InvalidHandshakeException;
33-
public abstract HandshakeState acceptHandshakeAsClient( Handshakedata request , Handshakedata response ) throws InvalidHandshakeException;
34-
public abstract List<Framedata> translateFrame( ByteBuffer buffer, int read );
35-
public abstract ByteBuffer createBinaryFrame( Framedata framedata ); //TODO Allow to send data on the base of an Iterator or InputStream
36-
public abstract List<Framedata> createFrames( String text , boolean mask );
37-
public abstract List<Framedata> createFrames( byte[] binary , boolean mask );
38-
public abstract HandshakeBuilder postProcessHandshakeRequestAsClient( HandshakeBuilder request ) throws InvalidHandshakeException;
39-
public abstract HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException;
30+
private static final byte[] FLASH_POLICY_REQUEST = "<policy-file-request/>".getBytes( UTF8_CHARSET );
4031

41-
public List<ByteBuffer> createHandshake( Handshakedata handshakedata , Role ownrole ){
42-
return createHandshake( handshakedata , ownrole , true );
32+
/**will return the index of the first \r\n or the index off the last element in arr*/
33+
public static int findNewLine( byte[] arr , int offset ) {
34+
int len = arr.length - 1;
35+
int i = offset;
36+
for ( ; i < len ; i++ )
37+
if( arr[i] == (byte)'\r' && arr[ i + 1 ] == (byte)'\n' )
38+
return i;
39+
return i;//the end of input will be handled like newline
4340
}
44-
45-
public List<ByteBuffer> createHandshake( Handshakedata handshakedata , Role ownrole , boolean withcontent ){
46-
StringBuilder bui = new StringBuilder ( 100 );
47-
if( ownrole == Role.CLIENT){
48-
bui.append ( "GET " );
49-
bui.append ( handshakedata.getResourceDescriptor () );
50-
bui.append ( " HTTP/1.1" );
51-
}
52-
else if( ownrole == Role.SERVER ){
53-
bui.append ( "HTTP/1.1 101 "+handshakedata.getHttpStatusMessage() );
54-
}
55-
else{
56-
throw new RuntimeException ( "unknow role" );
57-
}
58-
bui.append ( "\r\n" );
59-
Iterator<String> it = handshakedata.iterateHttpFields ();
60-
while( it.hasNext () ){
61-
String fieldname = it.next ();
62-
String fieldvalue = handshakedata.getFieldValue ( fieldname );
63-
bui.append ( fieldname );
64-
bui.append ( ": ");
65-
bui.append ( fieldvalue );
66-
bui.append ( "\r\n" );
41+
public static boolean isFlashEdgeCase( byte[] request , int requestsize ){
42+
for( int i = 0 ; i < requestsize && i < FLASH_POLICY_REQUEST.length ; i++ ){
43+
if( FLASH_POLICY_REQUEST[i] != request[i] ){
44+
return false;
45+
}
6746
}
68-
bui.append ( "\r\n" );
69-
byte[] httpheader = bui.toString ().getBytes ( UTF8_CHARSET );
70-
byte[] content = withcontent ? handshakedata.getContent() : null;
71-
ByteBuffer bytebuffer = ByteBuffer.allocate ( ( content==null ? 0 : content.length ) + httpheader.length );
72-
bytebuffer.put ( httpheader );
73-
if( content!=null )
74-
bytebuffer.put ( content );
75-
return Collections.singletonList( bytebuffer );
47+
return requestsize >= FLASH_POLICY_REQUEST.length;
7648
}
77-
7849
public static HandshakeBuilder translateHandshakeHttp( byte[] buffer, int readcount ) throws InvalidHandshakeException{
7950
HandshakedataImpl1 draft = new HandshakedataImpl1();
8051

@@ -114,28 +85,57 @@ public static HandshakeBuilder translateHandshakeHttp( byte[] buffer, int readco
11485
draft.setContent ( ByteBuffer.allocate ( length ).put ( lines, previndex , length ).array () );
11586
return draft;
11687
}
88+
public abstract HandshakeState acceptHandshakeAsClient( Handshakedata request , Handshakedata response ) throws InvalidHandshakeException;
89+
public abstract HandshakeState acceptHandshakeAsServer( Handshakedata handshakedata ) throws InvalidHandshakeException;
90+
public abstract ByteBuffer createBinaryFrame( Framedata framedata ); //TODO Allow to send data on the base of an Iterator or InputStream
91+
public abstract List<Framedata> createFrames( byte[] binary , boolean mask );
92+
public abstract List<Framedata> createFrames( String text , boolean mask );
11793

118-
public Handshakedata translateHandshake( byte[] buffer, int readcount ) throws InvalidHandshakeException{
119-
return translateHandshakeHttp( buffer , readcount );
120-
}
121-
122-
/**will return the index of the first \r\n or the index off the last element in arr*/
123-
public static int findNewLine( byte[] arr , int offset ) {
124-
int len = arr.length - 1;
125-
int i = offset;
126-
for ( ; i < len ; i++ )
127-
if( arr[i] == (byte)'\r' && arr[ i + 1 ] == (byte)'\n' )
128-
return i;
129-
return i;//the end of input will be handled like newline
94+
public List<ByteBuffer> createHandshake( Handshakedata handshakedata , Role ownrole ){
95+
return createHandshake( handshakedata , ownrole , true );
13096
}
13197

132-
public static boolean isFlashEdgeCase( byte[] request , int requestsize ){
133-
for( int i = 0 ; i < requestsize && i < FLASH_POLICY_REQUEST.length ; i++ ){
134-
if( FLASH_POLICY_REQUEST[i] != request[i] ){
135-
return false;
136-
}
98+
public List<ByteBuffer> createHandshake( Handshakedata handshakedata , Role ownrole , boolean withcontent ){
99+
StringBuilder bui = new StringBuilder ( 100 );
100+
if( ownrole == Role.CLIENT){
101+
bui.append ( "GET " );
102+
bui.append ( handshakedata.getResourceDescriptor () );
103+
bui.append ( " HTTP/1.1" );
137104
}
138-
return requestsize >= FLASH_POLICY_REQUEST.length;
105+
else if( ownrole == Role.SERVER ){
106+
bui.append ( "HTTP/1.1 101 "+handshakedata.getHttpStatusMessage() );
107+
}
108+
else{
109+
throw new RuntimeException ( "unknow role" );
110+
}
111+
bui.append ( "\r\n" );
112+
Iterator<String> it = handshakedata.iterateHttpFields ();
113+
while( it.hasNext () ){
114+
String fieldname = it.next ();
115+
String fieldvalue = handshakedata.getFieldValue ( fieldname );
116+
bui.append ( fieldname );
117+
bui.append ( ": ");
118+
bui.append ( fieldvalue );
119+
bui.append ( "\r\n" );
120+
}
121+
bui.append ( "\r\n" );
122+
byte[] httpheader = bui.toString ().getBytes ( UTF8_CHARSET );
123+
byte[] content = withcontent ? handshakedata.getContent() : null;
124+
ByteBuffer bytebuffer = ByteBuffer.allocate ( ( content==null ? 0 : content.length ) + httpheader.length );
125+
bytebuffer.put ( httpheader );
126+
if( content!=null )
127+
bytebuffer.put ( content );
128+
return Collections.singletonList( bytebuffer );
129+
}
130+
131+
public abstract HandshakeBuilder postProcessHandshakeRequestAsClient( HandshakeBuilder request ) throws InvalidHandshakeException;
132+
133+
public abstract HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException;
134+
135+
public abstract List<Framedata> translateFrame( ByteBuffer buffer, int read );
136+
137+
public Handshakedata translateHandshake( byte[] buffer, int readcount ) throws InvalidHandshakeException{
138+
return translateHandshakeHttp( buffer , readcount );
139139
}
140140

141141
}

0 commit comments

Comments
 (0)