diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 8e1a28865..a147aab24 100755 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -51,6 +51,13 @@ class Requests_Transport_cURL implements Requests_Transport { */ protected $fp; + /** + * Hook dispatcher instance + * + * @var Requests_Hooks + */ + protected $hooks; + /** * Have we finished the headers yet? * @@ -112,6 +119,8 @@ public function __construct() { * @return string Raw HTTP result */ public function request($url, $headers = array(), $data = array(), $options = array()) { + $this->hooks = $options['hooks']; + $this->setup_handle($url, $headers, $data, $options); $options['hooks']->dispatch('curl.before_send', array(&$this->fp)); @@ -251,6 +260,7 @@ public function &get_subrequest_handle($url, $headers, $data, $options) { if ($options['max_bytes'] !== false) { $this->response_byte_limit = $options['max_bytes']; } + $this->hooks = $options['hooks']; return $this->fp; } @@ -370,6 +380,7 @@ public function stream_headers($handle, $headers) { * @return integer Length of provided data */ protected function stream_body($handle, $data) { + $this->hooks->dispatch('request.progress', array($data, $this->response_bytes, $this->response_byte_limit)); $data_length = strlen($data); // Are we limiting the response size? diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 06d23659b..1a4493a79 100755 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -245,6 +245,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar // Are we in body mode now? if ($doingbody) { + $options['hooks']->dispatch('request.progress', array($block, $size, $this->max_bytes)); $data_length = strlen($block); if ($this->max_bytes) { // Have we already hit a limit? diff --git a/tests/Transport/Base.php b/tests/Transport/Base.php index ad436c47e..b591df07e 100755 --- a/tests/Transport/Base.php +++ b/tests/Transport/Base.php @@ -727,4 +727,17 @@ public function testAlternatePort() { $this->assertEquals(1, $num, 'Response should contain the port number'); $this->assertEquals(8080, $matches[1]); } + + public function testProgressCallback() { + $mock = $this->getMockBuilder('stdClass')->setMethods(array('progress'))->getMock(); + $mock->expects($this->atLeastOnce())->method('progress'); + $hooks = new Requests_Hooks(); + $hooks->register('request.progress', array($mock, 'progress')); + $options = array( + 'hooks' => $hooks, + ); + $options = $this->getOptions($options); + + $response = Requests::get(httpbin('/get'), array(), $options); + } }