vhostのワイルドカード

vhostで使えるワイルドカードの確認

  • オリジナル抜粋+α
#include <stdlib.h>
#include <stdio.h>

#define apr_tolower(c) (tolower(((unsigned char)(c))))

int ap_strcasecmp_match(const char *str, const char *expected)
{
    int x, y;

    printf("* s:%s e:%s\n", str, expected);
    for (x = 0, y = 0; expected[y]; ++y, ++x) {
        printf("  x:%d y:%d s:%c e:%c\n", x, y, str[x], expected[y]);
        if (!str[x] && expected[y] != '*')
            return -1;
        if (expected[y] == '*') {
            while (expected[++y] == '*');
            if (!expected[y])
                return 0;
            while (str[x]) {
                int ret;
                if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
                    return ret;
            }
            return -1;
        }
        else if (expected[y] != '?'
                 && apr_tolower(str[x]) != apr_tolower(expected[y]))
            return 1;
    }
    return (str[x] != '\0');
}

int main(int argc, char *argv[])
{
    if (argc == 3) {
        printf("res: %d\n", ap_strcasecmp_match(argv[1], argv[2]));
    }
    exit(0);
}
  • PHP版
<?php
function ap_strcasecmp_match($_str, $_exp)
{
    printf("* s:%s e:%s\n", $_str, $_exp);
    $str = str_split($_str); $str[] = "\0";
    $exp = str_split($_exp); $str[] = "\0";
    for ($x = 0, $y = 0; isset($exp[$y]); ++$y, ++$x) {
        printf("  x:%d y:%d s:%s e:%s\n", $x, $y, $str[$x], $exp[$y]);
        if ((!$str[$x]) && ($exp[$y] != '*')) {
            return -1;
        }
        if ($exp[$y] == '*') {
            while ($exp[++$y] == '*');
            if (!$exp[$y]) {
                return 0;
            }
            while ($str[$x]) {
                if (($ret = ap_strcasecmp_match(substr($_str, $x++), substr($_exp, $y))) != 1) {
                    return $ret;
                }
            }
            return -1;
        } else if (($exp[$y] != '?') && (strtolower($str[$x]) != strtolower($exp[$y]))) {
            return 1;
        }
    }
    return $str[$x] != '\0' ? 0 : 1;
}

if ($argc == 3) {
    printf("res: %d\n", ap_strcasecmp_match($argv[1], $argv[2]));
}

  • Next