FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ArduinoHttpClient/src/WebSocketClient.cpp at master · arduino-libraries/ArduinoHttpClient · GitHub
arduino-libraries
/
ArduinoHttpClient
Public
forked from
amcewen/HttpClient
Notifications
You must be signed in to change notification settings
Fork
175
Star
317
Code
Issues
51
Pull requests
15
Actions
Projects
Wiki
Security and quality
2
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ArduinoHttpClient
/
src
/
WebSocketClient.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
401 lines (333 loc) · 8.62 KB
Breadcrumbs
ArduinoHttpClient
/
src
/
WebSocketClient.cpp
Copy path
File metadata and controls
401 lines (333 loc) · 8.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
(c) Copyright Arduino. 2016
//
Released under Apache License, version 2.0
#
include
"
b64.h
"
#
include
"
WebSocketClient.h
"
WebSocketClient::WebSocketClient
(Client& aClient,
const
char
* aServerName,
uint16_t
aServerPort)
: HttpClient(aClient, aServerName, aServerPort),
iTxStarted(
false
),
iRxSize(
0
)
{
}
WebSocketClient::WebSocketClient
(Client& aClient,
const
String& aServerName,
uint16_t
aServerPort)
: HttpClient(aClient, aServerName, aServerPort),
iTxStarted(
false
),
iRxSize(
0
)
{
}
WebSocketClient::WebSocketClient
(Client& aClient,
const
IPAddress& aServerAddress,
uint16_t
aServerPort)
: HttpClient(aClient, aServerAddress, aServerPort),
iTxStarted(
false
),
iRxSize(
0
)
{
}
int
WebSocketClient::begin
(
const
char
* aPath)
{
//
start the GET request
beginRequest
();
connectionKeepAlive
();
int
status =
get
(aPath);
if
(status ==
0
)
{
uint8_t
randomKey[
16
];
char
base64RandomKey[
25
];
//
create a random key for the connection upgrade
for
(
int
i =
0
; i < (
int
)
sizeof
(randomKey); i++)
{
randomKey[i] =
random
(
0x01
,
0xff
);
}
memset
(base64RandomKey,
0x00
,
sizeof
(base64RandomKey));
b64_encode
(randomKey,
sizeof
(randomKey), (
unsigned
char
*)base64RandomKey,
sizeof
(base64RandomKey));
//
start the connection upgrade sequence
sendHeader
(
"
Upgrade
"
,
"
websocket
"
);
sendHeader
(
"
Connection
"
,
"
Upgrade
"
);
sendHeader
(
"
Sec-WebSocket-Key
"
, base64RandomKey);
sendHeader
(
"
Sec-WebSocket-Version
"
,
"
13
"
);
endRequest
();
status =
responseStatusCode
();
if
(status >
0
)
{
skipResponseHeaders
();
}
}
iRxSize =
0
;
//
status code of 101 means success
return
(status ==
101
) ?
0
: status;
}
int
WebSocketClient::begin
(
const
String& aPath)
{
return
begin
(aPath.
c_str
());
}
int
WebSocketClient::beginMessage
(
int
aType)
{
if
(iTxStarted)
{
//
fail TX already started
return
1
;
}
iTxStarted =
true
;
iTxMessageType = (aType &
0xf
);
iTxSize =
0
;
return
0
;
}
int
WebSocketClient::endMessage
()
{
if
(!iTxStarted)
{
//
fail TX not started
return
1
;
}
//
send FIN + the message type (opcode)
HttpClient::write
(
0x80
| iTxMessageType);
//
the message is masked (0x80)
//
send the length
if
(iTxSize <
126
)
{
HttpClient::write
(
0x80
| (
uint8_t
)iTxSize);
}
else
if
(iTxSize <
0xffff
)
{
HttpClient::write
(
0x80
|
126
);
HttpClient::write
((iTxSize >>
8
) &
0xff
);
HttpClient::write
((iTxSize >>
0
) &
0xff
);
}
else
{
HttpClient::write
(
0x80
|
127
);
HttpClient::write
((iTxSize >>
56
) &
0xff
);
HttpClient::write
((iTxSize >>
48
) &
0xff
);
HttpClient::write
((iTxSize >>
40
) &
0xff
);
HttpClient::write
((iTxSize >>
32
) &
0xff
);
HttpClient::write
((iTxSize >>
24
) &
0xff
);
HttpClient::write
((iTxSize >>
16
) &
0xff
);
HttpClient::write
((iTxSize >>
8
) &
0xff
);
HttpClient::write
((iTxSize >>
0
) &
0xff
);
}
uint8_t
maskKey[
4
];
//
create a random mask for the data and send
for
(
int
i =
0
; i < (
int
)
sizeof
(maskKey); i++)
{
maskKey[i] =
random
(
0xff
);
}
HttpClient::write
(maskKey,
sizeof
(maskKey));
//
mask the data and send
for
(
int
i =
0
; i < (
int
)iTxSize; i++) {
iTxBuffer[i] ^= maskKey[i %
sizeof
(maskKey)];
}
size_t
txSize = iTxSize;
iTxStarted =
false
;
iTxSize =
0
;
return
(
HttpClient::write
(iTxBuffer, txSize) == txSize) ?
0
:
1
;
}
size_t
WebSocketClient::write
(
uint8_t
aByte)
{
return
write
(&aByte,
sizeof
(aByte));
}
size_t
WebSocketClient::write
(
const
uint8_t
*aBuffer,
size_t
aSize)
{
if
(iState < eReadingBody)
{
//
have not upgraded the connection yet
return
HttpClient::write
(aBuffer, aSize);
}
if
(!iTxStarted)
{
//
fail TX not started
return
0
;
}
//
check if the write size, fits in the buffer
if
((iTxSize + aSize) >
sizeof
(iTxBuffer))
{
aSize =
sizeof
(iTxBuffer) - iTxSize;
}
//
copy data into the buffer
memcpy
(iTxBuffer + iTxSize, aBuffer, aSize);
iTxSize += aSize;
return
aSize;
}
int
WebSocketClient::parseMessage
()
{
flushRx
();
//
make sure 2 bytes (opcode + length)
//
are available
if
(
HttpClient::available
() <
2
)
{
return
0
;
}
//
read open code and length
uint8_t
opcode =
HttpClient::read
();
int
length =
HttpClient::read
();
if
((opcode &
0x0f
) ==
0
)
{
//
continuation, use previous opcode and update flags
iRxOpCode |= opcode;
}
else
{
iRxOpCode = opcode;
}
iRxMasked = (length &
0x80
);
length &=
0x7f
;
//
work out how many extended length and mask bytes follow the
//
initial opcode + length bytes
int
extendedLength =
0
;
if
(length ==
126
)
{
extendedLength =
2
;
}
else
if
(length ==
127
)
{
extendedLength =
8
;
}
int
maskLength = iRxMasked ? (
int
)
sizeof
(iRxMaskKey) :
0
;
//
make sure the remaining header bytes are available before reading
//
them, otherwise the frame header is incomplete
if
(
HttpClient::available
() < (extendedLength + maskLength))
{
return
0
;
}
//
read the RX size
iRxSize = length;
if
(length ==
126
)
{
//
read each byte in a well-defined order (the evaluation order of
//
multiple read() calls in a single expression is unspecified)
uint8_t
b1 =
HttpClient::read
();
uint8_t
b0 =
HttpClient::read
();
iRxSize = ((
uint64_t
)b1 <<
8
) | b0;
}
else
if
(length ==
127
)
{
uint8_t
b7 =
HttpClient::read
();
uint8_t
b6 =
HttpClient::read
();
uint8_t
b5 =
HttpClient::read
();
uint8_t
b4 =
HttpClient::read
();
uint8_t
b3 =
HttpClient::read
();
uint8_t
b2 =
HttpClient::read
();
uint8_t
b1 =
HttpClient::read
();
uint8_t
b0 =
HttpClient::read
();
iRxSize = ((
uint64_t
)b7 <<
56
) |
((
uint64_t
)b6 <<
48
) |
((
uint64_t
)b5 <<
40
) |
((
uint64_t
)b4 <<
32
) |
((
uint64_t
)b3 <<
24
) |
((
uint64_t
)b2 <<
16
) |
((
uint64_t
)b1 <<
8
) |
(
uint64_t
)b0;
}
//
read in the mask, if present
if
(iRxMasked)
{
for
(
int
i =
0
; i < (
int
)
sizeof
(iRxMaskKey); i++)
{
iRxMaskKey[i] =
HttpClient::read
();
}
}
iRxMaskIndex =
0
;
if
(
TYPE_CONNECTION_CLOSE
==
messageType
())
{
flushRx
();
stop
();
iRxSize =
0
;
}
else
if
(
TYPE_PING
==
messageType
())
{
beginMessage
(
TYPE_PONG
);
while
(
available
())
{
write
(
read
());
}
endMessage
();
iRxSize =
0
;
}
else
if
(
TYPE_PONG
==
messageType
())
{
flushRx
();
iRxSize =
0
;
}
return
iRxSize;
}
int
WebSocketClient::messageType
()
{
return
(iRxOpCode &
0x0f
);
}
bool
WebSocketClient::isFinal
()
{
return
((iRxOpCode &
0x80
) !=
0
);
}
String
WebSocketClient::readString
()
{
int
avail =
available
();
String s;
if
(avail >
0
)
{
s.
reserve
(avail);
for
(
int
i =
0
; i < avail; i++)
{
s += (
char
)
read
();
}
}
return
s;
}
int
WebSocketClient::ping
()
{
uint8_t
pingData[
16
];
//
create random data for the ping
for
(
int
i =
0
; i < (
int
)
sizeof
(pingData); i++)
{
pingData[i] =
random
(
0xff
);
}
beginMessage
(
TYPE_PING
);
write
(pingData,
sizeof
(pingData));
return
endMessage
();
}
int
WebSocketClient::available
()
{
if
(iState < eReadingBody)
{
return
HttpClient::available
();
}
return
iRxSize;
}
int
WebSocketClient::read
()
{
byte b;
if
(
read
(&b,
sizeof
(b)))
{
return
b;
}
return
-
1
;
}
int
WebSocketClient::read
(
uint8_t
*aBuffer,
size_t
aSize)
{
int
readCount =
HttpClient::read
(aBuffer, aSize);
if
(readCount >
0
)
{
iRxSize -= readCount;
//
unmask the RX data if needed
if
(iRxMasked)
{
for
(
int
i =
0
; i < (
int
)aSize; i++, iRxMaskIndex++)
{
aBuffer[i] ^= iRxMaskKey[iRxMaskIndex %
sizeof
(iRxMaskKey)];
}
}
}
return
readCount;
}
int
WebSocketClient::peek
()
{
int
p =
HttpClient::peek
();
if
(p != -
1
&& iRxMasked)
{
//
unmask the RX data if needed
p = (
uint8_t
)p ^ iRxMaskKey[iRxMaskIndex %
sizeof
(iRxMaskKey)];
}
return
p;
}
void
WebSocketClient::flushRx
()
{
while
(
available
())
{
read
();
}
}
Back
|
FazBrowse Home
|
New Git URL