Forms and uploads
On this page:
- spider.multipart:
multipart/form-dataparsing: the fields and the uploaded files of a form. Handlers usec.parseMultipart. - spider.MultipartData: The parsed parts of a multipart body: what
c.parseMultipartreturns. - 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
pub const UploadedFile = struct
One uploaded file of a multipart body.
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: TheContent-Typeof the part, as the client sent it;application/octet-streamwhen the part had none.data: []const u8: The bytes of the file. Not copied: a slice of the body given toparse, valid as long as that body is.size: usize: Length ofdatain bytes.
spider.multipart.MultipartData
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 toparse;deinitfrees with it.
multipart.MultipartData.getValue
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
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
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
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
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.
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.