url-parser.c

URL parsing library
git clone git://git.finwo.net/lib/url-parser.c
Log | Files | Refs | README | LICENSE

README.md (2555B)


      1 # url-parser
      2 
      3 A C library for parsing URLs, with support for Unix socket paths and empty hosts.
      4 
      5 ## Installation
      6 
      7 This library can be installed using the [dep](https://github.com/finwo/dep) package manager:
      8 
      9 ```bash
     10 dep add finwo/url-parser
     11 ```
     12 
     13 ## Usage
     14 
     15 ```c
     16 #include "url-parser.h"
     17 
     18 struct parsed_url *purl = parse_url("http://www.example.com/path?query=value#fragment");
     19 
     20 if (purl != NULL) {
     21     printf("Scheme:   %s\n", purl->scheme);
     22     printf("Host:     %s\n", purl->host);
     23     printf("Port:     %s\n", purl->port);
     24     printf("Path:     %s\n", purl->path);
     25     printf("Query:    %s\n", purl->query);
     26     printf("Fragment: %s\n", purl->fragment);
     27     printf("Username: %s\n", purl->username);
     28     printf("Password: %s\n", purl->password);
     29     
     30     parsed_url_free(purl);
     31 }
     32 ```
     33 
     34 ## Features
     35 
     36 ### Standard URL Parsing
     37 
     38 ```c
     39 parse_url("http://example.com/path");
     40 // scheme: "http", host: "example.com", path: "/path"
     41 
     42 parse_url("https://example.com:8080/path");
     43 // scheme: "https", host: "example.com", port: "8080", path: "/path"
     44 
     45 parse_url("ftp://user:password@ftp.example.com/file");
     46 // scheme: "ftp", host: "ftp.example.com", username: "user", password: "password", path: "/file"
     47 
     48 parse_url("http://[::1]:8080/path");
     49 // scheme: "http", host: "::1", port: "8080", path: "/path"
     50 ```
     51 
     52 ### Query and Fragment Only
     53 
     54 ```c
     55 parse_url("http://example.com?foo=bar");
     56 // scheme: "http", host: "example.com", query: "foo=bar"
     57 
     58 parse_url("http://example.com#section");
     59 // scheme: "http", host: "example.com", fragment: "section"
     60 ```
     61 
     62 ### Empty Host
     63 
     64 ```c
     65 parse_url("tcp://:6379");
     66 // scheme: "tcp", host: NULL, port: "6379"
     67 ```
     68 
     69 ### Unix Socket URLs
     70 
     71 The library supports Unix socket paths with various formats:
     72 
     73 ```c
     74 // Standard unix socket
     75 parse_url("unix:///var/run/redis.sock");
     76 // scheme: "unix", path: "/var/run/redis.sock", host: NULL
     77 
     78 // Unix socket with leading slash
     79 parse_url("unix:/path/to/socket");
     80 // scheme: "unix", path: "/path/to/socket"
     81 
     82 // Unix socket without slashes
     83 parse_url("unix:redis.sock");
     84 // scheme: "unix", path: "redis.sock"
     85 
     86 // Unix socket with credentials
     87 parse_url("unix://user:pass@/path/to/socket");
     88 // scheme: "unix", username: "user", password: "pass", path: "/path/to/socket"
     89 
     90 // Redis/Postgres style unix socket
     91 parse_url("redis:///var/run/redis.sock");
     92 // scheme: "redis", path: "/var/run/redis.sock"
     93 ```
     94 
     95 Note: For path-based schemes (`unix:`, `file:`, `cunix:`), the entire portion after `://` is treated as the path, and no host or port is parsed.
     96 
     97 ## License
     98 
     99 Copyright (c) 2026 finwo. See LICENSE.md for details.