Example #1
0
 /**
  * Create date-based header from string
  *
  * @param string $headerLine
  * @return AbstractDate
  * @throws Exception\InvalidArgumentException
  */
 public static function fromString($headerLine)
 {
     $dateHeader = new static();
     list($name, $date) = explode(': ', $headerLine, 2);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== strtolower($dateHeader->getFieldName())) {
         throw new Exception\InvalidArgumentException('Invalid header line for "' . $dateHeader->getFieldName() . '" header string');
     }
     $dateHeader->setDate($date);
     return $dateHeader;
 }
Example #2
0
 /**
  * Create location-based header from string
  *
  * @param string $headerLine
  * @return AbstractLocation
  * @throws Exception\InvalidArgumentException
  */
 public static function fromString($headerLine)
 {
     $locationHeader = new static();
     // ZF-5520 - IIS bug, no space after colon
     list($name, $uri) = GenericHeader::splitHeaderLine($headerLine);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== strtolower($locationHeader->getFieldName())) {
         throw new Exception\InvalidArgumentException('Invalid header line for "' . $locationHeader->getFieldName() . '" header string');
     }
     $locationHeader->setUri(trim($uri));
     return $locationHeader;
 }
Example #3
0
    /**
     * Factory method: parse Accept header string
     * 
     * @param  string $headerLine 
     * @return Accept
     */
    public static function fromString($headerLine)
    {
        $acceptHeader = new static();

        list($name, $values) = explode(': ', $headerLine, 2);

        // check to ensure proper header type for this factory
        if (strtolower($name) !== strtolower($acceptHeader->getFieldName())) {
            throw new Exception\InvalidArgumentException('Invalid header line for ' . $acceptHeader->getFieldName() . ' header string');
        }

        // process multiple accept values
        $acceptHeader->values = explode(',', $values);

        foreach ($acceptHeader->values as $index => $value) {
            $value = trim($value);
            $acceptHeader->values[$index] = $value;

            $payload = array(
                'type' => strtolower($value),
                'priority'   => 1,
            );
            if (strstr($value, ';')) {
                list($type, $priority) = explode(';', $value, 2);
                $payload['type'] = strtolower(trim($type));

                // parse priority
                $priority = explode(';', trim($priority));

                $finalPriority = 1;
                foreach ($priority as $p) {
                    list($type, $value) = explode('=', trim($p), 2);
                    if ($type == 'q') {
                        $payload['priority'] = $value;
                    } elseif ($type == 'level') {
                        $payload['level'] = $value;
                    }
                }
            }

            if (!isset($acceptHeader->types[$payload['type']])) {
                $acceptHeader->types[$payload['type']] = true;
            }

            $acceptHeader->prioritizedValues[] = $payload;
        }

        return $acceptHeader;
    }
Example #4
0
 /**
  * Create Retry-After header from string
  *
  * @param  string $headerLine
  * @return RetryAfter
  * @throws Exception\InvalidArgumentException
  */
 public static function fromString($headerLine)
 {
     $dateHeader = new static();
     list($name, $date) = GenericHeader::splitHeaderLine($headerLine);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== strtolower($dateHeader->getFieldName())) {
         throw new Exception\InvalidArgumentException('Invalid header line for "' . $dateHeader->getFieldName() . '" header string');
     }
     if (is_numeric($date)) {
         $dateHeader->setDeltaSeconds($date);
     } else {
         $dateHeader->setDate($date);
     }
     return $dateHeader;
 }
 /**
  * Factory create AcceptHeader from string representation.
  *
  * @param string $headerLine
  *
  * @return $this
  *
  * @throws InvalidArgumentException
  */
 public static function fromString($headerLine)
 {
     $headerLine = (string) $headerLine;
     $pos = strpos($headerLine, ':');
     $fieldName = substr($headerLine, 0, $pos);
     $header = new static();
     $items = array();
     if ($fieldName !== $header->getFieldName()) {
         throw new InvalidArgumentException(sprintf('Header "%s" can\'t parse value "%s".', $header->getFieldName(), $fieldName));
     }
     preg_match_all('#((("[^"\']++")|(\'[^"\']++\'))|[^,"\'])+#', trim(substr($headerLine, $pos + 1)), $items);
     foreach (array_map('trim', $items[0]) as $index => $itemValueString) {
         $item = AcceptHeaderItem::fromString($itemValueString);
         $item->setIndex($index);
         $header->add($item);
     }
     return $header;
 }
 /**
  * Create Content Security Policy header from a given header line
  *
  * @param string $headerLine The header line to parse.
  * @return self
  * @throws Exception\InvalidArgumentException If the name field in the given header line does not match.
  */
 public static function fromString($headerLine)
 {
     $header = new static();
     $headerName = $header->getFieldName();
     list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
     // Ensure the proper header name
     if (strcasecmp($name, $headerName) != 0) {
         throw new Exception\InvalidArgumentException(sprintf('Invalid header line for %s string: "%s"', $headerName, $name));
     }
     // As per http://www.w3.org/TR/CSP/#parsing
     $tokens = explode(';', $value);
     foreach ($tokens as $token) {
         $token = trim($token);
         if ($token) {
             list($directiveName, $directiveValue) = explode(' ', $token, 2);
             if (!isset($header->directives[$directiveName])) {
                 $header->directives[$directiveName] = $directiveValue;
             }
         }
     }
     return $header;
 }
Example #7
0
 /**
  * Create date-based header from Unix timestamp
  *
  * @param int $time
  *
  * @return self
  *
  * @throws Exception\InvalidArgumentException
  */
 public static function fromTimestamp($time)
 {
     $dateHeader = new static();
     if (!$time || !is_numeric($time)) {
         throw new Exception\InvalidArgumentException('Invalid time for "' . $dateHeader->getFieldName() . '" header string');
     }
     $dateHeader->setDate(new DateTime('@' . $time));
     return $dateHeader;
 }