Skip to content

Check HTTP response status code #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Arduino_ESP32_OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,25 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
return static_cast<int>(Error::HttpHeaderError);
}

/* TODO check http header 200 or else*/
/* Check HTTP response status code */
char const * http_response_ptr = strstr(http_header.c_str(), "HTTP/1.1");
if (!http_response_ptr)
{
DEBUG_ERROR("%s: Failure to extract http response from header", __FUNCTION__);
return static_cast<int>(Error::ParseHttpHeader);
}
/* Find start of numerical value. */
char * ptr = const_cast<char *>(http_response_ptr);
for (ptr += strlen("HTTP/1.1"); (*ptr != '\0') && !isDigit(*ptr); ptr++) { }
/* Extract numerical value. */
String http_response_str;
for (; isDigit(*ptr); ptr++) http_response_str += *ptr;
int const http_response = atoi(http_response_str.c_str());

if (http_response != 200) {
DEBUG_ERROR("%s: HTTP response status code = %d", __FUNCTION__, http_response);
return static_cast<int>(Error::HttpResponse);
}

/* Extract concent length from HTTP header. A typical entry looks like
* "Content-Length: 123456"
Expand All @@ -173,7 +191,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
return static_cast<int>(Error::ParseHttpHeader);
}
/* Find start of numerical value. */
char * ptr = const_cast<char *>(content_length_ptr);
ptr = const_cast<char *>(content_length_ptr);
for (; (*ptr != '\0') && !isDigit(*ptr); ptr++) { }
/* Extract numerical value. */
String content_length_str;
Expand Down
3 changes: 2 additions & 1 deletion src/Arduino_ESP32_OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class Arduino_ESP32_OTA
OtaHeaderCrc = -10,
OtaHeaterMagicNumber = -11,
OtaDownload = -12,
OtaHeaderTimeout = -13
OtaHeaderTimeout = -13,
HttpResponse = -14
};

Arduino_ESP32_OTA();
Expand Down