Parsing http headers

Today i updated my HTTP proxy a little. RFC 2616 describes Message Headers as following:

message-header = field-name ":" [ field-value ]
field-name     = token
field-value    = *( field-content | LWS )
field-content  = <the OCTETs making up the field-value and consisting of 
                 either *TEXT or combinations of token, separators, 
                 and quoted-string>

Here is the code i used to get the field-name and field-value. Do you see the bug?

my ($name, $value) = split /:/, $in;

Location headers look like “header: http://www.example.com”. Now, the problem is that split returns a list with “location”, “http” and “www.example.com”. Here is the solution:

my $in = "location: http://www.example.com";
my ($name, $value) = split /:/, $in, 2;

This entry was posted on Monday, November 14th, 2005 at 21:42 and is filed under Perl. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.