On Thu, Nov 19, 2026 at 2:44 PM Branko Čibej via dev <[email protected]> wrote:
>
>> On 18. 11. 2026 09:59, Ruediger Pluem via dev wrote:
>>
>> On 11/16/26 4:10 PM, Yann Ylavic wrote:
>>
>> diff -ru a/uri/apr_uri.c b/uri/apr_uri.c
>> --- a/uri/apr_uri.c 2026-11-16 16:02:56.759516227 +0100
>> +++ b/uri/apr_uri.c 2026-11-16 16:02:56.784734796 +0100
>> @@ -999,6 +999,7 @@
>> if (have_zone_id) {
>> percent_decode_scope_zone_id(uptr->hostname);
>> }
>> + uptr->port = apr_uri_port_of_scheme(uptr->scheme);
>> goto deal_with_path;
>> }
>> uptr->hostname = apr_pstrmemdup(p,
>>
>> Sounds sensible. Maybe AI can help us generate all the test cases such that
>> we can be sure it works and keeps working after changes.
See attached "apr-uri-default-port.patch" (including tests).
> I have an implementation ready that uses a generated minimal perfect hash
> function for lookups. The hash function is O(strlen(key)) to calculate the
> hash of the lookup key and O(1) to find the scheme. Except for the very first
> scheme in in the list, where linear lookup needs just a single strcasecmp(),
> it's faster by up to 5 times from the current lookup.
This adds quite some infrastructure to APR (Python generator,
template, generated .inc, uri_schemes.h, non-static apr__uri_schemes),
what about something like the attached "apr-uri-port-of-scheme.patch"
(including tests)?
It's simpler (IMHO), and in my testing it works faster (probably due
to the multiplies for the perfect hashing) and also avoids both
strcasecmp() (current code) and tolower() (your patch) which are
locale dependent.
Here are my numbers (ns per call, clock of 1.2 GHz, median over 5
interleaved rounds):
┌────────────────────────┬────────────────┬─────────────┬───────────────────────┐
│ scheme │ current linear │ switch │ perfect
hash │
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ http │ 13.7 │ 17.5 │ 51.8
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ https │ 33.4 │ 31.7 │ 54.3
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ prospero / Prospero │ 138.9 / 130.2 │ 27.8 / 31.3 │ 66.2 / 68.5
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ ssh / SSH │ 176.2 / 180.8 │ 35.8 / 36.8 │ 46.9 / 46.4
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ unknown │ 171.7 │ 5.8 │ 66.3
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ mean over all 24 names │ 100.8 │ 22.9 │ 53.5
│
└────────────────────────┴────────────────┴─────────────┴───────────────────────┘
Works for you?
Regards;
Yann.
apr_uri: set the scheme's default port when the authority has no port.
apr_uri_parse() left port 0 for "http://host/" while "http://host:/" got
the default port: set it in both cases. Test each path of apr_uri_parse().
---
diff --git a/include/apr_uri.h b/include/apr_uri.h
--- a/include/apr_uri.h
+++ b/include/apr_uri.h
@@ -105,7 +105,9 @@ struct apr_uri_t {
/** structure returned from gethostbyname() */
struct hostent *hostent;
- /** The port number, numeric, valid only if port_str != NULL */
+ /** The port number: the one given, else with an authority the scheme's
+ * default port (0 if unknown), else 0; port_str tells whether one was
+ * given */
apr_port_t port;
/** has the structure been initialized */
diff --git a/test/testuri.c b/test/testuri.c
--- a/test/testuri.c
+++ b/test/testuri.c
@@ -35,6 +35,7 @@ struct aup_test {
const char *query;
const char *fragment;
apr_port_t port;
+ const char *unparsed; /* apr_uri_unparse()'s result, if not uri */
};
struct aup_test aup_tests[] =
@@ -81,29 +82,112 @@ struct aup_test aup_tests[] =
},
{
"http://sonyamt:[email protected]/filespace/",
- 0, "http", "sonyamt:[email protected]", "sonyamt", "garbage", "127.0.0.1", NULL, "/filespace/", NULL, NULL, 0
+ 0, "http", "sonyamt:[email protected]", "sonyamt", "garbage", "127.0.0.1", NULL, "/filespace/", NULL, NULL, 80
},
{
"http://sonyamt:garbage@[fe80::1]/filespace/",
- 0, "http", "sonyamt:garbage@[fe80::1]", "sonyamt", "garbage", "fe80::1", NULL, "/filespace/", NULL, NULL, 0
+ 0, "http", "sonyamt:garbage@[fe80::1]", "sonyamt", "garbage", "fe80::1", NULL, "/filespace/", NULL, NULL, 80
},
{
"http://sonyamt@[fe80::1]/filespace/?arg1=store",
- 0, "http", "sonyamt@[fe80::1]", "sonyamt", NULL, "fe80::1", NULL, "/filespace/", "arg1=store", NULL, 0
+ 0, "http", "sonyamt@[fe80::1]", "sonyamt", NULL, "fe80::1", NULL, "/filespace/", "arg1=store", NULL, 80
},
{
/* https://datatracker.ietf.org/doc/html/rfc6874 */
"http://[fe80::1%25iface]/",
- 0, "http", "[fe80::1%25iface]", NULL, NULL, "fe80::1%iface", NULL, "/", NULL, NULL, 0
+ 0, "http", "[fe80::1%25iface]", NULL, NULL, "fe80::1%iface", NULL, "/", NULL, NULL, 80
},
{
/* https://datatracker.ietf.org/doc/html/rfc6874 */
"http://[ffff::1%25iface]/",
- 0, "http", "[ffff::1%25iface]", NULL, NULL, "ffff::1%25iface", NULL, "/", NULL, NULL, 0
+ 0, "http", "[ffff::1%25iface]", NULL, NULL, "ffff::1%25iface", NULL, "/", NULL, NULL, 80
+ },
+ {
+ "https://example.com/",
+ 0, "https", "example.com", NULL, NULL, "example.com", NULL, "/", NULL, NULL, 443
+ },
+ {
+ "http://example.com:/",
+ 0, "http", "example.com:", NULL, NULL, "example.com", "", "/", NULL, NULL, 80,
+ "http://example.com/"
},
{
"http://localhost",
- 0, "http", "localhost", NULL, NULL, "localhost", NULL, NULL, NULL, NULL, 0
+ 0, "http", "localhost", NULL, NULL, "localhost", NULL, NULL, NULL, NULL, 80
+ },
+ /* The port of a URI with an authority: the one given, else the scheme's
+ * default (0 for an unknown scheme), on every path of apr_uri_parse(). */
+ {
+ "http://example.com?q=1",
+ 0, "http", "example.com", NULL, NULL, "example.com", NULL, NULL, "q=1", NULL, 80
+ },
+ {
+ "http://example.com#frag",
+ 0, "http", "example.com", NULL, NULL, "example.com", NULL, NULL, NULL, "frag", 80
+ },
+ {
+ "HTTP://Example.COM/",
+ 0, "HTTP", "Example.COM", NULL, NULL, "Example.COM", NULL, "/", NULL, NULL, 80
+ },
+ {
+ "ftp://ftp.example.com/pub/",
+ 0, "ftp", "ftp.example.com", NULL, NULL, "ftp.example.com", NULL, "/pub/", NULL, NULL, 21
+ },
+ {
+ "ssh://[email protected]/",
+ 0, "ssh", "[email protected]", "user", NULL, "example.com", NULL, "/", NULL, NULL, 22
+ },
+ {
+ "http://user:[email protected]:/",
+ 0, "http", "user:[email protected]:", "user", "pass", "example.com", "", "/", NULL, NULL, 80,
+ "http://user:[email protected]/"
+ },
+ {
+ "http://[::1]/",
+ 0, "http", "[::1]", NULL, NULL, "::1", NULL, "/", NULL, NULL, 80
+ },
+ {
+ "http://[::1]:/",
+ 0, "http", "[::1]:", NULL, NULL, "::1", "", "/", NULL, NULL, 80,
+ "http://[::1]/"
+ },
+ {
+ "http://[::1]:8080/",
+ 0, "http", "[::1]:8080", NULL, NULL, "::1", "8080", "/", NULL, NULL, 8080
+ },
+ {
+ "http://example.com:0/",
+ 0, "http", "example.com:0", NULL, NULL, "example.com", "0", "/", NULL, NULL, 0,
+ "http://example.com/"
+ },
+ {
+ "https://example.com:80/",
+ 0, "https", "example.com:80", NULL, NULL, "example.com", "80", "/", NULL, NULL, 80
+ },
+ {
+ "http:///path",
+ 0, "http", "", NULL, NULL, "", NULL, "/path", NULL, NULL, 80
+ },
+ {
+ "foo://example.com/",
+ 0, "foo", "example.com", NULL, NULL, "example.com", NULL, "/", NULL, NULL, 0
+ },
+ {
+ "foo://example.com:1234/",
+ 0, "foo", "example.com:1234", NULL, NULL, "example.com", "1234", "/", NULL, NULL, 1234
+ },
+ {
+ "//example.com:8080/",
+ 0, NULL, "example.com:8080", NULL, NULL, "example.com", "8080", "/", NULL, NULL, 8080
+ },
+ /* No authority, no port */
+ {
+ "http:/path",
+ 0, "http", NULL, NULL, NULL, NULL, NULL, "/path", NULL, NULL, 0
+ },
+ {
+ "mailto:[email protected]",
+ 0, "mailto", NULL, NULL, NULL, NULL, NULL, "[email protected]", NULL, NULL, 0
},
{
"//www.apache.org/",
@@ -301,7 +385,7 @@ static void test_aup(abts_case *tc, void *data)
ABTS_INT_EQUAL(tc, t->port, info.port);
s = apr_uri_unparse(p, &info, APR_URI_UNP_REVEALPASSWORD);
- ABTS_STR_EQUAL(tc, t->uri, s);
+ ABTS_STR_EQUAL(tc, t->unparsed ? t->unparsed : t->uri, s);
s = apr_uri_unparse(p, &info, APR_URI_UNP_OMITSITEPART);
apr_uri_parse(p, s, &info);
diff --git a/uri/apr_uri.c b/uri/apr_uri.c
--- a/uri/apr_uri.c
+++ b/uri/apr_uri.c
@@ -998,6 +998,7 @@ deal_with_host:
if (have_zone_id) {
percent_decode_scope_zone_id(uptr->hostname);
}
+ uptr->port = apr_uri_port_of_scheme(uptr->scheme);
goto deal_with_path;
}
uptr->hostname = apr_pstrmemdup(p,
apr_uri: look up apr_uri_port_of_scheme() by the scheme's first character.
Replace the linear strcasecmp() search with a switch on the first
character and apr_cstr_casecmp(), which is ASCII only (locale independent).
Test every scheme, case-insensitivity, near-misses and NULL.
---
diff --git a/include/apr_uri.h b/include/apr_uri.h
--- a/include/apr_uri.h
+++ b/include/apr_uri.h
@@ -119,10 +119,11 @@ struct apr_uri_t {
/* apr_uri.c */
/**
- * Return the default port for a given scheme. The schemes recognized are
- * http, ftp, https, gopher, wais, nntp, snews, and prospero
+ * Return the default port for a given scheme (case-insensitive). The
+ * schemes recognized are those of the APR_URI_*_DEFAULT_PORT definitions
+ * above, plus z39.50r and z39.50s (the WAIS port).
* @param scheme_str The string that contains the current scheme
- * @return The default port for this scheme
+ * @return The default port for this scheme, 0 if unknown or NULL
*/
APR_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str);
diff --git a/test/testuri.c b/test/testuri.c
--- a/test/testuri.c
+++ b/test/testuri.c
@@ -339,12 +339,75 @@ static void test_uph(abts_case *tc, void *data)
}
}
+struct pos_test {
+ const char *scheme;
+ apr_port_t port;
+};
+
+static const struct pos_test pos_tests[] =
+{
+ /* every scheme recognized */
+ { "acap", APR_URI_ACAP_DEFAULT_PORT },
+ { "ftp", APR_URI_FTP_DEFAULT_PORT },
+ { "gopher", APR_URI_GOPHER_DEFAULT_PORT },
+ { "http", APR_URI_HTTP_DEFAULT_PORT },
+ { "https", APR_URI_HTTPS_DEFAULT_PORT },
+ { "imap", APR_URI_IMAP_DEFAULT_PORT },
+ { "ldap", APR_URI_LDAP_DEFAULT_PORT },
+ { "nfs", APR_URI_NFS_DEFAULT_PORT },
+ { "nntp", APR_URI_NNTP_DEFAULT_PORT },
+ { "pop", APR_URI_POP_DEFAULT_PORT },
+ { "prospero", APR_URI_PROSPERO_DEFAULT_PORT },
+ { "rtsp", APR_URI_RTSP_DEFAULT_PORT },
+ { "sip", APR_URI_SIP_DEFAULT_PORT },
+ { "snews", APR_URI_SNEWS_DEFAULT_PORT },
+ { "ssh", APR_URI_SSH_DEFAULT_PORT },
+ { "telnet", APR_URI_TELNET_DEFAULT_PORT },
+ { "tip", APR_URI_TIP_DEFAULT_PORT },
+ { "wais", APR_URI_WAIS_DEFAULT_PORT },
+ { "z39.50r", APR_URI_WAIS_DEFAULT_PORT },
+ { "z39.50s", APR_URI_WAIS_DEFAULT_PORT },
+ /* case-insensitive */
+ { "HTTP", APR_URI_HTTP_DEFAULT_PORT },
+ { "Https", APR_URI_HTTPS_DEFAULT_PORT },
+ { "Prospero", APR_URI_PROSPERO_DEFAULT_PORT },
+ { "sSh", APR_URI_SSH_DEFAULT_PORT },
+ { "Z39.50R", APR_URI_WAIS_DEFAULT_PORT },
+ /* unknown */
+ { "", 0 },
+ { "h", 0 },
+ { "htt", 0 },
+ { "httpx", 0 },
+ { "https2", 0 },
+ { "shttp", 0 },
+ { "z39.50", 0 },
+ { "z39.50x", 0 },
+ { "file", 0 },
+ { "mailto", 0 },
+ { "unknown", 0 },
+};
+
+static void test_port_of_scheme(abts_case *tc, void *data)
+{
+ apr_size_t i;
+
+ for (i = 0; i < sizeof(pos_tests) / sizeof(pos_tests[0]); i++) {
+ const struct pos_test *t = &pos_tests[i];
+ char msg[64];
+
+ apr_snprintf(msg, sizeof msg, "scheme \"%s\"", t->scheme);
+ ABTS_ASSERT(tc, msg, apr_uri_port_of_scheme(t->scheme) == t->port);
+ }
+ ABTS_INT_EQUAL(tc, 0, apr_uri_port_of_scheme(NULL));
+}
+
abts_suite *testuri(abts_suite *suite)
{
suite = ADD_SUITE(suite);
abts_run_test(suite, test_aup, NULL);
abts_run_test(suite, test_uph, NULL);
+ abts_run_test(suite, test_port_of_scheme, NULL);
return suite;
}
diff --git a/uri/apr_uri.c b/uri/apr_uri.c
--- a/uri/apr_uri.c
+++ b/uri/apr_uri.c
@@ -25,51 +25,13 @@
#include "apr.h"
#include "apr_general.h"
#include "apr_strings.h"
+#include "apr_cstr.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "apr_uri.h"
-typedef struct schemes_t schemes_t;
-
-/** Structure to store various schemes and their default ports */
-struct schemes_t {
- /** The name of the scheme */
- const char *name;
- /** The default port for the scheme */
- apr_port_t default_port;
-};
-
-/* Some WWW schemes and their default ports; this is basically /etc/services */
-/* This will become global when the protocol abstraction comes */
-/* As the schemes are searched by a linear search, */
-/* they are sorted by their expected frequency */
-static schemes_t schemes[] =
-{
- {"http", APR_URI_HTTP_DEFAULT_PORT},
- {"ftp", APR_URI_FTP_DEFAULT_PORT},
- {"https", APR_URI_HTTPS_DEFAULT_PORT},
- {"gopher", APR_URI_GOPHER_DEFAULT_PORT},
- {"ldap", APR_URI_LDAP_DEFAULT_PORT},
- {"nntp", APR_URI_NNTP_DEFAULT_PORT},
- {"snews", APR_URI_SNEWS_DEFAULT_PORT},
- {"imap", APR_URI_IMAP_DEFAULT_PORT},
- {"pop", APR_URI_POP_DEFAULT_PORT},
- {"sip", APR_URI_SIP_DEFAULT_PORT},
- {"rtsp", APR_URI_RTSP_DEFAULT_PORT},
- {"wais", APR_URI_WAIS_DEFAULT_PORT},
- {"z39.50r", APR_URI_WAIS_DEFAULT_PORT},
- {"z39.50s", APR_URI_WAIS_DEFAULT_PORT},
- {"prospero", APR_URI_PROSPERO_DEFAULT_PORT},
- {"nfs", APR_URI_NFS_DEFAULT_PORT},
- {"tip", APR_URI_TIP_DEFAULT_PORT},
- {"acap", APR_URI_ACAP_DEFAULT_PORT},
- {"telnet", APR_URI_TELNET_DEFAULT_PORT},
- {"ssh", APR_URI_SSH_DEFAULT_PORT},
- { NULL, 0xFFFF } /* unknown port */
-};
-
#define LINK_LOCAL(ipv6addr) ((strlen(ipv6addr) >= 5) && \
((ipv6addr)[4] == ':') && \
!strncasecmp(ipv6addr, "fe", 2) && \
@@ -173,14 +135,77 @@ static char *percent_encode_scope_zone_id(apr_pool_t *p, apr_uri_t const *uptr)
APR_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str)
{
- schemes_t *scheme;
-
- if (scheme_str) {
- for (scheme = schemes; scheme->name != NULL; ++scheme) {
- if (strcasecmp(scheme_str, scheme->name) == 0) {
- return scheme->default_port;
- }
- }
+ /* Some WWW schemes and their default ports (basically /etc/services),
+ * dispatched on their first character, case-insensitively (ASCII) */
+ if (!scheme_str) {
+ return 0;
+ }
+ switch (scheme_str[0]) {
+ case 'a': case 'A':
+ if (!apr_cstr_casecmp(scheme_str, "acap"))
+ return APR_URI_ACAP_DEFAULT_PORT;
+ break;
+ case 'f': case 'F':
+ if (!apr_cstr_casecmp(scheme_str, "ftp"))
+ return APR_URI_FTP_DEFAULT_PORT;
+ break;
+ case 'g': case 'G':
+ if (!apr_cstr_casecmp(scheme_str, "gopher"))
+ return APR_URI_GOPHER_DEFAULT_PORT;
+ break;
+ case 'h': case 'H':
+ if (!apr_cstr_casecmp(scheme_str, "http"))
+ return APR_URI_HTTP_DEFAULT_PORT;
+ if (!apr_cstr_casecmp(scheme_str, "https"))
+ return APR_URI_HTTPS_DEFAULT_PORT;
+ break;
+ case 'i': case 'I':
+ if (!apr_cstr_casecmp(scheme_str, "imap"))
+ return APR_URI_IMAP_DEFAULT_PORT;
+ break;
+ case 'l': case 'L':
+ if (!apr_cstr_casecmp(scheme_str, "ldap"))
+ return APR_URI_LDAP_DEFAULT_PORT;
+ break;
+ case 'n': case 'N':
+ if (!apr_cstr_casecmp(scheme_str, "nntp"))
+ return APR_URI_NNTP_DEFAULT_PORT;
+ if (!apr_cstr_casecmp(scheme_str, "nfs"))
+ return APR_URI_NFS_DEFAULT_PORT;
+ break;
+ case 'p': case 'P':
+ if (!apr_cstr_casecmp(scheme_str, "pop"))
+ return APR_URI_POP_DEFAULT_PORT;
+ if (!apr_cstr_casecmp(scheme_str, "prospero"))
+ return APR_URI_PROSPERO_DEFAULT_PORT;
+ break;
+ case 'r': case 'R':
+ if (!apr_cstr_casecmp(scheme_str, "rtsp"))
+ return APR_URI_RTSP_DEFAULT_PORT;
+ break;
+ case 's': case 'S':
+ if (!apr_cstr_casecmp(scheme_str, "snews"))
+ return APR_URI_SNEWS_DEFAULT_PORT;
+ if (!apr_cstr_casecmp(scheme_str, "sip"))
+ return APR_URI_SIP_DEFAULT_PORT;
+ if (!apr_cstr_casecmp(scheme_str, "ssh"))
+ return APR_URI_SSH_DEFAULT_PORT;
+ break;
+ case 't': case 'T':
+ if (!apr_cstr_casecmp(scheme_str, "tip"))
+ return APR_URI_TIP_DEFAULT_PORT;
+ if (!apr_cstr_casecmp(scheme_str, "telnet"))
+ return APR_URI_TELNET_DEFAULT_PORT;
+ break;
+ case 'w': case 'W':
+ if (!apr_cstr_casecmp(scheme_str, "wais"))
+ return APR_URI_WAIS_DEFAULT_PORT;
+ break;
+ case 'z': case 'Z':
+ if (!apr_cstr_casecmp(scheme_str, "z39.50r")
+ || !apr_cstr_casecmp(scheme_str, "z39.50s"))
+ return APR_URI_WAIS_DEFAULT_PORT;
+ break;
}
return 0;
}