Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
take care of #9 - work fine with relative url in relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidFenrir committed Apr 18, 2017
1 parent 35abefb commit 98e5b87
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions source/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Result downloadToBuffer(const char * url, u8 ** buf, u32 * bufsize)

httpcContext context;
Result ret = 0;
char *newurl = NULL;
char * newurl = NULL;
u32 statuscode = 0;
u32 contentsize = 0, readsize = 0, size = 0;
u8 * lastbuf = NULL;
Expand Down Expand Up @@ -47,18 +47,23 @@ Result downloadToBuffer(const char * url, u8 ** buf, u32 * bufsize)
}

ret = httpcGetResponseHeader(&context, "Location", newurl, 0x1000);
url = newurl; // Change pointer to the url that we just learned
httpcCloseContext(&context); // Close this context before we try the next
if (newurl[0] != '/') {
printf("Redirecting to url: %s\n",url);
ret = downloadToBuffer(newurl, buf, bufsize);
return ret;
}
else {
if (newurl != NULL) free(newurl);
printf("Error: relative urls not handled yet.\n");
return -2;

if (newurl[0] == '/') { //if the url starts with a slash, it's local
int slashpos = 0;
char * domainname = strdup(url);
if (strncmp("http", domainname, 4) == 0) slashpos = 7; //if the url in the entry starts with http:// or https:// we need to skip that
slashpos += (int )strchr(domainname+slashpos, '/');
domainname[slashpos] = '\0'; // replace the slash with a nullbyte to cut the url
char * copyurl = strdup(newurl);
sprintf(newurl, "%s%s", domainname, copyurl);
free(copyurl);
free(domainname);
}
url = newurl; // Change pointer to the url that we just learned
printf("Redirecting to url: %s\n", url);
ret = downloadToBuffer(newurl, buf, bufsize);
return ret;
}
} while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308));

Expand Down

0 comments on commit 98e5b87

Please sign in to comment.