private function applyBody(puzzle_message_RequestInterface $request) { if ($request->hasHeader('Content-Length')) { $size = (int) $request->getHeader('Content-Length'); } else { $size = null; } $request->getBody()->seek(0); // You can send the body as a string using curl's CURLOPT_POSTFIELDS $config = $request->getConfig(); if ($size !== null && $size < 32768 || isset($config['curl']['body_as_string'])) { $this->_closure_handleOptions[CURLOPT_POSTFIELDS] = $request->getBody()->getContents(); // Don't duplicate the Content-Length header $this->removeHeader('Content-Length', $this->_closure_handleOptions); $this->removeHeader('Transfer-Encoding', $this->_closure_handleOptions); } else { $this->_closure_handleOptions[CURLOPT_UPLOAD] = true; // Let cURL handle setting the Content-Length header if ($size !== null) { $this->_closure_handleOptions[CURLOPT_INFILESIZE] = $size; $this->removeHeader('Content-Length', $this->_closure_handleOptions); } } // If the Expect header is not present, prevent curl from adding it if (!$request->hasHeader('Expect')) { $this->_closure_handleOptions[CURLOPT_HTTPHEADER][] = 'Expect:'; } }
/** * Create a redirect request for a specific request object * * Takes into account strict RFC compliant redirection (e.g. redirect POST * with POST) vs doing what most clients do (e.g. redirect POST with GET). * * @param puzzle_message_RequestInterface $request * @param puzzle_message_ResponseInterface $response * * @return puzzle_message_RequestInterface Returns a new redirect request * @throws puzzle_exception_CouldNotRewindStreamException If the body cannot be rewound. */ private function createRedirectRequest(puzzle_message_RequestInterface $request, puzzle_message_ResponseInterface $response) { $config = $request->getConfig(); // Use a GET request if this is an entity enclosing request and we are // not forcing RFC compliance, but rather emulating what all browsers // would do. Be sure to disable redirects on the clone. $redirectRequest = clone $request; $redirectRequest->getEmitter()->detach($this); $statusCode = $response->getStatusCode(); if ($statusCode == 303 || $statusCode <= 302 && $request->getBody() && !$config->getPath('redirect/strict')) { $redirectRequest->setMethod('GET'); $redirectRequest->setBody(null); } $this->setRedirectUrl($redirectRequest, $response); $this->rewindEntityBody($redirectRequest); // Add the Referer header if it is told to do so and only // add the header if we are not redirecting from https to http. if ($config->getPath('redirect/referer') && ($redirectRequest->getScheme() == 'https' || $redirectRequest->getScheme() == $request->getScheme())) { $url = puzzle_Url::fromString($request->getUrl()); $url->setUsername(null)->setPassword(null); $redirectRequest->setHeader('Referer', (string) $url); } return $redirectRequest; }