Skip to content

Commit

Permalink
Add getHeader() function
Browse files Browse the repository at this point in the history
  • Loading branch information
dotancohen committed Mar 20, 2017
1 parent 17f7be4 commit 54b9cf8
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions misc.php
Expand Up @@ -22,3 +22,58 @@ function ensureArray($input)
return array($input);
}



/**
* Return a desired HTTP header
*
* Accepts pseudo-headers: Protocol and RespCode
* Known Issue: If multiple headers share a key, returns only the first
*
* @author Dotan Cohen
* @version 2017-03-20
*
* @param $headers array Array of HTTP headers
* @param $header string Name of desired HTTP header, or pseudo-header Protocol or RespCode
*
* @return null|string
*/
function getHeader(array $headers, $header)
{
// TODO: Support pseudo-headers ProtocolType ProtocolVersion RespDescription Charset

$header = trim(strtolower($header));

foreach ( $headers as $item ) {
$parts = explode(':', $item, 2);

// Support pseudo-headers
if ( count($parts) < 2 ) {
if ( substr($parts[0],0,4)=='HTTP' ) {

$parts = explode(' ', $item);
if ( 2<=count($parts) ) {
switch ($header) {
case 'protocol':
return $parts[0];
break;
case 'respcode':
return $parts[1];
break;
}
}
}

continue;
}

$key = trim(strtolower($parts[0]));
$val = trim($parts[1]);

if ( $key==$header ) {
return $val;
}
}

return NULL;
}

0 comments on commit 54b9cf8

Please sign in to comment.