1

I have a server that sends chunked transfer encoding data . I use InternetReadFile to read the data but InternetReadFile fails with 12004 after reading the first chunk. I used a fiddler ( Wireshark ) to intercept the received data . Wireshark displays the second chunk but InternetReadFile API fails .

Sample Code :

    CString totalbuffer ;

    While ( 1 )
    {
      char recv [ 10 ] = '\0' ;
      DWORD dwBytesRead = 0 ;
      if ( InternetReadFile ( httpSocket.hReq , recv, 10 , &dwBytesRead ) )
      {
         recv[ dwBytesRead] = '\0' ;
         totalbuffer += recv ;
         printf ( " received buffer : %s" , recv ) ;
         return 1 ;
      }
      else
      {
         printf ( " InternetReadFile failed with : %d" , GetLastError ( ) ) ;
         return 0 ;
      }
    };  

WireShark displays :

HTTP/1.1 200 OK

Transfer-Encoding: chunked

Date: Thu, 18 Sep 2014 14:16:16 GMT

Server: CHND

3

Ok\n

3

CMD

The client reads only the first 3 bytes chunk . When i try to read the next 3 bytes chunk "CMD" it isn't working.

Kindly help with what necessary changes should the client end do ? or should the server end needs to handle something extra ?

  • Related: You know that first 10 bytes you're reading, if fully populating that buffer, the line immediately after will invoke *undefined behavior* by exceeding your array size by one element. (i.e. `dwBytesRead == 10` is bad for `recv[dwBytesRead] = '\0';` when the array is only 10-elements wide (and thus indexable only from 0..9). – WhozCraig Sep 19 '14 at 08:46

1 Answers1

2

I had a similar issue, you need to "query" the connection for any extra info using InternetQueryDataAvailable():

  while(TRUE)
  {
    res = InternetQueryDataAvailable(hr, &len, 0, 0);
    if (!res || len == 0) break;

    res = InternetReadFile(hr, recv, len, &dwbread);
    if(!res || (res && dwbread == 0)) break;
    totalbuffer += recv ;
    ...
  }

Using it, it will see whether there's more data coming from this HTTP request, and that seems to take care of the chunked encoding, since the number of bytes (the "chunk" length) won't show up in the recv buffer, so we can't use it in a raw form.

Note: my program is in C, not C++, but you'll get the drift.

ndrix
  • 1,191
  • 12
  • 18