8
8
* License, or (at your option) any later version. *
9
9
* *
10
10
***************************************************************************/
11
- /* $Id: CurlHttpClient.class.php 45 2009-05-08 07:41:33Z lom $ */
12
11
13
12
/**
14
13
* @ingroup Http
@@ -23,6 +22,10 @@ final class CurlHttpClient implements HttpClient
23
22
private $ multiRequests = array ();
24
23
private $ multiResponses = array ();
25
24
private $ multiThreadOptions = array ();
25
+ /**
26
+ * @deprecated in the furure will work like this value is false;
27
+ */
28
+ private $ oldUrlConstructor = ONPHP_CURL_CLIENT_OLD_TO_STRING ;
26
29
27
30
/**
28
31
* @return CurlHttpClient
@@ -148,6 +151,26 @@ public function getMaxFileSize()
148
151
return $ this ->maxFileSize ;
149
152
}
150
153
154
+ /**
155
+ * @deprecated in the future value always false and method will be removed
156
+ * @param bool $oldUrlConstructor
157
+ * @return CurlHttpClient
158
+ */
159
+ public function setOldUrlConstructor ($ oldUrlConstructor = false )
160
+ {
161
+ $ this ->oldUrlConstructor = ($ oldUrlConstructor == true );
162
+ return $ this ;
163
+ }
164
+
165
+ /**
166
+ * @deprecated in the future value always false and method will be removed
167
+ * @return bool
168
+ */
169
+ public function isOldUrlConstructor ()
170
+ {
171
+ return $ this ->oldUrlConstructor ;
172
+ }
173
+
151
174
/**
152
175
* @return CurlHttpClient
153
176
**/
@@ -280,9 +303,14 @@ protected function makeHandle(HttpRequest $request, CurlHttpResponse $response)
280
303
break ;
281
304
282
305
case HttpMethod::POST :
306
+ if ($ request ->getGet ())
307
+ $ options [CURLOPT_URL ] .=
308
+ ($ request ->getUrl ()->getQuery () ? '& ' : '? ' )
309
+ .$ this ->argumentsToString ($ request ->getGet ());
310
+
283
311
$ options [CURLOPT_POST ] = true ;
284
- $ options [CURLOPT_POSTFIELDS ] =
285
- $ this -> argumentsToString ( $ request -> getPost ());
312
+ $ options [CURLOPT_POSTFIELDS ] = $ this -> getPostFields ( $ request );
313
+
286
314
break ;
287
315
288
316
default :
@@ -337,23 +365,68 @@ protected function makeResponse($handle, CurlHttpResponse $response)
337
365
return $ this ;
338
366
}
339
367
340
- private function argumentsToString ($ array )
368
+ private function argumentsToString ($ array, $ isFile = false )
341
369
{
342
- Assert::isArray ($ array );
343
- $ result = array ();
344
-
345
- foreach ($ array as $ key => $ value ) {
346
- if (is_array ($ value )) {
347
- foreach ($ value as $ valueKey => $ simpleValue ) {
348
- $ result [] =
349
- $ key .'[ ' .$ valueKey .']= ' .urlencode ($ simpleValue );
350
- }
370
+ if ($ this ->oldUrlConstructor )
371
+ return UrlParamsUtils::toStringOneDeepLvl ($ array );
372
+ else
373
+ return UrlParamsUtils::toString ($ array );
374
+ }
375
+
376
+ private function getPostFields (HttpRequest $ request )
377
+ {
378
+ if ($ request ->hasBody ()) {
379
+ return $ request ->getBody ();
380
+ } else {
381
+ if ($ this ->oldUrlConstructor ) {
382
+ return UrlParamsUtils::toStringOneDeepLvl ($ request ->getPost ());
351
383
} else {
352
- $ result [] = $ key .'= ' .urlencode ($ value );
384
+ $ fileList = array_map (
385
+ array ($ this , 'fileFilter ' ),
386
+ UrlParamsUtils::toParamsList ($ request ->getFiles ())
387
+ );
388
+ if (empty ($ fileList )) {
389
+ return UrlParamsUtils::toString ($ request ->getPost ());
390
+ } else {
391
+ $ postList = UrlParamsUtils::toParamsList ($ request ->getPost ());
392
+ if (!is_null ($ atParam = $ this ->findAtParamInPost ($ postList )))
393
+ throw new NetworkException (
394
+ 'Security excepion: not allowed send post param ' .$ atParam
395
+ . ' which begins from @ in request which contains files '
396
+ );
397
+
398
+ return array_merge ($ postList , $ fileList );
399
+ }
353
400
}
354
401
}
355
-
356
- return implode ('& ' , $ result );
402
+ }
403
+
404
+ /**
405
+ * Return param name which start with symbol @ or null
406
+ * @param array $postList
407
+ * @return string|null
408
+ */
409
+ private function findAtParamInPost ($ postList )
410
+ {
411
+ foreach ($ postList as $ param )
412
+ if (mb_stripos ($ param , '@ ' ) === 0 )
413
+ return $ param ;
414
+
415
+ return null ;
416
+ }
417
+
418
+ /**
419
+ * using in getPostFields - array_map func
420
+ * @param string $value
421
+ * @return string
422
+ */
423
+ private function fileFilter ($ value )
424
+ {
425
+ Assert::isTrue (
426
+ is_readable ($ value ) && is_file ($ value ),
427
+ 'couldn \'t access to file with path: ' .$ value
428
+ );
429
+ return '@ ' .$ value ;
357
430
}
358
431
}
359
432
?>
0 commit comments