karlseguin / http.zig

An HTTP/1.1 server for zig
MIT License
533 stars 41 forks source link

Form data does not seem to be parsed #34

Closed dephiros closed 8 months ago

dephiros commented 8 months ago

Hi thank you for your lib

Problem

Given this request

header:

Content-Type: multipart/form-data; boundary=---------------------------32849253763995902138980917115

body:

-----------------------------32849253763995902138980917115
Content-Disposition: form-data; name="use_cache"

on
-----------------------------32849253763995902138980917115--

When I follow the instruction in the doc and do:

_ = req.formData();
for (req.fd.keys[0..req.fd.len], 0..) |name, i| {
    const value = req.fd.values[i];
}

I got key:

-----------------------------6304654792511364587908115050
Content-Disposition: form-data; name

and value:

'"use_cache"

on
-----------------------------8849801338094729121585344871--

Maybe the parser does not support form/data boundary?

dephiros commented 8 months ago

Ah i see that the doc intend this method for x-www-form-urlencoded Does the lib support Content-Type: multipart/form-data;?

karlseguin commented 8 months ago

It doesn't. It's something I can look into adding though.

karlseguin commented 8 months ago

The multiform_part branch has a new method: req.multiFormData(). It's a different method then req.formData() because the "value" is a struct, not just a []const u8

const fd = try req.multiFormData();
const name = fd.get("name").?.value;

Currently the struct only exposes a .value: []const u8, but I'll be adding filename: ?[]const u8 and maybe content_type: []const u8 (since apparently each field can have its own content type)

You need to set the request.max_multiform_count to the maximum number of fields you want to accept, it defaults to 0.

If you run into any issues parsing the data, please provide the full request or at least an HTML form to replicate it. Parsing this was messy than I had initially thought!.

dephiros commented 8 months ago

Thank you. Appreciate your work 🙏