|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
/* Copyright 2022 Ivan Polyakov */
|
|
|
|
|
|
|
|
#include "../include/url.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
int rpd_url_parse(rpd_url *dest, const char *src)
|
|
|
|
{
|
|
|
|
const char *del = "/";
|
|
|
|
int i = count_char_entries(src, del[0]);
|
|
|
|
if (!i) {
|
|
|
|
dest->parts_len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->parts = (char **) malloc(sizeof(char **) * i);
|
|
|
|
if (!dest->parts) {
|
|
|
|
dest->parts_len = 0;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
char *tmp, *token;
|
|
|
|
tmp = rpd_strdup(src);
|
|
|
|
while ((token = rpd_strsep(&tmp, "/"))) {
|
|
|
|
if (!strlen(token))
|
|
|
|
continue;
|
|
|
|
dest->parts[i] = rpd_strdup(token);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
dest->parts_len = i;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpd_url_cleanup(rpd_url *url)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (i < url->parts_len) {
|
|
|
|
free(url->parts[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free(url->parts);
|
|
|
|
url->parts = NULL;
|
|
|
|
url->parts_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpd_url_params_parse_keys(rpd_keyval *dest, const rpd_url *tpl)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
for (int i = 0; i < tpl->parts_len; i++) {
|
|
|
|
if (*tpl->parts[i] == ':') {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->items = malloc(sizeof(rpd_keyval_item) * len);
|
|
|
|
if (!dest->items)
|
|
|
|
return 1;
|
|
|
|
dest->size = 0;
|
|
|
|
dest->capacity = len;
|
|
|
|
|
|
|
|
for (int i = 0; i < tpl->parts_len; i++) {
|
|
|
|
if (*tpl->parts[i] == ':') {
|
|
|
|
dest->items[dest->size].key = rpd_strdup(tpl->parts[i] + 1);
|
|
|
|
dest->size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpd_url_params_parse_vals(rpd_keyval *dest, const rpd_url *url, const rpd_url *tpl)
|
|
|
|
{
|
|
|
|
int i = 0, j = 0;
|
|
|
|
while (i < tpl->parts_len) {
|
|
|
|
if (*tpl->parts[i] == ':') {
|
|
|
|
dest->items[j++].val = rpd_strdup(url->parts[i]);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|