为什么 symfony 要求 headers 设置为二进制值,目的是什么?

why symfony request headers set to binary values, what is the purpose?

const HEADER_FORWARDED = 0b00001; // When using RFC 7239
const HEADER_X_FORWARDED_FOR = 0b00010;
const HEADER_X_FORWARDED_HOST = 0b00100;
const HEADER_X_FORWARDED_PROTO = 0b01000;
const HEADER_X_FORWARDED_PORT = 0b10000;
const HEADER_X_FORWARDED_ALL = 0b11110; // All "X-Forwarded-*" headers
const HEADER_X_FORWARDED_AWS_ELB = 0b11010; // AWS ELB doesn't send X-Forwarded-Host

我无法理解这个,为什么他们设置它?

他们这样做是为了能够使用 bitwise operators

Symfony\Component\HttpKernel\HttpCache\SubRequestHandler\handle中我们可以找到这个:

// save global state related to trusted headers and proxies
$trustedProxies = Request::getTrustedProxies();
$trustedHeaderSet = Request::getTrustedHeaderSet();

// remove untrusted values
$remoteAddr = $request->server->get('REMOTE_ADDR');
if (!IpUtils::checkIp($remoteAddr, $trustedProxies)) {
    $trustedHeaders = array(
        'FORWARDED' => $trustedHeaderSet & Request::HEADER_FORWARDED,
        'X_FORWARDED_FOR' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_FOR,
        'X_FORWARDED_HOST' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_HOST,
        'X_FORWARDED_PROTO' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_PROTO,
        'X_FORWARDED_PORT' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_PORT,
    );
    foreach (array_filter($trustedHeaders) as $name => $key) {
        $request->headers->remove($name);
    }
}

二进制符号本身只是一种使其显而易见的方式,从 PHP 的角度来看,它只是一个常规整数。