IHttpHandler over Https on IE

280 views Asked by At

I'm looking for your expertise/help figuring out why my http handler works perfectly over http and doesn't over https (only IE problem, other browsers work fine either way). I'm using the handler to display images and when https is used on IE, it shows some images and fails to show others images.

again, the problem is only happening on https and IE, all other mutations with other browsers work just fine. below is my code:

public void ProcessRequest(HttpContext context)
    {
        Stream iStream = null;
        try
        {
            if (!context.Response.IsClientConnected || HttpContext.Current == null || HttpContext.Current.Session["GoodUser"] == null)
                return;

            var url = context.Request.QueryString[@"ID"];
            if (string.IsNullOrWhiteSpace(url))
                return;
            var fileUrl = EncryptionManager.DecryptUrl(url);
            if (!File.Exists(fileUrl))
                return;

            var buffer = new byte[4096];

            // Open the file.
            iStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read, FileShare.Read);

            // Total bytes to read:
            var dataToRead = iStream.Length;

            context.Response.AddHeader(@"Accept-Ranges", @"bytes");
            context.Response.AddHeader(@"Content-Length", dataToRead.ToString());
            context.Response.StatusCode= (int)HttpStatusCode.OK;

            var file = new FileInfo(fileUrl);
            context.Response.ContentType = MimeTypeMap.GetMimeType(file.Extension);
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetNoStore();
            context.Response.Cache.SetExpires(DateTime.MinValue);

            if (!string.IsNullOrEmpty(context.Request.Headers[@"Range"]))
            {
                var range = context.Request.Headers[@"Range"].Split('=', '-');
                var startbyte = int.Parse(range[1]);
                iStream.Seek(startbyte, SeekOrigin.Begin);
                context.Response.StatusCode = 206;
                context.Response.AddHeader(@"Content-Range", $@" bytes {startbyte}-{dataToRead - 1}/{dataToRead}");
            }

            while (dataToRead > 0)
            {
                iStream.Read(buffer, 0, buffer.Length);
                if (context.Response.IsClientConnected)
                {
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    context.Response.Flush();
                    buffer = new byte[buffer.Length];
                    dataToRead = dataToRead - buffer.Length;
                }
                else
                {
                    dataToRead = -1;
                }
            }
        }
        catch (Exception exception)
        {

        }
        finally
        {
            iStream?.Close();
            context.ApplicationInstance.CompleteRequest();
        }
    }
0

There are 0 answers