Skip to content

Commit e3902cf

Browse files
committed
-made code work with flash based sockets( as it was before in 2c10a04 )
1 parent 1a1d5d4 commit e3902cf

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

src/net/tootallnate/websocket/Draft.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.nio.ByteBuffer;
44
import java.nio.charset.Charset;
5+
import java.util.Arrays;
56
import java.util.Iterator;
67
import java.util.List;
78

@@ -43,7 +44,7 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata req
4344
bui.append ( " HTTP/1.1" );
4445
}
4546
else if( ownrole == Role.SERVER ){
46-
bui.append ( "HTTP/1.1 101 Switching Protocols" );
47+
bui.append ( "HTTP/1.1 101 "+handshakedata.getHttpStatusMessage() );
4748
}
4849
else{
4950
throw new RuntimeException ( "unknow role" );
@@ -58,14 +59,13 @@ else if( ownrole == Role.SERVER ){
5859
bui.append ( fieldvalue );
5960
bui.append ( "\r\n" );
6061
}
62+
bui.append ( "\r\n" );
6163
byte[] httpheader = bui.toString ().getBytes ( UTF8_CHARSET );
6264
byte[] content = handshakedata.getContent ();
63-
ByteBuffer bytebuffer = ByteBuffer.allocate ( ( content==null ? 0 : content.length ) + httpheader.length + 2 );
65+
ByteBuffer bytebuffer = ByteBuffer.allocate ( ( content==null ? 0 : content.length ) + httpheader.length );
6466
bytebuffer.put ( httpheader );
6567
if( content!=null )
6668
bytebuffer.put ( content );
67-
bytebuffer.put ( ( byte )'\r' );
68-
bytebuffer.put ( ( byte )'\n' );
6969
return bytebuffer;
7070

7171
}
@@ -80,6 +80,9 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
8080
if ( index == lines.length )
8181
return null;
8282
String line = new String ( lines , previndex , index - previndex );
83+
String[] firstLineTokens = line.split(" ");
84+
String path = firstLineTokens[1];
85+
draft.setResourceDescriptor( path );
8386
//TODO Care about resources here like: GET /chat HTTP/1.1
8487
//if ( line.startsWith ( "GET" ) == false )
8588
//if ( line.startsWith ( "HTTP" ) == false )
@@ -88,18 +91,20 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
8891
index = findNewLine ( lines , previndex );
8992
int length = index - previndex;
9093
while ( length != 0 ) {
91-
9294
line = new String ( lines , previndex , length );
9395
if ( index != previndex ) {
9496
String[] pair = line.split ( ":" , 2 );
9597
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+
return null;
99+
draft.put ( pair[ 0 ] , pair[ 1 ].replaceFirst( "^ +" , "" ) );
98100
}
99101
previndex = index + 2;
100102
index = findNewLine ( lines , previndex );
101103
length = index - previndex;
102104
}
105+
previndex = index + 2;
106+
length = lines.length - previndex;
107+
draft.setContent ( ByteBuffer.allocate ( length ).put ( lines, previndex , length ).array () );
103108
return draft;
104109
}
105110

@@ -112,4 +117,15 @@ public static int findNewLine( byte[] arr , int offset ) {
112117
return i;
113118
return i;//the end of input will be handled like newline
114119
}
120+
121+
public static boolean isFlashEdgeCase( byte[] request , int requestsize ){
122+
byte[] req = "<policy-file-request/>".getBytes( UTF8_CHARSET );
123+
for( int i = 0 ; i < requestsize && i < req.length ; i++ ){
124+
if( req[i] != request[i] ){
125+
return false;
126+
}
127+
}
128+
return requestsize >= req.length;
129+
}
130+
115131
}

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public enum Role{
6161
public static final byte END_OF_FRAME = (byte)0xFF;
6262

6363
public static final boolean DEBUG = false;
64+
65+
static{
66+
if ( DEBUG ) {
67+
System.out.println("WebSocket debug mode enabled");
68+
}
69+
}
6470

6571

6672
// INSTANCE PROPERTIES /////////////////////////////////////////////////////
@@ -173,8 +179,16 @@ else if( bytesRead > maxpayloadsize ){
173179
else if ( bytesRead > 0) {
174180
if(DEBUG) System.out.println( "got: {" + new String( socketBuffer.array() , 0 , bytesRead ) + "}" );
175181
if( !handshakeComplete ){
182+
if(draft.isFlashEdgeCase( socketBuffer.array() , bytesRead )){
183+
channelWrite( ByteBuffer.wrap( wsl.getFlashPolicy( this ).getBytes( UTF8_CHARSET ) ) );
184+
return;
185+
}
176186
try{
177187
Handshakedata handshake = Draft.translateHandshake ( socketBuffer.array () , bytesRead );
188+
if( handshake == null){
189+
abort("This valid http header ");
190+
return;
191+
}
178192
if( role == Role.SERVER ){
179193
for( Draft d : known_drafts ){
180194
if( d.acceptHandshakeAsServer( handshake ) ){
@@ -369,5 +383,9 @@ private void printBytes(ByteBuffer buf, int len){
369383
}
370384

371385
}
386+
387+
public int getPort(){
388+
return socketChannel.socket().getLocalPort();
389+
}
372390

373391
}

src/net/tootallnate/websocket/WebSocketAdapter.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,26 @@ public void onMessage( WebSocket conn , byte[] blob ) {
3838
@Override
3939
public void onPong( ) {
4040
}
41+
42+
/**
43+
* Gets the XML string that should be returned if a client requests a Flash
44+
* security policy.
45+
*
46+
* The default implementation allows access from all remote domains, but
47+
* only on the port that this WebSocketServer is listening on.
48+
*
49+
* This is specifically implemented for gitime's WebSocket client for Flash:
50+
* http://github.com/gimite/web-socket-js
51+
*
52+
* @return An XML String that comforms to Flash's security policy. You MUST
53+
* not include the null char at the end, it is appended automatically.
54+
*/
55+
56+
57+
@Override
58+
public String getFlashPolicy( WebSocket conn ) {
59+
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\""
60+
+ conn.getPort() + "\" /></cross-domain-policy>\0";
61+
}
62+
4163
}

src/net/tootallnate/websocket/WebSocketListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ interface WebSocketListener {
5151
public void onError( Throwable ex );
5252

5353
public void onPong();
54+
55+
public String getFlashPolicy( WebSocket conn);
5456
}

0 commit comments

Comments
 (0)