MediaHandler should not append headers after headers have been written

Found in

EPiServer.CMS.Core 11.16.0

Fixed in

EPiServer.CMS.Core 11.18.0

Created

Jun 25, 2020

Updated

Aug 11, 2020

Area

CMS Core

State

Closed, Fixed and tested


Description

We should not try to append headers when headers have already been sent to client since that will only cause an exception "Cannot redirect after HTTP headers have been sent".

Steps to reprduce:
1. Upload two images like "image1.png" and "image2.png" to e.g. global assets
2. Add an initialization module like:
{{[InitializableModule]
public class RedirectMediaTest : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
context.Locate.Advanced.GetInstance<IContentRouteEvents>().RoutedContent += (o, e) =>
{
if (e.RoutingSegmentContext.RequestUrl.ToString().EndsWith("image1.png"))

{ HttpContext.Current.Response.BufferOutput = false; HttpContext.Current.Response.Redirect(e.RoutingSegmentContext.RequestUrl.ToString().Replace("image1.png", "image2.png"), false); }

};

}

public void Uninitialize(InitializationEngine context)
{

}
}}}
3. Make a request in browser for image1.png
4. look in the log4net log

Expected result: no errors
Actual result: An excption "Cannot redirect after HTTP headers have been sent" has been thrown