3
3
4
4
#define DEBUGGING
5
5
6
- WebSocket::WebSocket (byte ip[], const char *urlPrefix, int inPort) :
6
+ WebSocket::WebSocket (const char *urlPrefix, int inPort) :
7
7
socket_server(inPort),
8
8
socket_client(255 ),
9
9
socket_actions_population(0 ),
10
10
socket_urlPrefix(urlPrefix)
11
11
{
12
- ipAddress[0 ]=ip[0 ];
13
- ipAddress[1 ]=ip[1 ];
14
- ipAddress[2 ]=ip[2 ];
15
- ipAddress[3 ]=ip[3 ];
16
- port=inPort;
17
12
}
18
13
19
14
void WebSocket::begin () {
@@ -75,13 +70,13 @@ bool WebSocket::analyzeRequest(int bufferLength) {
75
70
// OK, it's a websockets handshake for sure
76
71
foundupgrade = true ;
77
72
} else if (temp.startsWith (" Origin: " )) {
78
- origin = temp.substring (8 ,temp.length ());
73
+ origin = temp.substring (8 ,temp.length () - 2 ); // Don't save last CR+LF
79
74
} else if (temp.startsWith (" Host: " )) {
80
- host = temp.substring (6 ,temp.length ());
75
+ host = temp.substring (6 ,temp.length () - 2 ); // Don't save last CR+LF
81
76
} else if (temp.startsWith (" Sec-WebSocket-Key1" )) {
82
- key[0 ]=temp.substring (20 ,temp.length ());
77
+ key[0 ]=temp.substring (20 ,temp.length () - 2 ); // Don't save last CR+LF
83
78
} else if (temp.startsWith (" Sec-WebSocket-Key2" )) {
84
- key[1 ]=temp.substring (20 ,temp.length ());
79
+ key[1 ]=temp.substring (20 ,temp.length () - 2 ); // Don't save last CR+LF
85
80
}
86
81
temp = " " ;
87
82
}
@@ -94,8 +89,8 @@ bool WebSocket::analyzeRequest(int bufferLength) {
94
89
if (foundupgrade == true && host.length () > 0 && key[0 ].length () > 0 && key[1 ].length () > 0 ) {
95
90
// All ok, proceed with challenge and MD5 digest
96
91
char key3[9 ] = {0 };
97
- // The last 8 bytes of temp should contain the third key
98
- temp.toCharArray (key3, 9 ); // TODO: 8 only?
92
+ // What now is in temp should be the third key
93
+ temp.toCharArray (key3, 9 );
99
94
100
95
// Process keys
101
96
for (int i = 0 ; i <= 1 ; i++) {
@@ -117,8 +112,6 @@ bool WebSocket::analyzeRequest(int bufferLength) {
117
112
}
118
113
119
114
unsigned char challenge[16 ] = {0 };
120
-
121
- // Big Endian
122
115
challenge[0 ] = (unsigned char ) ((intkey[0 ] >> 24 ) & 0xFF );
123
116
challenge[1 ] = (unsigned char ) ((intkey[0 ] >> 16 ) & 0xFF );
124
117
challenge[2 ] = (unsigned char ) ((intkey[0 ] >> 8 ) & 0xFF );
@@ -131,45 +124,27 @@ bool WebSocket::analyzeRequest(int bufferLength) {
131
124
memcpy (challenge + 8 , key3, 8 );
132
125
133
126
unsigned char md5Digest[16 ];
134
-
135
127
MD5 (challenge, md5Digest, 16 );
136
128
137
129
#ifdef DEBUGGING
138
130
Serial.println (" Sending response header" );
139
131
#endif
140
- socket_client.write (" HTTP/1.1 101 Web Socket Protocol Handshake" );
141
- socket_client.write (CRLF);
142
- socket_client.write (" Upgrade: WebSocket" );
143
- socket_client.write (CRLF);
144
- socket_client.write (" Connection: Upgrade" );
145
- socket_client.write (CRLF);
146
- socket_client.write (" Sec-WebSocket-Origin: " );
147
-
148
- // TODO: Fix this mess
149
- char corigin[origin.length () - 1 ];
150
- origin.toCharArray (corigin, origin.length () - 1 );
151
- socket_client.write (corigin);
152
- socket_client.write (CRLF);
153
-
154
- // TODO: Clean up this mess!
155
-
156
- // Assign buffer for conversions
157
- char buf[10 ];
132
+ socket_client.print (" HTTP/1.1 101 Web Socket Protocol Handshake\r\n " );
133
+ socket_client.print (" Upgrade: WebSocket\r\n " );
134
+ socket_client.print (" Connection: Upgrade\r\n " );
135
+ socket_client.print (" Sec-WebSocket-Origin: " );
136
+ socket_client.print (origin);
137
+ socket_client.print (CRLF);
158
138
159
139
// The "Host:" value should be used as location
160
- socket_client.write (" Sec-WebSocket-Location: ws://" );
161
- char cHost[host.length () - 1 ];
162
- host.toCharArray (cHost, host.length () - 1 );
163
- socket_client.write (cHost);
164
- socket_client.write (socket_urlPrefix);
165
- socket_client.write (CRLF);
166
- socket_client.write (CRLF);
140
+ socket_client.print (" Sec-WebSocket-Location: ws://" );
141
+ socket_client.print (host);
142
+ socket_client.print (socket_urlPrefix);
143
+ socket_client.print (CRLF);
144
+ socket_client.print (CRLF);
167
145
168
146
socket_client.write (md5Digest, 16 );
169
147
170
-
171
- // Need to keep global state
172
- socket_reading = true ;
173
148
return true ;
174
149
} else {
175
150
// Nope, failed handshake. Disconnect
@@ -214,64 +189,6 @@ void WebSocket::socketStream(int socketBufferLength) {
214
189
}
215
190
}
216
191
217
- /*
218
- void WebSocket::socketStream(int socketBufferLength) {
219
- while (socket_reading) {
220
- char bite;
221
- int frameLength = 0;
222
- // String to hold bytes sent by client to server.
223
- String socketString = String(socketBufferLength);
224
- // Timeout timeframe variable.
225
- unsigned long timeoutTime = millis() + TIMEOUT_IN_MS;
226
-
227
- if (!socket_client.connected()) {
228
- #ifdef DEBUGGING
229
- Serial.println("No connection!");
230
- #endif
231
- socket_reading = false;
232
- return;
233
- }
234
-
235
- // While there is a client stream to read...
236
- while ((bite = socket_client.read()) && socket_reading) {
237
- if (bite == 0)
238
- continue; // Don't save this byte
239
- if ((uint8_t) bite == 0xFF) {
240
- // Frame end
241
- // Timeout check.
242
- unsigned long currentTime = millis();
243
- if ((currentTime > timeoutTime) && !socket_client.connected()) {
244
- #ifdef DEBUGGING
245
- Serial.println("Connection timed out");
246
- #endif
247
- disconnectStream();
248
- return;
249
- } else {
250
- break; // Break out and process the frame
251
- }
252
- }
253
-
254
- // Append everything but Frame Start and Frame End to socketString
255
- socketString += bite;
256
- if (++frameLength > MAX_FRAME_LENGTH) {
257
- // Too big to handle! Abort and disconnect.
258
- #ifdef DEBUGGING
259
- Serial.print("Client send frame exceeding ");
260
- Serial.print(MAX_FRAME_LENGTH);
261
- Serial.print("bytes");
262
- #endif
263
- disconnectStream();
264
- socket_reading = false;
265
- return;
266
- }
267
- }
268
-
269
- // Process the String.
270
- executeActions(socketString);
271
- }
272
- }
273
- */
274
-
275
192
void WebSocket::addAction (Action *socketAction) {
276
193
#ifdef DEBUGGING
277
194
Serial.println (" Adding actions" );
@@ -286,10 +203,10 @@ void WebSocket::disconnectStream() {
286
203
Serial.println (" Terminating socket" );
287
204
#endif
288
205
// Should send 0xFF00 to client to tell it I'm quitting here.
206
+ // TODO: Check if I understood this properly
289
207
socket_client.write ((uint8_t ) 0xFF );
290
208
socket_client.write ((uint8_t ) 0x00 );
291
209
292
- socket_reading = false ;
293
210
socket_client.flush ();
294
211
delay (1 );
295
212
socket_client.stop ();
@@ -311,8 +228,8 @@ void WebSocket::sendData(const char *str) {
311
228
Serial.println (str);
312
229
#endif
313
230
if (socket_client.connected ()) {
314
- socket_client.write ((uint8_t ) 0x00 ); // Frame start
315
- socket_client.write (str);
316
- socket_client.write ((uint8_t ) 0xFF ); // Frame end
231
+ socket_client.print ((uint8_t ) 0x00 ); // Frame start
232
+ socket_client.print (str);
233
+ socket_client.print ((uint8_t ) 0xFF ); // Frame end
317
234
}
318
235
}
0 commit comments