2
2
3
3
import android .text .TextUtils ;
4
4
import android .util .Base64 ;
5
+ import android .util .Log ;
5
6
import org .apache .http .*;
6
7
import org .apache .http .message .BasicLineParser ;
7
8
import org .apache .http .message .BasicNameValuePair ;
8
9
9
10
import javax .net .SocketFactory ;
10
11
import javax .net .ssl .SSLSocketFactory ;
12
+ import java .io .EOFException ;
11
13
import java .io .IOException ;
12
- import java .io .InputStreamReader ;
13
14
import java .io .OutputStream ;
14
15
import java .io .PrintWriter ;
15
16
import java .net .Socket ;
@@ -74,17 +75,17 @@ public void run() {
74
75
out .print ("\r \n " );
75
76
out .flush ();
76
77
77
- InputStreamReader reader = new InputStreamReader (mSocket .getInputStream ());
78
+ HybiParser . HappyDataInputStream stream = new HybiParser . HappyDataInputStream (mSocket .getInputStream ());
78
79
79
80
// Read HTTP response status line.
80
- StatusLine statusLine = parseStatusLine (readLine (reader ));
81
+ StatusLine statusLine = parseStatusLine (readLine (stream ));
81
82
if (statusLine .getStatusCode () != HttpStatus .SC_SWITCHING_PROTOCOLS ) {
82
83
throw new ProtocolException ("Bad HTTP response: " + statusLine );
83
84
}
84
85
85
86
// Read HTTP response headers.
86
87
String line ;
87
- while (!TextUtils .isEmpty (line = readLine (reader ))) {
88
+ while (!TextUtils .isEmpty (line = readLine (stream ))) {
88
89
Header header = parseHeader (line );
89
90
if (header .getName ().equals ("Sec-WebSocket-Accept" )) {
90
91
// FIXME: Verify the response...
@@ -94,7 +95,11 @@ public void run() {
94
95
mHandler .onConnect ();
95
96
96
97
// Now decode websocket frames.
97
- mParser .start (mSocket );
98
+ mParser .start (stream );
99
+
100
+ } catch (EOFException ex ) {
101
+ Log .d (TAG , "WebSocket EOF!" , ex );
102
+ mHandler .onDisconnect (0 , "EOF" );
98
103
99
104
} catch (Exception ex ) {
100
105
mHandler .onError (ex );
@@ -125,17 +130,21 @@ private Header parseHeader(String line) {
125
130
}
126
131
127
132
// Can't use BufferedReader because it buffers past the HTTP data.
128
- private String readLine (InputStreamReader reader ) throws IOException {
133
+ private String readLine (HybiParser . HappyDataInputStream reader ) throws IOException {
129
134
int readChar = reader .read ();
130
135
if (readChar == -1 ) {
131
136
return null ;
132
137
}
133
138
StringBuilder string = new StringBuilder ("" );
134
- while (readChar != - 1 && readChar != '\n' ) {
139
+ while (readChar != '\n' ) {
135
140
if (readChar != '\r' ) {
136
141
string .append ((char ) readChar );
137
142
}
143
+
138
144
readChar = reader .read ();
145
+ if (readChar == -1 ) {
146
+ return null ;
147
+ }
139
148
}
140
149
return string .toString ();
141
150
}
0 commit comments