0

I receive request to receive file content from WebApi.

The request can also contain range header and so for that I use ByteRangeStreamContent to generate the content and then finally send this content as HttpResponse.

This is the code that I use

var contentType = new MediaTypeHeaderValue("application/octet-stream");
var bytes = fileProvider.GetFile(fileId);
var stream = new MemoryStream(bytes);

if (Request.Headers.Range != null)
{
     var content = new ByteRangeStreamContent(stream, Request.Headers.Range, contentType);
     return ByteArrayHttpResponseMessage(content);
}
else
{
     var content = new ByteArrayContent(stream.ToArray());
     return ByteArrayHttpResponseMessage(content);
}

I have to add CRC32 check with the data now.

This I can do easily when the range header is not sent, because then I have access the byte array which I can use to calculate and add CRC32.

The problem arises when the range variable is sent, because then I don't have access to the actual data in byte array form that is being sent, I only have ByteRangeStreamContent and so I cannot update it with CRC32 before sending.

I checked that it does have the method ReadAsByteArrayAsync(), but I don't know if it is the right way to extract array from ByteRangeStreamContent then add CRC32 to the array then again try to generate updated ByteRangeStreamContent object.

Is there any way to do this?

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • If you have a stream add the CRC to the end of the stream. Use a memorystream. You can fill the stream with bytes and then after the stream is filled move the position to position zero and read stream to calculate the CRC. – jdweng May 25 '20 at 12:32
  • @jdweng - Please read the question, I have explain that I have the byte array but after reading it to `ByteRangeStreamContent` I no more have bytes array of that range where I can add CRC. That is the main problem – Pawan Nogariya May 25 '20 at 13:39
  • The either resize the array or use a list where you can add the additional CRC bytes. Or if you have a stream simply write the CRC bytes to end of stream. – jdweng May 25 '20 at 14:03
  • @jdweng - Please read the question – Pawan Nogariya May 26 '20 at 05:48
  • I've read the question over 10 times. If you have a memory stream you can move position to zero and then perform CRC over entire stream, and then add 4 more bytes to the stream. – jdweng May 26 '20 at 05:58
  • Got it, but when we have it inside `ByteRangeStreamContent` the stream will not be same as the original stream, right? So for e.g. if I have stream from 1-100 and I have passed range from 10-50, the final data for me would be 10-50, right? So I have to calculate CRC32 of 10-50 and not the entire stream 1-100, so to add CRC32 to 10-50, I must have access to this small stream or bytes but once it is passed to `ByteRangeStreamContent`, I no more have access to this small ranged stream. Is my understanding right or I am understanding it wrongly? – Pawan Nogariya May 26 '20 at 06:13
  • @jdweng - Added more details of the code – Pawan Nogariya May 26 '20 at 06:15
  • I do not know the exact requirements of which bytes (or entire bytes) get the CRC and do not really care. A memorystream you can read any position of the stream to perform the CRC. You can also add 4 additional bytes (CRC) to end of the stream. Your request has a Header and a body. I believe the CRC is usually put on the Body and not the headers. The content is usually the body (can be on headers). As I said I do not know the requirements. Just that a memory stream is bytes and you can read any range of bytes to perform the CRC. – jdweng May 26 '20 at 06:42

0 Answers0