protected function prepareHeaderName($name)
    {
        if (! is_string($name)) {
            throw new InvalidArgumentException(sprintf(
                'Invalid header name "%s" provided; must be an string.',
                $name
            ));
        }

        $name = trim($name);

        if (! $name) {
            throw new InvalidArgumentException('Header name is empty.');
        }
        if (! preg_match('/^[a-zA-Z0-9\'\!#$%&\*\+-\.^_`\|~]+$/', $name)) {
            throw new InvalidArgumentException(sprintf(
                'Header name "%s" contains illegal characters.',
                $name
            ));
        }

        return $name;
    }