-->

Apache benchmark multipart/form-data

2019-02-11 06:56发布

问题:

i'm facing a strange problem with apache benchmark post file.

I need to stress a feature that handles file upload. So, I googled about, and found a post describing how to build a post file properly. Its contents looks like:

--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--

The ab line is this:

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

When ab make the requests the generated header is the following:

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

The expected return is a raise params.inspect, that let me see if the data is arriving to the other side. If I remove the boundary I can see data received in params, but this is not what I want. I want to get a file upload.

Anyone has a tip? I will really appreciate it.

回答1:

I found the answer, and decided to share with you.

After some struggle with the post file, I decided to create a php script just to output the $_FILES and $_REQUEST variables to see if the file was built correctly. Indeed, apache receive the file perfectly and I could see the file data, and other request params.

With Rails the same doesn't occur, and after reading the documentation of HTTP 1.1 multipart topic, I realize that the problem was related to the post file format.

When you built this kind of file, you need to build it in details, and this means include all the special characters like \r and \n in the right place.

So the correct post file looks like this (the \r\n is for illustration, but should be there, you know?):

--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

When you open this file you see this actually:

--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--

I hope that helps you.

Cheers.