From 622b8d63b15c6217c63f39e4eddfbde5c7462636 Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Sat, 22 Mar 2014 10:38:16 +0100 Subject: [PATCH 1/2] Fix metalink issues when type is not present --- src/ChangeLog | 6 ++++++ src/metalink.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index f222037..6e4729c 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2014-03-20 Jure Grabnar + + * metalink.c (parse_metalink): Find out resources' protocol type from URL. + Do not crash when there're no resources. + (elect_resources): Do not crash when protocol type is NULL. + 2014-03-04 Giuseppe Scrivano * http.c (modify_param_value, extract_param): Aesthetic change. diff --git a/src/metalink.c b/src/metalink.c index 76db2fa..18bc1ee 100644 --- a/src/metalink.c +++ b/src/metalink.c @@ -42,6 +42,7 @@ as that of the covered work. */ #include "sha256.h" #include "metalink.h" #include "utils.h" +#include "url.h" #define HASH_TYPES 3 @@ -134,7 +135,42 @@ parse_metalink(char *input_file) ++(file->num_of_res); resource->url = xstrdup ((*resources)->url); - resource->type = ((*resources)->type ? xstrdup ((*resources)->type) : NULL); + + if ((*resources)->type) + resource->type = xstrdup ((*resources)->type); + else + { + const char *type = NULL; + enum url_scheme res_scheme = url_scheme (resource->url); + + switch (res_scheme) + { + case SCHEME_HTTP: + case SCHEME_HTTPS: + type = "http"; + break; + case SCHEME_FTP: + type = "ftp"; + break; + default: + type = NULL; + } + + int len_url = strlen (resource->url); + if (len_url >= 8) + { + const char *url_ending = &(resource->url[len_url-8]); + if (0 == strncasecmp (url_ending, ".torrent", 8)) + type = "bittorrent"; + } + + if (type) + { + resource->type = malloc (strlen (type)); + sprintf(resource->type, type); + } + } + resource->location = ((*resources)->location ? xstrdup ((*resources)->location) : NULL); resource->preference = (*resources)->preference; resource->maxconnections = (*resources)->maxconnections; @@ -143,7 +179,7 @@ parse_metalink(char *input_file) (file->resources) = resource; } - for (checksums = (*files)->checksums; *checksums; ++checksums) + for (checksums = (*files)->checksums; checksums && *checksums; ++checksums) { mlink_checksum *checksum = malloc (sizeof(mlink_checksum)); @@ -215,7 +251,7 @@ elect_resources (mlink *mlink) while (res_next = res->next) { - if (strcmp(res_next->type, "ftp") && strcmp(res_next->type, "http")) + if (res_next->type == NULL || (strcmp(res_next->type, "ftp") && strcmp(res_next->type, "http"))) { res->next = res_next->next; free(res_next); @@ -224,7 +260,7 @@ elect_resources (mlink *mlink) res = res_next; } res = file->resources; - if (strcmp(res->type, "ftp") && strcmp(res->type, "http")) + if (res->type == NULL || (strcmp(res->type, "ftp") && strcmp(res->type, "http"))) { file->resources = res->next; free(res); -- 1.9.0