Commit 6e3a750c authored by jan.koester's avatar jan.koester
Browse files

Sitemap/absolute URLs: drop default ports (:443/:80)

libhttppp's HttpUrl::print() always spells out the port, so sitemap
<loc> entries read https://tuxist.de:443/blog/gtm while canonical and
og:url say https://tuxist.de/blog/gtm.

Add blogi::stripDefaultPort() (https:443, http:80, IPv6-aware; other
ports kept), apply it to sitemap <loc> output and to joinSiteUrl(), so
every absolute URL built from the site URL is normalized too.
siteurl_test covers :443/:80/:8080/:8443 and IPv6.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent db7e14ad
Loading
Loading
Loading
Loading
+1 −1
Changes for src/blogi.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -3207,7 +3207,7 @@ void blogi::Blogi::sitemapXML(libhttppp::HttpRequest &curreq, const int tid, con
         << "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
    for (const auto map : sitemap)
    {
        smap << "<url><loc>" << map.first.print() << "</loc>";
        smap << "<url><loc>" << blogi::stripDefaultPort(map.first.print()) << "</loc>";
        if (map.second != zero_date)
            smap << "<lastmod>" << map.second << "</lastmod>";
        smap << "</url>";
+25 −1
Changes for src/siteurl.h: 25 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -9,6 +9,30 @@

namespace blogi {

    // Drops a port that is the scheme's default (":443" for https, ":80" for
    // http) from an absolute URL's authority, so "https://tuxist.de:443/blog"
    // and "https://tuxist.de/blog" come out identical. libhttppp's
    // HttpUrl::print() always spells the port out, which made sitemap <loc>
    // entries disagree with canonical/og:url. Non-default ports are kept.
    inline std::string stripDefaultPort(const std::string &url) {
        size_t scheme = url.find("://");
        if (scheme == std::string::npos) return url;
        std::string proto = url.substr(0, scheme);
        for (char &c : proto) if (c >= 'A' && c <= 'Z') c = char(c - 'A' + 'a');
        const char *def = proto == "https" ? "443" : proto == "http" ? "80" : nullptr;
        if (!def) return url;
        size_t hostStart = scheme + 3;
        size_t authEnd = url.find_first_of("/?#", hostStart);
        if (authEnd == std::string::npos) authEnd = url.size();
        // the port colon is the last ':' of the authority, past any IPv6 "[...]"
        size_t bracket = url.rfind(']', authEnd - 1);
        size_t colon = url.rfind(':', authEnd - 1);
        if (colon == std::string::npos || colon < hostStart) return url;
        if (bracket != std::string::npos && bracket >= hostStart && colon < bracket) return url;
        if (url.compare(colon + 1, authEnd - colon - 1, def) != 0) return url;
        return url.substr(0, colon) + url.substr(authEnd);
    }

    // Joins the configured site URL with a server-absolute path into the
    // page's absolute URL.
    //
@@ -34,7 +58,7 @@ namespace blogi {
                 path[sitePath.size()] == '?' || path[sitePath.size()] == '#'))
                path.erase(0, sitePath.size());
        }
        return site + path;
        return stripDefaultPort(site + path);
    }

}
+26 −0
Changes for tests/siteurl_test.cpp: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,6 +17,15 @@ static void expect(const std::string &site, const std::string &path, const std::
    }
}

static void expectStrip(const std::string &url, const std::string &want) {
    std::string got = blogi::stripDefaultPort(url);
    if (got != want) {
        std::fprintf(stderr, "FAIL stripDefaultPort(\"%s\") = \"%s\", want \"%s\"\n",
                     url.c_str(), got.c_str(), want.c_str());
        ++failures;
    }
}

int main() {
    // site URL includes the mount path (production lnx/tuxist.de config)
    expect("https://tuxist.de/blog", "/blog/staticpage/projects", "https://tuxist.de/blog/staticpage/projects");
@@ -32,6 +41,23 @@ int main() {
    // no prefix at all
    expect("https://blog.another.org", "/content/index", "https://blog.another.org/content/index");
    expect("https://blog.another.org", "content/index", "https://blog.another.org/content/index");
    // default ports are dropped, others kept (sitemap must match canonical)
    expect("https://tuxist.de:443/blog", "/blog/gtm", "https://tuxist.de/blog/gtm");
    expect("http://tuxist.de:80/blog", "/blog/gtm", "http://tuxist.de/blog/gtm");
    expect("https://tuxist.de:8080/blog", "/blog/gtm", "https://tuxist.de:8080/blog/gtm");
    expect("http://tuxist.de:443", "/blog/gtm", "http://tuxist.de:443/blog/gtm");
    expectStrip("https://tuxist.de:443/blog/gtm", "https://tuxist.de/blog/gtm");
    expectStrip("HTTPS://tuxist.de:443/blog/gtm", "HTTPS://tuxist.de/blog/gtm");
    expectStrip("http://tuxist.de:80/", "http://tuxist.de/");
    expectStrip("http://tuxist.de:80", "http://tuxist.de");
    expectStrip("https://tuxist.de:443?x=1", "https://tuxist.de?x=1");
    expectStrip("https://tuxist.de:8443/blog", "https://tuxist.de:8443/blog");
    expectStrip("http://tuxist.de:8080/blog", "http://tuxist.de:8080/blog");
    expectStrip("https://tuxist.de/blog/a:443", "https://tuxist.de/blog/a:443");
    expectStrip("https://[2001:db8::1]:443/blog", "https://[2001:db8::1]/blog");
    expectStrip("https://[2001:db8::443]/blog", "https://[2001:db8::443]/blog");
    expectStrip("https://[2001:db8::1]:8443/blog", "https://[2001:db8::1]:8443/blog");
    expectStrip("https://tuxist.de/blog", "https://tuxist.de/blog");
    if (failures == 0) std::puts("siteurl_test: all passed");
    return failures == 0 ? 0 : 1;
}