@@ -202,42 +202,7 @@ private function createCurlOptions(RequestInterface $request, ResponseBuilder $r
202202 }
203203 $ options [CURLOPT_URL ] = (string ) $ request ->getUri ();
204204
205- /*
206- * Add body to request. Some HTTP methods can not have payload:
207- *
208- * - GET — cURL will automatically change method to PUT or POST if we set CURLOPT_UPLOAD or
209- * CURLOPT_POSTFIELDS.
210- * - HEAD — cURL treats HEAD as GET request with a same restrictions.
211- * - TRACE — According to RFC7231: a client MUST NOT send a message body in a TRACE request.
212- */
213- if (!in_array ($ request ->getMethod (), ['GET ' , 'HEAD ' , 'TRACE ' ], true )) {
214- $ body = $ request ->getBody ();
215- $ bodySize = $ body ->getSize ();
216- if ($ bodySize !== 0 ) {
217- // Message has non empty body.
218- if (null === $ bodySize || $ bodySize > 1024 * 1024 ) {
219- // Avoid full loading large or unknown size body into memory
220- $ options [CURLOPT_UPLOAD ] = true ;
221- if (null !== $ bodySize ) {
222- $ options [CURLOPT_INFILESIZE ] = $ bodySize ;
223- }
224- $ options [CURLOPT_READFUNCTION ] = function ($ ch , $ fd , $ length ) use ($ body ) {
225- return $ body ->read ($ length );
226- };
227- } else {
228- // Small body can be loaded into memory
229- $ options [CURLOPT_POSTFIELDS ] = (string ) $ body ;
230- }
231- }
232- }
233-
234- if ($ request ->getMethod () === 'HEAD ' ) {
235- // This will set HTTP method to "HEAD".
236- $ options [CURLOPT_NOBODY ] = true ;
237- } elseif ($ request ->getMethod () !== 'GET ' ) {
238- // GET is a default method. Other methods should be specified explicitly.
239- $ options [CURLOPT_CUSTOMREQUEST ] = $ request ->getMethod ();
240- }
205+ $ options = $ this ->addRequestBodyOptions ($ request , $ options );
241206
242207 $ options [CURLOPT_HTTPHEADER ] = $ this ->createHeaders ($ request , $ options );
243208
@@ -291,6 +256,56 @@ private function getProtocolVersion($requestVersion)
291256 return CURL_HTTP_VERSION_NONE ;
292257 }
293258
259+ /**
260+ * Add request body related cURL options.
261+ *
262+ * @param RequestInterface $request
263+ * @param array $options
264+ *
265+ * @return array
266+ */
267+ private function addRequestBodyOptions (RequestInterface $ request , array $ options )
268+ {
269+ /*
270+ * Some HTTP methods cannot have payload:
271+ *
272+ * - GET — cURL will automatically change method to PUT or POST if we set CURLOPT_UPLOAD or
273+ * CURLOPT_POSTFIELDS.
274+ * - HEAD — cURL treats HEAD as GET request with a same restrictions.
275+ * - TRACE — According to RFC7231: a client MUST NOT send a message body in a TRACE request.
276+ */
277+ if (!in_array ($ request ->getMethod (), ['GET ' , 'HEAD ' , 'TRACE ' ], true )) {
278+ $ body = $ request ->getBody ();
279+ $ bodySize = $ body ->getSize ();
280+ if ($ bodySize !== 0 ) {
281+ // Message has non empty body.
282+ if (null === $ bodySize || $ bodySize > 1024 * 1024 ) {
283+ // Avoid full loading large or unknown size body into memory
284+ $ options [CURLOPT_UPLOAD ] = true ;
285+ if (null !== $ bodySize ) {
286+ $ options [CURLOPT_INFILESIZE ] = $ bodySize ;
287+ }
288+ $ options [CURLOPT_READFUNCTION ] = function ($ ch , $ fd , $ length ) use ($ body ) {
289+ return $ body ->read ($ length );
290+ };
291+ } else {
292+ // Small body can be loaded into memory
293+ $ options [CURLOPT_POSTFIELDS ] = (string ) $ body ;
294+ }
295+ }
296+ }
297+
298+ if ($ request ->getMethod () === 'HEAD ' ) {
299+ // This will set HTTP method to "HEAD".
300+ $ options [CURLOPT_NOBODY ] = true ;
301+ } elseif ($ request ->getMethod () !== 'GET ' ) {
302+ // GET is a default method. Other methods should be specified explicitly.
303+ $ options [CURLOPT_CUSTOMREQUEST ] = $ request ->getMethod ();
304+ }
305+
306+ return $ options ;
307+ }
308+
294309 /**
295310 * Create headers array for CURLOPT_HTTPHEADER
296311 *
0 commit comments