Skip to content

Commit ec7c1c8

Browse files
committed
Allow the passage of a server certificate when downloading from URL
1 parent 6e0c3d2 commit ec7c1c8

File tree

3 files changed

+11
-27
lines changed

3 files changed

+11
-27
lines changed

main/CommandHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ int downloadFile(const uint8_t command[], uint8_t response[]) {
13171317
memcpy(&filename[strlen("/fs/")], &command[5 + command[3]], command[4 + command[3]]);
13181318

13191319
FILE* f = fopen(filename, "w");
1320-
downloadAndSaveFile(url, filename, f);
1320+
downloadAndSaveFile(url, f, 0);
13211321
fclose(f);
13221322

13231323
return 0;
@@ -1418,7 +1418,7 @@ int downloadOTA(const uint8_t command[], uint8_t response[])
14181418
response[4] = ERR_OPEN;
14191419
goto ota_cleanup;
14201420
}
1421-
downloadAndSaveFile(url, 0, f);
1421+
downloadAndSaveFile(url, f, 0);
14221422

14231423
/* Determine size of downloaded file. */
14241424
ota_size = ftell(f) - sizeof(ota_header.buf);

main/CommandHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ class CommandHandlerClass {
4545

4646
extern CommandHandlerClass CommandHandler;
4747

48-
extern "C" int downloadAndSaveFile(char* url, char* filename, FILE* f);
48+
extern "C" int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem);
4949

5050
#endif

main/http_client.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,28 @@
88

99
#define MAX_HTTP_RECV_BUFFER 128
1010

11-
static const char* TAG = "HTTP_HANDLER";
11+
static const char* TAG = "HTTP_CLIENT";
1212

13-
static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
13+
int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
1414
{
15-
switch(evt->event_id) {
16-
case HTTP_EVENT_ERROR:
17-
case HTTP_EVENT_ON_CONNECTED:
18-
case HTTP_EVENT_HEADER_SENT:
19-
case HTTP_EVENT_ON_FINISH:
20-
case HTTP_EVENT_DISCONNECTED:
21-
case HTTP_EVENT_ON_HEADER:
22-
break;
23-
case HTTP_EVENT_ON_DATA:
24-
if (!esp_http_client_is_chunked_response(evt->client)) {
25-
//fwrite((char*)evt->data, sizeof(uint8_t), evt->data_len, (FILE*)evt->user_data);
26-
}
27-
break;
28-
}
29-
return ESP_OK;
30-
}
31-
32-
int downloadAndSaveFile(char* url, char* filename, FILE* f) {
33-
3415
char *buffer = (char*)malloc(MAX_HTTP_RECV_BUFFER);
3516
if (buffer == NULL) {
3617
return -1;
3718
}
3819
esp_http_client_config_t config = {
3920
.url = url,
40-
.event_handler = _http_event_handler,
41-
.user_data = f,
21+
.cert_pem = cert_pem,
22+
.timeout_ms = 20000,
4223
};
4324

4425
esp_http_client_handle_t client = esp_http_client_init(&config);
4526
esp_err_t err;
4627
if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
28+
ESP_LOGE(TAG, "esp_http_client_open failed: %d", err);
4729
free(buffer);
4830
return -1;
4931
}
50-
int content_length = esp_http_client_fetch_headers(client);
32+
int content_length = esp_http_client_fetch_headers(client);
5133
int total_read_len = 0, read_len;
5234
while (total_read_len < content_length) {
5335
read_len = esp_http_client_read(client, buffer, MAX_HTTP_RECV_BUFFER);
@@ -56,7 +38,9 @@ int downloadAndSaveFile(char* url, char* filename, FILE* f) {
5638
break;
5739
}
5840
total_read_len += read_len;
41+
ESP_LOGV(TAG, "esp_http_client_read data received: %d, total %d", read_len, total_read_len);
5942
}
43+
ESP_LOGV(TAG, "connection closed, cleaning up, total %d bytes received", total_read_len);
6044
esp_http_client_close(client);
6145
esp_http_client_cleanup(client);
6246
free(buffer);

0 commit comments

Comments
 (0)