4
I think a simple approach would be:
read
a chunk of the response into a char array of known size- iterate through the buffer, looking for the CRLF characters that signify the end of the header section
- copy the header into a dynamically-allocated char array
- if you didn't get to the CRLF characters that signify the end of the header section,
read
more data from the socket into the known-size char array - if it takes multiple
read
operations to get through the header section, then eachread
you can allocate a longer header string, copy your old data to it, and then append the next chunk - when you hit the CRLF, or if
read
indicates no more data is waiting in the socket (eg connection terminated early) then you can throw a null character at the end of your string and that's the headers
This is a somewhat naive implementation. Instead of reallocating and copying the header string array for each read
, a linked-list data structure could hold the chunks of buffered data. Then at the end you combine them into a string that's exactly the right size.