@@ -128,6 +128,8 @@ static char *strnstr(const char *haystack, const char *needle, size_t haystack_l
128
128
extern char * strnstr (const char * haystack , const char * needle , size_t haystack_sz );
129
129
#endif
130
130
131
+ static int parse_protocol (struct downloader * dl , const char * url );
132
+
131
133
static int http_get_request_send (struct downloader * dl )
132
134
{
133
135
int err ;
@@ -224,7 +226,19 @@ static int http_header_parse(struct downloader *dl, size_t buf_len)
224
226
parse_len = buf_len ;
225
227
}
226
228
229
+ /* Convert HTTP headers to lowercase, but not the values (for example URI) */
230
+ bool value = false;
227
231
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
+ }
228
242
dl -> cfg .buf [i ] = tolower (dl -> cfg .buf [i ]);
229
243
}
230
244
@@ -251,11 +265,17 @@ static int http_header_parse(struct downloader *dl, size_t buf_len)
251
265
if (q ) {
252
266
253
267
/* Received entire line */
254
- p += strlen ("\r\nlocation:" );
268
+ p += strlen ("\r\nlocation: " );
255
269
* q = '\0' ;
256
270
257
271
LOG_INF ("Resource moved to %s" , p );
258
272
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
+
259
279
err = dl_parse_url_host (p , dl -> hostname , sizeof (dl -> hostname ));
260
280
if (err ) {
261
281
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)
441
461
return false;
442
462
}
443
463
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 )
446
465
{
447
466
int err ;
448
467
struct transport_params_http * http ;
449
468
450
469
http = (struct transport_params_http * )dl -> transport_internal ;
451
470
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 ;
457
471
458
472
http -> sock .proto = IPPROTO_TCP ;
459
473
http -> sock .type = SOCK_STREAM ;
460
474
461
475
if (strncmp (url , HTTPS , (sizeof (HTTPS ) - 1 )) == 0 ||
462
476
(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 ))) {
464
478
http -> sock .proto = IPPROTO_TLS_1_2 ;
465
479
http -> sock .type = SOCK_STREAM ;
466
480
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 ) {
468
482
LOG_WRN ("No security tag provided for TLS/DTLS" );
469
483
return - EINVAL ;
470
484
}
@@ -483,14 +497,30 @@ static int dl_http_init(struct downloader *dl, struct downloader_host_cfg *dl_ho
483
497
LOG_DBG ("Port not specified, using default: %d" , http -> sock .port );
484
498
}
485
499
486
- if (dl_host_cfg -> set_native_tls ) {
500
+ if (dl -> host_cfg . set_native_tls ) {
487
501
LOG_DBG ("Enabled native TLS" );
488
502
http -> sock .type |= SOCK_NATIVE_TLS ;
489
503
}
490
504
491
505
return 0 ;
492
506
}
493
507
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
+
494
524
static int dl_http_deinit (struct downloader * dl )
495
525
{
496
526
struct transport_params_http * http ;
0 commit comments