@@ -3,6 +3,8 @@ use std::ops::Deref;
33
44use fluxheim_protocol:: { Http1RequestTarget , Http1Version , http1_request_target} ;
55
6+ use crate :: request_body_budget:: NativeRequestBodyReservation ;
7+
68#[ derive( Debug , Eq , PartialEq ) ]
79pub struct NativeHttp1Request {
810 pub method : String ,
@@ -19,48 +21,68 @@ pub struct NativeHttp1Request {
1921 pub trailers : Vec < ( String , String ) > ,
2022}
2123
22- #[ derive( Default , Eq , PartialEq ) ]
23- pub struct NativeHttp1RequestBody ( Vec < u8 > ) ;
24+ #[ derive( Default ) ]
25+ pub struct NativeHttp1RequestBody {
26+ bytes : Vec < u8 > ,
27+ admission : Option < NativeRequestBodyReservation > ,
28+ }
2429
2530impl NativeHttp1RequestBody {
2631 #[ must_use]
2732 pub const fn empty ( ) -> Self {
28- Self ( Vec :: new ( ) )
33+ Self {
34+ bytes : Vec :: new ( ) ,
35+ admission : None ,
36+ }
2937 }
3038
3139 #[ must_use]
3240 pub const fn from_vec ( body : Vec < u8 > ) -> Self {
33- Self ( body)
41+ Self {
42+ bytes : body,
43+ admission : None ,
44+ }
45+ }
46+
47+ pub ( crate ) fn attach_admission ( & mut self , admission : NativeRequestBodyReservation ) {
48+ if self . admission . is_some ( ) {
49+ log:: error!(
50+ target: "fluxheim::security" ,
51+ "request body admission was attached more than once"
52+ ) ;
53+ std:: process:: abort ( ) ;
54+ }
55+ self . admission = Some ( admission) ;
3456 }
3557
3658 pub ( crate ) fn extend_from_slice (
3759 & mut self ,
3860 bytes : & [ u8 ] ,
3961 ) -> Result < ( ) , fluxheim_protocol:: Http1ParseError > {
4062 let required = self
41- . 0
63+ . bytes
4264 . len ( )
4365 . checked_add ( bytes. len ( ) )
4466 . ok_or ( fluxheim_protocol:: Http1ParseError :: BodyTooLarge ) ?;
4567 self . reserve_capacity ( required) ?;
46- self . 0 . extend_from_slice ( bytes) ;
68+ self . bytes . extend_from_slice ( bytes) ;
4769 Ok ( ( ) )
4870 }
4971
5072 pub ( crate ) fn reserve_capacity (
5173 & mut self ,
5274 required : usize ,
5375 ) -> Result < ( ) , fluxheim_protocol:: Http1ParseError > {
54- if required <= self . 0 . capacity ( ) {
76+ if required <= self . bytes . capacity ( ) {
5577 return Ok ( ( ) ) ;
5678 }
5779 let mut replacement = Vec :: new ( ) ;
5880 replacement
5981 . try_reserve_exact ( required)
6082 . map_err ( |_| fluxheim_protocol:: Http1ParseError :: BodyTooLarge ) ?;
61- replacement. extend_from_slice ( & self . 0 ) ;
62- sanitization:: unsafe_wipe:: volatile_sanitize_vec ( & mut self . 0 ) ;
63- self . 0 = replacement;
83+ replacement. extend_from_slice ( & self . bytes ) ;
84+ sanitization:: unsafe_wipe:: volatile_sanitize_vec ( & mut self . bytes ) ;
85+ self . bytes = replacement;
6486 Ok ( ( ) )
6587 }
6688
@@ -72,22 +94,30 @@ impl NativeHttp1RequestBody {
7294 replacement
7395 . try_reserve_exact ( required)
7496 . map_err ( |_| fluxheim_protocol:: Http1ParseError :: BodyTooLarge ) ?;
75- replacement. extend_from_slice ( & self . 0 ) ;
76- sanitization:: unsafe_wipe:: volatile_sanitize_vec ( & mut self . 0 ) ;
77- self . 0 = replacement;
97+ replacement. extend_from_slice ( & self . bytes ) ;
98+ sanitization:: unsafe_wipe:: volatile_sanitize_vec ( & mut self . bytes ) ;
99+ self . bytes = replacement;
78100 Ok ( ( ) )
79101 }
80102
81103 pub ( crate ) fn capacity ( & self ) -> usize {
82- self . 0 . capacity ( )
104+ self . bytes . capacity ( )
105+ }
106+ }
107+
108+ impl PartialEq for NativeHttp1RequestBody {
109+ fn eq ( & self , other : & Self ) -> bool {
110+ self . bytes == other. bytes
83111 }
84112}
85113
114+ impl Eq for NativeHttp1RequestBody { }
115+
86116impl std:: fmt:: Debug for NativeHttp1RequestBody {
87117 fn fmt ( & self , formatter : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
88118 formatter
89119 . debug_struct ( "NativeHttp1RequestBody" )
90- . field ( "len" , & self . 0 . len ( ) )
120+ . field ( "len" , & self . bytes . len ( ) )
91121 . field ( "contents" , & "<redacted>" )
92122 . finish ( )
93123 }
@@ -97,19 +127,19 @@ impl Deref for NativeHttp1RequestBody {
97127 type Target = [ u8 ] ;
98128
99129 fn deref ( & self ) -> & Self :: Target {
100- & self . 0
130+ & self . bytes
101131 }
102132}
103133
104134impl AsRef < [ u8 ] > for NativeHttp1RequestBody {
105135 fn as_ref ( & self ) -> & [ u8 ] {
106- & self . 0
136+ & self . bytes
107137 }
108138}
109139
110140impl fluxheim_protocol:: Http1ChunkSink for NativeHttp1RequestBody {
111141 fn len ( & self ) -> usize {
112- self . 0 . len ( )
142+ self . bytes . len ( )
113143 }
114144
115145 fn append ( & mut self , bytes : & [ u8 ] ) -> Result < ( ) , fluxheim_protocol:: Http1ParseError > {
@@ -119,7 +149,7 @@ impl fluxheim_protocol::Http1ChunkSink for NativeHttp1RequestBody {
119149
120150impl Drop for NativeHttp1RequestBody {
121151 fn drop ( & mut self ) {
122- sanitization:: unsafe_wipe:: volatile_sanitize_vec ( & mut self . 0 ) ;
152+ sanitization:: unsafe_wipe:: volatile_sanitize_vec ( & mut self . bytes ) ;
123153 }
124154}
125155
0 commit comments