Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating multipart boundary

Tags:

multipart

I'm writing a script that uploads a file to a cgi script that expects a multipart request, such as a form on a HTML page. The boundary is a unique token that annotates the file contents in the request body. Here's an example body:

--BOUNDARY Content-Disposition: form-data; name="paramname"; filename="foo.txt" Content-Type: text/plain  ... file contents here ... --BOUNDARY-- 

The boundary cannot be present in the file contents, for obvious reasons.

What should I do in order to create an unique boundary? Should I generate a random string, check to see if it is in the file contents, and if it is, generate a new, rinse and repeat, until I have a unique string? Or would a "pretty random token" (say, combination of timestamp, process id, etc) be enough?

like image 518
August Lilleaas Avatar asked Jan 15 '10 12:01

August Lilleaas


People also ask

How do you create a boundary in multipart form data?

The boundary is included to separate name/value pair in the multipart/form-data . The boundary parameter acts like a marker for each pair of name and value in the multipart/form-data. The boundary parameter is automatically added to the Content-Type in the http (Hyper Text Transfer Protocol) request header.

What is boundary in multipart request?

A boundary is just the 'key' to separate the multiple "parts" of a multipart payload. Normally something like '&' is enough to separate the variables but you need something more unique to separate the payloads within the payload.

How do you set boundaries in HTTP request?

The boundary is specified like this: Content-Type: multipart/form-data; boundary=AaB03x . So, without a proper content-type, you almost can't have true multipart/form-data.

What is WebKitFormBoundary?

Each item in a multipart message is separated by a boundary marker. Webkit based browsers put "WebKitFormBoundary" in the name of that boundary. The Network tab of developer tools do not show file data in a multipart message report: They can be too big.


1 Answers

If you use something random enough like a GUID there shouldn't be any need to hunt through the payload to check for an alias of the boundary. Something like:-

----=NextPart_3676416B-9AD6-440C-B3C8-FC66DDC7DB45
Header:....

Payload
----=NextPart_3676416B-9AD6-440C-B3C8-FC66DDC7DB45--

like image 56
AnthonyWJones Avatar answered Oct 01 '22 19:10

AnthonyWJones