@@ -42,60 +42,33 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::startOTA() {
42
42
}
43
43
);
44
44
45
- // make the http get request
45
+ // check url
46
46
if (strcmp (context->parsed_url .schema (), " https" ) == 0 ) {
47
47
http_client = new HttpClient (*client, context->parsed_url .host (), context->parsed_url .port ());
48
48
} else {
49
49
return UrlParseErrorFail;
50
50
}
51
51
52
- http_client->beginRequest ();
53
- auto res = http_client->get (context->parsed_url .path ());
54
-
55
- if (username != nullptr && password != nullptr ) {
56
- http_client->sendBasicAuth (username, password);
57
- }
58
-
59
- http_client->endRequest ();
60
-
61
- if (res == HTTP_ERROR_CONNECTION_FAILED) {
62
- DEBUG_VERBOSE (" OTA ERROR: http client error connecting to server \" %s:%d\" " ,
63
- context->parsed_url .host (), context->parsed_url .port ());
64
- return ServerConnectErrorFail;
65
- } else if (res == HTTP_ERROR_TIMED_OUT) {
66
- DEBUG_VERBOSE (" OTA ERROR: http client timeout \" %s\" " , OTACloudProcessInterface::context->url );
67
- return OtaHeaderTimeoutFail;
68
- } else if (res != HTTP_SUCCESS) {
69
- DEBUG_VERBOSE (" OTA ERROR: http client returned %d on get \" %s\" " , res, OTACloudProcessInterface::context->url );
70
- return OtaDownloadFail;
71
- }
72
-
73
- int statusCode = http_client->responseStatusCode ();
74
-
75
- if (statusCode != 200 ) {
76
- DEBUG_VERBOSE (" OTA ERROR: get response on \" %s\" returned status %d" , OTACloudProcessInterface::context->url , statusCode);
77
- return HttpResponseFail;
78
- }
79
-
80
- context->contentLength = http_client->contentLength ();
52
+ // make the http get request
53
+ requestOta (OtaFetchTime);
81
54
82
55
// The following call is required to save the header value , keep it
56
+ context->contentLength = http_client->contentLength ();
83
57
if (context->contentLength == HttpClient::kNoContentLengthHeader ) {
84
58
DEBUG_VERBOSE (" OTA ERROR: the response header doesn't contain \" ContentLength\" field" );
85
59
return HttpHeaderErrorFail;
86
60
}
87
61
88
62
context->lastReportTime = millis ();
89
63
DEBUG_VERBOSE (" OTA file length: %d" , context->contentLength );
90
-
91
64
return Fetch;
92
65
}
93
66
94
67
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch () {
95
68
OTACloudProcessInterface::State res = Fetch;
96
69
97
70
if (fetchMode == OtaFetchChunk) {
98
- res = requestChunk ( );
71
+ res = requestOta (OtaFetchChunk );
99
72
}
100
73
101
74
context->downloadedChunkSize = 0 ;
@@ -169,10 +142,8 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
169
142
return res;
170
143
}
171
144
172
- OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk ( ) {
145
+ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestOta (OTAFetchMode mode ) {
173
146
int http_res = 0 ;
174
- uint32_t start = millis ();
175
- char range[128 ] = {0 };
176
147
177
148
/* stop connected client */
178
149
http_client->stop ();
@@ -185,10 +156,14 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk()
185
156
http_client->sendBasicAuth (username, password);
186
157
}
187
158
188
- size_t rangeSize = context->downloadedSize + maxChunkSize > context->contentLength ? context->contentLength - context->downloadedSize : maxChunkSize;
189
- sprintf (range, " bytes=%d-%d" , context->downloadedSize , context->downloadedSize + rangeSize);
190
- DEBUG_VERBOSE (" OTA downloading range: %s" , range);
191
- http_client->sendHeader (" Range" , range);
159
+ if (mode == OtaFetchChunk) {
160
+ char range[128 ] = {0 };
161
+ size_t rangeSize = context->downloadedSize + maxChunkSize > context->contentLength ? context->contentLength - context->downloadedSize : maxChunkSize;
162
+ sprintf (range, " bytes=%d-%d" , context->downloadedSize , context->downloadedSize + rangeSize);
163
+ DEBUG_VERBOSE (" OTA downloading range: %s" , range);
164
+ http_client->sendHeader (" Range" , range);
165
+ }
166
+
192
167
http_client->endRequest ();
193
168
194
169
if (http_res == HTTP_ERROR_CONNECTION_FAILED) {
@@ -205,7 +180,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk()
205
180
206
181
int statusCode = http_client->responseStatusCode ();
207
182
208
- if (statusCode != 206 ) {
183
+ if (((mode == OtaFetchChunk) && ( statusCode != 206 )) || ((mode == OtaFetchTime) && (statusCode != 200 )) ) {
209
184
DEBUG_VERBOSE (" OTA ERROR: get response on \" %s\" returned status %d" , OTACloudProcessInterface::context->url , statusCode);
210
185
return HttpResponseFail;
211
186
}
0 commit comments