Merge branch 'fix/websocket_first_packet_v5.3' into 'release/v5.3'

fix(ws_transport): fixed `server-key` corruption (backport v5.3)

See merge request espressif/esp-idf!30963
This commit is contained in:
Jiang Jiang Jian
2024-05-22 07:41:55 +08:00

View File

@ -131,7 +131,7 @@ static int esp_transport_read_internal(transport_ws_t *ws, char *buffer, int len
return to_read;
}
static char *trimwhitespace(const char *str)
static char *trimwhitespace(char *str)
{
char *end;
@ -141,19 +141,19 @@ static char *trimwhitespace(const char *str)
}
if (*str == 0) {
return (char *)str;
return str;
}
// Trim trailing space
end = (char *)(str + strlen(str) - 1);
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) {
end--;
}
// Write new null terminator
*(end + 1) = 0;
*(end + 1) = '\0';
return (char *)str;
return str;
}
static int get_http_status_code(const char *buffer)
@ -162,11 +162,11 @@ static int get_http_status_code(const char *buffer)
const char *found = strcasestr(buffer, http);
char status_code[4];
if (found) {
found += sizeof(http)/sizeof(http[0]) - 1;
found += sizeof(http) - 1;
found = strchr(found, ' ');
if (found) {
found++;
strncpy(status_code, found, 4);
strncpy(status_code, found, 3);
status_code[3] = '\0';
int code = atoi(status_code);
ESP_LOGD(TAG, "HTTP status code is %d", code);
@ -176,14 +176,14 @@ static int get_http_status_code(const char *buffer)
return -1;
}
static char *get_http_header(const char *buffer, const char *key)
static char *get_http_header(char *buffer, const char *key)
{
char *found = strcasestr(buffer, key);
if (found) {
found += strlen(key);
char *found_end = strstr(found, "\r\n");
if (found_end) {
found_end[0] = 0;//terminal string
*found_end = '\0'; // terminal string
return trimwhitespace(found);
}
@ -307,20 +307,6 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
return -1;
}
if (delim_ptr != NULL) {
size_t delim_pos = delim_ptr - ws->buffer + sizeof(delimiter) - 1;
size_t remaining_len = ws->buffer_len - delim_pos;
if (remaining_len > 0) {
memmove(ws->buffer, ws->buffer + delim_pos, remaining_len);
ws->buffer_len = remaining_len;
} else {
#ifdef CONFIG_WS_DYNAMIC_BUFFER
free(ws->buffer);
ws->buffer = NULL;
#endif
ws->buffer_len = 0;
}
}
// See esp_crypto_sha1() arg size
unsigned char expected_server_sha1[20];
// Size of base64 coded string see above
@ -340,6 +326,22 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
ESP_LOGE(TAG, "Invalid websocket key");
return -1;
}
if (delim_ptr != NULL) {
size_t delim_pos = delim_ptr - ws->buffer + sizeof(delimiter) - 1;
size_t remaining_len = ws->buffer_len - delim_pos;
if (remaining_len > 0) {
memmove(ws->buffer, ws->buffer + delim_pos, remaining_len);
ws->buffer_len = remaining_len;
} else {
#ifdef CONFIG_WS_DYNAMIC_BUFFER
free(ws->buffer);
ws->buffer = NULL;
#endif
ws->buffer_len = 0;
}
}
return 0;
}