Click here to Skip to main content
14,452,838 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi i develop a Http server,
my Summary of response code is this:

PHTTP_DATA_CHUND p = new HTTP_DATA_CHUND[count];
for (int i = 0; i<count; i++)
{
  p[i].DataChunkType = HttpDataChunkFromMemory;
  p[i].FromMemory.pBuffer = "dfdff"; 
  p[i].FromMemory.BufferLength = 5;
}

HTTP_RESPONSE response;  
ZeroMemory(&response,sizeof(HTTP_RESPONSE));  
PCSTR Reason="OK";  
response.StatusCode=200;  
response.pReason=Reason;  
response.ReasonLength=strlen(Reason);  
ADD_KNOWN_HEADER(response, HttpHeaderContentType, "text/html");
ADD_KNOWN_HEADER(response, HttpHeaderConnection, "keep-alive");
ADD_KNOWN_HEADER(response, HttpHeaderTransferEncoding, "chunked");
ADD_KNOWN_HEADER(response, HttpHeaderContentLength, chLen);

response.EntityChunkCoun = count;
response.pEntityChunks=p;
ULONG BytesSent;
ULONG result = HttpSendHttpResponse(ReqQueueHandle, HttpRequest->RequestId, 
                                            0, &response, NULL,&BytesSent, NULL, 
                                            0,NULL,NULL, NULL);


but the result is 87 !!!
now if i remove this line of code:
ADD_KNOWN_HEADER(response, HttpHeaderTransferEncoding, "chunked");

the result is 0 and my response is sent to client.
How i to make Chunked transfer encoding?
Posted
Comments
Jochen Arndt 16-Dec-13 3:46am
   
When using chunked transfers, the Content-Length header is not used. Have you tried sending the response without adding the Content-Length header?

What is the value of your count variable? It must be <= 100.
Zon-cpp 16-Dec-13 4:48am
   
thank you
Jochen Arndt 17-Dec-13 3:23am
   
Some general hints on using CodeProject:

Don't use solutions to add additional information or ask new questions. Instead, use the green 'Improve question' link to edit your question.

When you want to reply to a comment, use the Reply button on the right of the member name (like I have done with this comment). When doing so, the author of the first comment gets an email notification.

The same applies to solutions when using the 'Have a Question or Comment' link.
Rate this:
Please Sign up or sign in to vote.

Solution 3

My suggestion to remove the Content-Length header seems to solve the problem (see my above comment).

With chunked transfers, the Content-Length header is not used. It seems that the HttpSendHttpResponse function checks the headers and returns with an invalid parameter error when the transfer encoding is chunked and the content length is also present.

This behaviour complies with RFC 2616[^], section 4.4:
Quote:
If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present).


[UPDATE to answer the question why the client gets normal text rather the chunked data]:

Chunked is a transfer method. The additional information is used only for transferring data and does not belong to the original data. When a client calls some API function to get received HTTP data, this API function will decode chunked data, add a Content-Length header with the size of the decoded data and provides the decoded original data.
   
v3
Comments
CPallini 17-Dec-13 3:36am
   
Can't understand why it was downvoted. Have my 5.
Jochen Arndt 17-Dec-13 3:41am
   
Thank you Carlo.
I guess it was downvoted because I did not answer the additional questions posted as solutions.
Zon-cpp 17-Dec-13 4:06am
   
the client is a socket client and does not use API Function.
what does the client to decode a chunke data that it is received?
Jochen Arndt 17-Dec-13 4:38am
   
When using low level socket functions, you may receive chunked data. Check your received headers. If there is a Content-Length header, the chunked data has been decoded somewhere (e.g. by a proxy/gateway). I don't know how HttpSendHttpResponse() works, but it may be also done by that function (e.g. when the client requests HTTP 1.0 instead of HTTP 1.1). If you want to know where the decoding happens, you must sniff the network traffic at all possible points.
Rate this:
Please Sign up or sign in to vote.

Solution 6

i changed my code : this is ok to send response

response.EntityChunkCount = 0;
response.pEntityChunks=0;
ULONG BytesSent;
ULONG result = HttpSendHttpResponse(ReqQueueHandle, HttpRequest->RequestId,
                                            0, &response, NULL,&BytesSent, NULL,
                                            0,NULL,NULL, NULL);
for (/*buffers */)
{
PHTTP_DATA_CHUND chunk;
chunk.DataChunkType = HttpDataChunkFromMemory;
chunk.FromMemory.pBuffer = buffer[i]; //--- "bufferLen\r\n .... \r\n"
chunk.FromMemory.BufferLength = len[i];
HttpSendResponseEntityBody(ReqQueueHandle, HttpRequest->requestId, 
                                 HTT_SEND_RESPONSE_FLAG_MORE_DATA, 1, &chunk, 0
                                 NULL, 0, NULL, NULL);
}

PHTTP_DATA_CHUND chunkEnd;
chunkEnd.DataChunkType = HttpDataChunkFromMemory;
chunkEnd.FromMemory.pBuffer = "\r\n0\r\n";
chunkEnd.FromMemory.BufferLength = 5;
HttpSendResponseEntityBody(ReqQueueHandle, HttpRequest->requestId,
                                 HTT_SEND_RESPONSE_FLAG_DISCONNECT, 1, &chunkEnd, 0
                                 NULL, 0, NULL, NULL);
   
Comments
Zon-cpp 22-Dec-13 4:22am
   
"\r\n0\r\n" for final sending has problem for IE
for resolve it should change it:

PHTTP_DATA_CHUND chunkEnd;
chunkEnd.DataChunkType = HttpDataChunkFromMemory;
chunkEnd.FromMemory.pBuffer = "0\r\n";
chunkEnd.FromMemory.BufferLength = 3;
HttpSendResponseEntityBody(ReqQueueHandle, HttpRequest->requestId,
HTTP_SEND_RESPONSE_FLAG_MORE_DATA, 1, &chunkEnd, 0
NULL, 0, NULL, NULL);
HttpSendResponseEntityBody(ReqQueueHandle, HttpRequest->requestId,
HTTP_SEND_RESPONSE_FLAG_DISCONNECT, 0, NULL, 0
NULL, 0, NULL, NULL);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100