@@ -3,17 +3,18 @@ use std::mem;
33use http:: request:: { Parts , Request } ;
44use http_body_util:: Either as EitherBody ;
55use hyper:: body:: Incoming ;
6- use mlua:: { AnyUserData , Error , FromLua , Lua , Result , String as LuaString , UserData , UserDataMethods , Value } ;
6+ use mlua:: {
7+ AnyUserData , Error , FromLua , Lua , MetaMethod , Result , String as LuaString , Table , UserData ,
8+ UserDataMethods , Value ,
9+ } ;
710
8- use super :: headers:: LuaHeaderMapExt ;
9- use crate :: http:: { LuaBody , LuaHeaders , LuaMethod } ;
11+ use crate :: http:: { LuaBody , LuaHeaderMapExt , LuaHeaders , LuaMethod } ;
1012use crate :: time:: Duration ;
1113
12- /// A Lua-accessible HTTP request
14+ /// A Lua wrapper around [`http::Request`].
1315pub struct LuaRequest {
1416 pub ( crate ) head : Parts ,
1517 pub ( crate ) body : EitherBody < LuaBody , AnyUserData > ,
16- pub ( crate ) timeout : Option < Duration > ,
1718}
1819
1920impl Default for LuaRequest {
@@ -23,12 +24,12 @@ impl Default for LuaRequest {
2324 LuaRequest {
2425 head,
2526 body : EitherBody :: Left ( LuaBody :: new ( ) ) ,
26- timeout : None ,
2727 }
2828 }
2929}
3030
3131impl LuaRequest {
32+ /// Consumes the LuaRequest and returns its parts.
3233 pub fn into_parts ( self ) -> ( Parts , LuaBody ) {
3334 let LuaRequest { head, body, .. } = self ;
3435 let body = match body {
@@ -37,6 +38,14 @@ impl LuaRequest {
3738 } ;
3839 ( head, body)
3940 }
41+
42+ #[ allow( unused) ]
43+ pub ( crate ) fn params ( & self ) -> RequestParams {
44+ ( self . head . extensions )
45+ . get :: < RequestParams > ( )
46+ . cloned ( )
47+ . unwrap_or_default ( )
48+ }
4049}
4150
4251impl UserData for LuaRequest {
@@ -92,7 +101,7 @@ impl UserData for LuaRequest {
92101 registry. add_method_mut ( "body" , |lua, this, ( ) | {
93102 match & mut this. body {
94103 EitherBody :: Left ( body) => {
95- // Move the body to Lua
104+ // Move the body into Lua
96105 let ud_body = lua. create_userdata ( mem:: take ( body) ) ?;
97106 this. body = EitherBody :: Right ( ud_body. clone ( ) ) ;
98107 Ok ( ud_body)
@@ -105,6 +114,18 @@ impl UserData for LuaRequest {
105114 this. body = EitherBody :: Left ( body) ;
106115 Ok ( ( ) )
107116 } ) ;
117+
118+ registry. add_meta_method ( MetaMethod :: ToString , |_, this, ( ) | {
119+ let mut buf = String :: with_capacity ( 1024 ) ;
120+ let ( method, uri, version) = ( & this. head . method , & this. head . uri , this. head . version ) ;
121+ buf. push_str ( & format ! ( "{method} {uri} {version:?}\n " ) ) ;
122+ // Iterate headers
123+ for ( name, value) in & this. head . headers {
124+ let value = String :: from_utf8_lossy ( value. as_bytes ( ) ) ;
125+ buf. push_str ( & format ! ( "{name}: {value}\n " ) ) ;
126+ }
127+ Ok ( buf)
128+ } ) ;
108129 }
109130}
110131
@@ -114,7 +135,6 @@ impl From<Request<Incoming>> for LuaRequest {
114135 LuaRequest {
115136 head,
116137 body : EitherBody :: Left ( LuaBody :: from ( body) ) ,
117- timeout : None ,
118138 }
119139 }
120140}
@@ -140,21 +160,36 @@ impl FromLua for LuaRequest {
140160 }
141161 // TODO: json, form, etc
142162
143- // Additional parameters
144- let timeout = opt_param ! ( Duration , Some ( & params) , "timeout" ) ? ;
163+ // Additional custom parameters
164+ head . extensions . insert ( RequestParams :: from_table ( & params) ? ) ;
145165
146166 Ok ( Self {
147167 head,
148168 body : EitherBody :: Left ( body) ,
149- timeout,
150169 } )
151170 }
152171 Value :: UserData ( ud) if ud. is :: < Self > ( ) => ud. take :: < Self > ( ) ,
153172 _ => Err ( Error :: FromLuaConversionError {
154173 from : value. type_name ( ) ,
155174 to : "Request" . to_string ( ) ,
156- message : Some ( "expected Table or Request" . to_string ( ) ) ,
175+ message : Some ( "expected Table or Request userdata " . to_string ( ) ) ,
157176 } ) ,
158177 }
159178 }
160179}
180+
181+ /// Additional custom request parameters
182+ #[ derive( Clone , Debug , Default ) ]
183+ pub ( crate ) struct RequestParams {
184+ pub ( crate ) timeout : Option < Duration > ,
185+ }
186+
187+ impl RequestParams {
188+ pub ( crate ) fn from_table ( table : & Table ) -> Result < Self > {
189+ let mut params = RequestParams :: default ( ) ;
190+ if let Some ( timeout) = opt_param ! ( Duration , Some ( table) , "timeout" ) ? {
191+ params. timeout = Some ( timeout) ;
192+ }
193+ Ok ( params)
194+ }
195+ }
0 commit comments