mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-09-25 14:20:54 +02:00
Merge pull request #771 from glmfe/feat/add-http-redir
feat(websocket): Add websocket HTTP redirect
This commit is contained in:
@@ -64,6 +64,12 @@ static const char *TAG = "websocket_client";
|
|||||||
#define WS_OVER_TCP_SCHEME "ws"
|
#define WS_OVER_TCP_SCHEME "ws"
|
||||||
#define WS_OVER_TLS_SCHEME "wss"
|
#define WS_OVER_TLS_SCHEME "wss"
|
||||||
#define WS_HTTP_BASIC_AUTH "Basic "
|
#define WS_HTTP_BASIC_AUTH "Basic "
|
||||||
|
#define WS_HTTP_REDIRECT(code) ((code >= 300) && (code < 400))
|
||||||
|
|
||||||
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
|
||||||
|
// Features supported in 5.5.0
|
||||||
|
#define WS_TRANSPORT_REDIRECT_HEADER_SUPPORT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
const static int STOPPED_BIT = BIT0;
|
const static int STOPPED_BIT = BIT0;
|
||||||
const static int CLOSE_FRAME_SENT_BIT = BIT1; // Indicates that a close frame was sent by the client
|
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)
|
|||||||
esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT);
|
esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#if WS_TRANSPORT_REDIRECT_HEADER_SUPPORT
|
||||||
|
else if (WS_HTTP_REDIRECT(result)) {
|
||||||
|
const char *redir = esp_transport_ws_get_redir_uri(client->transport);
|
||||||
|
if (redir) {
|
||||||
|
// Redirecting to a new URI
|
||||||
|
free(client->config->uri);
|
||||||
|
|
||||||
|
client->config->uri = strdup(redir);
|
||||||
|
client->config->port = 0;
|
||||||
|
|
||||||
|
esp_websocket_client_set_uri(client, client->config->uri);
|
||||||
|
|
||||||
|
if (client->config->port == 0) {
|
||||||
|
client->config->port = esp_transport_get_default_port(client->transport);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rerun the connection with the redir uri.
|
||||||
|
client->state = WEBSOCKET_STATE_INIT;
|
||||||
|
ESP_LOGI(TAG, "Redirecting to %s", client->config->uri);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
ESP_LOGD(TAG, "Transport connected to %s://%s:%d", client->config->scheme, client->config->host, client->config->port);
|
ESP_LOGD(TAG, "Transport connected to %s://%s:%d", client->config->scheme, client->config->host, client->config->port);
|
||||||
|
|
||||||
client->state = WEBSOCKET_STATE_CONNECTED;
|
client->state = WEBSOCKET_STATE_CONNECTED;
|
||||||
|
Reference in New Issue
Block a user