@@ -404,12 +404,15 @@ void WebSocketsClient::handleClientData(void) {
404
404
}
405
405
#endif
406
406
407
+
407
408
/* *
408
409
* send the WebSocket header to Server
409
410
* @param client WSclient_t * ptr to the client struct
410
411
*/
411
412
void WebSocketsClient::sendHeader (WSclient_t * client) {
412
413
414
+ static const char * NEW_LINE = " \r\n " ;
415
+
413
416
DEBUG_WEBSOCKETS (" [WS-Client][sendHeader] sending header...\n " );
414
417
415
418
uint8_t randomKey[16 ] = { 0 };
@@ -424,45 +427,59 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
424
427
unsigned long start = micros ();
425
428
#endif
426
429
427
- String transport;
428
430
String handshake;
429
- if (!client->isSocketIO || (client->isSocketIO && client->cSessionId .length () > 0 )) {
430
- if (client->isSocketIO ) {
431
- transport = " &transport=websocket&sid=" + client->cSessionId ;
432
- }
433
- handshake = " GET " + client->cUrl + transport + " HTTP/1.1\r\n "
434
- " Host: " + _host + " :" + _port + " \r\n "
435
- " Connection: Upgrade\r\n "
436
- " Upgrade: websocket\r\n "
437
- " Sec-WebSocket-Version: 13\r\n "
438
- " Sec-WebSocket-Key: " + client->cKey + " \r\n " ;
439
-
440
- if (client->cProtocol .length () > 0 ) {
441
- handshake += " Sec-WebSocket-Protocol: " + client->cProtocol + " \r\n " ;
442
- }
443
-
444
- if (client->cExtensions .length () > 0 ) {
445
- handshake += " Sec-WebSocket-Extensions: " + client->cExtensions + " \r\n " ;
446
- }
447
-
448
- } else {
449
- handshake = " GET " + client->cUrl + " &transport=polling HTTP/1.1\r\n "
450
- " Host: " + _host + " :" + _port + " \r\n "
451
- " Connection: keep-alive\r\n " ;
452
- }
453
-
454
- handshake += " Origin: file://\r\n "
455
- " User-Agent: arduino-WebSocket-Client\r\n " ;
456
-
457
- if (client->base64Authorization .length () > 0 ) {
458
- handshake += " Authorization: Basic " + client->base64Authorization + " \r\n " ;
459
- }
460
-
461
- if (client->plainAuthorization .length () > 0 ) {
462
- handshake += " Authorization: " + client->plainAuthorization + " \r\n " ;
431
+ bool ws_header = true ;
432
+ String url = client->cUrl ;
433
+
434
+ if (client->isSocketIO ) {
435
+ if (client->cSessionId .length () == 0 ) {
436
+ url += WEBSOCKETS_STRING (" &transport=polling" );
437
+ ws_header = false ;
438
+ } else {
439
+ url += WEBSOCKETS_STRING (" &transport=websocket&sid=" );
440
+ url += client->cSessionId ;
441
+ }
463
442
}
464
443
465
- handshake += " \r\n " ;
444
+ handshake = WEBSOCKETS_STRING (" GET " );
445
+ handshake += url + WEBSOCKETS_STRING (" HTTP/1.1\r\n "
446
+ " Host: " );
447
+ handshake += _host + " :" + _port + NEW_LINE;
448
+
449
+ if (ws_header) {
450
+ handshake += WEBSOCKETS_STRING (" Connection: Upgrade\r\n "
451
+ " Upgrade: websocket\r\n "
452
+ " Sec-WebSocket-Version: 13\r\n "
453
+ " Sec-WebSocket-Key: " );
454
+ handshake += client->cKey + NEW_LINE;
455
+
456
+ if (client->cProtocol .length () > 0 ) {
457
+ handshake += WEBSOCKETS_STRING (" Sec-WebSocket-Protocol: " );
458
+ handshake +=client->cProtocol + NEW_LINE;
459
+ }
460
+
461
+ if (client->cExtensions .length () > 0 ) {
462
+ handshake += WEBSOCKETS_STRING (" Sec-WebSocket-Extensions: " );
463
+ handshake +=client->cExtensions + NEW_LINE;
464
+ }
465
+ } else {
466
+ handshake += WEBSOCKETS_STRING (" Connection: keep-alive\r\n " );
467
+ }
468
+
469
+ handshake += WEBSOCKETS_STRING (" Origin: file://\r\n "
470
+ " User-Agent: arduino-WebSocket-Client\r\n " );
471
+
472
+ if (client->base64Authorization .length () > 0 ) {
473
+ handshake += WEBSOCKETS_STRING (" Authorization: Basic " );
474
+ handshake += client->base64Authorization + NEW_LINE;
475
+ }
476
+
477
+ if (client->plainAuthorization .length () > 0 ) {
478
+ handshake += WEBSOCKETS_STRING (" Authorization: " );
479
+ handshake += client->plainAuthorization + NEW_LINE;
480
+ }
481
+
482
+ handshake += NEW_LINE;
466
483
467
484
DEBUG_WEBSOCKETS (" [WS-Client][sendHeader] handshake %s" , (uint8_t *)handshake.c_str ());
468
485
client->tcp ->write ((uint8_t *)handshake.c_str (), handshake.length ());
@@ -486,32 +503,32 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
486
503
if (headerLine->length () > 0 ) {
487
504
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine->c_str ());
488
505
489
- if (headerLine->startsWith (" HTTP/1." )) {
506
+ if (headerLine->startsWith (WEBSOCKETS_STRING ( " HTTP/1." ) )) {
490
507
// "HTTP/1.1 101 Switching Protocols"
491
508
client->cCode = headerLine->substring (9 , headerLine->indexOf (' ' , 9 )).toInt ();
492
509
} else if (headerLine->indexOf (' :' )) {
493
510
String headerName = headerLine->substring (0 , headerLine->indexOf (' :' ));
494
511
String headerValue = headerLine->substring (headerLine->indexOf (' :' ) + 2 );
495
512
496
- if (headerName.equalsIgnoreCase (" Connection" )) {
497
- if (headerValue.equalsIgnoreCase (" upgrade" )) {
513
+ if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Connection" ) )) {
514
+ if (headerValue.equalsIgnoreCase (WEBSOCKETS_STRING ( " upgrade" ) )) {
498
515
client->cIsUpgrade = true ;
499
516
}
500
- } else if (headerName.equalsIgnoreCase (" Upgrade" )) {
501
- if (headerValue.equalsIgnoreCase (" websocket" )) {
517
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Upgrade" ) )) {
518
+ if (headerValue.equalsIgnoreCase (WEBSOCKETS_STRING ( " websocket" ) )) {
502
519
client->cIsWebsocket = true ;
503
520
}
504
- } else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Accept" )) {
521
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Sec-WebSocket-Accept" ) )) {
505
522
client->cAccept = headerValue;
506
523
client->cAccept .trim (); // see rfc6455
507
- } else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Protocol" )) {
524
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Sec-WebSocket-Protocol" ) )) {
508
525
client->cProtocol = headerValue;
509
- } else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Extensions" )) {
526
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Sec-WebSocket-Extensions" ) )) {
510
527
client->cExtensions = headerValue;
511
- } else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Version" )) {
528
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Sec-WebSocket-Version" ) )) {
512
529
client->cVersion = headerValue.toInt ();
513
- } else if (headerName.equalsIgnoreCase (" Set-Cookie" )) {
514
- if (headerValue.indexOf (" HttpOnly" ) > -1 ) {
530
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING ( " Set-Cookie" ) )) {
531
+ if (headerValue.indexOf (WEBSOCKETS_STRING ( " HttpOnly" )) > -1 ) {
515
532
client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 , headerValue.indexOf (" ;" ));
516
533
} else {
517
534
client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 );
0 commit comments