| Conditions | 91 |
| Paths | > 20000 |
| Total Lines | 336 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 164 | public function sendRequest($url,$data='',$method='GET'){ |
||
| 165 | $this->start = $this->time(); |
||
| 166 | $this->error = ''; |
||
| 167 | $this->status = 0; |
||
| 168 | $this->status = 0; |
||
| 169 | $this->resp_body = ''; |
||
| 170 | $this->resp_headers = array(); |
||
| 171 | |||
| 172 | // don't accept gzip if truncated bodies might occur |
||
| 173 | if($this->max_bodysize && |
||
| 174 | !$this->max_bodysize_abort && |
||
| 175 | $this->headers['Accept-encoding'] == 'gzip'){ |
||
| 176 | unset($this->headers['Accept-encoding']); |
||
| 177 | } |
||
| 178 | |||
| 179 | // parse URL into bits |
||
| 180 | $uri = parse_url($url); |
||
| 181 | $server = $uri['host']; |
||
| 182 | $path = $uri['path']; |
||
| 183 | if(empty($path)) $path = '/'; |
||
| 184 | if(!empty($uri['query'])) $path .= '?'.$uri['query']; |
||
| 185 | if(!empty($uri['port'])) $port = $uri['port']; |
||
| 186 | if(isset($uri['user'])) $this->user = $uri['user']; |
||
| 187 | if(isset($uri['pass'])) $this->pass = $uri['pass']; |
||
| 188 | |||
| 189 | // proxy setup |
||
| 190 | if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){ |
||
| 191 | $request_url = $url; |
||
| 192 | $server = $this->proxy_host; |
||
| 193 | $port = $this->proxy_port; |
||
| 194 | if (empty($port)) $port = 8080; |
||
| 195 | $use_tls = $this->proxy_ssl; |
||
| 196 | }else{ |
||
| 197 | $request_url = $path; |
||
| 198 | if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80; |
||
| 199 | $use_tls = ($uri['scheme'] == 'https'); |
||
| 200 | } |
||
| 201 | |||
| 202 | // add SSL stream prefix if needed - needs SSL support in PHP |
||
| 203 | if($use_tls) { |
||
| 204 | if(!in_array('ssl', stream_get_transports())) { |
||
| 205 | $this->status = -200; |
||
| 206 | $this->error = 'This PHP version does not support SSL - cannot connect to server'; |
||
| 207 | } |
||
| 208 | $server = 'ssl://'.$server; |
||
| 209 | } |
||
| 210 | |||
| 211 | // prepare headers |
||
| 212 | $headers = $this->headers; |
||
| 213 | $headers['Host'] = $uri['host']; |
||
| 214 | if(!empty($uri['port'])) $headers['Host'].= ':'.$uri['port']; |
||
| 215 | $headers['User-Agent'] = $this->agent; |
||
| 216 | $headers['Referer'] = $this->referer; |
||
| 217 | |||
| 218 | if($method == 'POST'){ |
||
| 219 | if(is_array($data)){ |
||
| 220 | if (empty($headers['Content-Type'])) { |
||
| 221 | $headers['Content-Type'] = null; |
||
| 222 | } |
||
| 223 | switch ($headers['Content-Type']) { |
||
| 224 | case 'multipart/form-data': |
||
| 225 | $headers['Content-Type'] = 'multipart/form-data; boundary=' . $this->boundary; |
||
| 226 | $data = $this->postMultipartEncode($data); |
||
| 227 | break; |
||
| 228 | default: |
||
| 229 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
| 230 | $data = $this->postEncode($data); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | }elseif($method == 'GET'){ |
||
| 234 | $data = ''; //no data allowed on GET requests |
||
| 235 | } |
||
| 236 | |||
| 237 | $contentlength = strlen($data); |
||
| 238 | if($contentlength) { |
||
| 239 | $headers['Content-Length'] = $contentlength; |
||
| 240 | } |
||
| 241 | |||
| 242 | if($this->user) { |
||
| 243 | $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass); |
||
| 244 | } |
||
| 245 | if($this->proxy_user) { |
||
| 246 | $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass); |
||
| 247 | } |
||
| 248 | |||
| 249 | // already connected? |
||
| 250 | $connectionId = $this->uniqueConnectionId($server,$port); |
||
| 251 | $this->debug('connection pool', self::$connections); |
||
| 252 | $socket = null; |
||
| 253 | if (isset(self::$connections[$connectionId])) { |
||
| 254 | $this->debug('reusing connection', $connectionId); |
||
| 255 | $socket = self::$connections[$connectionId]; |
||
| 256 | } |
||
| 257 | if (is_null($socket) || feof($socket)) { |
||
| 258 | $this->debug('opening connection', $connectionId); |
||
| 259 | // open socket |
||
| 260 | $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout); |
||
| 261 | if (!$socket){ |
||
| 262 | $this->status = -100; |
||
| 263 | $this->error = "Could not connect to $server:$port\n$errstr ($errno)"; |
||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | // try establish a CONNECT tunnel for SSL |
||
| 268 | try { |
||
| 269 | if($this->ssltunnel($socket, $request_url)){ |
||
| 270 | // no keep alive for tunnels |
||
| 271 | $this->keep_alive = false; |
||
| 272 | // tunnel is authed already |
||
| 273 | if(isset($headers['Proxy-Authentication'])) unset($headers['Proxy-Authentication']); |
||
| 274 | } |
||
| 275 | } catch (HTTPClientException $e) { |
||
| 276 | $this->status = $e->getCode(); |
||
| 277 | $this->error = $e->getMessage(); |
||
| 278 | fclose($socket); |
||
| 279 | return false; |
||
| 280 | } |
||
| 281 | |||
| 282 | // keep alive? |
||
| 283 | if ($this->keep_alive) { |
||
| 284 | self::$connections[$connectionId] = $socket; |
||
| 285 | } else { |
||
| 286 | unset(self::$connections[$connectionId]); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | if ($this->keep_alive && !$this->proxy_host) { |
||
| 291 | // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive |
||
| 292 | // connection token to a proxy server. We still do keep the connection the |
||
| 293 | // proxy alive (well except for CONNECT tunnels) |
||
| 294 | $headers['Connection'] = 'Keep-Alive'; |
||
| 295 | } else { |
||
| 296 | $headers['Connection'] = 'Close'; |
||
| 297 | } |
||
| 298 | |||
| 299 | try { |
||
| 300 | //set non-blocking |
||
| 301 | stream_set_blocking($socket, 0); |
||
| 302 | |||
| 303 | // build request |
||
| 304 | $request = "$method $request_url HTTP/".$this->http.HTTP_NL; |
||
| 305 | $request .= $this->buildHeaders($headers); |
||
| 306 | $request .= $this->getCookies(); |
||
| 307 | $request .= HTTP_NL; |
||
| 308 | $request .= $data; |
||
| 309 | |||
| 310 | $this->debug('request',$request); |
||
| 311 | $this->sendData($socket, $request, 'request'); |
||
| 312 | |||
| 313 | // read headers from socket |
||
| 314 | $r_headers = ''; |
||
| 315 | do{ |
||
| 316 | $r_line = $this->readLine($socket, 'headers'); |
||
| 317 | $r_headers .= $r_line; |
||
| 318 | }while($r_line != "\r\n" && $r_line != "\n"); |
||
| 319 | |||
| 320 | $this->debug('response headers',$r_headers); |
||
| 321 | |||
| 322 | // check if expected body size exceeds allowance |
||
| 323 | if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){ |
||
| 324 | if($match[1] > $this->max_bodysize){ |
||
| 325 | if ($this->max_bodysize_abort) |
||
| 326 | throw new HTTPClientException('Reported content length exceeds allowed response size'); |
||
| 327 | else |
||
| 328 | $this->error = 'Reported content length exceeds allowed response size'; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | // get Status |
||
| 333 | if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) |
||
| 334 | throw new HTTPClientException('Server returned bad answer '.$r_headers); |
||
| 335 | |||
| 336 | $this->status = $m[2]; |
||
| 337 | |||
| 338 | // handle headers and cookies |
||
| 339 | $this->resp_headers = $this->parseHeaders($r_headers); |
||
| 340 | if(isset($this->resp_headers['set-cookie'])){ |
||
| 341 | foreach ((array) $this->resp_headers['set-cookie'] as $cookie){ |
||
| 342 | list($cookie) = explode(';',$cookie,2); |
||
| 343 | list($key,$val) = explode('=',$cookie,2); |
||
| 344 | $key = trim($key); |
||
| 345 | if($val == 'deleted'){ |
||
| 346 | if(isset($this->cookies[$key])){ |
||
| 347 | unset($this->cookies[$key]); |
||
| 348 | } |
||
| 349 | }elseif($key){ |
||
| 350 | $this->cookies[$key] = $val; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | $this->debug('Object headers',$this->resp_headers); |
||
| 356 | |||
| 357 | // check server status code to follow redirect |
||
| 358 | if($this->status == 301 || $this->status == 302 ){ |
||
| 359 | if (empty($this->resp_headers['location'])){ |
||
| 360 | throw new HTTPClientException('Redirect but no Location Header found'); |
||
| 361 | }elseif($this->redirect_count == $this->max_redirect){ |
||
| 362 | throw new HTTPClientException('Maximum number of redirects exceeded'); |
||
| 363 | }else{ |
||
| 364 | // close the connection because we don't handle content retrieval here |
||
| 365 | // that's the easiest way to clean up the connection |
||
| 366 | fclose($socket); |
||
| 367 | unset(self::$connections[$connectionId]); |
||
| 368 | |||
| 369 | $this->redirect_count++; |
||
| 370 | $this->referer = $url; |
||
| 371 | // handle non-RFC-compliant relative redirects |
||
| 372 | if (!preg_match('/^http/i', $this->resp_headers['location'])){ |
||
| 373 | if($this->resp_headers['location'][0] != '/'){ |
||
| 374 | $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port']. |
||
| 375 | dirname($uri['path']).'/'.$this->resp_headers['location']; |
||
| 376 | }else{ |
||
| 377 | $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port']. |
||
| 378 | $this->resp_headers['location']; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | // perform redirected request, always via GET (required by RFC) |
||
| 382 | return $this->sendRequest($this->resp_headers['location'],array(),'GET'); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | // check if headers are as expected |
||
| 387 | if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)) |
||
| 388 | throw new HTTPClientException('The received headers did not match the given regexp'); |
||
| 389 | |||
| 390 | //read body (with chunked encoding if needed) |
||
| 391 | $r_body = ''; |
||
| 392 | if( |
||
| 393 | ( |
||
| 394 | isset($this->resp_headers['transfer-encoding']) && |
||
| 395 | $this->resp_headers['transfer-encoding'] == 'chunked' |
||
| 396 | ) || ( |
||
| 397 | isset($this->resp_headers['transfer-coding']) && |
||
| 398 | $this->resp_headers['transfer-coding'] == 'chunked' |
||
| 399 | ) |
||
| 400 | ) { |
||
| 401 | $abort = false; |
||
| 402 | do { |
||
| 403 | $chunk_size = ''; |
||
| 404 | while (preg_match('/^[a-zA-Z0-9]?$/',$byte=$this->readData($socket,1,'chunk'))){ |
||
| 405 | // read chunksize until \r |
||
| 406 | $chunk_size .= $byte; |
||
| 407 | if (strlen($chunk_size) > 128) // set an abritrary limit on the size of chunks |
||
| 408 | throw new HTTPClientException('Allowed response size exceeded'); |
||
| 409 | } |
||
| 410 | $this->readLine($socket, 'chunk'); // readtrailing \n |
||
| 411 | $chunk_size = hexdec($chunk_size); |
||
| 412 | |||
| 413 | if($this->max_bodysize && $chunk_size+strlen($r_body) > $this->max_bodysize){ |
||
| 414 | if ($this->max_bodysize_abort) |
||
| 415 | throw new HTTPClientException('Allowed response size exceeded'); |
||
| 416 | $this->error = 'Allowed response size exceeded'; |
||
| 417 | $chunk_size = $this->max_bodysize - strlen($r_body); |
||
| 418 | $abort = true; |
||
| 419 | } |
||
| 420 | |||
| 421 | if ($chunk_size > 0) { |
||
| 422 | $r_body .= $this->readData($socket, $chunk_size, 'chunk'); |
||
| 423 | $this->readData($socket, 2, 'chunk'); // read trailing \r\n |
||
| 424 | } |
||
| 425 | } while ($chunk_size && !$abort); |
||
| 426 | }elseif(isset($this->resp_headers['content-length']) && !isset($this->resp_headers['transfer-encoding'])){ |
||
| 427 | /* RFC 2616 |
||
| 428 | * If a message is received with both a Transfer-Encoding header field and a Content-Length |
||
| 429 | * header field, the latter MUST be ignored. |
||
| 430 | */ |
||
| 431 | |||
| 432 | // read up to the content-length or max_bodysize |
||
| 433 | // for keep alive we need to read the whole message to clean up the socket for the next read |
||
| 434 | if( |
||
| 435 | !$this->keep_alive && |
||
| 436 | $this->max_bodysize && |
||
| 437 | $this->max_bodysize < $this->resp_headers['content-length'] |
||
| 438 | ) { |
||
| 439 | $length = $this->max_bodysize + 1; |
||
| 440 | }else{ |
||
| 441 | $length = $this->resp_headers['content-length']; |
||
| 442 | } |
||
| 443 | |||
| 444 | $r_body = $this->readData($socket, $length, 'response (content-length limited)', true); |
||
| 445 | }elseif( !isset($this->resp_headers['transfer-encoding']) && $this->max_bodysize && !$this->keep_alive){ |
||
| 446 | $r_body = $this->readData($socket, $this->max_bodysize+1, 'response (content-length limited)', true); |
||
| 447 | } elseif ((int)$this->status === 204) { |
||
| 448 | // request has no content |
||
| 449 | } else{ |
||
| 450 | // read entire socket |
||
| 451 | while (!feof($socket)) { |
||
| 452 | $r_body .= $this->readData($socket, 4096, 'response (unlimited)', true); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | // recheck body size, we might have read max_bodysize+1 or even the whole body, so we abort late here |
||
| 457 | if($this->max_bodysize){ |
||
| 458 | if(strlen($r_body) > $this->max_bodysize){ |
||
| 459 | if ($this->max_bodysize_abort) { |
||
| 460 | throw new HTTPClientException('Allowed response size exceeded'); |
||
| 461 | } else { |
||
| 462 | $this->error = 'Allowed response size exceeded'; |
||
| 463 | } |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | } catch (HTTPClientException $err) { |
||
| 468 | $this->error = $err->getMessage(); |
||
| 469 | if ($err->getCode()) |
||
| 470 | $this->status = $err->getCode(); |
||
| 471 | unset(self::$connections[$connectionId]); |
||
| 472 | fclose($socket); |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | |||
| 476 | if (!$this->keep_alive || |
||
| 477 | (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) { |
||
| 478 | // close socket |
||
| 479 | fclose($socket); |
||
| 480 | unset(self::$connections[$connectionId]); |
||
| 481 | } |
||
| 482 | |||
| 483 | // decode gzip if needed |
||
| 484 | if(isset($this->resp_headers['content-encoding']) && |
||
| 485 | $this->resp_headers['content-encoding'] == 'gzip' && |
||
| 486 | strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){ |
||
| 487 | $this->resp_body = @gzinflate(substr($r_body, 10)); |
||
| 488 | if($this->resp_body === false){ |
||
| 489 | $this->error = 'Failed to decompress gzip encoded content'; |
||
| 490 | $this->resp_body = $r_body; |
||
| 491 | } |
||
| 492 | }else{ |
||
| 493 | $this->resp_body = $r_body; |
||
| 494 | } |
||
| 495 | |||
| 496 | $this->debug('response body',$this->resp_body); |
||
| 497 | $this->redirect_count = 0; |
||
| 498 | return true; |
||
| 499 | } |
||
| 500 | |||
| 875 |