예제 #1
0
파일: Client.php 프로젝트: PeeHaa/Artax
 private function normalizeRequestBodyHeaders(Request $request, array $options)
 {
     if ($request->hasHeader('Content-Length')) {
         // If the user manually assigned a Content-Length we don't need to do anything here.
         return;
     }
     $body = $request->getBody();
     $method = $request->getMethod();
     if (empty($body) && $body !== '0' && in_array($method, ['POST', 'PUT', 'PATCH'])) {
         $request->setHeader('Content-Length', '0');
         $request->removeHeader('Transfer-Encoding');
     } elseif (is_scalar($body) && $body !== '') {
         $body = (string) $body;
         $request->setBody($body);
         $request->setHeader('Content-Length', strlen($body));
         $request->removeHeader('Transfer-Encoding');
     } elseif ($body instanceof \Iterator) {
         $request->removeHeader('Content-Length');
         $request->setHeader('Transfer-Encoding', 'chunked');
         $chunkedBody = new ChunkingIterator($body);
         $request->setBody($chunkedBody);
     } elseif ($body !== null) {
         throw new \InvalidArgumentException('Request entity body must be a scalar or Iterator');
     }
     if ($body && $options[self::OP_EXPECT_CONTINUE] && !$request->hasHeader('Expect')) {
         $request->setHeader('Expect', '100-continue');
     }
     if ($method === 'TRACE' || $method === 'HEAD' || $method === 'OPTIONS') {
         $request->setBody(null);
     }
 }