Skip to content

Commit 507e5ec

Browse files
committed
Redesigned class structur torwards extensibility, Draft 10 server site works minimal. Other drafts won't work yet
1 parent a65070d commit 507e5ec

19 files changed

Lines changed: 979 additions & 476 deletions
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package net.tootallnate.websocket;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.charset.Charset;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
import net.tootallnate.websocket.WebSocket.Role;
9+
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
10+
11+
public abstract class Draft{
12+
13+
/**
14+
* The WebSocket protocol expects UTF-8 encoded bytes.
15+
*/
16+
public final static Charset UTF8_CHARSET = Charset.forName ( "UTF-8" );
17+
18+
public abstract boolean acceptHandshakeAsServer( Handshakedata handshakedata ) throws InvalidHandshakeException;
19+
public abstract boolean acceptHandshakeAsClient( Handshakedata request , Handshakedata response );
20+
public abstract List<Framedata> translateFrame( byte[] buffer, int read );
21+
public abstract ByteBuffer createBinaryFrame( Framedata framedata ); //TODO Allow to send data on the base of an Iterator or InputStream
22+
public abstract List<Framedata> createFrames( String text , boolean mask );
23+
public abstract List<Framedata> createFrames( byte[] binary , boolean mask );
24+
25+
public HandshakeBuilder postProcessHandshakeRequestAsClient( HandshakeBuilder request ){
26+
request.put ( "Upgrade" , "websocket" );
27+
request.put ( "Connection" , "Upgrade" );
28+
return request;
29+
}
30+
31+
public HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException{
32+
//sb.append ( "HTTP/1.1 101 Switching Protocols\r\n" );
33+
response.put ( "Upgrade" , "websocket" );
34+
response.put ( "Connection" , "Upgrade" );
35+
return response;
36+
}
37+
38+
public /*static*/ ByteBuffer createHandshake( Handshakedata handshakedata , Role ownrole ){
39+
StringBuilder bui = new StringBuilder ( 100 );
40+
if( ownrole == Role.CLIENT){
41+
bui.append ( "GET " );
42+
bui.append ( handshakedata.getResourceDescriptor () );
43+
bui.append ( " HTTP/1.1" );
44+
}
45+
else if( ownrole == Role.SERVER ){
46+
bui.append ( "HTTP/1.1 101 Switching Protocols" );
47+
}
48+
else{
49+
throw new RuntimeException ( "unknow role" );
50+
}
51+
bui.append ( "\r\n" );
52+
Iterator<String> it = handshakedata.iterateHttpFields ();
53+
while( it.hasNext () ){
54+
String fieldname = it.next ();
55+
String fieldvalue = handshakedata.getFieldValue ( fieldname );
56+
bui.append ( fieldname );
57+
bui.append ( ": ");
58+
bui.append ( fieldvalue );
59+
bui.append ( "\r\n" );
60+
}
61+
byte[] httpheader = bui.toString ().getBytes ( UTF8_CHARSET );
62+
byte[] content = handshakedata.getContent ();
63+
ByteBuffer bytebuffer = ByteBuffer.allocate ( ( content==null ? 0 : content.length ) + httpheader.length + 2 );
64+
bytebuffer.put ( httpheader );
65+
if( content!=null )
66+
bytebuffer.put ( content );
67+
bytebuffer.put ( ( byte )'\r' );
68+
bytebuffer.put ( ( byte )'\n' );
69+
return bytebuffer;
70+
71+
}
72+
public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
73+
HandshakedataImpl1 draft = new HandshakedataImpl1();
74+
75+
ByteBuffer message = ByteBuffer.allocate ( readcount );
76+
message.put ( buffer , 0 , readcount );
77+
byte[] lines = message.array ();
78+
int previndex = 0;
79+
int index = findNewLine ( lines , previndex );
80+
if ( index == -1 )
81+
return null;
82+
String line = new String ( lines , previndex , index - previndex );
83+
//TODO Care about resources here like: GET /chat HTTP/1.1
84+
//if ( line.startsWith ( "GET" ) == false )
85+
//if ( line.startsWith ( "HTTP" ) == false )
86+
87+
previndex = index + 2;
88+
index = findNewLine ( lines , previndex );
89+
90+
while ( index != -1 ) {
91+
int length = index - previndex;
92+
line = new String ( lines , previndex , length );
93+
if ( index != previndex ) {
94+
String[] pair = line.split ( ":" , 2 );
95+
if ( pair.length != 2 )
96+
draft.setContent ( ByteBuffer.allocate ( length ).put ( lines, previndex,length).array () ); //this approach will also accept suspicious looking handshakes...
97+
draft.put ( pair[ 0 ] , pair[ 1 ] );
98+
}
99+
previndex = index + 2;
100+
index = findNewLine ( lines , previndex );
101+
}
102+
return draft;
103+
}
104+
105+
public static int findNewLine( byte[] arr , int offset ) {
106+
int len = arr.length - 1;
107+
for ( int i = offset ; i < len ; i++ )
108+
if( arr[i] == (byte)'\r' && arr[ i + 1 ] == (byte)'\n' )
109+
return i;
110+
return -1;
111+
}
112+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.tootallnate.websocket;
2+
3+
public interface FrameBuilder extends Framedata{
4+
5+
public abstract void setFin( boolean fin );
6+
7+
public abstract void setOptcode( Opcode optcode );
8+
9+
public abstract void setPayload( byte[] payload );
10+
11+
public abstract void setTransferemasked( boolean transferemasked );
12+
13+
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.tootallnate.websocket;
2+
3+
import java.util.HashMap;
4+
import net.tootallnate.websocket.exeptions.InvalidFrameException;
5+
6+
public interface Framedata {
7+
enum Opcode{
8+
CONTINIOUS,
9+
TEXT,
10+
BINARY,
11+
PING,
12+
PONG,
13+
CLOSING
14+
//more to come
15+
}
16+
public boolean isFin();
17+
public boolean getTransfereMasked();
18+
public Opcode getOpcode();
19+
public byte[] getPayloadData();
20+
public abstract void append( Framedata nextframe ) throws InvalidFrameException;
21+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package net.tootallnate.websocket;
2+
3+
import java.nio.ByteBuffer;
4+
5+
public class FramedataImpl1 implements FrameBuilder {
6+
private boolean fin;
7+
private Opcode optcode;
8+
private ByteBuffer unmaskedpayload;
9+
private boolean transferemasked;
10+
11+
public FramedataImpl1() {
12+
}
13+
14+
@Override
15+
public boolean isFin( ) {
16+
return fin;
17+
}
18+
19+
@Override
20+
public Opcode getOpcode( ) {
21+
return optcode;
22+
}
23+
24+
@Override
25+
public boolean getTransfereMasked( ) {
26+
return transferemasked;
27+
}
28+
29+
@Override
30+
public byte[] getPayloadData( ) {
31+
return unmaskedpayload.array ();
32+
}
33+
34+
35+
@Override
36+
public void setFin( boolean fin ) {
37+
this.fin = fin;
38+
}
39+
40+
41+
@Override
42+
public void setOptcode( Opcode optcode ) {
43+
this.optcode = optcode;
44+
}
45+
46+
@Override
47+
public void setPayload( byte[] payload ) {
48+
unmaskedpayload=ByteBuffer.wrap ( payload );
49+
}
50+
51+
52+
@Override
53+
public void setTransferemasked( boolean transferemasked ) {
54+
this.transferemasked = transferemasked;
55+
}
56+
57+
58+
@Override
59+
public void append( Framedata nextframe ) {
60+
if( unmaskedpayload == null ){
61+
unmaskedpayload = ByteBuffer.wrap ( nextframe.getPayloadData () );
62+
}
63+
else{
64+
ByteBuffer tmp = ByteBuffer.allocate ( nextframe.getPayloadData ().length + unmaskedpayload.capacity () );
65+
tmp.put ( unmaskedpayload.array () );
66+
tmp.put ( nextframe.getPayloadData () );
67+
}
68+
}
69+
70+
71+
72+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.tootallnate.websocket;
2+
3+
public interface HandshakeBuilder extends Handshakedata{
4+
5+
public abstract void setContent( byte[] content );
6+
7+
public abstract void setResourceDescriptor( String resourcedescriptor );
8+
9+
public abstract void put( String name , String value );
10+
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.tootallnate.websocket;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
6+
public interface Handshakedata {
7+
public String getResourceDescriptor();
8+
public Iterator<String> iterateHttpFields();
9+
public String getFieldValue( String name );
10+
public byte[] getContent();
11+
//public boolean isComplete();
12+
}
13+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.tootallnate.websocket;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
6+
7+
public class HandshakedataImpl1 implements HandshakeBuilder {
8+
9+
private String resourcedescriptor;
10+
private byte[] content;
11+
private Map<String,String> map;
12+
13+
public HandshakedataImpl1() {
14+
map = new HashMap<String,String>();
15+
}
16+
17+
@Override
18+
public String getResourceDescriptor( ) {
19+
return resourcedescriptor == null ? "" : resourcedescriptor ;
20+
}
21+
22+
@Override
23+
public Iterator<String> iterateHttpFields( ) {
24+
return map.keySet ().iterator ();
25+
}
26+
27+
@Override
28+
public String getFieldValue( String name ) {
29+
String s = map.get ( name );
30+
if( s == null ){
31+
return "";
32+
}
33+
return s;
34+
}
35+
36+
@Override
37+
public byte[] getContent( ) {
38+
return content;
39+
}
40+
41+
@Override
42+
public void setContent( byte[] content ) {
43+
this.content = content;
44+
}
45+
46+
@Override
47+
public void setResourceDescriptor( String resourcedescriptor ) {
48+
this.resourcedescriptor = resourcedescriptor;
49+
}
50+
51+
@Override
52+
public void put( String name, String value ){
53+
map.put ( name , value );
54+
}
55+
56+
}

0 commit comments

Comments
 (0)