pub struct Upload {
pub bytes: Vec<u8>,
pub content_type: String,
}http only.Expand description
A raw request body — an uploaded file, rather than a JSON:API document.
The counterpart of Download on the way in.
Use it as a resource’s Create input
when the body is the file:
impl Resource<App> for Doc {
type Create = Upload;
type Update = Upload;
// …
}
impl Create<Doc> for App {
async fn create(&self, input: Upload, ctx: &Context) -> Result<Doc, Self::Error> {
Ok(self.store(input.content_type, input.bytes))
}
}§Limits
The default cap is MAX_UPLOAD, not MAX_BODY: a file is expected to
be larger than a document. Both exist because an unbounded read is a
denial of service written in one line — a client that can make the server
buffer without limit does not need any other exploit.
Raise or lower it per resource with Upload::limited, and restrict what
may be sent with Upload::accepting.
Fields§
§bytes: Vec<u8>The bytes, as sent.
content_type: StringThe Content-Type the client declared.
Taken at the client’s word and never sniffed — but see
Upload::accepting, and note that
Download sends nosniff so a lie here
cannot become active content in your origin.
Implementations§
Source§impl Upload
impl Upload
Sourcepub async fn limited(
request: Request,
ctx: &Context,
limit: usize,
) -> Result<Self, Errors>
pub async fn limited( request: Request, ctx: &Context, limit: usize, ) -> Result<Self, Errors>
Read a request body as an upload, capped at limit bytes.
The building block behind the FromRequest impl; call it directly
from your own impl to use a different limit:
impl FromRequest<App> for Avatar {
async fn from_request(req: Request, _s: &App, ctx: &Context) -> Result<Self, Errors> {
let upload = Upload::limited(req, ctx, 256 * 1024).await?;
Ok(Avatar(upload))
}
}Sourcepub async fn accepting(
request: Request,
ctx: &Context,
limit: usize,
accepted: &[&str],
) -> Result<Self, Errors>
pub async fn accepting( request: Request, ctx: &Context, limit: usize, accepted: &[&str], ) -> Result<Self, Errors>
Read an upload, refusing a media type not in accepted.
The check a file endpoint should not be without: an avatar route that
accepts text/html is a stored-XSS vector waiting for someone to
serve it back inline. Compared against the type alone, so
image/png; charset=… still matches image/png.
Trait Implementations§
Source§impl<S: Sync> FromRequest<S> for Upload
Reads any request body as an upload, capped at MAX_UPLOAD.
impl<S: Sync> FromRequest<S> for Upload
Reads any request body as an upload, capped at MAX_UPLOAD.
Deliberately does not check the media type — an upload is not a JSON:API
document, so the JSON:API media-type check would reject every real file. Use
Upload::accepting from your own impl when the endpoint should only
take certain types.