Merge branch 'MDL-49497' of git://github.com/stronk7/moodle

This commit is contained in:
Dan Poltawski 2015-06-23 16:25:04 +01:00
commit b32ba584b2
2 changed files with 141 additions and 3 deletions

View file

@ -2684,6 +2684,7 @@ class curl {
/** @var array cURL options */ /** @var array cURL options */
private $options; private $options;
/** @var string Proxy host */ /** @var string Proxy host */
private $proxy_host = ''; private $proxy_host = '';
/** @var string Proxy auth */ /** @var string Proxy auth */
@ -3004,15 +3005,36 @@ class curl {
} }
$this->setopt($options); $this->setopt($options);
// reset before set options
// Reset before set options.
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader')); curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
// set headers
// Setting the User-Agent based on options provided.
$useragent = '';
if (!empty($options['CURLOPT_USERAGENT'])) {
$useragent = $options['CURLOPT_USERAGENT'];
} else if (!empty($this->options['CURLOPT_USERAGENT'])) {
$useragent = $this->options['CURLOPT_USERAGENT'];
} else {
$useragent = 'MoodleBot/1.0';
}
// Set headers.
if (empty($this->header)) { if (empty($this->header)) {
$this->setHeader(array( $this->setHeader(array(
'User-Agent: MoodleBot/1.0', 'User-Agent: ' . $useragent,
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection: keep-alive' 'Connection: keep-alive'
)); ));
} else if (!in_array('User-Agent: ' . $useragent, $this->header)) {
// Remove old User-Agent if one existed.
// We have to partial search since we don't know what the original User-Agent is.
if ($match = preg_grep('/User-Agent.*/', $this->header)) {
$key = array_keys($match)[0];
unset($this->header[$key]);
}
$this->setHeader(array('User-Agent: ' . $useragent));
} }
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header); curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);

View file

@ -878,4 +878,120 @@ EOF;
$this->assertEquals('image', $mimeinfo['png']['string']); $this->assertEquals('image', $mimeinfo['png']['string']);
$this->assertEquals(true, $mimeinfo['txt']['defaulticon']); $this->assertEquals(true, $mimeinfo['txt']['defaulticon']);
} }
/**
* Test curl agent settings.
*/
public function test_curl_useragent() {
$curl = new testable_curl();
$options = $curl->get_options();
$this->assertNotEmpty($options);
$curl->call_apply_opt($options);
$this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
$this->assertFalse(in_array('User-Agent: Test/1.0', $curl->header));
$options['CURLOPT_USERAGENT'] = 'Test/1.0';
$curl->call_apply_opt($options);
$this->assertTrue(in_array('User-Agent: Test/1.0', $curl->header));
$this->assertFalse(in_array('User-Agent: MoodleBot/1.0', $curl->header));
$curl->set_option('CURLOPT_USERAGENT', 'AnotherUserAgent/1.0');
$curl->call_apply_opt();
$this->assertTrue(in_array('User-Agent: AnotherUserAgent/1.0', $curl->header));
$this->assertFalse(in_array('User-Agent: Test/1.0', $curl->header));
$curl->set_option('CURLOPT_USERAGENT', 'AnotherUserAgent/1.1');
$options = $curl->get_options();
$curl->call_apply_opt($options);
$this->assertTrue(in_array('User-Agent: AnotherUserAgent/1.1', $curl->header));
$this->assertFalse(in_array('User-Agent: AnotherUserAgent/1.0', $curl->header));
$curl->unset_option('CURLOPT_USERAGENT');
$curl->call_apply_opt();
$this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
// Finally, test it via exttests, to ensure the agent is sent properly.
// Matching.
$testurl = $this->getExternalTestFileUrl('/test_agent.php');
$extcurl = new curl();
$contents = $extcurl->get($testurl, array(), array('CURLOPT_USERAGENT' => 'AnotherUserAgent/1.2'));
$response = $extcurl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $extcurl->get_errno());
$this->assertSame('OK', $contents);
// Not matching.
$contents = $extcurl->get($testurl, array(), array('CURLOPT_USERAGENT' => 'NonMatchingUserAgent/1.2'));
$response = $extcurl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $extcurl->get_errno());
$this->assertSame('', $contents);
}
}
/**
* Test-specific class to allow easier testing of curl functions.
*
* @copyright 2015 Dave Cooper
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_curl extends curl {
/**
* Accessor for private options array using reflection.
*
* @return array
*/
public function get_options() {
// Access to private property.
$rp = new ReflectionProperty('curl', 'options');
$rp->setAccessible(true);
return $rp->getValue($this);
}
/**
* Setter for private options array using reflection.
*
* @param array $options
*/
public function set_options($options) {
// Access to private property.
$rp = new ReflectionProperty('curl', 'options');
$rp->setAccessible(true);
$rp->setValue($this, $options);
}
/**
* Setter for individual option.
* @param string $option
* @param string $value
*/
public function set_option($option, $value) {
$options = $this->get_options();
$options[$option] = $value;
$this->set_options($options);
}
/**
* Unsets an option on the curl object
* @param string $option
*/
public function unset_option($option) {
$options = $this->get_options();
unset($options[$option]);
$this->set_options($options);
}
/**
* Wrapper to access the private curl::apply_opt() method using reflection.
*
* @param array $options
* @return resource The curl handle
*/
public function call_apply_opt($options = null) {
// Access to private method.
$rm = new ReflectionMethod('curl', 'apply_opt');
$rm->setAccessible(true);
$ch = curl_init();
return $rm->invoke($this, $ch, $options);
}
} }