Exemple #1
0
 /**
  * Prepares the headers.
  *
  * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
  * @param boolean                                             $associative     TRUE if the prepared headers should be associative else FALSE.
  * @param boolean                                             $contentType     TRUE if the content type header should be prepared else FALSE.
  * @param boolean                                             $contentLength   TRUE if the content length header should be prepared else FALSE.
  *
  * @return array The prepared headers.
  */
 protected function prepareHeaders(InternalRequestInterface $internalRequest, $associative = true, $contentType = true, $contentLength = false)
 {
     if (!$internalRequest->hasHeader('Connection')) {
         $internalRequest->setHeader('Connection', $this->configuration->getKeepAlive() ? 'keep-alive' : 'close');
     }
     if (!$internalRequest->hasHeader('Content-Type')) {
         if ($this->configuration->hasEncodingType()) {
             $internalRequest->setHeader('Content-Type', $this->configuration->getEncodingType());
         } elseif ($contentType && $internalRequest->hasFiles()) {
             $internalRequest->setHeader('Content-Type', ConfigurationInterface::ENCODING_TYPE_FORMDATA . '; boundary=' . $this->configuration->getBoundary());
         } elseif ($contentType && $internalRequest->hasDatas()) {
             $internalRequest->setHeader('Content-Type', ConfigurationInterface::ENCODING_TYPE_URLENCODED);
         }
     }
     if ($contentLength && !$internalRequest->hasHeader('Content-Length') && ($length = strlen($this->prepareBody($internalRequest))) > 0) {
         $internalRequest->setHeader('Content-Length', $length);
     }
     if (!$internalRequest->hasHeader('User-Agent')) {
         $internalRequest->setHeader('User-Agent', $this->configuration->getUserAgent());
     }
     return HeadersNormalizer::normalize($internalRequest->getHeaders(), $associative);
 }