Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge bumped version from upstream #1

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clib.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "url",
"version": "0.2.0",
"version": "0.2.1",
"repo": "jwerle/url.h",
"description": "Parse URLs much like Node's url module",
"keywords": ["url", "parse"],
Expand Down
87 changes: 42 additions & 45 deletions url.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ strdup (const char *str) {


static char *
strff (char *ptr, int n) {
for (int i = 0; i < n; ++i) {
strff (char *ptr, size_t n) {
for (size_t i = 0; i < n; ++i) {
(void) *ptr++;
}

return strdup(ptr);
}

static char *
strrwd (char *ptr, int n) {
for (int i = 0; i < n; ++i) {
strrwd (char *ptr, size_t n) {
for (size_t i = 0; i < n; ++i) {
(void) *ptr--;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ url_parse (char *url) {
return NULL;
}
// length of protocol plus ://
int protocol_len = (int) strlen(protocol) + 3;
const size_t protocol_len = strlen(protocol) + 3;
data->protocol = protocol;

is_ssh = url_is_ssh(protocol);
Expand All @@ -126,9 +126,7 @@ url_parse (char *url) {

data->auth = auth;

char *hostname;

hostname = (is_ssh)
char *hostname = (is_ssh)
? get_part(tmp_url, "%[^:]", protocol_len + auth_len)
: get_part(tmp_url, "%[^/]", protocol_len + auth_len);

Expand All @@ -137,11 +135,11 @@ url_parse (char *url) {
url_free(data);
return NULL;
}
int hostname_len = (int) strlen(hostname);
const size_t hostname_len = strlen(hostname);
char *tmp_hostname = strdup(hostname);
data->hostname = hostname;

char *host = (char *) malloc((strlen(tmp_hostname)+1) * sizeof(char));
char *host = (char *) malloc((strlen(tmp_hostname)+1));
sscanf(tmp_hostname, "%[^:]", host);
free(tmp_hostname);
if (!host) {
Expand All @@ -151,20 +149,18 @@ url_parse (char *url) {
}
data->host = host;

int host_len = (int)strlen(host);
const size_t host_len = strlen(host);
if (hostname_len > host_len) {
data->port = strff(hostname, host_len + 1); // +1 for ':' char;
} else {
data->port = NULL;
}

char *tmp_path;

tmp_path = (is_ssh)
char *tmp_path = (is_ssh)
? get_part(tmp_url, ":%s", protocol_len + auth_len + hostname_len)
: get_part(tmp_url, "/%s", protocol_len + auth_len + hostname_len);

char *path = (char *) malloc((strlen(tmp_path) + 2) * sizeof(char));
char *path = (char *) malloc((strlen(tmp_path) + 2));
if (!path) {
free(tmp_url);
url_free(data);
Expand All @@ -174,7 +170,7 @@ url_parse (char *url) {
sprintf(path, fmt, tmp_path);
data->path = path;

char *pathname = (char *) malloc((strlen(tmp_path) + 2) * sizeof(char));
char *pathname = (char *) malloc((strlen(tmp_path) + 2));
free(tmp_path);
if (!pathname) {
free(tmp_url);
Expand All @@ -184,7 +180,7 @@ url_parse (char *url) {
strcat(pathname, "");
tmp_path = strdup(path);
sscanf(tmp_path, "%[^? | ^#]", pathname);
int pathname_len = (int)strlen(pathname);
const size_t pathname_len = strlen(pathname);
data->pathname = pathname;

char *search = (char *) malloc(sizeof(search));
Expand All @@ -199,7 +195,7 @@ url_parse (char *url) {
strcpy(search, "");
sscanf(tmp_path, "%[^#]", search);
data->search = search;
int search_len = (int)strlen(search);
const size_t search_len = strlen(search);
free(tmp_path);

char *query = (char *) malloc(sizeof(char));
Expand Down Expand Up @@ -229,9 +225,9 @@ url_parse (char *url) {

bool
url_is_protocol (char *str) {
int count = sizeof(URL_SCHEMES) / sizeof(URL_SCHEMES[0]);
const unsigned count = sizeof(URL_SCHEMES) / sizeof(URL_SCHEMES[0]);

for (int i = 0; i < count; ++i) {
for (unsigned i = 0; i < count; ++i) {
if (0 == strcmp(URL_SCHEMES[i], str)) {
return true;
}
Expand All @@ -255,7 +251,7 @@ url_is_ssh (char *str) {

char *
url_get_protocol (char *url) {
char *protocol = (char *) malloc(URL_PROTOCOL_MAX_LENGTH * sizeof(char));
char *protocol = (char *) malloc(URL_PROTOCOL_MAX_LENGTH);
if (!protocol) return NULL;
sscanf(url, "%[^://]", protocol);
if (url_is_protocol(protocol)) return protocol;
Expand All @@ -267,22 +263,24 @@ char *
url_get_auth (char *url) {
char *protocol = url_get_protocol(url);
if (!protocol) return NULL;
int l = (int) strlen(protocol) + 3;
const size_t l = strlen(protocol) + 3;
return get_part(url, "%[^@]", l);
}

char *
url_get_hostname (char *url) {
int l = 3;
size_t l = 3;
char *protocol = url_get_protocol(url);
char *tmp_protocol = strdup(protocol);
char *auth = url_get_auth(url);

if (!protocol) return NULL;
if (auth) l += strlen(auth) + 1; // add one @ symbol
if (auth) free(auth);
if (auth) {
l += strlen(auth) + 1; // add one @ symbol
free(auth);
}

l += (int) strlen(protocol);
l += strlen(protocol);

free(protocol);

Expand Down Expand Up @@ -324,7 +322,7 @@ url_get_pathname (char *url) {

char *
url_get_path (char *url) {
int l = 3;
size_t l = 3;
char *tmp_path;
char *protocol = url_get_protocol(url);
char *auth = url_get_auth(url);
Expand All @@ -336,25 +334,24 @@ url_get_path (char *url) {

bool is_ssh = url_is_ssh(protocol);

l += (int) strlen(protocol) + (int) strlen(hostname);
l += strlen(protocol) + strlen(hostname);

if (auth) l+= (int) strlen(auth) +1; // @ symbol
if (auth) l+= strlen(auth) +1; // @ symbol

tmp_path = (is_ssh)
? get_part(url, ":%s", l)
: get_part(url, "/%s", l);

const char *fmt = (is_ssh)? "%s" : "/%s";
char *path = (char *) malloc(strlen(tmp_path) * sizeof(char));
char *path = (char *) malloc(strlen(tmp_path));
sprintf(path, fmt, tmp_path);

if (auth) free(auth);
free(auth);
free(protocol);
free(hostname);
free(tmp_path);

return path;

}

char *
Expand Down Expand Up @@ -399,8 +396,8 @@ url_get_hash (char *url) {
if (!pathname) return NULL;
char *search = url_get_search(url);

int pathname_len = (int) strlen(pathname);
int search_len = (int) strlen(search);
const size_t pathname_len = strlen(pathname);
const size_t search_len = strlen(search);
char *tmp_path = strff(path, pathname_len + search_len);

strcat(hash, "");
Expand All @@ -409,7 +406,7 @@ url_get_hash (char *url) {
free(tmp_path);
free(pathname);
free(path);
if (search) free(search);
free(search);

return hash;
}
Expand Down Expand Up @@ -454,15 +451,15 @@ url_data_inspect (url_data_t *data) {
void
url_free (url_data_t *data) {
if (!data) return;
if (data->auth) free(data->auth);
if (data->protocol) free(data->protocol);
if (data->hostname) free(data->hostname);
if (data->host) free(data->host);
if (data->pathname) free(data->pathname);
if (data->path) free(data->path);
if (data->hash) free(data->hash);
if (data->port) free(data->port);
if (data->search) free(data->search);
if (data->query) free(data->query);
free(data->auth);
free(data->protocol);
free(data->hostname);
free(data->host);
free(data->pathname);
free(data->path);
free(data->hash);
free(data->port);
free(data->search);
free(data->query);
free(data);
}
2 changes: 1 addition & 1 deletion url.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* url.h version
*/

#define URL_VERSION 0.2.0
#define URL_VERSION 0.2.1


/**
Expand Down