Repository navigation
v0.17.0
Changed
The public API is smaller: the 13 public modules become multipartkit and multipartkit/stream, and 71 public functions become 29. Code that used the old modules needs updating.
multipartkit/error,part,parser,encoder,form,query,validate,content_disposition,header,limitandinferare removed. Their types and functions move tomultipartkit, and the re-export aliases inmultipartkitare gone.parseandstream.parsetake the limits as a third argument.parse_with_limits,parse_streamandparse_stream_with_limitsare removed.stream.encodereplacesencode_stream.Limitsis a plain record anddefault_limitsis a constant: writeLimits(..multipartkit.default_limits, max_parts: 10).limit.new,new_limits,LimitConfigErrorand themax_*getters are removed. Values below 1 are no longer rejected.encodetakes the parts first:encode(parts, boundary).encode_formtakes aList(Part).- The
Formbuilder is removed.new_field(name:, value:)andnew_file(name:, filename:, content_type:, body:)returnResult(Part, MultipartError)and behave like the oldadd_field_strictandadd_file_strict. The lenientadd_fieldandadd_file, which removed CR, LF and NUL and renamed empty names to_unnamed_<n>, are removed, as areunsafe_add_partandFormError. An empty or whitespace-only name isEmptyFieldName(name), and CR, LF or NUL in a name, filename or content type isInvalidHeaderValue(header, value). part.new(headers:, name:, filename:, content_type:, body:)becomesnew_part(headers:, body:).name,filenameandcontent_typeare always read from the headers, as for a parsed part, so the old header synthesis from those arguments is removed, and an unparseableContent-Dispositionor an empty header name is rejected. Built and parsed parts compare equal with==, soequal_on_wireandlist_equal_on_wireare removed.headerreturnsResult(String, Nil)instead ofOption(String).part.headers(part, name)is removed, andall_headersis renamedheaders(also inmultipartkit/stream).fieldandfilereturnResultwith the behaviour of the oldrequired_fieldandrequired_file, which are removed.validate.has_fieldandvalidate.max_file_sizeare removed.allowed_content_typesmoves tomultipartkit.content_disposition.parsebecomesparse_content_dispositionand returns aContentDispositionrecord withdisposition,name,filenameandparametersfields instead of getters.- The
infermodule andadd_file_auto/add_file_auto_withare replaced byinfer_content_type(filename, body), which tries the extension, then the leading bytes, then returnsapplication/octet-stream. header.boundaryandheader.validate_boundary,stream.from_datastreamandstream.to_datastream, and theUnsupportedFeatureerror variant, which nothing returned, are removed.mimetype0.27.0 or later is required.
Before:
let form =
form.new()
|> form.add_field("title", "hello")
|> form.add_file("avatar", "cat.png", "image/png", png)
let #(content_type, body) = multipartkit.encode_form(form)
let assert Ok(parts) = multipartkit.parse(body, content_type)
let assert Ok(title) = query.required_field(parts, "title")
let assert Some(avatar) = query.file(parts, "avatar")
let filename = part.filename(avatar)After:
let assert Ok(title) = multipartkit.new_field(name: "title", value: "hello")
let assert Ok(avatar) =
multipartkit.new_file(
name: "avatar",
filename: "cat.png",
content_type: "image/png",
body: png,
)
let #(content_type, body) = multipartkit.encode_form([title, avatar])
let assert Ok(parts) =
multipartkit.parse(body, content_type, multipartkit.default_limits)
let assert Ok(title) = multipartkit.field(parts, "title")
let assert Ok(avatar) = multipartkit.file(parts, "avatar")
let filename = multipartkit.filename(avatar)Fixed
parseandstream.parsereturnInvalidQuotedPairfor aContent-Dispositionthat escapes a control character, as documented. Before, they returnedInvalidContentDisposition.- A
filename*orname*parameter that decodes to CR, LF or NUL is rejected withInvalidContentDisposition, as the quoted form already was. - A
"or\followed by a combining mark in a name or filename is escaped. Before, it went out unescaped and could end the quoted string early for a receiver that reads bytes. new_partrejects a header name that starts with a space or tab, whichparsewould read as a folded line.- A header block with no blank line after it fails with
HeaderTooLargeonce it is overmax_header_bytes. Before,stream.parsekept buffering up tomax_body_bytes. stream.parsecounts the epilogue of a body with no parts againstmax_body_bytes, asparsedoes.
Install
gleam add multipartkitSee HexDocs for the API
reference.