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