Binary data corrupted when hosting ServiceStack in

2019-01-26 19:35发布

问题:

I have a ServiceStack service with a method to handle a GET request. This method returns binary data.

public object Get(DownloadFile request) {
  return new HttpResult(new FileInfo("some file"), "application/octet-stream", asAttachment: true);
}

When the host is Windows it works fine but when I'm running it in Linux with Mono+FastCGI the data I download is not the same.

I analyzed the returned bytes for a few files and concluded that there is a pattern. The data is getting wrapped in this way:

original data size + \r\n + original data + \r\n\r\n0\r\n\r\n

Why is this happening and how to fix it?

Edit:

Turns out this is due to chunked transfers which are part of HTTP 1.1.

Knocte's answer pointed me in the right direction and I was able to work around the problem by forcing my request to use HTTP 1.0:

var req = (HttpWebRequest)WebRequest.Create(url);
req.ProtocolVersion = new Version("1.0");

I didn't need to try the patch suggested by knocte but it looks like it's the proper way to fix the problem instead of avoiding it like I did.

回答1:

I think you're being affected by this bug.

If the patch that is attached to it works for you, then you could clean it up, and propose it as a pull request to mono in github.