Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress hook #180

Merged
merged 3 commits into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
13 changes: 13 additions & 0 deletions tests/Transport/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}