Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can HTTP headers contain colons in the field value?

I've been working with HTTP headers recently. I am parsing field and value from HTTP header requesrts based on the colon separated mandated by RFC. In python:

header_request_line.split(":")

However, this messes up if colons are allowed in the value fields. Consider:

User-Agent: Mozilla:4.0

which would be split into 3 strings, not 2 as I wanted.

like image 954
jeffrey Avatar asked Nov 14 '14 22:11

jeffrey


2 Answers

Yes. So you can do something like this (pseudo):

header = "User-Agent: Mozilla:4.0"
headerParts = header.split(":")

key = headerParts[0]
value = headerParts.substring(key.length).trim()

// or
value = headerParts.skip(1).join(":")

But you'll probably run into various issues when parsing headers from various servers, so why not use a library?

like image 152
CodeCaster Avatar answered Sep 30 '22 02:09

CodeCaster


Yes it can

In your example you might simply use split with maxsplit parameter specified like this:

header_request_line.split(":", 1)

It would produce the following result and would work despite the number of colons in the field value:

In [2]: 'User-Agent: Mozilla:4.0'.split(':', 1)
Out[2]: ['User-Agent', ' Mozilla:4.0']
like image 44
ffeast Avatar answered Sep 30 '22 02:09

ffeast