IIS7拒绝分块编码的文件上传

时间:2011-11-21 11:01:28

标签: php iis iis-7 chunked-encoding

我有一个Windows / Apache2 / PHP应用程序,使用分块编码接收文件。原因是上传的文件是动态的,并且在传输之前其长度未知。这一直都很好用。

现在我需要将应用程序移植到IIS7 / PHP。 问题是IIS无法接收分块文件:上传文件时,服务器根本没有响应。我该如何解决这个问题?

请注意,在我的测试中,我甚至不使用PHP。我只是有一个.php扩展名,因为IIS拒绝.htm文件上的POST(这是有意义的)。

As suggested by rupello in this answer,我用cURL进行了测试,以确保我的客户端没有损坏。 cURL也没有得到答案,尽管如果转移没有分块,一切正常。

我做了以下测试:

test.php的:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
  </head>
  <body>
    <form method="post" enctype="multipart/form-data">
      File: <input type="file" name="upfile" />
      <input type="submit" value="go"/>
  </form>
  </body>
</html>

此命令不会返回(等待回答等等)

curl.exe http://serveur/test.php --form "upfile=@text.txt" 
   -H "Transfer-Encoding: chunked" -H "Expect:"

注意:-H "Expect:"是要取消curl发出的Expect 100-Continue。没有这个标题的结果是相同的,execpt当然是额外的往返。 发送:

POST http://serveur/test.php HTTP/1.1
User-Agent: curl/7.15.3 (i586-pc-mingw32msvc) libcurl/7.15.3 zlib/1.2.2
Host: serveur
Pragma: no-cache
Accept: */*
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: multipart/form-data; boundary=----------------------------310dbcc6761b

8c
------------------------------310dbcc6761b
Content-Disposition: form-data; name="upfile"; filename="text.txt"
Content-Type: text/plain


5
hello
30

------------------------------310dbcc6761b--

0

问题:服务器没有返回任何内容。服务器看起来像是在等待。卷曲不会回来。

没有块编码的相同命令按预期工作:

发送:

POST http://serveur/test.php HTTP/1.1
User-Agent: curl/7.15.3 (i586-pc-mingw32msvc) libcurl/7.15.3 zlib/1.2.2
Host: serveur
Pragma: no-cache
Accept: */*
Connection: Keep-Alive
Content-Length: 193
Content-Type: multipart/form-data; boundary=----------------------------e2d761bc173a

------------------------------e2d761bc173a
Content-Disposition: form-data; name="upfile"; filename="text.txt"
Content-Type: text/plain

hello
------------------------------e2d761bc173a--

服务器现在正确回复:

HTTP/1.1 200 OK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.8
X-Powered-By: ASP.NET
Date: Mon, 21 Nov 2011 10:47:57 GMT
Content-Length: 272

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
</head>
<body>
  <form method="post" enctype="multipart/form-data">
    File: <input type="file" name="upfile" />
    <input type="submit" value="go"/>
  </form>
</body>
</html>

对具有相同文件和请求的普通LAMP服务器的测试工作正常。

那么如何在IIS上启用请求块编码?

注意:我确实尝试过相关的ASP参数,但无济于事:

C:\...\inetsrv>appcmd.exe set config /section:asp /enableChunkedEncoding:True
Applied configuration changes to section "system.webServer/asp" for "MACHINE/
WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST"

2 个答案:

答案 0 :(得分:3)

IIS 7(至少IIS 7.5)确实支持chuncked文件上传。 当块长度错误时,IIS返回Http错误400:请求中的内容长度或块长度无效。 (例如,在添加

时替换
5 
hello 

5
h ello 

问题在于将CGI调用从IIS传输到PHP。 IIS似乎不是PHP(快速)CGI无法处理分块文件上传的唯一环境。 见PHP-Bugs ID 60826

答案 1 :(得分:0)

根据HTTP 1.1 spec,分块编码被定义为服务器编码 - 即服务器以这种编码方式发送响应,而不是相反(Wiki页面也说相同)。没有提及Server 接受分块编码请求,因此它未在IIS中实现。

话虽如此,似乎APACHE在HTTP规范之外实现了这一点。