55import java .util .Iterator ;
66import java .util .List ;
77import java .util .Locale ;
8+ import java .util .regex .Pattern ;
89
910import org .java_websocket .WebSocket .Role ;
1011import org .java_websocket .exeptions .InvalidDataException ;
@@ -26,6 +27,8 @@ public enum HandshakeState {
2627 }
2728
2829 public static final byte [] FLASH_POLICY_REQUEST = Charsetfunctions .utf8Bytes ( "<policy-file-request/>\0 " );
30+ private static Pattern getpattern = Pattern .compile ( "" ); // GET / HTTP/1.1
31+ private static Pattern statuspattern = Pattern .compile ( "" ); // HTTP/1.1 101 Switching Protocols
2932
3033 /** In some cases the handshake will be parsed different depending on whether */
3134 protected Role role = null ;
@@ -56,42 +59,42 @@ public static String readStringLine( ByteBuffer buf ) {
5659 }
5760
5861 public static HandshakeBuilder translateHandshakeHttp ( ByteBuffer buf , Role role ) throws InvalidHandshakeException {
59- HandshakedataImpl1 draft = new HandshakedataImpl1 () ;
62+ HandshakeBuilder handshake ;
6063
6164 String line = readStringLine ( buf );
6265 if ( line == null )
6366 throw new InvalidHandshakeException ( "could not match http status line" );
6467
65- String [] firstLineTokens = line .split ( " " );// eg. GET / HTTP/1.1
66-
67- if ( role == Role . CLIENT && firstLineTokens . length == 4 ) {
68- // translating/parsing the response from the SERVER
69- draft . setHttpVersion ( firstLineTokens [ 0 ] );
70- draft . setHttpStatus ( Short . parseShort ( firstLineTokens [ 1 ] ) );
71- draft . setHttpStatusMessage ( firstLineTokens [ 2 ] + ' ' + firstLineTokens [ 3 ] );
72- } else if ( role == Role . SERVER && firstLineTokens . length == 3 ) {
73- // translating/parsing the request from the CLIENT
74- draft . setMethod ( firstLineTokens [ 0 ] );
75- draft . setResourceDescriptor ( firstLineTokens [ 1 ] );
76- draft . setHttpVersion ( firstLineTokens [ 2 ] );
77- } else {
78- throw new InvalidHandshakeException ( "could not match http status line" ) ;
68+ String [] firstLineTokens = line .split ( " " , 3 );// eg. HTTP/1.1 101 Switching the Protocols
69+ if ( firstLineTokens . length != 3 ) {
70+ throw new InvalidHandshakeException ();
71+ }
72+
73+ if ( role == Role . CLIENT ) { // translating/parsing the response from the SERVER
74+ handshake = new HandshakeImpl1Server ( );
75+ ServerHandshakeBuilder serverhandshake = ( ServerHandshakeBuilder ) handshake ;
76+ serverhandshake . setHttpStatus ( Short . parseShort ( firstLineTokens [ 1 ] ) );
77+ serverhandshake . setHttpStatusMessage ( firstLineTokens [ 2 ] );
78+ } else { // translating/parsing the request from the CLIENT
79+ ClientHandshakeBuilder clienthandshake = new HandshakeImpl1Client ( );
80+ clienthandshake . setResourceDescriptor ( firstLineTokens [ 1 ] );
81+ handshake = clienthandshake ;
7982 }
8083
8184 line = readStringLine ( buf );
8285 while ( line != null && line .length () > 0 ) {
8386 String [] pair = line .split ( ":" , 2 );
8487 if ( pair .length != 2 )
8588 throw new InvalidHandshakeException ( "not an http header" );
86- draft .put ( pair [ 0 ], pair [ 1 ].replaceFirst ( "^ +" , "" ) );
89+ handshake .put ( pair [ 0 ], pair [ 1 ].replaceFirst ( "^ +" , "" ) );
8790 line = readStringLine ( buf );
8891 }
89- return draft ;
92+ return handshake ;
9093 }
9194
92- public abstract HandshakeState acceptHandshakeAsClient ( Handshakedata request , Handshakedata response ) throws InvalidHandshakeException ;
95+ public abstract HandshakeState acceptHandshakeAsClient ( ClientHandshake request , ServerHandshake response ) throws InvalidHandshakeException ;
9396
94- public abstract HandshakeState acceptHandshakeAsServer ( Handshakedata handshakedata ) throws InvalidHandshakeException ;
97+ public abstract HandshakeState acceptHandshakeAsServer ( ClientHandshake handshakedata ) throws InvalidHandshakeException ;
9598
9699 protected boolean basicAccept ( Handshakedata handshakedata ) {
97100 return handshakedata .getFieldValue ( "Upgrade" ).equalsIgnoreCase ( "websocket" ) && handshakedata .getFieldValue ( "Connection" ).toLowerCase ( Locale .ENGLISH ).contains ( "upgrade" );
@@ -111,12 +114,12 @@ public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownro
111114
112115 public List <ByteBuffer > createHandshake ( Handshakedata handshakedata , Role ownrole , boolean withcontent ) {
113116 StringBuilder bui = new StringBuilder ( 100 );
114- if ( ownrole == Role . CLIENT ) {
117+ if ( handshakedata instanceof ClientHandshake ) {
115118 bui .append ( "GET " );
116- bui .append ( handshakedata .getResourceDescriptor () );
119+ bui .append ( ( ( ClientHandshake ) handshakedata ) .getResourceDescriptor () );
117120 bui .append ( " HTTP/1.1" );
118- } else if ( ownrole == Role . SERVER ) {
119- bui .append ( "HTTP/1.1 101 " + handshakedata .getHttpStatusMessage () );
121+ } else if ( handshakedata instanceof ServerHandshake ) {
122+ bui .append ( "HTTP/1.1 101 " + ( ( ServerHandshake ) handshakedata ) .getHttpStatusMessage () );
120123 } else {
121124 throw new RuntimeException ( "unknow role" );
122125 }
@@ -142,9 +145,9 @@ public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownro
142145 return Collections .singletonList ( bytebuffer );
143146 }
144147
145- public abstract HandshakeBuilder postProcessHandshakeRequestAsClient ( HandshakeBuilder request ) throws InvalidHandshakeException ;
148+ public abstract ClientHandshakeBuilder postProcessHandshakeRequestAsClient ( ClientHandshakeBuilder request ) throws InvalidHandshakeException ;
146149
147- public abstract HandshakeBuilder postProcessHandshakeResponseAsServer ( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException ;
150+ public abstract HandshakeBuilder postProcessHandshakeResponseAsServer ( ClientHandshake request , ServerHandshakeBuilder response ) throws InvalidHandshakeException ;
148151
149152 public abstract List <Framedata > translateFrame ( ByteBuffer buffer ) throws InvalidDataException ;
150153
0 commit comments