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

Improve query string normalization and add hooks system #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
87 changes: 76 additions & 11 deletions src/Unirest/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Request
private static $socketTimeout = null;
private static $verifyPeer = true;
private static $verifyHost = true;
private static $hooks = array();

private static $auth = array (
'user' => '',
Expand Down Expand Up @@ -412,9 +413,13 @@ public static function send($method, $url, $body = null, $headers = array(), $us

$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
}

$url=self::encodeUrl($url);

self::dispatch('before_request', array(&$method, &$url, &$body, &$headers, &$username, &$password));

$curl_base_options = [
CURLOPT_URL => self::encodeUrl($url),
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
Expand Down Expand Up @@ -473,16 +478,23 @@ public static function send($method, $url, $body = null, $headers = array(), $us
$info = self::getInfo();

if ($error) {
self::dispatch('before_error_response_exception', array($error,$info,$method, $url, $body, $headers, $username, $password));
throw new Exception($error);
}

self::dispatch('before_parse_response', array(&$response,&$info,$method, $url, $body, $headers, $username, $password));

// Split the full response in its headers and body
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$httpCode = $info['http_code'];

$uResponse=new Response($httpCode, $body, $header, self::$jsonOpts);

self::dispatch('after_response', array(&$uResponse,$info,$method, $url, $body, $headers, $username, $password));

return new Response($httpCode, $body, $header, self::$jsonOpts);
return $uResponse;
}

public static function getInfo($opt = false)
Expand Down Expand Up @@ -522,16 +534,32 @@ public static function getFormattedHeaders($headers)
return $formattedHeaders;
}

private static function getArrayFromQuerystring($query)
{
$query = preg_replace_callback('/(?:^|(?<=&))[^=[]+/', function ($match) {
return bin2hex(urldecode($match[0]));
}, $query);

parse_str($query, $values);
/**
* Normalizes a query string from the given parameters.
*
* @param string $query Query string
* @return string A normalized query string for the Request
*/
public static function normalizeQueryString($query)
{
if ('' == $query) {
return '';
}

return array_combine(array_map('hex2bin', array_keys($values)), $values);
}
$parts = array();
foreach (explode('&', $query) as $param) {
if ('' === $param || '=' === $param[0]) {
continue;
}
$keyValuePair = explode('=', $param, 2);
// RFC 3986 with rawurlencode.
$parts[] = isset($keyValuePair[1]) ?
rawurlencode(urldecode($keyValuePair[0])).'='.rawurlencode(urldecode($keyValuePair[1])) :
rawurlencode(urldecode($keyValuePair[0]));
}
return implode('&', $parts);
}

/**
* Ensure that a URL is encoded and safe to use with cURL
Expand All @@ -549,7 +577,7 @@ private static function encodeUrl($url)
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);

if ($query !== null) {
$query = '?' . http_build_query(self::getArrayFromQuerystring($query));
$query = '?' .self::normalizeQueryString($query);
}

if ($port && $port[0] !== ':') {
Expand All @@ -576,4 +604,41 @@ private static function mergeCurlOptions(&$existing_options, $new_options)
$existing_options = $new_options + $existing_options;
return $existing_options;
}

/**
* Register a callback for a hook
*
* @param string $hook Hook name
* @param callback $callback Function/method to call on event
* @param int $priority Priority number. <0 is executed earlier, >0 is executed later
*/
public static function register($hook, $callback, $priority = 0) {
if (!isset(self::$hooks[$hook])) {
self::$hooks[$hook] = array();
}
if (!isset(self::$hooks[$hook][$priority])) {
self::$hooks[$hook][$priority] = array();
}
self::$hooks[$hook][$priority][] = $callback;
}

/**
* Dispatch a message
*
* @param string $hook Hook name
* @param array $parameters Parameters to pass to callbacks
* @return boolean Successfulness
*/
private static function dispatch($hook, $parameters = array()) {
if (empty(self::$hooks[$hook])) {
return false;
}
foreach (self::$hooks[$hook] as $priority => $hooked) {
foreach ($hooked as $callback) {
call_user_func_array($callback, $parameters);
}
}
return true;
}

}