@@ -64,6 +64,12 @@ static const char *TAG = "websocket_client";
64
64
#define WS_OVER_TCP_SCHEME "ws"
65
65
#define WS_OVER_TLS_SCHEME "wss"
66
66
#define WS_HTTP_BASIC_AUTH "Basic "
67
+ #define WS_HTTP_REDIRECT (code ) ((code >= 300) && (code < 400))
68
+
69
+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (5 , 5 , 0 )
70
+ // Features supported in 5.5.0
71
+ #define WS_TRANSPORT_REDIRECT_HEADER_SUPPORT 1
72
+ #endif
67
73
68
74
const static int STOPPED_BIT = BIT0 ;
69
75
const static int CLOSE_FRAME_SENT_BIT = BIT1 ; // Indicates that a close frame was sent by the client
@@ -1072,6 +1078,29 @@ static void esp_websocket_client_task(void *pv)
1072
1078
esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
1073
1079
break ;
1074
1080
}
1081
+ #if WS_TRANSPORT_REDIRECT_HEADER_SUPPORT
1082
+ else if (WS_HTTP_REDIRECT (result )) {
1083
+ const char * redir = esp_transport_ws_get_redir_uri (client -> transport );
1084
+ if (redir ) {
1085
+ // Redirecting to a new URI
1086
+ free (client -> config -> uri );
1087
+
1088
+ client -> config -> uri = strdup (redir );
1089
+ client -> config -> port = 0 ;
1090
+
1091
+ esp_websocket_client_set_uri (client , client -> config -> uri );
1092
+
1093
+ if (client -> config -> port == 0 ) {
1094
+ client -> config -> port = esp_transport_get_default_port (client -> transport );
1095
+ }
1096
+
1097
+ // Rerun the connection with the redir uri.
1098
+ client -> state = WEBSOCKET_STATE_INIT ;
1099
+ ESP_LOGI (TAG , "Redirecting to %s" , client -> config -> uri );
1100
+ break ;
1101
+ }
1102
+ }
1103
+ #endif
1075
1104
ESP_LOGD (TAG , "Transport connected to %s://%s:%d" , client -> config -> scheme , client -> config -> host , client -> config -> port );
1076
1105
1077
1106
client -> state = WEBSOCKET_STATE_CONNECTED ;
0 commit comments