I am a little confused about Accept-Encoding. I have Web Service which would accept compressed file submission using POST method. I have added code below which look for Accept-Encoding: gzip and decompress submitted file. The problem is with a files which are submitted from internet browsers. Request headers contain gzip but file is not actually compressed. Maybe someone could help to understand how to detect is file actually compressed?
var httpPostedFile = httpRequest.Files[0];
var contentLength = httpPostedFile.ContentLength;
var buffer = new byte[contentLength];
httpPostedFile.InputStream.Read(buffer, 0, contentLength);
const string acceptEncoding = "Accept-Encoding";
if (httpRequest.Headers[acceptEncoding] != null && httpRequest.Headers[acceptEncoding].Contains("gzip"))
{
buffer = Decompress(buffer);
}
static byte[] Decompress(byte[] gzip)
{
using (var stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
var buffer = new byte[size];
using (var memory = new MemoryStream())
{
int count;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
Accept-Encoding indicates what encodings the client accepts.
The encoding of a given request is specified using Transfer-Encoding and Content-Encoding, but note that these will always apply to the whole request body, not just part of a multipart payload.