|
20 | 20 |
|
21 | 21 | #include "mbed.h"
|
22 | 22 | #include "Socket.h"
|
| 23 | +#include "mbedtls/base64.h" |
23 | 24 |
|
24 | 25 | #ifdef MBED_WS_HAS_MBED_HTTP
|
25 | 26 | #include "http_request.h"
|
|
42 | 43 | #define MBED_WS_PING_INTERVAL_MS 10000
|
43 | 44 | #endif
|
44 | 45 |
|
| 46 | +#ifndef MBED_WS_USER_AGENT |
| 47 | +#define MBED_WS_USER_AGENT "Mbed-WS-Client" |
| 48 | +#endif |
| 49 | + |
45 | 50 | // #define MBED_WS_DEBUG 1
|
46 | 51 |
|
47 | 52 | // this library returns nsapi_error_t codes, plus these
|
@@ -174,22 +179,24 @@ class WebsocketClientBase {
|
174 | 179 | return r;
|
175 | 180 | }
|
176 | 181 |
|
177 |
| - // @todo: calculate new keys myself |
178 |
| - // var key = 'L159VM0TWUzyDxwJEIEzjw==' |
179 |
| - // var combined = 'L159VM0TWUzyDxwJEIEzjw==' + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' |
180 |
| - // var h = require('crypto').createHash('sha1') |
181 |
| - // h.update(combined).digest('base64') |
182 |
| - |
183 | 182 | // This might seem weird... because we support both ws:// and wss://
|
184 | 183 | // but we already have a good working socket with TLS connection, and so the only thing
|
185 | 184 | // we do is act on that socket. So it's fine to reference HttpRequest
|
186 | 185 | // the TCPSocket casting is also weird, but it's just setting pointers, so it's fine for now
|
187 | 186 | // This might break if Mbed HTTP changes inner workings though!!
|
| 187 | + |
| 188 | + uint8_t randomBytes[16], wsSecKey[24]; |
| 189 | + for (size_t i = 0; i < 16; i++) { |
| 190 | + randomBytes[i] = rand(); |
| 191 | + } |
| 192 | + mbedtls_base64_encode(&wsSecKey[0], sizeof(wsSecKey), NULL, &randomBytes[0], sizeof(randomBytes)); |
| 193 | + |
188 | 194 | HttpRequest* req = new HttpRequest((TCPSocket*)_socket, HTTP_GET, _url);
|
189 | 195 | req->set_header("Upgrade", "Websocket");
|
190 | 196 | req->set_header("Connection", "Upgrade");
|
191 | 197 | req->set_header("Sec-WebSocket-Key", "L159VM0TWUzyDxwJEIEzjw==");
|
192 | 198 | req->set_header("Sec-WebSocket-Version", "13");
|
| 199 | + req->set_header("User-Agent", MBED_WS_USER_AGENT); |
193 | 200 |
|
194 | 201 | HttpResponse* res = req->send();
|
195 | 202 | if (!res) {
|
|
0 commit comments