# URL / URI Parsing

```c
#define FIO_URL
#include "fio-stl.h"
```

Zero-copy, zero-allocation URI parser. Returns `fio_buf_info_s` slices pointing into the original string. No decoding is performed; pass the raw, encoded URI.

### Types

#### `fio_url_s`

```c
typedef struct {
  fio_buf_info_s scheme;
  fio_buf_info_s user;
  fio_buf_info_s password;
  fio_buf_info_s host;
  fio_buf_info_s port;
  fio_buf_info_s path;
  fio_buf_info_s query;
  fio_buf_info_s target;
} fio_url_s;
```

Parsed URL components. Empty components have `.buf == NULL` and `.len == 0`. Strings are **not** NUL terminated.

#### `fio_url_tls_info_s`

```c
typedef struct {
  fio_buf_info_s key;
  fio_buf_info_s cert;
  fio_buf_info_s pass;
  bool tls;
} fio_url_tls_info_s;
```

TLS material extracted from scheme or query parameters.

#### `fio_url_query_each_s`

```c
typedef struct {
  fio_buf_info_s name;
  fio_buf_info_s value;
  fio_buf_info_s private___;
} fio_url_query_each_s;
```

Iterator state for `FIO_URL_QUERY_EACH`. Do not touch `private___`.

### API Functions

#### `fio_url_parse`

```c
SFUNC fio_url_s fio_url_parse(const char *url, size_t len);
```

Parses `url` of length `len` into components. Accepts many shapes:

- `/path?query#target`
- `host:port/path?query#target`
- `user:password@host:port/path?query#target`
- `scheme://user:password@host:port/path?query#target`

Invalid input produces unpredictable results — the parser does not validate.

**Note:** `file`, `unix`, and `priv` schemes are treated as file paths; the parser puts the whole remainder into `path` and clears host/port/user/password.

#### `fio_url_is_tls`

```c
SFUNC fio_url_tls_info_s fio_url_is_tls(fio_url_s u);
```

Detects TLS from scheme or query. Implicit TLS schemes include `wss`, `https`, `sses`, `ssse`, `tcps`, `stcp`, `tls`, `ssl`, `udps`, `sudp`. Explicit TLS is enabled by query keys `tls`, `key`, `cert`, `pass`, or `password`.

### Macros

#### `FIO_URL_QUERY_EACH`

```c
#define FIO_URL_QUERY_EACH(query_buf, i)                                       \
  for (fio_url_query_each_s i = fio_url_query_each_next(                       \
           (fio_url_query_each_s){.private___ = (query_buf)});                 \
       i.name.buf;                                                             \
       i = fio_url_query_each_next(i))
```

Iterates over query parameters. The iterator does not URL-decode names or values.

### Helper Functions

#### `fio_url_query_each_next`

```c
FIO_SFUNC fio_url_query_each_s fio_url_query_each_next(fio_url_query_each_s i);
```

Advances the query iterator. Usually called only through `FIO_URL_QUERY_EACH`.

### Example

```c
#define FIO_URL
#include "fio-stl.h"

int main(void) {
  const char *s = "https://user:pass@example.com:8443/path?k=v#section";
  fio_url_s u = fio_url_parse(s, strlen(s));

  printf("scheme: %.*s\n", (int)u.scheme.len, u.scheme.buf);
  printf("host:   %.*s\n", (int)u.host.len, u.host.buf);
  printf("port:   %.*s\n", (int)u.port.len, u.port.buf);
  printf("path:   %.*s\n", (int)u.path.len, u.path.buf);

  FIO_URL_QUERY_EACH(u.query, p) {
    printf("%.*s = %.*s\n",
           (int)p.name.len, p.name.buf,
           (int)p.value.len, p.value.buf);
  }

  fio_url_tls_info_s tls = fio_url_is_tls(u);
  printf("tls: %d\n", tls.tls);
  return 0;
}
```

------------------------------------------------------------
