Apache基准测试multipart / form-data

我正面临一个apache基准post文件的奇怪问题。

我需要强调一个处理文件上传的function。 所以,我搜索了一下,发现了一篇描述如何正确构建post文件的post。 其内容如下:

--1234567 Content-Disposition: form-data; name="user_id" 3 --1234567 Content-Disposition: form-data; name="file"; filename="cf_login.png" Content-Type: image/png [base64 encoded file content] --1234567-- 

ab线是这样的:

 $ ab -c 1 -n 5 -v 4 -T 'multipart/form-data; boundary=1234567' -p post_file.txt http://localhost:3000/files 

当ab发出请求时,生成的标头如下:

 INFO: POST header == --- POST /files.json/ HTTP/1.0 Content-length: 229 Content-type: multipart/form-data; boundary=simpleboundary Host: localhost:3000 User-Agent: ApacheBench/2.3 Accept: */* --- LOG: header received: HTTP/1.1 500 Internal Server Error Content-Type: text/html; charset=utf-8 Content-Length: 13265 X-Request-Id: 9ad57d19cd71886a1bb817d00fac651b X-Runtime: 0.015504 Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) Date: Tue, 25 Sep 2012 13:54:29 GMT Connection: close 

预期的回报是提高params.inspect,让我看看数据是否到达另一方。 如果我删除边界,我可以看到在params中收到的数据,但这不是我想要的。 我想要上传文件。

有人有小费吗? 我真的很感激。

我找到了答案,并决定与你分享。

在使用post文件进行了一些努力之后,我决定创建一个php脚本来输出$ _FILES和$ _REQUEST变量来查看文件是否构建正确。 实际上,apache完全接收文件,我可以看到文件数据和其他请求参数。

使用Rails也不会发生同样的事情,在阅读HTTP 1.1 multipart主题的文档后,我意识到问题与post文件格式有关。

构建此类文件时,需要详细构建它,这意味着在正确的位置包含所有特殊字符,如\ r和\ n。

所以正确的post文件看起来像这样(\ r \ n用于说明,但应该在那里,你知道吗?):

 --boundary_hash\r\n Content-Disposition: form-data; name="your_form_field";\r\n Content-Type: text/plain\r\n \r\n your form field data\r\n --boundary_hash\r\n Content-Disposition: form-data; name="another_field";\r\n Content-Type: text/plain\r\n \r\n another field data\r\n --boundary_hash\r\n Content-Disposition: form-data; name="filename"; filename="cf_login.png"\r\n Content-Type: image/png\r\n \r\n base64 file content\r\n --boundary_hash--\r\n 

当你打开这个文件时,你会看到这个:

 --boundary_hash Content-Disposition: form-data; name="your_form_field"; Content-Type: text/plain your form field data --boundary_hash Content-Disposition: form-data; name="another_field"; Content-Type: text/plain another field data --boundary_hash Content-Disposition: form-data; name="filename"; filename="cf_login.png" Content-Type: image/png base64 file content --boundary_hash-- 

我希望能帮助你。

干杯。