Conditions | 89 |
Paths | > 20000 |
Total Lines | 311 |
Code Lines | 200 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
244 | function sendRequest($url,$data='',$method='GET'){ |
||
245 | $this->start = $this->_time(); |
||
246 | $this->error = ''; |
||
247 | $this->status = 0; |
||
248 | $this->status = 0; |
||
249 | $this->resp_body = ''; |
||
250 | $this->resp_headers = array(); |
||
251 | |||
252 | // don't accept gzip if truncated bodies might occur |
||
253 | if($this->max_bodysize && |
||
254 | !$this->max_bodysize_abort && |
||
255 | $this->headers['Accept-encoding'] == 'gzip'){ |
||
256 | unset($this->headers['Accept-encoding']); |
||
257 | } |
||
258 | |||
259 | // parse URL into bits |
||
260 | $uri = parse_url($url); |
||
261 | $server = $uri['host']; |
||
262 | $path = $uri['path']; |
||
263 | if(empty($path)) $path = '/'; |
||
264 | if(!empty($uri['query'])) $path .= '?'.$uri['query']; |
||
265 | if(!empty($uri['port'])) $port = $uri['port']; |
||
266 | if(isset($uri['user'])) $this->user = $uri['user']; |
||
267 | if(isset($uri['pass'])) $this->pass = $uri['pass']; |
||
268 | |||
269 | // proxy setup |
||
270 | if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){ |
||
271 | $request_url = $url; |
||
272 | $server = $this->proxy_host; |
||
273 | $port = $this->proxy_port; |
||
274 | if (empty($port)) $port = 8080; |
||
275 | }else{ |
||
276 | $request_url = $path; |
||
277 | if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80; |
||
278 | } |
||
279 | |||
280 | // add SSL stream prefix if needed - needs SSL support in PHP |
||
281 | if($port == 443 || $this->proxy_ssl) { |
||
282 | if(!in_array('ssl', stream_get_transports())) { |
||
283 | $this->status = -200; |
||
284 | $this->error = 'This PHP version does not support SSL - cannot connect to server'; |
||
285 | } |
||
286 | $server = 'ssl://'.$server; |
||
287 | } |
||
288 | |||
289 | // prepare headers |
||
290 | $headers = $this->headers; |
||
291 | $headers['Host'] = $uri['host']; |
||
292 | if(!empty($uri['port'])) $headers['Host'].= ':'.$uri['port']; |
||
293 | $headers['User-Agent'] = $this->agent; |
||
294 | $headers['Referer'] = $this->referer; |
||
295 | |||
296 | if($method == 'POST'){ |
||
297 | if(is_array($data)){ |
||
298 | if($headers['Content-Type'] == 'multipart/form-data'){ |
||
299 | $headers['Content-Type'] = 'multipart/form-data; boundary='.$this->boundary; |
||
300 | $data = $this->_postMultipartEncode($data); |
||
301 | }else{ |
||
302 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
303 | $data = $this->_postEncode($data); |
||
304 | } |
||
305 | } |
||
306 | $headers['Content-Length'] = strlen($data); |
||
307 | }elseif($method == 'GET'){ |
||
308 | $data = ''; //no data allowed on GET requests |
||
309 | } |
||
310 | if($this->user) { |
||
311 | $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass); |
||
312 | } |
||
313 | if($this->proxy_user) { |
||
314 | $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass); |
||
315 | } |
||
316 | |||
317 | // already connected? |
||
318 | $connectionId = $this->_uniqueConnectionId($server,$port); |
||
319 | $this->_debug('connection pool', self::$connections); |
||
320 | $socket = null; |
||
321 | if (isset(self::$connections[$connectionId])) { |
||
322 | $this->_debug('reusing connection', $connectionId); |
||
323 | $socket = self::$connections[$connectionId]; |
||
324 | } |
||
325 | if (is_null($socket) || feof($socket)) { |
||
326 | $this->_debug('opening connection', $connectionId); |
||
327 | // open socket |
||
328 | $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout); |
||
329 | if (!$socket){ |
||
330 | $this->status = -100; |
||
331 | $this->error = "Could not connect to $server:$port\n$errstr ($errno)"; |
||
332 | return false; |
||
333 | } |
||
334 | |||
335 | // try establish a CONNECT tunnel for SSL |
||
336 | try { |
||
337 | if($this->_ssltunnel($socket, $request_url)){ |
||
338 | // no keep alive for tunnels |
||
339 | $this->keep_alive = false; |
||
340 | // tunnel is authed already |
||
341 | if(isset($headers['Proxy-Authentication'])) unset($headers['Proxy-Authentication']); |
||
342 | } |
||
343 | } catch (HTTPClientException $e) { |
||
344 | $this->status = $e->getCode(); |
||
345 | $this->error = $e->getMessage(); |
||
346 | fclose($socket); |
||
347 | return false; |
||
348 | } |
||
349 | |||
350 | // keep alive? |
||
351 | if ($this->keep_alive) { |
||
352 | self::$connections[$connectionId] = $socket; |
||
353 | } else { |
||
354 | unset(self::$connections[$connectionId]); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | if ($this->keep_alive && !$this->proxy_host) { |
||
359 | // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive |
||
360 | // connection token to a proxy server. We still do keep the connection the |
||
361 | // proxy alive (well except for CONNECT tunnels) |
||
362 | $headers['Connection'] = 'Keep-Alive'; |
||
363 | } else { |
||
364 | $headers['Connection'] = 'Close'; |
||
365 | } |
||
366 | |||
367 | try { |
||
368 | //set non-blocking |
||
369 | stream_set_blocking($socket, 0); |
||
370 | |||
371 | // build request |
||
372 | $request = "$method $request_url HTTP/".$this->http.HTTP_NL; |
||
373 | $request .= $this->_buildHeaders($headers); |
||
374 | $request .= $this->_getCookies(); |
||
375 | $request .= HTTP_NL; |
||
376 | $request .= $data; |
||
377 | |||
378 | $this->_debug('request',$request); |
||
379 | $this->_sendData($socket, $request, 'request'); |
||
380 | |||
381 | // read headers from socket |
||
382 | $r_headers = ''; |
||
383 | do{ |
||
384 | $r_line = $this->_readLine($socket, 'headers'); |
||
385 | $r_headers .= $r_line; |
||
386 | }while($r_line != "\r\n" && $r_line != "\n"); |
||
387 | |||
388 | $this->_debug('response headers',$r_headers); |
||
389 | |||
390 | // check if expected body size exceeds allowance |
||
391 | if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){ |
||
392 | if($match[1] > $this->max_bodysize){ |
||
393 | if ($this->max_bodysize_abort) |
||
394 | throw new HTTPClientException('Reported content length exceeds allowed response size'); |
||
395 | else |
||
396 | $this->error = 'Reported content length exceeds allowed response size'; |
||
397 | } |
||
398 | } |
||
399 | |||
400 | // get Status |
||
401 | if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) |
||
402 | throw new HTTPClientException('Server returned bad answer '.$r_headers); |
||
403 | |||
404 | $this->status = $m[2]; |
||
405 | |||
406 | // handle headers and cookies |
||
407 | $this->resp_headers = $this->_parseHeaders($r_headers); |
||
408 | if(isset($this->resp_headers['set-cookie'])){ |
||
409 | foreach ((array) $this->resp_headers['set-cookie'] as $cookie){ |
||
410 | list($cookie) = explode(';',$cookie,2); |
||
411 | list($key,$val) = explode('=',$cookie,2); |
||
412 | $key = trim($key); |
||
413 | if($val == 'deleted'){ |
||
414 | if(isset($this->cookies[$key])){ |
||
415 | unset($this->cookies[$key]); |
||
416 | } |
||
417 | }elseif($key){ |
||
418 | $this->cookies[$key] = $val; |
||
419 | } |
||
420 | } |
||
421 | } |
||
422 | |||
423 | $this->_debug('Object headers',$this->resp_headers); |
||
424 | |||
425 | // check server status code to follow redirect |
||
426 | if($this->status == 301 || $this->status == 302 ){ |
||
427 | if (empty($this->resp_headers['location'])){ |
||
428 | throw new HTTPClientException('Redirect but no Location Header found'); |
||
429 | }elseif($this->redirect_count == $this->max_redirect){ |
||
430 | throw new HTTPClientException('Maximum number of redirects exceeded'); |
||
431 | }else{ |
||
432 | // close the connection because we don't handle content retrieval here |
||
433 | // that's the easiest way to clean up the connection |
||
434 | fclose($socket); |
||
435 | unset(self::$connections[$connectionId]); |
||
436 | |||
437 | $this->redirect_count++; |
||
438 | $this->referer = $url; |
||
439 | // handle non-RFC-compliant relative redirects |
||
440 | if (!preg_match('/^http/i', $this->resp_headers['location'])){ |
||
441 | if($this->resp_headers['location'][0] != '/'){ |
||
442 | $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port']. |
||
443 | dirname($uri['path']).'/'.$this->resp_headers['location']; |
||
444 | }else{ |
||
445 | $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port']. |
||
446 | $this->resp_headers['location']; |
||
447 | } |
||
448 | } |
||
449 | // perform redirected request, always via GET (required by RFC) |
||
450 | return $this->sendRequest($this->resp_headers['location'],array(),'GET'); |
||
451 | } |
||
452 | } |
||
453 | |||
454 | // check if headers are as expected |
||
455 | if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)) |
||
456 | throw new HTTPClientException('The received headers did not match the given regexp'); |
||
457 | |||
458 | //read body (with chunked encoding if needed) |
||
459 | $r_body = ''; |
||
460 | if((isset($this->resp_headers['transfer-encoding']) && $this->resp_headers['transfer-encoding'] == 'chunked') |
||
461 | || (isset($this->resp_headers['transfer-coding']) && $this->resp_headers['transfer-coding'] == 'chunked')){ |
||
462 | $abort = false; |
||
463 | do { |
||
464 | $chunk_size = ''; |
||
465 | while (preg_match('/^[a-zA-Z0-9]?$/',$byte=$this->_readData($socket,1,'chunk'))){ |
||
466 | // read chunksize until \r |
||
467 | $chunk_size .= $byte; |
||
468 | if (strlen($chunk_size) > 128) // set an abritrary limit on the size of chunks |
||
469 | throw new HTTPClientException('Allowed response size exceeded'); |
||
470 | } |
||
471 | $this->_readLine($socket, 'chunk'); // readtrailing \n |
||
472 | $chunk_size = hexdec($chunk_size); |
||
473 | |||
474 | if($this->max_bodysize && $chunk_size+strlen($r_body) > $this->max_bodysize){ |
||
475 | if ($this->max_bodysize_abort) |
||
476 | throw new HTTPClientException('Allowed response size exceeded'); |
||
477 | $this->error = 'Allowed response size exceeded'; |
||
478 | $chunk_size = $this->max_bodysize - strlen($r_body); |
||
479 | $abort = true; |
||
480 | } |
||
481 | |||
482 | if ($chunk_size > 0) { |
||
483 | $r_body .= $this->_readData($socket, $chunk_size, 'chunk'); |
||
484 | $this->_readData($socket, 2, 'chunk'); // read trailing \r\n |
||
485 | } |
||
486 | } while ($chunk_size && !$abort); |
||
487 | }elseif(isset($this->resp_headers['content-length']) && !isset($this->resp_headers['transfer-encoding'])){ |
||
488 | /* RFC 2616 |
||
489 | * If a message is received with both a Transfer-Encoding header field and a Content-Length |
||
490 | * header field, the latter MUST be ignored. |
||
491 | */ |
||
492 | |||
493 | // read up to the content-length or max_bodysize |
||
494 | // for keep alive we need to read the whole message to clean up the socket for the next read |
||
495 | if(!$this->keep_alive && $this->max_bodysize && $this->max_bodysize < $this->resp_headers['content-length']){ |
||
496 | $length = $this->max_bodysize; |
||
497 | }else{ |
||
498 | $length = $this->resp_headers['content-length']; |
||
499 | } |
||
500 | |||
501 | $r_body = $this->_readData($socket, $length, 'response (content-length limited)', true); |
||
502 | }elseif( !isset($this->resp_headers['transfer-encoding']) && $this->max_bodysize && !$this->keep_alive){ |
||
503 | $r_body = $this->_readData($socket, $this->max_bodysize, 'response (content-length limited)', true); |
||
504 | }else{ |
||
505 | // read entire socket |
||
506 | while (!feof($socket)) { |
||
507 | $r_body .= $this->_readData($socket, 4096, 'response (unlimited)', true); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | // recheck body size, we might had to read the whole body, so we abort late or trim here |
||
512 | if($this->max_bodysize){ |
||
513 | if(strlen($r_body) > $this->max_bodysize){ |
||
514 | if ($this->max_bodysize_abort) { |
||
515 | throw new HTTPClientException('Allowed response size exceeded'); |
||
516 | } else { |
||
517 | $this->error = 'Allowed response size exceeded'; |
||
518 | } |
||
519 | } |
||
520 | } |
||
521 | |||
522 | } catch (HTTPClientException $err) { |
||
523 | $this->error = $err->getMessage(); |
||
524 | if ($err->getCode()) |
||
525 | $this->status = $err->getCode(); |
||
526 | unset(self::$connections[$connectionId]); |
||
527 | fclose($socket); |
||
528 | return false; |
||
529 | } |
||
530 | |||
531 | if (!$this->keep_alive || |
||
532 | (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) { |
||
533 | // close socket |
||
534 | fclose($socket); |
||
535 | unset(self::$connections[$connectionId]); |
||
536 | } |
||
537 | |||
538 | // decode gzip if needed |
||
539 | if(isset($this->resp_headers['content-encoding']) && |
||
540 | $this->resp_headers['content-encoding'] == 'gzip' && |
||
541 | strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){ |
||
542 | $this->resp_body = @gzinflate(substr($r_body, 10)); |
||
543 | if($this->resp_body === false){ |
||
544 | $this->error = 'Failed to decompress gzip encoded content'; |
||
545 | $this->resp_body = $r_body; |
||
546 | } |
||
547 | }else{ |
||
548 | $this->resp_body = $r_body; |
||
549 | } |
||
550 | |||
551 | $this->_debug('response body',$this->resp_body); |
||
552 | $this->redirect_count = 0; |
||
553 | return true; |
||
554 | } |
||
555 | |||
932 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.