add http_parse_headers, update test case

This commit is contained in:
wing328
2015-12-06 23:58:25 +08:00
parent bb341832a5
commit b282d4fbea
9 changed files with 146 additions and 52 deletions

View File

@@ -216,7 +216,7 @@ class ApiClient
// Make the request
$response = curl_exec($curl);
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$http_header = substr($response, 0, $http_header_size);
$http_header = $this->http_parse_headers(substr($response, 0, $http_header_size));
$http_body = substr($response, $http_header_size);
$response_info = curl_getinfo($curl);
@@ -287,4 +287,52 @@ class ApiClient
return implode(',', $content_type);
}
}
/**
* Return an array of HTTP response headers
*
* @param string $raw_headers A string of raw HTTP response headers
*
* @return string[] Array of HTTP response heaers
*/
protected function http_parse_headers($raw_headers)
{
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
$headers = array();
$key = ''; // [+]
foreach(explode("\n", $raw_headers) as $i => $h)
{
$h = explode(':', $h, 2);
if (isset($h[1]))
{
if (!isset($headers[$h[0]]))
$headers[$h[0]] = trim($h[1]);
elseif (is_array($headers[$h[0]]))
{
// $tmp = array_merge($headers[$h[0]], array(trim($h[1]))); // [-]
// $headers[$h[0]] = $tmp; // [-]
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); // [+]
}
else
{
// $tmp = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [-]
// $headers[$h[0]] = $tmp; // [-]
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [+]
}
$key = $h[0]; // [+]
}
else // [+]
{ // [+]
if (substr($h[0], 0, 1) == "\t") // [+]
$headers[$key] .= "\r\n\t".trim($h[0]); // [+]
elseif (!$key) // [+]
$headers[0] = trim($h[0]);trim($h[0]); // [+]
} // [+]
}
return $headers;
}
}

View File

@@ -198,7 +198,7 @@ class ObjectSerializer
$deserialized = $data;
} elseif ($class === '\SplFileObject') {
// determine file name
if (preg_match('/Content-Disposition: inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader, $match)) {
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader['Content-Disposition'], $match)) {
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1];
} else {
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');

View File

@@ -208,7 +208,9 @@ use \{{invokerPackage}}\ObjectSerializer;
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader), $statusCode, $httpHeader);
{{/returnType}}{{#returnType}}
return array(null, $statusCode, $httpHeader);
{{/returnType}}
} catch (ApiException $e) {
switch ($e->getCode()) { {{#responses}}{{#dataType}}
@@ -220,9 +222,6 @@ use \{{invokerPackage}}\ObjectSerializer;
throw $e;
}
{{#returnType}}
return array(null, $statusCode, $httpHeader);
{{/returnType}}
}
{{/operation}}
}