Skip to main content

UploadBody

Struct UploadBody 

Source
pub struct UploadBody { /* private fields */ }
Expand description

The request body of an upload.

A body has one of two forms:

  • bytes gives the whole body. The request declares its Content-Length, and hyper takes the bytes in frames of 64 KiB. A followed redirect sends the bytes again. bytes_with_sha256 also gives the SHA-256 of the body to the signer.
  • channel streams the body. The caller writes the body into the UploadWriter while the upload runs. Over HTTP/1.1, the body travels chunked.

If a channel body is dropped before an upload sends it, or after its upload ends, each write of its writer fails with io::ErrorKind::BrokenPipe. If the body of a POST or a PUT is at its end when the fetcher hands over the request, the request declares a Content-Length of zero.

Implementations§

Source§

impl UploadBody

Source

pub fn bytes(bytes: Vec<u8>) -> UploadBody

Creates a body of bytes.

The request sends the vector in frames cut from it with no copy.

Source

pub fn bytes_with_sha256(bytes: Vec<u8>, sha256: [u8; 32]) -> UploadBody

Creates a body of bytes, whose SHA-256 is sha256.

The body is a body of bytes. A signer gets it as SignPayload::Sha256 with sha256, so a signer that signs the SHA-256 of the body hashes no byte of it on the task of the request. Each retry and each redirect hop of the upload uses the same digest, and so does a copy from try_clone.

The fetcher does not check the digest. A wrong digest signs other bytes than the bytes that the request sends, and the server refuses the request.

Source

pub fn channel() -> (UploadBody, UploadWriter)

Creates a streamed body and the writer that fills it.

Source

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the bytes of a body of bytes, or None for a streamed body.

Source

pub fn sha256(&self) -> Option<[u8; 32]>

Returns the SHA-256 of a body of bytes_with_sha256, or None for another body.

Source

pub fn try_clone(&self) -> Option<UploadBody>

Returns a second body with the same bytes, or None for a streamed body.

The two bodies of bytes share the one buffer, so the call copies no byte. The copy keeps the SHA-256 of a body of bytes_with_sha256. A caller sends the copy in a second request, for example after the first one failed with Error::UploadInterrupted. A channel body has one writer, so it has no copy.

Source

pub fn into_vec(self) -> Result<Vec<u8>, UploadBody>

Returns the bytes of a body of bytes as a vector, or the body unchanged.

If no other body shares the buffer, the call gives the buffer back with its capacity, and copies no byte. A caller can then fill the vector again for a later request. The vector does not keep the SHA-256 of a body of bytes_with_sha256. A copy from try_clone shares the buffer, and so does an upload that still holds the body. The call then returns the body as the error, as it does for a channel body.

§Errors

Returns the body unchanged if it is a streamed body, or if another body shares its buffer.

Trait Implementations§

Source§

impl Debug for UploadBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Vec<u8>> for UploadBody

Source§

fn from(bytes: Vec<u8>) -> UploadBody

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !Freeze for UploadBody

§

impl Forget for UploadBody

§

impl Move for UploadBody

§

impl RefUnwindSafe for UploadBody

§

impl Send for UploadBody
where Bytes: Send, Arc<Exchange>: Send,

§

impl Sync for UploadBody
where Bytes: Sync, Arc<Exchange>: Sync,

§

impl Unpin for UploadBody
where *const u8: Unpin, Align8<*mut ()>: Unpin, &'static Vtable: Unpin, Arc<Exchange>: Unpin,

§

impl UnsafeUnpin for UploadBody
where *const u8: UnsafeUnpin, Align8<*mut ()>: UnsafeUnpin, &'static Vtable: UnsafeUnpin, *const ArcInner<Exchange> is !null: UnsafeUnpin, PhantomData<ArcInner<Exchange>>: UnsafeUnpin,

§

impl UnwindSafe for UploadBody
where *const u8: UnwindSafe, Align8<*mut ()>: UnwindSafe, &'static Vtable: UnwindSafe, Arc<Exchange>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more