From ac9ca32923a993a58b8d4bed641603454cc349be Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 16 Apr 2013 01:14:32 -0700 Subject: [PATCH] fixing bug with arrays in querystring --- lib/HttpResponse.php | 15 ++++++++------- lib/Unicorn.php | 27 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/HttpResponse.php b/lib/HttpResponse.php index 8bab2f9..b864496 100644 --- a/lib/HttpResponse.php +++ b/lib/HttpResponse.php @@ -57,16 +57,17 @@ class HttpResponse { private function get_headers_from_curl_response($headers) { - foreach (explode("\r\n", $headers) as $i => $line) { + foreach (explode("\r\n", $headers) as $i => $line) { if ($i !== 0) { - list ($key, $value) = explode(': ', $line); - if (!empty($key) && substr($key, 0, 4) != "HTTP") $result[$key] = $value; - } + if (!empty($key) && substr($key, 0, 4) != "HTTP") { + $result[$key] = $value; + } else { + list ($key, $value) = explode(': ', $line); + } + } } - return $result; + return $result; } - } ?> - diff --git a/lib/Unicorn.php b/lib/Unicorn.php index dc575ed..6de0f3a 100644 --- a/lib/Unicorn.php +++ b/lib/Unicorn.php @@ -113,20 +113,29 @@ class Unicorn { } private static function encodeUrl($url) { - $parsedUrl = parse_url($url); - parse_str( $parsedUrl['query'], $query ); // generating an array by reference (yes, kinda weird) + // Parse URL into pieces + $url_parsed = parse_url($url); - $result = $parsedUrl["scheme"] . "://" . $parsedUrl["host"] . (($parsedUrl["port"] != NULL ? ":" . $parsedUrl["port"] : "")) . $parsedUrl["path"] . "?"; - + // Build the basics bypassing notices + $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 ); + + // Do we need to encode anything? if ($query != null) { - foreach($query as $key => $val) { - $result .= $key . "=" . rawurlencode($val) . "&"; - } - $result = substr($result, 0, strlen($result) - 1); + // Break up the query into an array + parse_str($url_parsed['query'], $query_parsed); + // Encode and build query based on RFC 1738 + $query = '?'.http_build_query($query_parsed); } + + // Return the completed URL + $result = $scheme . $host . $port . $path . $query; return $result; } } -?> +?> \ No newline at end of file