I'm wondering how I can tell if all the data has been received from a socket. It's a simple web proxy and right now I'm handling the request part, so what's sent should terminate with '\r\n\r\n' I have no idea how long the request will be. I've read some posts on here that say I should check for 0 being returned from the read function? But others that say 0 is only returned when the client closes the connection? Otherwise I can check the last characters of the buffer and see if they match the above?
The plan is just to load the read data into a buffer, save that data, if there is more data repeat.
Thanks
HTTP is not a trivial protocol, taking several RFCs. Just matching double linefeed/new line won't do. At the very minimum you will have to parse the request headers to figure out what encoding is there, and then, optionally, work through the request body.
Look into libcurl or any of the available open-source web-servers to appreciate the complexity.
I think the plan you have is almost right. You need to keep loading the read data into a buffer until you find the desired pattern '\r\n\r\n'
, then you know you have the entire request and can pass it off to your processing logic, remove the request from the buffer and repeat.
It depends if you are on a Blocking or Non-Blocking socket.
- If you are on a non-blocking socket, it can return 0 and set errno to EAGAIN. It just means you have to wait before trying again to read
- If you are on a blocking socket, 0 will indicate end of File