Body length of HTTP POST

I have a HTTP POST request that sends 16kB of data in the body, which is broken into segments. The first segment contains the HTTP head and 1kB of the data in the body.

I am using the driver code provided by WIZnet found at https://github.com/Wiznet/ioLibrary_Driver.

The problem I am facing is I cannot get the size of the data in the first segment (the 1kB).

The library breaks the POST request into a st_http_request structure and use Content-Length to determine the body size. My problem is my Content-Length is 16kB therefore I cannot use that number.

I intended to take the length returned from the recv command and subtract the size of the header (current pointer - receive buffer pointer). The receive buffer pointer seems to be the pointer passed to the httpServer_init() function. However, this does not work because the current pointer points to the st_http_request struct, which is no longer inside the receive buffer.

I’d very appreciate it if somebody could share how to do this.

I did not get the problem. You first get data until CRLFCRLF, then you parse headers and get content length, and then get ‘content-length’ bytes of data. Whatever content-length value is, you keep calling receive function until get all the data or timeout occurs.

Hi Eugeny,

You first get data until CRLFCRLF, then you parse headers and get content length, and then get ‘content-length’ bytes of data.

On the first recv() call in the http_run() routine, it read the first segment which includes the header and 1kB data body. I want to copy this 1kB data to some other buffers.

In other words, how do I know the length of the part of body that in the initial segment with the header? (the first 1kB that I mentioned. I know it is 1kB because I monitor it using Wireshark, not by the firmware. I want to get this number in the firmware)

The segments are as follow:
1st segment: header + 1kB of data
2nd segment: 1kB of data
… until everything is sent.

Do you need to? What you know for sure you will NOT (should) get more data than the data size in content-length.
And you know total this data size from content-length, and you know size of the header which ends with CRLFCRLF, and you know size of data you received from recv(), thus you can calculate how much exact data is in the first packet, and how much data will follow.

Do you need to?

Yes, because I want to use memcpy() the copy the data somewhere else, I have the pointer pointing to the data using strstr((char*)uri, “\r\n\r\n”), I need to know the size of this (1kB) to call memcpy(). The buffer holds the copy of the data is used to write to a flash IC to save the data.

How do you process a piece of data not knowing its size?

And you know total this data size from content-length, and you know size of the header which ends with CRLFCRLF, and you know size of data you received from recv(), thus you can calculate how much exact data is in the first packet, and how much data will follow.

That is exactly what I am trying to do. The problem is I cannot get the header size.
Do you use the provided httpServer by WIZnet or do you write or own code? As I tried to explain in the question, the library use strtok() to break down the request into three parts and save them in a st_http_request struct. Then the URI element of this struct is used to continue to process the http request. This loses track of the header length.

No I do not use library. And you must be able to find pointer to the start of data using strstr you mentioned, therefore you know the size of header. Then there must be a way to know how much data exactly you have got using recv function, and to know data size you subtract header size from the size received by recv.