Neos\Flow\Http\Cookie::createFromRawSetCookieHeader PHP Метод

createFromRawSetCookieHeader() публичный статический Метод

Creates a cookie (an instance of this class) by a provided raw header string like "foo=507d9f20317a5; path=/; domain=.example.org" This is is an implementation of the algorithm explained in RFC 6265, Section 5.2 A basic statement of this algorithm is to "ignore the set-cookie-string entirely" in case a required condition is not met. In these cases this function will return NULL rather than the created cookie.
См. также: http://tools.ietf.org/html/rfc6265
public static createFromRawSetCookieHeader ( string $header ) : Cookie
$header string The Set-Cookie string without the actual "Set-Cookie:" part
Результат Cookie
    public static function createFromRawSetCookieHeader($header)
    {
        $nameValueAndUnparsedAttributes = explode(';', $header, 2);
        $expectedNameValuePair = $nameValueAndUnparsedAttributes[0];
        $unparsedAttributes = isset($nameValueAndUnparsedAttributes[1]) ? $nameValueAndUnparsedAttributes[1] : '';
        if (strpos($expectedNameValuePair, '=') === false) {
            return null;
        }
        $cookieNameAndValue = explode('=', $expectedNameValuePair, 2);
        $cookieName = trim($cookieNameAndValue[0]);
        $cookieValue = isset($cookieNameAndValue[1]) ? trim($cookieNameAndValue[1]) : '';
        if ($cookieName === '') {
            return null;
        }
        $expiresAttribute = 0;
        $maxAgeAttribute = null;
        $domainAttribute = null;
        $pathAttribute = null;
        $secureAttribute = false;
        $httpOnlyAttribute = true;
        if ($unparsedAttributes !== '') {
            foreach (explode(';', $unparsedAttributes) as $cookieAttributeValueString) {
                $attributeNameAndValue = explode('=', $cookieAttributeValueString, 2);
                $attributeName = trim($attributeNameAndValue[0]);
                $attributeValue = isset($attributeNameAndValue[1]) ? trim($attributeNameAndValue[1]) : '';
                switch (strtoupper($attributeName)) {
                    case 'EXPIRES':
                        try {
                            $expiresAttribute = new \DateTime($attributeValue);
                        } catch (\Exception $exception) {
                            // as of RFC 6265 Section 5.2.1, a non parsable Expires date should result into
                            // ignoring, but since the Cookie constructor relies on it, we'll
                            // assume a Session cookie with an expiry date of 0.
                            $expiresAttribute = 0;
                        }
                        break;
                    case 'MAX-AGE':
                        if (preg_match(self::PATTERN_MAX_AGE, $attributeValue) === 1) {
                            $maxAgeAttribute = intval($attributeValue);
                        }
                        break;
                    case 'DOMAIN':
                        if ($attributeValue !== '') {
                            $domainAttribute = strtolower(ltrim($attributeValue, '.'));
                        }
                        break;
                    case 'PATH':
                        if ($attributeValue === '' || substr($attributeValue, 0, 1) !== '/') {
                            $pathAttribute = '/';
                        } else {
                            $pathAttribute = $attributeValue;
                        }
                        break;
                    case 'SECURE':
                        $secureAttribute = true;
                        break;
                    case 'HTTPONLY':
                        $httpOnlyAttribute = true;
                        break;
                }
            }
        }
        $cookie = new static($cookieName, $cookieValue, $expiresAttribute, $maxAgeAttribute, $domainAttribute, $pathAttribute, $secureAttribute, $httpOnlyAttribute);
        return $cookie;
    }

Usage Example

Пример #1
0
 /**
  * Creates a response from the given raw, that is plain text, HTTP response.
  *
  * @param string $rawResponse
  * @param Response $parentResponse Parent response, if called recursively
  *
  * @throws \InvalidArgumentException
  * @return Response
  */
 public static function createFromRaw($rawResponse, Response $parentResponse = null)
 {
     $response = new static($parentResponse);
     $lines = explode(chr(10), $rawResponse);
     $statusLine = array_shift($lines);
     if (substr($statusLine, 0, 5) !== 'HTTP/') {
         throw new \InvalidArgumentException('The given raw HTTP message is not a valid response.', 1335175601);
     }
     list($version, $statusCode, $reasonPhrase) = explode(' ', $statusLine, 3);
     $response->setVersion($version);
     $response->setStatus((int) $statusCode, trim($reasonPhrase));
     $parsingHeader = true;
     $contentLines = [];
     $headers = new Headers();
     foreach ($lines as $line) {
         if ($parsingHeader) {
             if (trim($line) === '') {
                 $parsingHeader = false;
                 continue;
             }
             $fieldName = trim(substr($line, 0, strpos($line, ':')));
             $fieldValue = trim(substr($line, strlen($fieldName) + 1));
             if (strtoupper(substr($fieldName, 0, 10)) === 'SET-COOKIE') {
                 $cookie = Cookie::createFromRawSetCookieHeader($fieldValue);
                 if ($cookie !== null) {
                     $headers->setCookie($cookie);
                 }
             } else {
                 $headers->set($fieldName, $fieldValue, false);
             }
         } else {
             $contentLines[] = $line;
         }
     }
     $content = implode(chr(10), $contentLines);
     $response->setHeaders($headers);
     $response->setContent($content);
     return $response;
 }
All Usage Examples Of Neos\Flow\Http\Cookie::createFromRawSetCookieHeader