OMG! Squeee! Parsing HTTP responses in C!
And ok, it's a bit half assed at the moment, all I'm doing is splitting on the colon, but to get there I've copied/adapted the code from K&R for streamed input (FILE*
stuff) to wrap the low level tcp_read
, which gives me (the equivalent to) buffered getch
, so I can now build up to a proper parser, if that's what I want.
I'm still thinking about it. I know I want the output to be an ObjInstance*
, with fields for status, headers (a nested instance) and body (which is probably going to be an ObjString*
, except I want to to be file backed if it's bigger than a couple of meg, or if it's binary).
TODO
- Symbolic access to object fields (e.g.,
o["name"]
). Easy, just need to adaptOP_GET/SET_INDEX
- "Proper" header parsing. I should be ignoring the ignorable whitespace, and joining joinable header values
- Maybe flatten header names to lower case and strip non-alphanumerics.
- Read-to-end on errors, but I'm not sure about that - if the server sends me a bad response I should just go ahead and close the connection, I don't want to waste time trying to recover (and I'm fairly sure that's what the standard says)
- Do something sensible with bodies.
But ignoring those, squeee!