Skip to content

Commit 5ec2be4

Browse files
committed
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
2 parents 1d3d63c + a3d7711 commit 5ec2be4

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

components/tcp_transport/transport_ws.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static int esp_transport_read_internal(transport_ws_t *ws, char *buffer, int len
131131
return to_read;
132132
}
133133

134-
static char *trimwhitespace(const char *str)
134+
static char *trimwhitespace(char *str)
135135
{
136136
char *end;
137137

@@ -141,19 +141,19 @@ static char *trimwhitespace(const char *str)
141141
}
142142

143143
if (*str == 0) {
144-
return (char *)str;
144+
return str;
145145
}
146146

147147
// Trim trailing space
148-
end = (char *)(str + strlen(str) - 1);
148+
end = str + strlen(str) - 1;
149149
while (end > str && isspace((unsigned char)*end)) {
150150
end--;
151151
}
152152

153153
// Write new null terminator
154-
*(end + 1) = 0;
154+
*(end + 1) = '\0';
155155

156-
return (char *)str;
156+
return str;
157157
}
158158

159159
static int get_http_status_code(const char *buffer)
@@ -162,11 +162,11 @@ static int get_http_status_code(const char *buffer)
162162
const char *found = strcasestr(buffer, http);
163163
char status_code[4];
164164
if (found) {
165-
found += sizeof(http)/sizeof(http[0]) - 1;
165+
found += sizeof(http) - 1;
166166
found = strchr(found, ' ');
167167
if (found) {
168168
found++;
169-
strncpy(status_code, found, 4);
169+
strncpy(status_code, found, 3);
170170
status_code[3] = '\0';
171171
int code = atoi(status_code);
172172
ESP_LOGD(TAG, "HTTP status code is %d", code);
@@ -176,14 +176,14 @@ static int get_http_status_code(const char *buffer)
176176
return -1;
177177
}
178178

179-
static char *get_http_header(const char *buffer, const char *key)
179+
static char *get_http_header(char *buffer, const char *key)
180180
{
181181
char *found = strcasestr(buffer, key);
182182
if (found) {
183183
found += strlen(key);
184184
char *found_end = strstr(found, "\r\n");
185185
if (found_end) {
186-
found_end[0] = 0;//terminal string
186+
*found_end = '\0'; // terminal string
187187

188188
return trimwhitespace(found);
189189
}
@@ -307,20 +307,6 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
307307
return -1;
308308
}
309309

310-
if (delim_ptr != NULL) {
311-
size_t delim_pos = delim_ptr - ws->buffer + sizeof(delimiter) - 1;
312-
size_t remaining_len = ws->buffer_len - delim_pos;
313-
if (remaining_len > 0) {
314-
memmove(ws->buffer, ws->buffer + delim_pos, remaining_len);
315-
ws->buffer_len = remaining_len;
316-
} else {
317-
#ifdef CONFIG_WS_DYNAMIC_BUFFER
318-
free(ws->buffer);
319-
ws->buffer = NULL;
320-
#endif
321-
ws->buffer_len = 0;
322-
}
323-
}
324310
// See esp_crypto_sha1() arg size
325311
unsigned char expected_server_sha1[20];
326312
// 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
340326
ESP_LOGE(TAG, "Invalid websocket key");
341327
return -1;
342328
}
329+
330+
if (delim_ptr != NULL) {
331+
size_t delim_pos = delim_ptr - ws->buffer + sizeof(delimiter) - 1;
332+
size_t remaining_len = ws->buffer_len - delim_pos;
333+
if (remaining_len > 0) {
334+
memmove(ws->buffer, ws->buffer + delim_pos, remaining_len);
335+
ws->buffer_len = remaining_len;
336+
} else {
337+
#ifdef CONFIG_WS_DYNAMIC_BUFFER
338+
free(ws->buffer);
339+
ws->buffer = NULL;
340+
#endif
341+
ws->buffer_len = 0;
342+
}
343+
}
344+
343345
return 0;
344346
}
345347

0 commit comments

Comments
 (0)