@@ -547,45 +547,50 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
547
547
*/
548
548
void WebSocketsClient::handleHeader (WSclient_t * client, String * headerLine) {
549
549
550
- headerLine->trim (); // remove \r
550
+ headerLine->trim (); // remove \r
551
551
552
- if (headerLine->length () > 0 ) {
553
- DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine->c_str ());
552
+ if (headerLine->length () > 0 ) {
553
+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine->c_str ());
554
554
555
- if (headerLine->startsWith (WEBSOCKETS_STRING (" HTTP/1." ))) {
556
- // "HTTP/1.1 101 Switching Protocols"
557
- client->cCode = headerLine->substring (9 , headerLine->indexOf (' ' , 9 )).toInt ();
558
- } else if (headerLine->indexOf (' :' )) {
559
- String headerName = headerLine->substring (0 , headerLine->indexOf (' :' ));
560
- String headerValue = headerLine->substring (headerLine->indexOf (' :' ) + 2 );
555
+ if (headerLine->startsWith (WEBSOCKETS_STRING (" HTTP/1." ))) {
556
+ // "HTTP/1.1 101 Switching Protocols"
557
+ client->cCode = headerLine->substring (9 , headerLine->indexOf (' ' , 9 )).toInt ();
558
+ } else if (headerLine->indexOf (' :' )) {
559
+ String headerName = headerLine->substring (0 , headerLine->indexOf (' :' ));
560
+ String headerValue = headerLine->substring (headerLine->indexOf (' :' ) + 1 );
561
561
562
- if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Connection" ))) {
563
- if (headerValue.equalsIgnoreCase (WEBSOCKETS_STRING (" upgrade" ))) {
564
- client->cIsUpgrade = true ;
565
- }
566
- } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Upgrade" ))) {
567
- if (headerValue.equalsIgnoreCase (WEBSOCKETS_STRING (" websocket" ))) {
568
- client->cIsWebsocket = true ;
569
- }
570
- } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Accept" ))) {
571
- client->cAccept = headerValue;
572
- client->cAccept .trim (); // see rfc6455
573
- } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Protocol" ))) {
574
- client->cProtocol = headerValue;
575
- } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Extensions" ))) {
576
- client->cExtensions = headerValue;
577
- } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Version" ))) {
578
- client->cVersion = headerValue.toInt ();
579
- } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Set-Cookie" ))) {
580
- if (headerValue.indexOf (WEBSOCKETS_STRING (" HttpOnly" )) > -1 ) {
581
- client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 , headerValue.indexOf (" ;" ));
582
- } else {
583
- client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 );
584
- }
585
- }
586
- } else {
587
- DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine->c_str ());
588
- }
562
+ // remove space in the beginning (RFC2616)
563
+ if (headerValue[0 ] == ' ' ) {
564
+ headerValue.remove (0 , 1 );
565
+ }
566
+
567
+ if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Connection" ))) {
568
+ if (headerValue.equalsIgnoreCase (WEBSOCKETS_STRING (" upgrade" ))) {
569
+ client->cIsUpgrade = true ;
570
+ }
571
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Upgrade" ))) {
572
+ if (headerValue.equalsIgnoreCase (WEBSOCKETS_STRING (" websocket" ))) {
573
+ client->cIsWebsocket = true ;
574
+ }
575
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Accept" ))) {
576
+ client->cAccept = headerValue;
577
+ client->cAccept .trim (); // see rfc6455
578
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Protocol" ))) {
579
+ client->cProtocol = headerValue;
580
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Extensions" ))) {
581
+ client->cExtensions = headerValue;
582
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Sec-WebSocket-Version" ))) {
583
+ client->cVersion = headerValue.toInt ();
584
+ } else if (headerName.equalsIgnoreCase (WEBSOCKETS_STRING (" Set-Cookie" ))) {
585
+ if (headerValue.indexOf (WEBSOCKETS_STRING (" HttpOnly" )) > -1 ) {
586
+ client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 , headerValue.indexOf (" ;" ));
587
+ } else {
588
+ client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 );
589
+ }
590
+ }
591
+ } else {
592
+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine->c_str ());
593
+ }
589
594
590
595
(*headerLine) = " " ;
591
596
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
0 commit comments