开发者

How to get the functionality of http_parse_headers without PECL?

I am using PHP 5.3.5 on windows, and I didn't find any pecl_http.dll that works with my installation.

So my question is,

How do I get the functionality of http_parse_he开发者_运维知识库aders without using PECL?


From the documentation page, first comment:

 if( !function_exists( 'http_parse_headers' ) ) {
     function http_parse_headers( $header )
     {
         $retVal = array();
         $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
         foreach( $fields as $field ) {
             if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
                 $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
                 if( isset($retVal[$match[1]]) ) {
                     $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                 } else {
                     $retVal[$match[1]] = trim($match[2]);
                 }
             }
         }
         return $retVal;
     }
}

Alternatively, you may want to read how to install a PECL extension on Windows, to be honest, I don't know anything about that.


This is also lifted from the PHP Documentation for http_parse_headers. When I compared it with @Berry Langerak's answer (using microtime) I found it to be 17% faster on average, using a sample of 10 headers (presumably because it doesn't use regular expressions).

if (!function_exists('http_parse_headers')) {
    function http_parse_headers($raw_headers) {
        $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]])) {
                    $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
                }
                else {
                    $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]); 
            }
        }
        
        return $headers;
    }
}

Note: this includes the fix of the small mistake the author points out in a later comment.


Regex Function

0.00035881996
0.00036096572
0.00034999847
0.00043797492
0.00033497810

Average: 0.000368547434

This Function

0.00006198883
0.00006604194
0.00007104873
0.00006413459
0.00006389617

Average 0.000065422052


You can get the extension for Windows at

  • http://downloads.php.net/pierre/

It's one of the php_http-5.3-*-x86.zip files. Check which PHP you have installed and pick the right one, e.g. my PHP is a php-5.3.6-nts-Win32-VC9-x86, so I needed php_http-5.3-nts-svn20091125-vc9-x86.zip.

Download the zip and extract the php_http.dll to your extension folder. The extension folder should be the /ext folder in your php installation directory. If you are not sure, open your php.ini file and search for these lines:

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = .\ext

The value for extension_dir is where you have to place the dll. If you are unsure where your php.ini is located, open a command prompt and do

php --ini

This will tell you where your php.ini is. It will output something like

Configuration File (php.ini) Path: C:\Windows
Loaded Configuration File:         C:\php5\php-5.3.6-nts-Win32-VC9-x86\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

After you have copied the dll, add the extension to your php.ini to enable it. Find where it says something like

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

There should be multiple lines similar to this:

;extension=php_sybase_ct.dll
extension=php_tidy.dll
;extension=php_xmlrpc.dll
extension=php_xsl.dll
;extension=php_zip.dll

Add the following line:

extension=php_http.dll

Save the php.ini and type the following at your command prompt:

php --ri http

You should now get a rather extensive output starting with

http
HTTP Support => enabled
Extension Version => 1.7.0-dev
… more stuff

This means, you have successfully installed the extension and can use it now.

Note, that in order to be able to load this extension on Windows, you additionally need to load the following PHP extensions: hash, iconv and SPL.


Since I could not find any function that behaved exactly like the PECL once, I wrote my own, compared to the others it turns out to be pretty fast…

function dirtyHeaderParser($headers, $strict = true){
    $arr = array();
    $s = strtok($headers, ':');
    while ($s){
        if ( ($s[0] === ' ') || ($s[0] === "\t") ){
            if (count($arr) != 0){
                $tail = strtok('');
                $tail = "{$s}:{$tail}";
                $v = strtok($tail, "\n");
                if (is_array($arr[$key])){
                    end($arr[$key]);
                    $last = key($arr[$key]);
                    $arr[$key][$last] = "{$arr[$key][$last]}\n{$v}";
                    reset($arr[$key]);
                } else {
                    $arr[$key] = "{$arr[$key]}\n{$v}";
                }
            }
        } else {
            $v = strtok("\n");
            if ($v){
                $key = strtolower($s);
                if (((strpos($key, "\n") !== false) || (strpos($key, "\t") !== false) || (strpos($key, " ") !== false)) && $strict) {
                    return false;
                } 
                if (array_key_exists($key, $arr)){
                    if (!is_array($arr[$key])){
                        $arr[$key] = array($arr[$key]);
                    }
                    $arr[$key][] = trim($v);
                } else {
                    $arr[$key] = trim($v);
                }
            } else {
                break;
            }
        }
        $s = strtok(':');
    }
    return (count($arr) == 0) ? false : $arr;
}

Strict mode means, it will return false if a header key contains \n, whitespace or \t. It supports multiple line headers and double header values (with multiple lines too), if anyone finds something not behaving like the PECL version, I would be happy if you leave a comment.


Here is a modified version form the documentation page which works just like the PECL version :)

function http_parse_headers( $header ) {
        $retVal = array();
        $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
        foreach( $fields as $field ) {
            if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
                $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
                if( isset($retVal[$match[1]]) ) {
                    if ( is_array( $retVal[$match[1]] ) ) {
                        $i = count($retVal[$match[1]]);
                        $retVal[$match[1]][$i] = $match[2];
                    }
                    else {
                        $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                    }
                } else {
                    $retVal[$match[1]] = trim($match[2]);
                }
            }
        }
        return $retVal;
    }


Hate to continue such an old thread that's already so long, but this one is superior because:

  • Doesn't use regexps
  • Handles duplicate headers correctly
  • Not so many array and string mutation calls
  • Parses the Status line

(You may need code to check if the extension already exists)

function http_parse_headers ($raw) {
    $res = [];
    foreach (explode("\n", $raw) as $h) {
        $h = explode(':', $h, 2);
        $first = trim($h[0]);
        $last = trim($h[1]);
        if (array_key_exists($first, $res)) {
            $res[$first] .= ", " . $last;
        } else if (isset($h[1])) {
            $res[$first] = $last;
        } else {
            $res[] = $first;
        }
    }
    return $res;
}


function parse_headers($headers)
{
    $headers = preg_replace('/^\r\n/m', '', $headers);
    $headers = preg_replace('/\r\n\s+/m', ' ', $headers);
    preg_match_all('/^([^: ]+):\s(.+?(?:\r\n\s(?:.+?))*)?\r\n/m', $headers . "\r\n", $matches);

    $result = array();
    foreach ($matches[1] as $key => $value)
        $result[$value] = (array_key_exists($value, $result) ? $result[$value] . "\n" : '') . $matches[2][$key];

    return $result;
}


Unfortunately, for the function provided in the documentation page, if there are more than two of the same header (ie Set-Cookie), the structure of the array will be incorrect, eg:

    [Set-Cookie] => Array
    (
        [0] => Array
        (
            [0] => AWESOMESESSIONID=xQ5TRl1GXDQcQCXytfb1PK!-744872953!NONE; Path=/cte-lps2s-test2/; Secure
            [1] => AWESOME_SESSH_c25c28d0-b763-11df-979f23a029aa77=%2Fcte-lps2s-test2; Path=/
        ) 
        [1] => MOREAWESOME=2_uGgNpo2-jm-CjfaefLzjFhmmP-y3HzwNZKE0gsTeP+; Path=/; Secure
    )

The below modification to the code will fix this error (see the comment):

if( !function_exists( 'http_parse_headers' ) ) {
     function http_parse_headers( $header )
     {
         $retVal = array();
         $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ',$header));
         foreach( $fields as $field ) {
             if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
                 $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")',     strtolower(trim($match[1])));
                 // We need to check if an array of values has already been created, to     maintain the correct level in the $retVal array.
                 if(is_array($retVal[$match[1]])) {
                        $retVal[$match[1]] []= $match[2];
                 }
                 else {
                    $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                 }
             } else {
                 $retVal[$match[1]] = trim($match[2]);
             }
         }
         return $retVal;
     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜