@@ -128,6 +128,8 @@ static char *strnstr(const char *haystack, const char *needle, size_t haystack_l
128128extern char * strnstr (const char * haystack , const char * needle , size_t haystack_sz );
129129#endif
130130
131+ static int parse_protocol (struct downloader * dl , const char * url );
132+
131133static int http_get_request_send (struct downloader * dl )
132134{
133135 int err ;
@@ -224,7 +226,19 @@ static int http_header_parse(struct downloader *dl, size_t buf_len)
224226 parse_len = buf_len ;
225227 }
226228
229+ /* Convert HTTP headers to lowercase, but not the values (for example URI) */
230+ bool value = false;
227231 for (size_t i = 0 ; i < parse_len ; i ++ ) {
232+ if (dl -> cfg .buf [i ] == '\r' || dl -> cfg .buf [i ] == '\n' ) {
233+ value = false;
234+ }
235+ if (value ) {
236+ continue ;
237+ }
238+ if (dl -> cfg .buf [i ] == ':' ) {
239+ value = true;
240+ continue ;
241+ }
228242 dl -> cfg .buf [i ] = tolower (dl -> cfg .buf [i ]);
229243 }
230244
@@ -251,11 +265,17 @@ static int http_header_parse(struct downloader *dl, size_t buf_len)
251265 if (q ) {
252266
253267 /* Received entire line */
254- p += strlen ("\r\nlocation:" );
268+ p += strlen ("\r\nlocation: " );
255269 * q = '\0' ;
256270
257271 LOG_INF ("Resource moved to %s" , p );
258272
273+ err = parse_protocol (dl , p );
274+ if (err ) {
275+ LOG_ERR ("Failed to parse protocol, err %d, url %s" , err , p );
276+ return - EBADMSG ;
277+ }
278+
259279 err = dl_parse_url_host (p , dl -> hostname , sizeof (dl -> hostname ));
260280 if (err ) {
261281 LOG_ERR ("Failed to parse hostname, err %d, url %s" , err , p );
@@ -441,30 +461,24 @@ static bool dl_http_proto_supported(struct downloader *dl, const char *url)
441461 return false;
442462}
443463
444- static int dl_http_init (struct downloader * dl , struct downloader_host_cfg * dl_host_cfg ,
445- const char * url )
464+ static int parse_protocol (struct downloader * dl , const char * url )
446465{
447466 int err ;
448467 struct transport_params_http * http ;
449468
450469 http = (struct transport_params_http * )dl -> transport_internal ;
451470
452- /* Reset http internal struct except config. */
453- struct downloader_transport_http_cfg tmp_cfg = http -> cfg ;
454-
455- memset (http , 0 , sizeof (struct transport_params_http ));
456- http -> cfg = tmp_cfg ;
457471
458472 http -> sock .proto = IPPROTO_TCP ;
459473 http -> sock .type = SOCK_STREAM ;
460474
461475 if (strncmp (url , HTTPS , (sizeof (HTTPS ) - 1 )) == 0 ||
462476 (strncmp (url , HTTP , (sizeof (HTTP ) - 1 )) != 0 &&
463- (dl_host_cfg -> sec_tag_count != 0 && dl_host_cfg -> sec_tag_list != NULL ))) {
477+ (dl -> host_cfg . sec_tag_count != 0 && dl -> host_cfg . sec_tag_list != NULL ))) {
464478 http -> sock .proto = IPPROTO_TLS_1_2 ;
465479 http -> sock .type = SOCK_STREAM ;
466480
467- if (dl_host_cfg -> sec_tag_list == NULL || dl_host_cfg -> sec_tag_count == 0 ) {
481+ if (dl -> host_cfg . sec_tag_list == NULL || dl -> host_cfg . sec_tag_count == 0 ) {
468482 LOG_WRN ("No security tag provided for TLS/DTLS" );
469483 return - EINVAL ;
470484 }
@@ -483,14 +497,30 @@ static int dl_http_init(struct downloader *dl, struct downloader_host_cfg *dl_ho
483497 LOG_DBG ("Port not specified, using default: %d" , http -> sock .port );
484498 }
485499
486- if (dl_host_cfg -> set_native_tls ) {
500+ if (dl -> host_cfg . set_native_tls ) {
487501 LOG_DBG ("Enabled native TLS" );
488502 http -> sock .type |= SOCK_NATIVE_TLS ;
489503 }
490504
491505 return 0 ;
492506}
493507
508+ static int dl_http_init (struct downloader * dl , struct downloader_host_cfg * dl_host_cfg ,
509+ const char * url )
510+ {
511+ struct transport_params_http * http ;
512+
513+ http = (struct transport_params_http * )dl -> transport_internal ;
514+
515+ /* Reset http internal struct except config. */
516+ struct downloader_transport_http_cfg tmp_cfg = http -> cfg ;
517+
518+ memset (http , 0 , sizeof (struct transport_params_http ));
519+ http -> cfg = tmp_cfg ;
520+
521+ return parse_protocol (dl , url );
522+ }
523+
494524static int dl_http_deinit (struct downloader * dl )
495525{
496526 struct transport_params_http * http ;
0 commit comments