URL

Introduction

xpp::net::Url is an RAII wrapper around libx's xUrl. Parsing returns Result<Url, UrlParseError> — explicit error handling, no exceptions.

#include <xpp/net/url.h>

auto r = xpp::net::Url::parse("https://example.com:8080/path?q=1");
if (r.is_ok()) {
    auto u = std::move(r).unwrap();
    u.scheme();   // "https"
    u.host();     // "example.com"
    u.port_num(); // 8080
    u.path();     // "/path"
}

API Reference

Url

MethodReturnsDescription
parse(raw)Result<Url, UrlParseError>Parse a URL string
parse(std::string)Result<Url, UrlParseError>Parse from std::string
scheme()std::stringe.g. "https"
host()std::stringe.g. "example.com"
port_num()uint16_tExplicit or scheme default (http=80, https=443)
path()std::stringe.g. "/api"
query()std::stringe.g. "q=1"
fragment()std::stringe.g. "section1"
userinfo()std::stringe.g. "user:pass"
raw()const xUrl&Underlying libx handle

UrlParseError

VariantMeaning
EmptyInput was NULL or empty
InvalidFormatNot a valid URL (missing scheme, host, or malformed)

How it works

Url::parse() calls xUrlParse which makes an internal copy of the input string. All accessor fields point into this copy. ~Url() calls xUrlFree to release it.

The error type is a dedicated enum (UrlParseError) rather than the generic xErrno — matching the pattern of AddrParseError in addr.h.

Usage Examples

Parse and inspect

auto r = xpp::net::Url::parse("http://localhost:3000/api");
if (r.is_ok()) {
    auto u = std::move(r).unwrap();
    EXPECT_EQ(u.scheme(), "http");
    EXPECT_EQ(u.host(), "localhost");
    EXPECT_EQ(u.port_num(), 3000);
    EXPECT_EQ(u.path(), "/api");
}

Default port

port_num() returns the explicit port if present, otherwise the scheme default:

xpp::net::Url::parse("http://localhost/api").unwrap().port_num();   // 80
xpp::net::Url::parse("https://localhost/api").unwrap().port_num();  // 443

Error handling

auto r = xpp::net::Url::parse("not a url");
if (r.is_err()) {
    auto e = r.unwrap_err();  // UrlParseError::InvalidFormat
    printf("parse failed: %s\n", xpp::net::url_error_message(e));
}