Which ContentEncoding to be used for WOFF and TTF with HttpListener response?

1.1k Views Asked by At

Chrome and Firefox don't render Font-Awesome's WOFF/TTF, even if they download them from HttpListener, that said, Safari on iOS is rendering Font-Awesome correctly. I am sending HTTP response using .NET's HttpListener class as follows:

private void Send(HttpListenerContext context, byte[] response, string contentType)
    {
        context.Response.StatusCode = (int)HttpStatusCode.OK;
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.ContentType = contentType;
        context.Response.ContentLength64 = response.Length;
        context.Response.AddHeader("Server", SERVER);

        if (response == null || response.Length == 0)
        {
            logger.Error("Send: Engine generated null or empty content");
            context.Response.StatusCode = (int)HttpStatusCode.NotFound;
            return;
        }

        using (var s = context.Response.OutputStream)
        {
            s.Write(response, 0, response.Length);
        }
    }

Is there another encoding type (other than UTF8) for WOFF? or is there something that I have to take care of in Chrome or Firefox?

Any help or pointer appreciated, thanks.

2

There are 2 best solutions below

2
On

In my own implementation, I leave out the ContentEncoding header all together when serving a font or binary.

This should work:

context.Response.ContentLength64 = response.Length;
context.Response.ContentType = "application/x-font-woff";
context.Response.OutputStream.Write(response, 0, response.Length);

context.Response.OutputStream.Close();
0
On

The problem was I had all the www content as Embedded Resource, and I created a method to convert those Embedded Resource Stream to a byte array by first converting that to a string using UTF-8 encoding. So this stackoverflow question helped me to pinpoint the root cause. Once I converted the Embedded Resource Stream directly to a byte array, Chrome started showing Font-Awesome fonts correctly!