0

I have the following problem:

I have to split an HTTP header which has been serialized in a string. The current format is as follows:

Content-type: xml\nContent-Length: 12\nlast-modified: date\n ..etc etc

I need to store the keys and values of the header into a std::map .

(map["Content-type"] = "xml"; map["Content-Length"] = "12" ...)

Any suggestions on how to do that? Is it possible with boost:regex?

1 Answer 1

3

There's no need to use regular expressions for this, especially not boost (I still consider it a horrible, horrible dependency due to it's size alone). Since the separators are fixed and always the same characters (line breaks and colons) there's no real need to use any regular expressions engine.

Instead, use strtok() or one of the related functions (e.g. the wide char version; keep in mind that this function has its own downsides and problems, depending on the actual usage, e.g. it might not be threadsafe).

First step, you'd use it to split the header into single lines. Once you receive an empty line, you'll know that the header is over.

Then, to read your header line, you'll split it at the :, which will get you both a key as well as a value.

As an alternative, you can as well use std::string and the member function find_first_of() to look for the correct positions. The rest of the strategy would be the same.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for not recommending regular expressions ... -1 for the inaccurate depiction of boost ... -1 for recommending strtok.
Is it inaccurate? True, you can use small parts of boost, get them alone etc, but in general this is very tedious and time consuming (at least from my personal experience); downloading and compiling the whole library is still very massive. As for strtok(), I agree, it should be noted that there are downsides (which I added a note for).
I tried to do as you said but I have a problem to split on '\n'. I tried with both strtok and boost::char_separator<char> but they take into account just one character. So it splits on \ or on 'n'. Any solutions?
That shouldn't happen, since \n is only one character (this is just the escape sequence representing a non-printable character). Are you sure you got that character sequence right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.