Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

14 Nov 2005

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;