Question

Is there an easy way to parse a string into k,v pairs?

GET / HTTP/1.1
Host: localhost:1337
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: JSESSIONID.e5614f49=1ns34hepyv53k1hg1o78k3z4zy; screenResolution=1920x1080
Connection: keep-alive

Pas de solution correcte

Autres conseils

"Easy" depends on how robust and secure you want your code to be.

You can use strchr() in a loop, to repeatedly find the spaces and newlines, and memcpy() to copy those parts of the string into the structure where you want them.

Or you can use strtok() to butcher the string in-place, while collecting pointers into it.

Or you can get more elaborate. Here's a link to the code in Apache HTTPd to do exactly this task: http://svn.apache.org/repos/asf/httpd/httpd/trunk/server/apreq_parser_header.c

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top