On Thu, Nov 26, 2026 at 9:51 AM Ruediger Pluem via dev
<[email protected]> wrote:
>
> Can't we even speed up things more for https by
>
> Instead of
>
>     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;
> using
>
>
>     case 'h': case 'H':
>         if (!apr_cstr_casecmpn(scheme_str, "http", 4))
>             switch (scheme_str[4]) {
>                case '\0':
>                   return APR_URI_HTTP_DEFAULT_PORT;
>                case 's': case: 'S':
>                   return APR_URI_HTTPS_DEFAULT_PORT;
>             }
>         break;

Yes, my latest version (attached) is something like this. Not sure
Branko will like it though, to me it reads better than the previous
one :p
Index: include/apr_uri.h
===================================================================
--- include/apr_uri.h	(revision 1940632)
+++ include/apr_uri.h	(working copy)
@@ -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);
 
Index: test/testuri.c
===================================================================
--- test/testuri.c	(revision 1940632)
+++ test/testuri.c	(working copy)
@@ -339,6 +339,68 @@ 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);
@@ -345,6 +407,7 @@ abts_suite *testuri(abts_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;
 }
Index: uri/apr_uri.c
===================================================================
--- uri/apr_uri.c	(revision 1940632)
+++ uri/apr_uri.c	(working copy)
@@ -25,6 +25,7 @@
 #include "apr.h"
 #include "apr_general.h"
 #include "apr_strings.h"
+#include "apr_cstr.h"
 
 #define APR_WANT_STRFUNC
 #include "apr_want.h"
@@ -31,45 +32,6 @@
 
 #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,15 +135,122 @@ static char *percent_encode_scope_zone_id(apr_pool
 
 APR_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str)
 {
-    schemes_t *scheme;
+    if (!scheme_str) {
+        return 0;
+    }
 
-    if (scheme_str) {
-        for (scheme = schemes; scheme->name != NULL; ++scheme) {
-            if (strcasecmp(scheme_str, scheme->name) == 0) {
-                return scheme->default_port;
-            }
+    /* lowercase (ASCII/EBCDIC), compare to a const lowercase letter only */
+#if !APR_CHARSET_EBCDIC
+#define AS_LOWER(c) ((unsigned char)(c) | 0x20)
+#else
+#define AS_LOWER(c) ((unsigned char)(c) & ~0x40)
+#endif
+
+    /* whether the rest matches (from offset 1) */
+#define REST_EQ(x, y) \
+    (apr_cstr_casecmp(&(x)[1], &(y)[1]) == 0)
+
+    /* whether the rest matches up to n (from offset 1) */
+#define REST_EQn(x, y, n) \
+    (apr_cstr_casecmpn(&(x)[1], &(y)[1], (n) - 1) == 0)
+
+    /* WWW schemes and their default ports (basically /etc/services),
+     * macthed case-insensitive (no locale)
+     */
+    switch (AS_LOWER(scheme_str[0])) {
+    case 'a':
+        if (REST_EQ(scheme_str, "acap"))
+            return APR_URI_ACAP_DEFAULT_PORT;
+        break;
+
+    case 'f':
+        if (REST_EQ(scheme_str, "ftp"))
+            return APR_URI_FTP_DEFAULT_PORT;
+        break;
+
+    case 'g':
+        if (REST_EQ(scheme_str, "gopher"))
+            return APR_URI_GOPHER_DEFAULT_PORT;
+        break;
+
+    case 'h':
+        if (REST_EQn(scheme_str, "http", 4)) {
+            if (scheme_str[4] == '\0')
+                return APR_URI_HTTP_DEFAULT_PORT;
+
+            if (AS_LOWER(scheme_str[4]) == 's' && scheme_str[5] == '\0')
+                return APR_URI_HTTPS_DEFAULT_PORT;
         }
+        break;
+
+    case 'i':
+        if (REST_EQ(scheme_str, "imap"))
+            return APR_URI_IMAP_DEFAULT_PORT;
+        break;
+
+    case 'l':
+        if (REST_EQ(scheme_str, "ldap"))
+            return APR_URI_LDAP_DEFAULT_PORT;
+        break;
+
+    case 'n':
+        if (REST_EQ(scheme_str, "nntp"))
+            return APR_URI_NNTP_DEFAULT_PORT;
+
+        if (REST_EQ(scheme_str, "nfs"))
+            return APR_URI_NFS_DEFAULT_PORT;
+        break;
+
+    case 'p':
+        if (REST_EQ(scheme_str, "pop"))
+            return APR_URI_POP_DEFAULT_PORT;
+
+        if (REST_EQ(scheme_str, "prospero"))
+            return APR_URI_PROSPERO_DEFAULT_PORT;
+        break;
+
+    case 'r':
+        if (REST_EQ(scheme_str, "rtsp"))
+            return APR_URI_RTSP_DEFAULT_PORT;
+        break;
+
+    case 's':
+        if (REST_EQ(scheme_str, "snews"))
+            return APR_URI_SNEWS_DEFAULT_PORT;
+
+        if (REST_EQ(scheme_str, "sip"))
+            return APR_URI_SIP_DEFAULT_PORT;
+
+        if (REST_EQ(scheme_str, "ssh"))
+            return APR_URI_SSH_DEFAULT_PORT;
+        break;
+
+    case 't':
+        if (REST_EQ(scheme_str, "tip"))
+            return APR_URI_TIP_DEFAULT_PORT;
+
+        if (REST_EQ(scheme_str, "telnet"))
+            return APR_URI_TELNET_DEFAULT_PORT;
+        break;
+
+    case 'w':
+        if (REST_EQ(scheme_str, "wais"))
+            return APR_URI_WAIS_DEFAULT_PORT;
+        break;
+
+    case 'z':
+        if (REST_EQn(scheme_str, "z39.50", 6)) {
+            unsigned char c = AS_LOWER(scheme_str[6]);
+            if ((c == 's' || c == 'r') && scheme_str[7] == '\0')
+                return APR_URI_WAIS_DEFAULT_PORT;
+        }
+        break;
     }
+
+#undef REST_EQ
+#undef REST_EQn
+#undef AS_LOWER
+
     return 0;
 }
 

Reply via email to