ResponseBodyBuffer

Struct ResponseBodyBuffer 

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

An opaque object that represents the underlying Envoy Http response body buffer. This is used to interact with it from the module code. The buffer consists of one or more slices. The slices are the contiguous memory regions that represent the buffer.

This corresponds to either a frame of the response body or the whole body.

This is a shallow wrapper around the raw pointer to the Envoy response body buffer.

Implementations§

Source§

impl ResponseBodyBuffer

Source

pub fn length(&self) -> usize

Returns the number of bytes in the buffer.

Source

pub fn slices_count(&self) -> usize

Returns the number of slices in the buffer.

Source

pub fn slices(&self) -> Vec<&mut [u8]>

Returns the slices of the buffer.

Examples found in repository?
example/example.rs (line 440)
401    fn response_body(
402        &mut self,
403        response_body_frame: &ResponseBodyBuffer,
404        end_of_stream: bool,
405    ) -> ResponseBodyStatus {
406        println!(
407            "new response body frame: {}",
408            String::from_utf8(response_body_frame.copy()).unwrap()
409        );
410        if !end_of_stream {
411            // Wait for the end of the stream to see the full body.
412            return ResponseBodyStatus::StopIterationAndBuffer;
413        }
414
415        // Get the entire response body reference - this does not copy the body.
416        let entire_body = self.envoy_filter_instance.get_response_body_buffer();
417        println!(
418            "entire response body: {}",
419            String::from_utf8(entire_body.copy()).unwrap()
420        );
421
422        // This demonstrates how to use the reader to read the body.
423        let mut reader = entire_body.reader();
424        let mut buf = vec![0; 2];
425        let mut offset = 0;
426        loop {
427            let n = reader.read(&mut buf).unwrap();
428            if n == 0 {
429                break;
430            }
431            println!(
432                "response body read 2 bytes offset at {}: \"{}\"",
433                offset,
434                std::str::from_utf8(&buf[..n]).unwrap()
435            );
436            offset += 2;
437        }
438
439        // Replace the entire body with 'Y' without copying.
440        for i in entire_body.slices() {
441            for j in i {
442                *j = b'Y';
443            }
444        }
445
446        ResponseBodyStatus::Continue
447    }
Source

pub fn copy(&self) -> Vec<u8>

Copies the entire buffer into a single contiguous Vec managed in Rust.

Examples found in repository?
example/example.rs (line 408)
401    fn response_body(
402        &mut self,
403        response_body_frame: &ResponseBodyBuffer,
404        end_of_stream: bool,
405    ) -> ResponseBodyStatus {
406        println!(
407            "new response body frame: {}",
408            String::from_utf8(response_body_frame.copy()).unwrap()
409        );
410        if !end_of_stream {
411            // Wait for the end of the stream to see the full body.
412            return ResponseBodyStatus::StopIterationAndBuffer;
413        }
414
415        // Get the entire response body reference - this does not copy the body.
416        let entire_body = self.envoy_filter_instance.get_response_body_buffer();
417        println!(
418            "entire response body: {}",
419            String::from_utf8(entire_body.copy()).unwrap()
420        );
421
422        // This demonstrates how to use the reader to read the body.
423        let mut reader = entire_body.reader();
424        let mut buf = vec![0; 2];
425        let mut offset = 0;
426        loop {
427            let n = reader.read(&mut buf).unwrap();
428            if n == 0 {
429                break;
430            }
431            println!(
432                "response body read 2 bytes offset at {}: \"{}\"",
433                offset,
434                std::str::from_utf8(&buf[..n]).unwrap()
435            );
436            offset += 2;
437        }
438
439        // Replace the entire body with 'Y' without copying.
440        for i in entire_body.slices() {
441            for j in i {
442                *j = b'Y';
443            }
444        }
445
446        ResponseBodyStatus::Continue
447    }
Source

pub fn reader(&self) -> ResponseBodyBufferReader

Returns a reader that implements the std::io::Read trait.

Examples found in repository?
example/example.rs (line 423)
401    fn response_body(
402        &mut self,
403        response_body_frame: &ResponseBodyBuffer,
404        end_of_stream: bool,
405    ) -> ResponseBodyStatus {
406        println!(
407            "new response body frame: {}",
408            String::from_utf8(response_body_frame.copy()).unwrap()
409        );
410        if !end_of_stream {
411            // Wait for the end of the stream to see the full body.
412            return ResponseBodyStatus::StopIterationAndBuffer;
413        }
414
415        // Get the entire response body reference - this does not copy the body.
416        let entire_body = self.envoy_filter_instance.get_response_body_buffer();
417        println!(
418            "entire response body: {}",
419            String::from_utf8(entire_body.copy()).unwrap()
420        );
421
422        // This demonstrates how to use the reader to read the body.
423        let mut reader = entire_body.reader();
424        let mut buf = vec![0; 2];
425        let mut offset = 0;
426        loop {
427            let n = reader.read(&mut buf).unwrap();
428            if n == 0 {
429                break;
430            }
431            println!(
432                "response body read 2 bytes offset at {}: \"{}\"",
433                offset,
434                std::str::from_utf8(&buf[..n]).unwrap()
435            );
436            offset += 2;
437        }
438
439        // Replace the entire body with 'Y' without copying.
440        for i in entire_body.slices() {
441            for j in i {
442                *j = b'Y';
443            }
444        }
445
446        ResponseBodyStatus::Continue
447    }
Source

pub fn append(&self, data: &[u8])

Appends the given data to the buffer.

After this operation, previous slices might be invalidated.

Examples found in repository?
example/example.rs (line 557)
545    fn response_body(
546        &mut self,
547        _response_body_frame: &ResponseBodyBuffer,
548        end_of_stream: bool,
549    ) -> ResponseBodyStatus {
550        if !end_of_stream {
551            // Wait for the end of the stream to see the full body.
552            return ResponseBodyStatus::StopIterationAndBuffer;
553        }
554
555        let entire_body = self.envoy_filter_instance.get_response_body_buffer();
556        if !self.response_append.is_empty() {
557            entire_body.append(self.response_append.as_bytes());
558        }
559        if !self.response_prepend.is_empty() {
560            entire_body.prepend(self.response_prepend.as_bytes());
561        }
562        if !self.response_replace.is_empty() {
563            entire_body.replace(self.response_replace.as_bytes());
564        }
565        ResponseBodyStatus::Continue
566    }
Source

pub fn prepend(&self, data: &[u8])

Prepends the given data to the buffer.

After this operation, previous slices might be invalidated.

Examples found in repository?
example/example.rs (line 560)
545    fn response_body(
546        &mut self,
547        _response_body_frame: &ResponseBodyBuffer,
548        end_of_stream: bool,
549    ) -> ResponseBodyStatus {
550        if !end_of_stream {
551            // Wait for the end of the stream to see the full body.
552            return ResponseBodyStatus::StopIterationAndBuffer;
553        }
554
555        let entire_body = self.envoy_filter_instance.get_response_body_buffer();
556        if !self.response_append.is_empty() {
557            entire_body.append(self.response_append.as_bytes());
558        }
559        if !self.response_prepend.is_empty() {
560            entire_body.prepend(self.response_prepend.as_bytes());
561        }
562        if !self.response_replace.is_empty() {
563            entire_body.replace(self.response_replace.as_bytes());
564        }
565        ResponseBodyStatus::Continue
566    }
Source

pub fn drain(&self, size: usize)

Drains the buffer by the given size.

After this operation, previous slices might be invalidated.

Source

pub fn replace(&self, data: &[u8])

Replaces the entire buffer with the given data.

After this operation, previous slices might be invalidated.

Examples found in repository?
example/example.rs (line 563)
545    fn response_body(
546        &mut self,
547        _response_body_frame: &ResponseBodyBuffer,
548        end_of_stream: bool,
549    ) -> ResponseBodyStatus {
550        if !end_of_stream {
551            // Wait for the end of the stream to see the full body.
552            return ResponseBodyStatus::StopIterationAndBuffer;
553        }
554
555        let entire_body = self.envoy_filter_instance.get_response_body_buffer();
556        if !self.response_append.is_empty() {
557            entire_body.append(self.response_append.as_bytes());
558        }
559        if !self.response_prepend.is_empty() {
560            entire_body.prepend(self.response_prepend.as_bytes());
561        }
562        if !self.response_replace.is_empty() {
563            entire_body.replace(self.response_replace.as_bytes());
564        }
565        ResponseBodyStatus::Continue
566    }

Trait Implementations§

Source§

impl Clone for ResponseBodyBuffer

Source§

fn clone(&self) -> ResponseBodyBuffer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ResponseBodyBuffer

Source§

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

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

impl From<ResponseBodyBuffer> for ResponseBodyBufferReader

Source§

fn from(buffer: ResponseBodyBuffer) -> Self

Converts to this type from the input type.
Source§

impl Copy for ResponseBodyBuffer

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.