.net IIS Classic 应用程序池将字节注入我的图像
.net IIS Classic app pool injecting bytes into my image
我有一个旧的 asp.net Web 应用程序 运行 在 IIS 中以经典模式运行,它运行的代码类似于下面的重现案例。似乎每次调用 Write
方法时,我都会在写入数据之前将额外的几个字节(字节大小的十六进制字符串,后跟 CR 和 NL)注入到我的输出流中。
default.aspx.cs
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
public partial class _Default : Page
{
protected override void OnInit(EventArgs e)
{
string str1 = ResolveUrl("~/image.jpg");
string str2 = this.Server.MapPath(str1);
string str = HttpUtility.UrlEncode(Path.GetFileName(str2), Encoding.UTF8).Replace("+", "%20");
this.Response.ContentType = "image/jpeg";
this.Response.AddHeader("Content-Disposition", "filename=" + str);
using (Stream stream = new FileStream(str2, FileMode.Open, FileAccess.Read, FileShare.Read))
{
this.Response.Cache.SetExpires(DateTime.Now.AddYears(-1));
long lengthLeft = stream.Length;
byte[] buffer = new byte[8192];
while (lengthLeft > 0L && this.Response.IsClientConnected)
{
int count = stream.Read(buffer, 0, 8192);
this.Response.OutputStream.Write(buffer, 0, count);
this.Response.Flush();
lengthLeft -= (long)count;
}
}
base.OnInit(e);
}
}
default.aspx:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="None" />
<compilation debug="false" />
</system.web>
</configuration>
将任何 image.jpg
放在目录中,并使该目录成为 iis 中的虚拟应用程序。
到目前为止,这只发生在一台服务器上(此应用程序的 qa 环境),Server 2008 R2 Standard 所有 Windows 已应用更新补丁。
有谁知道为什么 Response.OutputStream.Write(buffer, 0, count)
会在缓冲区内容之前将 string.Format("{0:x}\r\n", count)
写入输出流?
编辑:分辨率:
在 IIS 经典模式下的 Server 2016 上不会发生此问题,而且该应用程序似乎在集成管道模式下也能正常运行,这也不会产生此问题。虽然我仍然想知道为什么会出现这种倒退,但由于我们有 2 个解决方法,而且它似乎不太可能影响我们的生产,所以这个问题不再紧迫。
每次写入后刷新缓冲区可能会插入可疑的 return 和换行符。您不需要在每次写入后刷新缓冲区,事实上,在许多情况下它是不受欢迎的。移除冲洗并重新测试。
我有一个旧的 asp.net Web 应用程序 运行 在 IIS 中以经典模式运行,它运行的代码类似于下面的重现案例。似乎每次调用 Write
方法时,我都会在写入数据之前将额外的几个字节(字节大小的十六进制字符串,后跟 CR 和 NL)注入到我的输出流中。
default.aspx.cs
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
public partial class _Default : Page
{
protected override void OnInit(EventArgs e)
{
string str1 = ResolveUrl("~/image.jpg");
string str2 = this.Server.MapPath(str1);
string str = HttpUtility.UrlEncode(Path.GetFileName(str2), Encoding.UTF8).Replace("+", "%20");
this.Response.ContentType = "image/jpeg";
this.Response.AddHeader("Content-Disposition", "filename=" + str);
using (Stream stream = new FileStream(str2, FileMode.Open, FileAccess.Read, FileShare.Read))
{
this.Response.Cache.SetExpires(DateTime.Now.AddYears(-1));
long lengthLeft = stream.Length;
byte[] buffer = new byte[8192];
while (lengthLeft > 0L && this.Response.IsClientConnected)
{
int count = stream.Read(buffer, 0, 8192);
this.Response.OutputStream.Write(buffer, 0, count);
this.Response.Flush();
lengthLeft -= (long)count;
}
}
base.OnInit(e);
}
}
default.aspx:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="None" />
<compilation debug="false" />
</system.web>
</configuration>
将任何 image.jpg
放在目录中,并使该目录成为 iis 中的虚拟应用程序。
到目前为止,这只发生在一台服务器上(此应用程序的 qa 环境),Server 2008 R2 Standard 所有 Windows 已应用更新补丁。
有谁知道为什么 Response.OutputStream.Write(buffer, 0, count)
会在缓冲区内容之前将 string.Format("{0:x}\r\n", count)
写入输出流?
编辑:分辨率:
在 IIS 经典模式下的 Server 2016 上不会发生此问题,而且该应用程序似乎在集成管道模式下也能正常运行,这也不会产生此问题。虽然我仍然想知道为什么会出现这种倒退,但由于我们有 2 个解决方法,而且它似乎不太可能影响我们的生产,所以这个问题不再紧迫。
每次写入后刷新缓冲区可能会插入可疑的 return 和换行符。您不需要在每次写入后刷新缓冲区,事实上,在许多情况下它是不受欢迎的。移除冲洗并重新测试。