Skip to content
Myr

uri.c

This file

Path

uri.c

278 lines · 7.8 KiB · C

Last changed by

Not known.

History of this file

Take it

1#include "uri.h"23#include <stdlib.h>4#include <string.h>5#include <limits.h>6#include <unistd.h>78#define LOG_MODULE "uri"9#define LOG_ENABLE_DBG 010#include "log.h"11#include "debug.h"12#include "macros.h"13#include "xmalloc.h"1415enum {16    HEX_DIGIT_INVALID = 1617};1819static inline uint8_t20hex2nibble(char c)21{22    switch (c) {23    case '0': case '1': case '2': case '3': case '4':24    case '5': case '6': case '7': case '8': case '9':25        return c - '0';2627    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':28        return c - 'a' + 10;2930    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':31        return c - 'A' + 10;32    }3334    return HEX_DIGIT_INVALID;35}3637bool38uri_parse(const char *uri, size_t len,39          char **scheme, char **user, char **password, char **host,40          uint16_t *port, char **path, char **query, char **fragment)41{42    LOG_DBG("parse URI: \"%.*s\"", (int)len, uri);4344    if (scheme != NULL)   *scheme = NULL;45    if (user != NULL)     *user = NULL;46    if (password != NULL) *password = NULL;47    if (host != NULL)     *host = NULL;48    if (port != NULL)     *port = 0;49    if (path != NULL)     *path = NULL;50    if (query != NULL)    *query = NULL;51    if (fragment != NULL) *fragment = NULL;5253    size_t left = len;54    const char *start = uri;55    const char *end = NULL;5657    if ((end = memchr(start, ':', left)) == NULL)58        goto err;5960    size_t scheme_len = end - start;61    if (scheme_len == 0)62        goto err;6364    if (scheme != NULL)65        *scheme = xstrndup(start, scheme_len);6667    LOG_DBG("scheme: \"%.*s\"", (int)scheme_len, start);6869    start = end + 1;70    left = len - (start - uri);7172    /* Authinfo */73    if (left >= 2 && start[0] == '/' && start[1] == '/') {74        start += 2;75        left -= 2;7677        /* [user[:password]@]@host[:port] */7879        /* Find beginning of path segment (required component80         * following the authinfo) */81        const char *path_segment = memchr(start, '/', left);82        if (path_segment == NULL)83            goto err;8485        size_t auth_left = path_segment - start;8687        /* Do we have a user (and optionally a password)? */88        const char *user_pw_end = memchr(start, '@', auth_left);89        if (user_pw_end != NULL) {90            size_t user_pw_len = user_pw_end - start;9192            /* Do we have a password? */93            const char *user_end = memchr(start, ':', user_pw_end - start);94            if (user_end != NULL) {95                size_t user_len = user_end - start;96                if (user_len == 0)97                    goto err;9899                if (user != NULL)100                    *user = xstrndup(start, user_len);101102                const char *pw = user_end + 1;103                size_t pw_len = user_pw_end - pw;104                if (pw_len == 0)105                    goto err;106107                if (password != NULL)108                    *password = xstrndup(pw, pw_len);109110                LOG_DBG("user: \"%.*s\"", (int)user_len, start);111                LOG_DBG("password: \"%.*s\"", (int)pw_len, pw);112            } else {113                size_t user_len = user_pw_end - start;114                if (user_len == 0)115                    goto err;116117                if (user != NULL)118                    *user = xstrndup(start, user_len);119120                LOG_DBG("user: \"%.*s\"", (int)user_len, start);121            }122123            start = user_pw_end + 1;124            left = len - (start - uri);125            auth_left -= user_pw_len + 1;126        }127128        const char *host_end = memchr(start, ':', auth_left);129        if (host_end != NULL) {130            size_t host_len = host_end - start;131            if (host != NULL)132                *host = xstrndup(start, host_len);133134            const char *port_str = host_end + 1;135            size_t port_len = path_segment - port_str;136            if (port_len == 0)137                goto err;138139            uint16_t _port = 0;140            for (size_t i = 0; i < port_len; i++) {141                if (!(port_str[i] >= '0' && port_str[i] <= '9'))142                    goto err;143144                _port *= 10;145                _port += port_str[i] - '0';146            }147148            if (port != NULL)149                *port = _port;150151            LOG_DBG("host: \"%.*s\"", (int)host_len, start);152            LOG_DBG("port: \"%.*s\" (%hu)", (int)port_len, port_str, _port);153        } else {154            size_t host_len = path_segment - start;155            if (host != NULL)156                *host = xstrndup(start, host_len);157158            LOG_DBG("host: \"%.*s\"", (int)host_len, start);159        }160161        start = path_segment;162        left = len - (start - uri);163    }164165    /* Do we have a query? */166    const char *query_start = memchr(start, '?', left);167    const char *fragment_start = memchr(start, '#', left);168169    size_t path_len =170        query_start != NULL ? query_start - start :171        fragment_start != NULL ? fragment_start - start :172        left;173174    if (path_len == 0)175        goto err;176177    /* Path - decode %xx encoded characters */178    if (path != NULL) {179        const char *encoded = start;180        char *decoded = xmalloc(path_len + 1);181        char *p = decoded;182183        size_t encoded_len = path_len;184        size_t UNUSED decoded_len = 0;185186        while (true) {187            /* Find next '%' */188            const char *next = memchr(encoded, '%', encoded_len);189190            if (next == NULL) {191                strncpy(p, encoded, encoded_len);192                decoded_len += encoded_len;193                p += encoded_len;194                break;195            }196197            /* Copy everything leading up to the '%' */198            size_t prefix_len = next - encoded;199            memcpy(p, encoded, prefix_len);200201            p += prefix_len;202            encoded_len -= prefix_len;203            decoded_len += prefix_len;204205            if (hex2nibble(next[1]) <= 15 && hex2nibble(next[2]) <= 15) {206                *p++ = hex2nibble(next[1]) << 4 | hex2nibble(next[2]);207                decoded_len++;208                encoded_len -= 3;209                encoded = next + 3;210            } else {211                *p++ = *next;212                decoded_len++;213                encoded_len -= 1;214                encoded = next + 1;215            }216        }217218        *p = '\0';219        *path = decoded;220221        LOG_DBG("path: encoded=\"%.*s\", decoded=\"%s\"", (int)path_len, start, decoded);222    } else223        LOG_DBG("path: encoded=\"%.*s\", decoded=<skipped>", (int)path_len, start);224225    start = query_start != NULL ? query_start + 1 : fragment_start != NULL ? fragment_start + 1 : uri + len;226    left = len - (start - uri);227228    if (query_start != NULL) {229        size_t query_len = fragment_start != NULL230            ? fragment_start - start : left;231232        if (query_len == 0)233            goto err;234235        if (query != NULL)236            *query = xstrndup(start, query_len);237238        LOG_DBG("query: \"%.*s\"", (int)query_len, start);239240        start = fragment_start != NULL ? fragment_start + 1 : uri + len;241        left = len - (start - uri);242    }243244    if (fragment_start != NULL) {245        if (left == 0)246            goto err;247248        if (fragment != NULL)249            *fragment = xstrndup(start, left);250251        LOG_DBG("fragment: \"%.*s\"", (int)left, start);252    }253254    return true;255256err:257    if (scheme != NULL)   free(*scheme);258    if (user != NULL)     free(*user);259    if (password != NULL) free(*password);260    if (host != NULL)     free(*host);261    if (path != NULL)     free(*path);262    if (query != NULL)    free(*query);263    if (fragment != NULL) free(*fragment);264    return false;265}266267bool268hostname_is_localhost(const char *hostname)269{270    char this_host[_POSIX_HOST_NAME_MAX];271    if (gethostname(this_host, sizeof(this_host)) < 0)272        this_host[0] = '\0';273274    return (hostname != NULL && (275                strcmp(hostname, "") == 0 ||276                strcmp(hostname, "localhost") == 0||277                strcmp(hostname, this_host) == 0));278}