/**
  * 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 = $internalRequest->withHeader('Connection', $this->configuration->getKeepAlive() ? 'keep-alive' : 'close');
     }
     if (!$internalRequest->hasHeader('Content-Type')) {
         $rawDatas = (string) $internalRequest->getBody();
         $datas = $internalRequest->getDatas();
         $files = $internalRequest->getFiles();
         if ($this->configuration->hasEncodingType()) {
             $internalRequest = $internalRequest->withHeader('Content-Type', $this->configuration->getEncodingType());
         } elseif ($contentType && !empty($files)) {
             $internalRequest = $internalRequest->withHeader('Content-Type', ConfigurationInterface::ENCODING_TYPE_FORMDATA . '; boundary=' . $this->configuration->getBoundary());
         } elseif ($contentType && (!empty($datas) || !empty($rawDatas))) {
             $internalRequest = $internalRequest->withHeader('Content-Type', ConfigurationInterface::ENCODING_TYPE_URLENCODED);
         }
     }
     if ($contentLength && !$internalRequest->hasHeader('Content-Length') && ($length = strlen($this->prepareBody($internalRequest))) > 0) {
         $internalRequest = $internalRequest->withHeader('Content-Length', (string) $length);
     }
     if (!$internalRequest->hasHeader('User-Agent')) {
         $internalRequest = $internalRequest->withHeader('User-Agent', $this->configuration->getUserAgent());
     }
     return HeadersNormalizer::normalize($internalRequest->getHeaders(), $associative);
 }