Skip to content

Commit

Permalink
Added url preprocessing and removed repeated encoding from library (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 authored Mar 24, 2022
1 parent 847149c commit a45c4c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To install `apimatic/unirest-php` with Composer, just add the following to your
```json
{
"require": {
"apimatic/unirest-php": "^2.2.1"
"apimatic/unirest-php": "^2.2.2"
}
}
```
Expand Down
57 changes: 21 additions & 36 deletions src/Unirest/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public static function send($method, $url, $body = null, $headers = array(), $us
}

$curl_base_options = [
CURLOPT_URL => self::encodeUrl($url),
CURLOPT_URL => self::validateUrl($url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
Expand Down Expand Up @@ -794,47 +794,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);

return array_combine(array_map('hex2bin', array_keys($values)), $values);
}

/**
* Ensure that a URL is encoded and safe to use with cURL
* @param string $url URL to encode
* @return string
* Validates and processes the given Url to ensure safe usage with cURL.
* @param string $url The given Url to process
* @return string Pre-processed Url as string
* @throws Exception
*/
private static function encodeUrl($url)
public static function validateUrl($url)
{
$url_parsed = parse_url($url);

$scheme = $url_parsed['scheme'] . '://';
$host = $url_parsed['host'];
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null);
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null);
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);

if ($query !== null) {
$query = '?' . http_build_query(self::getArrayFromQuerystring($query));
//perform parameter validation
if (!is_string($url)) {
throw new Exception('Invalid Url.');
}

if (null !== $port) {
if (!is_string($port)) {
$port = strval($port);
}
if ($port[0] !== ':') {
$port = ':' . $port;
}
//ensure that the urls are absolute
$matchCount = preg_match("#^(https?://[^/]+)#", $url, $matches);
if ($matchCount == 0) {
throw new Exception('Invalid Url format.');
}
//get the http protocol match
$protocol = $matches[1];

$result = $scheme . $host . $port . $path . $query;
return $result;
//remove redundant forward slashes
$query = substr($url, strlen($protocol));
$query = preg_replace("#//+#", "/", $query);

//return process url
return $protocol . $query;
}

private static function getHeaderString($key, $val)
Expand Down

0 comments on commit a45c4c7

Please sign in to comment.