Skip to content

Commit ab42ca0

Browse files
committed
Check HTTP response status code
1 parent 78def8d commit ab42ca0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Diff for: src/Arduino_ESP32_OTA.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,25 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
161161
return static_cast<int>(Error::HttpHeaderError);
162162
}
163163

164-
/* TODO check http header 200 or else*/
164+
/* Check HTTP response status code */
165+
char const * http_response_ptr = strstr(http_header.c_str(), "HTTP/1.1");
166+
if (!http_response_ptr)
167+
{
168+
DEBUG_ERROR("%s: Failure to extract http response from header", __FUNCTION__);
169+
return static_cast<int>(Error::ParseHttpHeader);
170+
}
171+
/* Find start of numerical value. */
172+
char * ptr = const_cast<char *>(http_response_ptr);
173+
for (ptr += strlen("HTTP/1.1"); (*ptr != '\0') && !isDigit(*ptr); ptr++) { }
174+
/* Extract numerical value. */
175+
String http_response_str;
176+
for (; isDigit(*ptr); ptr++) http_response_str += *ptr;
177+
int const http_response = atoi(http_response_str.c_str());
178+
179+
if (http_response != 200) {
180+
DEBUG_ERROR("%s: HTTP response status code = %d", __FUNCTION__, http_response);
181+
return static_cast<int>(Error::HttpResponse);
182+
}
165183

166184
/* Extract concent length from HTTP header. A typical entry looks like
167185
* "Content-Length: 123456"
@@ -173,7 +191,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
173191
return static_cast<int>(Error::ParseHttpHeader);
174192
}
175193
/* Find start of numerical value. */
176-
char * ptr = const_cast<char *>(content_length_ptr);
194+
ptr = const_cast<char *>(content_length_ptr);
177195
for (; (*ptr != '\0') && !isDigit(*ptr); ptr++) { }
178196
/* Extract numerical value. */
179197
String content_length_str;

Diff for: src/Arduino_ESP32_OTA.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class Arduino_ESP32_OTA
7272
OtaHeaderCrc = -10,
7373
OtaHeaterMagicNumber = -11,
7474
OtaDownload = -12,
75-
OtaHeaderTimeout = -13
75+
OtaHeaderTimeout = -13,
76+
HttpResponse = -14
7677
};
7778

7879
Arduino_ESP32_OTA();

0 commit comments

Comments
 (0)