@@ -161,7 +161,25 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
161
161
return static_cast <int >(Error::HttpHeaderError);
162
162
}
163
163
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
+ }
165
183
166
184
/* Extract concent length from HTTP header. A typical entry looks like
167
185
* "Content-Length: 123456"
@@ -173,7 +191,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
173
191
return static_cast <int >(Error::ParseHttpHeader);
174
192
}
175
193
/* Find start of numerical value. */
176
- char * ptr = const_cast <char *>(content_length_ptr);
194
+ ptr = const_cast <char *>(content_length_ptr);
177
195
for (; (*ptr != ' \0 ' ) && !isDigit (*ptr); ptr++) { }
178
196
/* Extract numerical value. */
179
197
String content_length_str;
0 commit comments