부터: 2.0
저자: Qiang Xue (qiang.xue@gmail.com)
상속: extends yii\base\Object, implements IteratorAggregate, implements ArrayAccess, implements Countable
예제 #1
0
 /**
  * Returns the header collection.
  * The header collection contains the HTTP headers associated with HTTP message.
  * @return HeaderCollection the header collection
  */
 public function getHeaders()
 {
     if (!is_object($this->_headers)) {
         $headerCollection = new HeaderCollection();
         if (is_array($this->_headers)) {
             foreach ($this->_headers as $name => $value) {
                 if (is_int($name)) {
                     // parse raw header :
                     $rawHeader = $value;
                     if (($separatorPos = strpos($rawHeader, ':')) !== false) {
                         $name = strtolower(trim(substr($rawHeader, 0, $separatorPos)));
                         $value = trim(substr($rawHeader, $separatorPos + 1));
                         $headerCollection->add($name, $value);
                     } elseif (strpos($rawHeader, 'HTTP/') === 0) {
                         $parts = explode(' ', $rawHeader, 3);
                         $headerCollection->add('http-code', $parts[1]);
                     } else {
                         $headerCollection->add('raw', $rawHeader);
                     }
                 } else {
                     $headerCollection->set($name, $value);
                 }
             }
         }
         $this->_headers = $headerCollection;
     }
     return $this->_headers;
 }
 /**
  * @return bool
  */
 private function checkHeaders()
 {
     if (!isset($this->currentParams['headers'])) {
         return true;
     }
     $originalStructure = $this->currentParams['headers'];
     /**
      * if token validator is active
      */
     foreach ($originalStructure as $header) {
         if (!$this->requestHeaders->get($header)) {
             throw new InvalidParamException(\Yii::t('system', 'Header \'{0}\' is empty', [$header]), 406);
         }
     }
     return true;
 }
예제 #3
0
 /**
  * Detects format from headers.
  * @param HeaderCollection $headers source headers.
  * @return null|string format name, 'null' - if detection failed.
  */
 protected function detectFormatByHeaders(HeaderCollection $headers)
 {
     $contentType = $headers->get('content-type');
     if (!empty($contentType)) {
         if (stripos($contentType, 'json') !== false) {
             return Client::FORMAT_JSON;
         }
         if (stripos($contentType, 'urlencoded') !== false) {
             return Client::FORMAT_URLENCODED;
         }
         if (stripos($contentType, 'xml') !== false) {
             return Client::FORMAT_XML;
         }
     }
     return null;
 }
예제 #4
0
 /**
  * Sets HPKP header.
  * @param HeaderCollection $headers
  */
 public function addPublicKeyPins(HeaderCollection $headers)
 {
     if ($this->hpkpPins && is_array($this->hpkpPins)) {
         $values = [];
         foreach ($this->hpkpPins as $pin) {
             $values[] = "pin-sha256=\"" . $pin . "\"";
         }
         if (!empty($this->hpkpReportUri)) {
             $values[] = "report-uri=\"" . $this->hpkpReportUri . "\"";
         }
         $values[] = "max-age=" . $this->hpkpMaxAge;
         if ($this->hpkpIncludeSubdomains) {
             $values[] = "includeSubDomains";
         }
         $headers->set('Public-Key-Pins', implode("; ", $values));
     }
 }