Zig 0.17.0

Forms and uploads

On this page:

  • spider.multipart: multipart/form-data parsing: the fields and the uploaded files of a form. Handlers use c.parseMultipart.
  • spider.MultipartData: The parsed parts of a multipart body: what c.parseMultipart returns.
  • spider.UploadedFile: One uploaded file of a multipart body: its name, content type and bytes.

spider.multipart

multipart/form-data parsing: the text fields and the uploaded files of a form. Handlers call c.parseMultipart(); parse and extractBoundary are for a body that did not come through a request.

spider.multipart.UploadedFile

zig
pub const UploadedFile = struct

One uploaded file of a multipart body.

zig
const mp = try c.parseMultipart();
const files = mp.getFile("avatar") orelse return error.BadRequest;
const first = files[0];

Fields

  • filename: []const u8: The file name the client sent, as it sent it. It is not checked: never use it as a path.
  • content_type: []const u8: The Content-Type of the part, as the client sent it; application/octet-stream when the part had none.
  • data: []const u8: The bytes of the file. Not copied: a slice of the body given to parse, valid as long as that body is.
  • size: usize: Length of data in bytes.

spider.multipart.MultipartData

zig
pub const MultipartData = struct

The parsed parts of a multipart body: what c.parseMultipart() and parse return. Read it with getValue and getFile.

Fields

  • value: std.StringHashMap([]const u8): The text fields by name. When a name comes more than once, the last one stays.
  • file: std.StringHashMapUnmanaged(std.ArrayList(UploadedFile)): The uploaded files by field name; one field can carry several.
  • allocator: std.mem.Allocator: The allocator given to parse; deinit frees with it.

multipart.MultipartData.getValue

zig
pub fn getValue(self: *const MultipartData, name: []const u8) ?[]const u8

The text field name, or null when the form has none.

multipart.MultipartData.getFile

zig
pub fn getFile(self: *const MultipartData, name: []const u8) ?[]UploadedFile

The files uploaded in the field name, in the order they came, or null when the field has no file part.

multipart.MultipartData.deinit

zig
pub fn deinit(self: *MultipartData) void

Frees the names, the field values and the file metadata. Not needed when the data came from c.parseMultipart(): the request arena frees it.

spider.multipart.extractBoundary

zig
pub fn extractBoundary(content_type: []const u8) ?[]const u8

The boundary of a Content-Type: multipart/form-data; boundary=... header value, without its quotes. Null when the type is not multipart/form-data (in lower case), when there is no boundary=, when nothing or more than 70 bytes follow it, or when an opening quote is never closed. A slice of content_type.

spider.multipart.parse

zig
pub fn parse(allocator: std.mem.Allocator, body: []const u8, boundary: []const u8) !MultipartData

Parses a multipart body whose parts are separated by boundary (as extractBoundary gives it). Names and field values are copied with allocator; file contents are not (see UploadedFile.data), so body must outlive the result. Free with deinit.

Fails with error.InvalidMultipartEncoding when the body does not follow the format, error.MissingFieldName for a part without a name, and error.BoundaryTooLong for a boundary over 254 bytes.

zig
var mp = try spider.multipart.parse(allocator, body, boundary);
defer mp.deinit();
const info = mp.getValue("info");

spider.MultipartData

The same as spider.multipart.MultipartData.

spider.UploadedFile

The same as spider.multipart.UploadedFile.