pub struct UploadSession { /* private fields */ }Available on crate feature
fs only.Expand description
The push-style form for event-driven servers: hand each body chunk to
push as reads complete, then finish.
Dropping a session without finish deletes every file it stored.
captured_token is available as soon as the
token field ends, so the token can be verified while files are still
arriving.
§Examples
use form_net::{Limits, NoInspector, Policy, Store, UploadSession};
let store = Store::temp()?;
let body = b"--X\r\nContent-Disposition: form-data; name=a\r\n\r\n1\r\n--X--";
let mut session = UploadSession::new(b"multipart/form-data; boundary=X", &Limits::DEFAULT, &Policy::DEFAULT, &store)?;
for chunk in body.chunks(5) {
session.push(chunk, &mut NoInspector)?;
}
let upload = session.finish(&mut NoInspector)?;
assert_eq!(upload.field("a"), Some("1"));Implementations§
Source§impl UploadSession
impl UploadSession
Sourcepub fn new(
content_type: &[u8],
limits: &Limits,
policy: &Policy,
store: &Store,
) -> Result<Self, FormError>
pub fn new( content_type: &[u8], limits: &Limits, policy: &Policy, store: &Store, ) -> Result<Self, FormError>
A session for one request body.
§Errors
UnsupportedMediaType and the boundary errors of
FormKind::detect; InvalidLimits.
§Examples
use form_net::{Limits, Policy, Store, UploadSession};
let (store, policy) = (Store::memory_only(), Policy::DEFAULT);
let session = UploadSession::new(b"multipart/form-data; boundary=X", &Limits::DEFAULT, &policy, &store)?;
assert_eq!(session.bytes_seen(), 0);
let e = UploadSession::new(b"text/plain", &Limits::DEFAULT, &policy, &store).unwrap_err();
assert_eq!(e.code(), "unsupported_media_type");Sourcepub fn push<I: Inspector>(
&mut self,
chunk: &[u8],
inspector: &mut I,
) -> Result<(), FormError>
pub fn push<I: Inspector>( &mut self, chunk: &[u8], inspector: &mut I, ) -> Result<(), FormError>
Feeds one chunk; consumes all of it or fails.
§Errors
Every multipart error, Refused, Io, Store, Limit { Disk };
Poisoned after an error.
§Examples
use form_net::{Limits, NoInspector, Policy, Store, UploadSession};
let (store, policy) = (Store::memory_only(), Policy::DEFAULT);
let mut session = UploadSession::new(b"multipart/form-data; boundary=X", &Limits::DEFAULT, &policy, &store)?;
let body = b"--X\r\nContent-Disposition: form-data; name=f; filename=a.txt\r\n\r\nhello\r\n--X--";
for chunk in body.chunks(7) {
session.push(chunk, &mut NoInspector)?;
}
let upload = session.finish(&mut NoInspector)?;
assert_eq!(upload.file("f").unwrap().bytes(), Some(&b"hello"[..]));Sourcepub fn finish<I: Inspector>(
self,
inspector: &mut I,
) -> Result<Upload, FormError>
pub fn finish<I: Inspector>( self, inspector: &mut I, ) -> Result<Upload, FormError>
Ends the body and returns the upload.
§Errors
Syntax { Truncated } if the close delimiter was not seen; as
push.
§Examples
use form_net::{Limits, NoInspector, Policy, Store, UploadSession};
let (store, policy) = (Store::memory_only(), Policy::DEFAULT);
let mut session = UploadSession::new(b"multipart/form-data; boundary=X", &Limits::DEFAULT, &policy, &store)?;
// The body stops before its close delimiter.
session.push(b"--X\r\nContent-Disposition: form-data; name=a\r\n\r\n1", &mut NoInspector)?;
let e = session.finish(&mut NoInspector).unwrap_err();
assert_eq!(e.code(), "syntax.truncated");Sourcepub fn captured_token(&self) -> Option<&str>
pub fn captured_token(&self) -> Option<&str>
The token field’s value, once its part has ended.
§Examples
use form_net::{Limits, NoInspector, Policy, Store, TokenField, UploadSession};
let store = Store::memory_only();
let policy = Policy::DEFAULT.with_token_field(TokenField::new("csrf"));
let mut session = UploadSession::new(b"multipart/form-data; boundary=X", &Limits::DEFAULT, &policy, &store)?;
let body = b"--X\r\nContent-Disposition: form-data; name=csrf\r\n\r\nt0k\r\n--X\r\nContent-Disposition: form-data; name=f; filename=a.txt\r\n\r\nhello\r\n--X--";
session.push(body, &mut NoInspector)?;
// Known before `finish`, so the token can be checked first.
assert_eq!(session.captured_token(), Some("t0k"));Sourcepub fn bytes_seen(&self) -> u64
pub fn bytes_seen(&self) -> u64
Body bytes consumed so far.
§Examples
use form_net::{Limits, NoInspector, Policy, Store, UploadSession};
let (store, policy) = (Store::memory_only(), Policy::DEFAULT);
let mut session = UploadSession::new(b"multipart/form-data; boundary=X", &Limits::DEFAULT, &policy, &store)?;
let body = b"--X\r\nContent-Disposition: form-data; name=f; filename=a.txt\r\n\r\nhello\r\n--X--";
session.push(&body[..10], &mut NoInspector)?;
assert_eq!(session.bytes_seen(), 10);Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for UploadSession
impl !UnwindSafe for UploadSession
impl Forget for UploadSession
impl Freeze for UploadSession
impl Move for UploadSession
impl Send for UploadSession
impl Sync for UploadSession
impl Unpin for UploadSession
impl UnsafeUnpin for UploadSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more