Skip to content

Conversation

@jirikanda
Copy link

resize.min.cs contains byte order mark.
Unfortunately, the content of the file is used in the js bundle and the byte order mark appears in the "middle" of the bundle.
This causes problems on some firewalls/gateways which do not expect this symbol here.

image

The commit/pull request only removes the byte order mark from the beginning of the resize.min.cs. Thats why the diff of the commit does not look "understandable".

What is byte order mark?
see https://en.wikipedia.org/wiki/Byte_order_mark

@LarsBlaabjerg
Copy link

Would be nice if this was merged. It is causing problems with Azure.

@jirikanda
Copy link
Author

@LarsBlaabjerg Yes, we also found this issue with Azure. It was solved somehow by the customer's Azure network administrator, but I have no information, how.

As a developer, I found the following awful workaround - use a middleware to remove the BOM.
We did NOT use this at production finally, use it at your own risk.

public class HangfireConsoleJsByteOrderMaskRemovingMiddleware
{
	private RequestDelegate _next;

	public HangfireConsoleJsByteOrderMaskRemovingMiddleware(RequestDelegate next)
	{
		_next = next;
	}

	/// <summary>
	/// The middleware Invoke method.
	/// </summary>
	/// <param name="httpContext">The current <see cref="HttpContext"/>.</param>
	/// <returns>A Task to support async calls.</returns>
	public async Task Invoke(HttpContext httpContext)
	{
		byte[] byteOrderMask = System.Text.UTF8Encoding.UTF8.GetPreamble();
		Stream originalBodyStream = httpContext.Response.Body;
		try
		{
			var memoryStream = new MemoryStream();
			httpContext.Response.Body = memoryStream;

			await _next(httpContext).ConfigureAwait(false);

			memoryStream.Position = 0;
			var content = memoryStream.ToArray();

			int indexOfBom = content.AsSpan().IndexOf(byteOrderMask);
			Contract.Assert(indexOfBom >= 0);

			await originalBodyStream.WriteAsync(content, 0, indexOfBom);
			await originalBodyStream.WriteAsync(content, indexOfBom + byteOrderMask.Length, content.Length - indexOfBom - byteOrderMask.Length);
		}
		finally
		{
			httpContext.Response.Body = originalBodyStream;
		}
	}
}

and register the middleware:

app.UseWhen(context => context.Request.Path.Value.StartsWith("/hangfire/js"), appBuilder =>
{
        appBuilder.UseMiddleware<HangfireConsoleJsByteOrderMaskRemovingMiddleware>();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants