Skip to content

Commit

Permalink
Make wget_iri_set_scheme() update iri->uri
Browse files Browse the repository at this point in the history
* include/wget/wget.h (wget_iri_st): Add field uri_allocated
* libwget/iri.c: Make wget_iri_set_scheme() update iri->uri
  • Loading branch information
akashrawal committed Jul 25, 2017
1 parent e0b1a6b commit 45a5646
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/wget/wget.h
Expand Up @@ -916,6 +916,9 @@ typedef struct wget_iri_st {
/* If set, port was explicitly given */
bool
port_given;
/* If set, free uri in iri_free() */
bool
uri_allocated : true;
/* If set, free host in iri_free() */
bool
host_allocated : true;
Expand Down
21 changes: 20 additions & 1 deletion libwget/iri.c
Expand Up @@ -259,6 +259,8 @@ char *wget_iri_unescape_inline(char *src)
void wget_iri_free_content(wget_iri_t *iri)
{
if (iri) {
if (iri->uri_allocated)
xfree(iri->uri);
if (iri->host_allocated)
xfree(iri->host);
if (iri->path_allocated)
Expand Down Expand Up @@ -559,7 +561,10 @@ wget_iri_t *wget_iri_clone(const wget_iri_t *iri)
else
clone->host = iri->host ? (char *)clone + (size_t) (iri->host - (const char *)iri) : NULL;

clone->uri = iri->uri ? (char *)clone + (size_t) (iri->uri - (const char *)iri) : NULL;
if (iri->uri_allocated)
clone->uri = wget_strdup(iri->uri);
else
clone->uri = iri->uri ? (char *)clone + (size_t) (iri->uri - (const char *)iri) : NULL;
clone->display = iri->display ? (char *)clone + (size_t) (iri->display - (const char *)iri): NULL;
// not adjust scheme, it is a pointer to a static string
clone->userinfo = iri->userinfo ? (char *)clone + (size_t) (iri->userinfo - (const char *)iri): NULL;
Expand Down Expand Up @@ -1227,6 +1232,20 @@ const char *wget_iri_set_scheme(wget_iri_t *iri, const char *scheme)
}
}

// Rewrite the URI if scheme has changed
if (old_scheme != iri->scheme) {
size_t old_scheme_len = strlen(old_scheme);
if (strncmp(iri->uri, old_scheme, old_scheme_len) == 0) {
if (strncmp(iri->uri + old_scheme_len, "://", 3) == 0) {
char *new_uri = wget_aprintf("%s://%s", iri->scheme, iri->uri + old_scheme_len + 3);
if (iri->uri_allocated)
xfree(iri->uri);
iri->uri = new_uri;
iri->uri_allocated = true;
}
}
}

return old_scheme;
}

Expand Down

0 comments on commit 45a5646

Please sign in to comment.