MCPcopy Create free account
hub / github.com/JoaoLopesF/RemoteDebug / sendHeader

Method sendHeader

src/utility/WebSocketsClient.cpp:460–545  ·  view source on GitHub ↗

* send the WebSocket header to Server * @param client WSclient_t * ptr to the client struct */

Source from the content-addressed store, hash-verified

458 * @param client WSclient_t * ptr to the client struct
459 */
460void WebSocketsClient::sendHeader(WSclient_t * client) {
461
462 static const char * NEW_LINE = "\r\n";
463
464 DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
465
466 uint8_t randomKey[16] = { 0 };
467
468 for(uint8_t i = 0; i < sizeof(randomKey); i++) {
469 randomKey[i] = random(0xFF);
470 }
471
472 client->cKey = base64_encode(&randomKey[0], 16);
473
474#ifndef NODEBUG_WEBSOCKETS
475 unsigned long start = micros();
476#endif
477
478 String handshake;
479 bool ws_header = true;
480 String url = client->cUrl;
481
482 if(client->isSocketIO) {
483 if(client->cSessionId.length() == 0) {
484 url += WEBSOCKETS_STRING("&transport=polling");
485 ws_header = false;
486 } else {
487 url += WEBSOCKETS_STRING("&transport=websocket&sid=");
488 url += client->cSessionId;
489 }
490 }
491
492 handshake = WEBSOCKETS_STRING("GET ");
493 handshake += url + WEBSOCKETS_STRING(" HTTP/1.1\r\n"
494 "Host: ");
495 handshake += _host + ":" + _port + NEW_LINE;
496
497 if(ws_header) {
498 handshake += WEBSOCKETS_STRING("Connection: Upgrade\r\n"
499 "Upgrade: websocket\r\n"
500 "Sec-WebSocket-Version: 13\r\n"
501 "Sec-WebSocket-Key: ");
502 handshake += client->cKey + NEW_LINE;
503
504 if(client->cProtocol.length() > 0) {
505 handshake += WEBSOCKETS_STRING("Sec-WebSocket-Protocol: ");
506 handshake += client->cProtocol + NEW_LINE;
507 }
508
509 if(client->cExtensions.length() > 0) {
510 handshake += WEBSOCKETS_STRING("Sec-WebSocket-Extensions: ");
511 handshake += client->cExtensions + NEW_LINE;
512 }
513 } else {
514 handshake += WEBSOCKETS_STRING("Connection: keep-alive\r\n");
515 }
516
517 // add extra headers; by default this includes "Origin: file://"

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected