Exemplo n.º 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) = 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');
     }
     $dateHeader->setDate($date);
     return $dateHeader;
 }
Exemplo n.º 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;
 }
Exemplo n.º 3
0
 /**
  * Create Allow header from header line
  *
  * @param string $headerLine
  * @return Allow
  * @throws Exception\InvalidArgumentException
  */
 public static function fromString($headerLine)
 {
     list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== 'allow') {
         throw new Exception\InvalidArgumentException('Invalid header line for Allow string: "' . $name . '"');
     }
     $header = new static();
     $header->disallowMethods(array_keys($header->getAllMethods()));
     $header->allowMethods(explode(',', $value));
     return $header;
 }
Exemplo n.º 4
0
 /**
  * Parse a full header line or just the field value part.
  *
  * @param string $headerLine
  */
 public function parseHeaderLine($headerLine)
 {
     if (strpos($headerLine, ':') !== false) {
         list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
         if (strtolower($name) !== strtolower($this->getFieldName())) {
             $value = $headerLine;
             // This is just for preserve the BC.
         }
     } else {
         $value = $headerLine;
     }
     foreach ($this->getFieldValuePartsFromHeaderLine($value) as $value) {
         $this->addFieldValuePartToQueue($value);
     }
 }
Exemplo n.º 5
0
 /**
  * Create Allow header from header line
  *
  * @param string $headerLine
  * @return Allow
  * @throws Exception\InvalidArgumentException
  */
 public static function fromString($headerLine)
 {
     $header = new static();
     list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== 'allow') {
         throw new Exception\InvalidArgumentException('Invalid header line for Allow string: "' . $name . '"');
     }
     // reset list of methods
     $header->methods = array_fill_keys(array_keys($header->methods), false);
     // allow methods from header line
     foreach (explode(',', $value) as $method) {
         $method = trim(strtoupper($method));
         $header->methods[$method] = true;
     }
     return $header;
 }
Exemplo n.º 6
0
 public static function fromString($headerLine)
 {
     $header = new static();
     list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== 'cookie') {
         throw new Exception\InvalidArgumentException('Invalid header line for Server string: "' . $name . '"');
     }
     $nvPairs = preg_split('#;\\s*#', $value);
     $arrayInfo = array();
     foreach ($nvPairs as $nvPair) {
         $parts = explode('=', $nvPair, 2);
         if (count($parts) != 2) {
             throw new Exception\RuntimeException('Malformed Cookie header found');
         }
         list($name, $value) = $parts;
         $arrayInfo[$name] = urldecode($value);
     }
     $header->exchangeArray($arrayInfo);
     return $header;
 }
Exemplo n.º 7
0
 /**
  * Factory method: create an object from a string representation
  *
  * @param  string $headerLine
  * @return self
  */
 public static function fromString($headerLine)
 {
     list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== 'content-type') {
         throw new Exception\InvalidArgumentException(sprintf('Invalid header line for Content-Type string: "%s"', $name));
     }
     $parts = explode(';', $value);
     $mediaType = array_shift($parts);
     $header = new static($value, trim($mediaType));
     if (count($parts) > 0) {
         $parameters = array();
         foreach ($parts as $parameter) {
             $parameter = trim($parameter);
             if (!preg_match('/^(?P<key>[^\\s\\=]+)\\="?(?P<value>[^\\s\\"]*)"?$/', $parameter, $matches)) {
                 continue;
             }
             $parameters[$matches['key']] = $matches['value'];
         }
         $header->setParameters($parameters);
     }
     return $header;
 }
Exemplo n.º 8
0
 /**
  * @static
  * @throws Exception\InvalidArgumentException
  * @param  $headerLine
  * @param  bool $bypassHeaderFieldName
  * @return array|SetCookie
  */
 public static function fromString($headerLine, $bypassHeaderFieldName = false)
 {
     static $setCookieProcessor = null;
     if ($setCookieProcessor === null) {
         $setCookieClass = get_called_class();
         $setCookieProcessor = function ($headerLine) use($setCookieClass) {
             $header = new $setCookieClass();
             $keyValuePairs = preg_split('#;\\s*#', $headerLine);
             foreach ($keyValuePairs as $keyValue) {
                 if (preg_match('#^(?P<headerKey>[^=]+)=\\s*("?)(?P<headerValue>[^"]*)\\2#', $keyValue, $matches)) {
                     $headerKey = $matches['headerKey'];
                     $headerValue = $matches['headerValue'];
                 } else {
                     $headerKey = $keyValue;
                     $headerValue = null;
                 }
                 // First K=V pair is always the cookie name and value
                 if ($header->getName() === NULL) {
                     $header->setName($headerKey);
                     $header->setValue(urldecode($headerValue));
                     continue;
                 }
                 // Process the remaining elements
                 switch (str_replace(array('-', '_'), '', strtolower($headerKey))) {
                     case 'expires':
                         $header->setExpires($headerValue);
                         break;
                     case 'domain':
                         $header->setDomain($headerValue);
                         break;
                     case 'path':
                         $header->setPath($headerValue);
                         break;
                     case 'secure':
                         $header->setSecure(true);
                         break;
                     case 'httponly':
                         $header->setHttponly(true);
                         break;
                     case 'version':
                         $header->setVersion((int) $headerValue);
                         break;
                     case 'maxage':
                         $header->setMaxAge((int) $headerValue);
                         break;
                     default:
                         // Intentionally omitted
                 }
             }
             return $header;
         };
     }
     list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
     // some sites return set-cookie::value, this is to get rid of the second :
     $name = strtolower($name) == 'set-cookie:' ? 'set-cookie' : $name;
     // check to ensure proper header type for this factory
     if (strtolower($name) !== 'set-cookie') {
         throw new Exception\InvalidArgumentException('Invalid header line for Set-Cookie string: "' . $name . '"');
     }
     $multipleHeaders = preg_split('#(?<!Sun|Mon|Tue|Wed|Thu|Fri|Sat),\\s*#', $value);
     if (count($multipleHeaders) <= 1) {
         return $setCookieProcessor(array_pop($multipleHeaders));
     } else {
         $headers = array();
         foreach ($multipleHeaders as $headerLine) {
             $headers[] = $setCookieProcessor($headerLine);
         }
         return $headers;
     }
 }