HTTP.php 3.4 KB

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