Requests_Proxy_HTTP

HTTP Proxy connection interface.

Defined (1)

The class is defined in the following location(s).

/wp-includes/Requests/Proxy/HTTP.php  
  1. class Requests_Proxy_HTTP implements Requests_Proxy { 
  2. /** 
  3. * Proxy host and port 
  4. * Notation: "host:port" (eg 127.0.0.1:8080 or someproxy.com:3128) 
  5. * @var string 
  6. */ 
  7. public $proxy; 
  8.  
  9. /** 
  10. * Username 
  11. * @var string 
  12. */ 
  13. public $user; 
  14.  
  15. /** 
  16. * Password 
  17. * @var string 
  18. */ 
  19. public $pass; 
  20.  
  21. /** 
  22. * Do we need to authenticate? (ie username & password have been provided) 
  23. * @var boolean 
  24. */ 
  25. public $use_authentication; 
  26.  
  27. /** 
  28. * Constructor 
  29. * @since 1.6 
  30. * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) 
  31. * @param array|null $args Array of user and password. Must have exactly two elements 
  32. */ 
  33. public function __construct($args = null) { 
  34. if (is_string($args)) { 
  35. $this->proxy = $args; 
  36. elseif (is_array($args)) { 
  37. if (count($args) == 1) { 
  38. list($this->proxy) = $args; 
  39. elseif (count($args) == 3) { 
  40. list($this->proxy, $this->user, $this->pass) = $args; 
  41. $this->use_authentication = true; 
  42. else { 
  43. throw new Requests_Exception('Invalid number of arguments', 'proxyhttpbadargs'); 
  44.  
  45. /** 
  46. * Register the necessary callbacks 
  47. * @since 1.6 
  48. * @see curl_before_send 
  49. * @see fsockopen_remote_socket 
  50. * @see fsockopen_remote_host_path 
  51. * @see fsockopen_header 
  52. * @param Requests_Hooks $hooks Hook system 
  53. */ 
  54. public function register(Requests_Hooks &$hooks) { 
  55. $hooks->register('curl.before_send', array(&$this, 'curl_before_send')); 
  56.  
  57. $hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket')); 
  58. $hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path')); 
  59. if ($this->use_authentication) { 
  60. $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); 
  61.  
  62. /** 
  63. * Set cURL parameters before the data is sent 
  64. * @since 1.6 
  65. * @param resource $handle cURL resource 
  66. */ 
  67. public function curl_before_send(&$handle) { 
  68. curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
  69. curl_setopt($handle, CURLOPT_PROXY, $this->proxy); 
  70.  
  71. if ($this->use_authentication) { 
  72. curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); 
  73. curl_setopt($handle, CURLOPT_PROXYUSERPWD, $this->get_auth_string()); 
  74.  
  75. /** 
  76. * Alter remote socket information before opening socket connection 
  77. * @since 1.6 
  78. * @param string $remote_socket Socket connection string 
  79. */ 
  80. public function fsockopen_remote_socket(&$remote_socket) { 
  81. $remote_socket = $this->proxy; 
  82.  
  83. /** 
  84. * Alter remote path before getting stream data 
  85. * @since 1.6 
  86. * @param string $path Path to send in HTTP request string ("GET ...") 
  87. * @param string $url Full URL we're requesting 
  88. */ 
  89. public function fsockopen_remote_host_path(&$path, $url) { 
  90. $path = $url; 
  91.  
  92. /** 
  93. * Add extra headers to the request before sending 
  94. * @since 1.6 
  95. * @param string $out HTTP header string 
  96. */ 
  97. public function fsockopen_header(&$out) { 
  98. $out .= sprintf("Proxy-Authorization: Basic %s\r\n", base64_encode($this->get_auth_string())); 
  99.  
  100. /** 
  101. * Get the authentication string (user:pass) 
  102. * @since 1.6 
  103. * @return string 
  104. */ 
  105. public function get_auth_string() { 
  106. return $this->user . ':' . $this->pass;