module Amber::Router::Parsers::Multipart

Defined in:

amber/router/parsers/multipart.cr

Class Method Summary

Class Method Detail

def self.each_part(request : HTTP::Request, limits : Amber::Configuration::ParamsConfig, & : HTTP::FormData::Part -> ) : Nil #

Yields each part of a multipart request. The body is read through a BoundedIO capped at max_multipart_body_bytes, each part's boundary and headers are capped at max_part_header_bytes, and any epilogue after the closing boundary is drained within the body cap. The block must consume each part's body.


[View source]
def self.parse(request : HTTP::Request, budget : ParamBudget = request.param_budget) : Tuple(Types::Params, Types::Files, ::File | Nil) #

Parses a multipart body, charging budget (the request's own budget by default). The whole body is bounded by max_multipart_body_bytes, each part's headers by max_part_header_bytes, and each file by max_file_bytes while they stream; every part counts toward max_params, and field values are bounded by max_value_bytes. If any limit trips, every tempfile already written is removed. A later file part with the same name replaces the earlier one, whose tempfile is removed at once.

Amber parses the body before the action runs (the CSRF pipe reads _csrf from params), which drains it. So that the action can still read request.body, a body whose declared Content-Length is within max_multipart_restore_bytes (and max_multipart_body_bytes) is first copied to a tempfile in the private upload directory, parsed from there, and rewound into request.body. A larger body, or one without Content-Length, is parsed as it streams and stays drained, so a large upload is never copied twice.

Returns the fields, the files, and that copy (nil when the body was not copied). The caller deletes the copy when the request finishes, as Amber::Router::Params#delete_uploaded_files does.


[View source]