I understand that HTTP chunk is something like this
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
7\r\n
Mozilla\r\n
9\r\n
Developer\r\n
7\r\n
Network\r\n
0\r\n
\r\n
When server send response data to client, should the server send the data chunk by chunk?
For example,
server ==> client, first send headers, then first chunk:
7\r\n
Mozilla\r\n
then second chunk, and so on.
Can server send the data like follows:
first write
7\r\n
Mozill
second write
a\r\n
9\r\n
Developer\r\n
7\r\n
and so on?
TCP is a protocol to transfer a byte stream. There are no boundaries relevant at the TCP level between data send by different write
. In fact, if you do multiple write
or send
shortly after each other the data from these calls often end up together in the same network packet (Nagle algorithm) and they are also read together at the receivers end.
This means that it should not matter if you write partial chunks like 3\r\n
followed by 123\r\n
instead of a full chunk 3\r\n123\r\n
. You can even write multiple chunks or 1.5 chunks etc within a single write
or send
.
But note also that there are always clients where the developers did not properly understand how sockets and TCP work and assume that what you send
is what you recv
or assume that they will always get a full chunk with a single recv
or have similar wrong assumptions. While you might try to be nice against such broken applications you might also deliberately try to break these so that they get fixed early instead of showing random and unreproducible problems later.