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
impl ResponseBodyBuffer
Sourcepub fn slices_count(&self) -> usize
pub fn slices_count(&self) -> usize
Returns the number of slices in the buffer.
Sourcepub fn slices(&self) -> Vec<&mut [u8]>
pub fn slices(&self) -> Vec<&mut [u8]>
Returns the slices of the buffer.
Examples found in repository?
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 }Sourcepub fn copy(&self) -> Vec<u8> ⓘ
pub fn copy(&self) -> Vec<u8> ⓘ
Copies the entire buffer into a single contiguous Vec
Examples found in repository?
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 }Sourcepub fn reader(&self) -> ResponseBodyBufferReader ⓘ
pub fn reader(&self) -> ResponseBodyBufferReader ⓘ
Returns a reader that implements the std::io::Read trait.
Examples found in repository?
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 }Sourcepub fn append(&self, data: &[u8])
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?
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 }Sourcepub fn prepend(&self, data: &[u8])
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?
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 }Sourcepub fn drain(&self, size: usize)
pub fn drain(&self, size: usize)
Drains the buffer by the given size.
After this operation, previous slices might be invalidated.
Sourcepub fn replace(&self, data: &[u8])
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?
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
impl Clone for ResponseBodyBuffer
Source§fn clone(&self) -> ResponseBodyBuffer
fn clone(&self) -> ResponseBodyBuffer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more