11use std:: env;
22use std:: net:: SocketAddr ;
3- use std:: str:: FromStr ;
43use std:: sync:: Arc ;
54
65use anyhow:: Result ;
76use hyper:: service:: { make_service_fn, service_fn} ;
87use hyper:: { Body , Method , Request , Response , StatusCode , Uri } ;
98use payjoin:: { base64, bitcoin} ;
10- use tracing:: { debug, info, trace} ;
9+ use tracing:: { debug, error , info, trace} ;
1110use tracing_subscriber:: filter:: LevelFilter ;
1211use tracing_subscriber:: EnvFilter ;
1312
@@ -72,7 +71,7 @@ fn init_ohttp() -> Result<ohttp::Server> {
7271 let server_config = ohttp:: KeyConfig :: new ( KEY_ID , KEM , Vec :: from ( SYMMETRIC ) ) ?;
7372 let encoded_config = server_config. encode ( ) ?;
7473 let b64_config = base64:: encode_config (
75- encoded_config,
74+ & encoded_config,
7675 base64:: Config :: new ( base64:: CharacterSet :: UrlSafe , false ) ,
7776 ) ;
7877 info ! ( "ohttp server config base64 UrlSafe: {:?}" , b64_config) ;
@@ -112,33 +111,41 @@ async fn handle_ohttp(
112111) -> Result < Response < Body > , HandlerError > {
113112 // decapsulate
114113 let ohttp_body =
115- hyper:: body:: to_bytes ( body) . await . map_err ( |_ | HandlerError :: InternalServerError ) ?;
114+ hyper:: body:: to_bytes ( body) . await . map_err ( |e | HandlerError :: BadRequest ( e . into ( ) ) ) ?;
116115
117- let ( bhttp_req, res_ctx) = ohttp. decapsulate ( & ohttp_body) . unwrap ( ) ;
116+ let ( bhttp_req, res_ctx) =
117+ ohttp. decapsulate ( & ohttp_body) . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
118118 let mut cursor = std:: io:: Cursor :: new ( bhttp_req) ;
119- let req = bhttp:: Message :: read_bhttp ( & mut cursor) . unwrap ( ) ;
119+ let req =
120+ bhttp:: Message :: read_bhttp ( & mut cursor) . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
120121 let uri = Uri :: builder ( )
121- . scheme ( req. control ( ) . scheme ( ) . unwrap ( ) )
122- . authority ( req. control ( ) . authority ( ) . unwrap ( ) )
123- . path_and_query ( req. control ( ) . path ( ) . unwrap ( ) )
124- . build ( )
125- . unwrap ( ) ;
122+ . scheme ( req. control ( ) . scheme ( ) . unwrap_or_default ( ) )
123+ . authority ( req. control ( ) . authority ( ) . unwrap_or_default ( ) )
124+ . path_and_query ( req. control ( ) . path ( ) . unwrap_or_default ( ) )
125+ . build ( ) ?;
126126 let body = req. content ( ) . to_vec ( ) ;
127- let mut http_req = Request :: builder ( ) . uri ( uri) . method ( req. control ( ) . method ( ) . unwrap ( ) ) ;
127+ let mut http_req =
128+ Request :: builder ( ) . uri ( uri) . method ( req. control ( ) . method ( ) . unwrap_or_default ( ) ) ;
128129 for header in req. header ( ) . fields ( ) {
129130 http_req = http_req. header ( header. name ( ) , header. value ( ) )
130131 }
131- let request = http_req. body ( Body :: from ( body) ) . unwrap ( ) ;
132+ let request = http_req. body ( Body :: from ( body) ) ? ;
132133
133134 let response = handle_v2 ( pool, request) . await ?;
134135
135136 let ( parts, body) = response. into_parts ( ) ;
136137 let mut bhttp_res = bhttp:: Message :: response ( parts. status . as_u16 ( ) ) ;
137- let full_body = hyper:: body:: to_bytes ( body) . await . unwrap ( ) ;
138+ let full_body = hyper:: body:: to_bytes ( body)
139+ . await
140+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
138141 bhttp_res. write_content ( & full_body) ;
139142 let mut bhttp_bytes = Vec :: new ( ) ;
140- bhttp_res. write_bhttp ( bhttp:: Mode :: KnownLength , & mut bhttp_bytes) . unwrap ( ) ;
141- let ohttp_res = res_ctx. encapsulate ( & bhttp_bytes) . unwrap ( ) ;
143+ bhttp_res
144+ . write_bhttp ( bhttp:: Mode :: KnownLength , & mut bhttp_bytes)
145+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
146+ let ohttp_res = res_ctx
147+ . encapsulate ( & bhttp_bytes)
148+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
142149 Ok ( Response :: new ( Body :: from ( ohttp_res) ) )
143150}
144151
@@ -159,16 +166,22 @@ async fn handle_v2(pool: DbPool, req: Request<Body>) -> Result<Response<Body>, H
159166
160167enum HandlerError {
161168 PayloadTooLarge ,
162- InternalServerError ,
163- BadRequest ,
169+ InternalServerError ( Box < dyn std :: error :: Error > ) ,
170+ BadRequest ( Box < dyn std :: error :: Error > ) ,
164171}
165172
166173impl HandlerError {
167174 fn to_response ( & self ) -> Response < Body > {
168175 let status = match self {
169176 HandlerError :: PayloadTooLarge => StatusCode :: PAYLOAD_TOO_LARGE ,
170- HandlerError :: BadRequest => StatusCode :: BAD_REQUEST ,
171- _ => StatusCode :: INTERNAL_SERVER_ERROR ,
177+ Self :: InternalServerError ( e) => {
178+ error ! ( "Internal server error: {}" , e) ;
179+ StatusCode :: INTERNAL_SERVER_ERROR
180+ }
181+ Self :: BadRequest ( e) => {
182+ error ! ( "Bad request: {}" , e) ;
183+ StatusCode :: BAD_REQUEST
184+ }
172185 } ;
173186
174187 let mut res = Response :: new ( Body :: empty ( ) ) ;
@@ -178,17 +191,19 @@ impl HandlerError {
178191}
179192
180193impl From < hyper:: http:: Error > for HandlerError {
181- fn from ( _ : hyper:: http:: Error ) -> Self { HandlerError :: InternalServerError }
194+ fn from ( e : hyper:: http:: Error ) -> Self { HandlerError :: InternalServerError ( e . into ( ) ) }
182195}
183196
184197async fn post_enroll ( body : Body ) -> Result < Response < Body > , HandlerError > {
185198 let b64_config = base64:: Config :: new ( base64:: CharacterSet :: UrlSafe , false ) ;
186- let bytes = hyper:: body:: to_bytes ( body) . await . map_err ( |_| HandlerError :: BadRequest ) ?;
187- let base64_id = String :: from_utf8 ( bytes. to_vec ( ) ) . map_err ( |_| HandlerError :: BadRequest ) ?;
188- let pubkey_bytes: Vec < u8 > =
189- base64:: decode_config ( base64_id, b64_config) . map_err ( |_| HandlerError :: BadRequest ) ?;
199+ let bytes =
200+ hyper:: body:: to_bytes ( body) . await . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
201+ let base64_id =
202+ String :: from_utf8 ( bytes. to_vec ( ) ) . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
203+ let pubkey_bytes: Vec < u8 > = base64:: decode_config ( base64_id, b64_config)
204+ . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
190205 let pubkey = bitcoin:: secp256k1:: PublicKey :: from_slice ( & pubkey_bytes)
191- . map_err ( |_ | HandlerError :: BadRequest ) ?;
206+ . map_err ( |e | HandlerError :: BadRequest ( e . into ( ) ) ) ?;
192207 tracing:: info!( "Enrolled valid pubkey: {:?}" , pubkey) ;
193208 Ok ( Response :: builder ( ) . status ( StatusCode :: NO_CONTENT ) . body ( Body :: empty ( ) ) ?)
194209}
@@ -223,20 +238,23 @@ async fn post_fallback(
223238) -> Result < Response < Body > , HandlerError > {
224239 tracing:: trace!( "Post fallback" ) ;
225240 let id = shorten_string ( id) ;
226- let req = hyper:: body:: to_bytes ( body) . await . map_err ( |_| HandlerError :: InternalServerError ) ?;
241+ let req = hyper:: body:: to_bytes ( body)
242+ . await
243+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
244+
227245 if req. len ( ) > MAX_BUFFER_SIZE {
228246 return Err ( HandlerError :: PayloadTooLarge ) ;
229247 }
230248
231249 match pool. push_req ( & id, req. into ( ) ) . await {
232250 Ok ( _) => ( ) ,
233- Err ( _ ) => return Err ( HandlerError :: BadRequest ) ,
251+ Err ( e ) => return Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
234252 } ;
235253
236254 match pool. peek_res ( & id) . await {
237255 Some ( result) => match result {
238256 Ok ( buffered_res) => Ok ( Response :: new ( Body :: from ( buffered_res) ) ) ,
239- Err ( _ ) => Err ( HandlerError :: BadRequest ) ,
257+ Err ( e ) => Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
240258 } ,
241259 None => Ok ( none_response) ,
242260 }
@@ -247,19 +265,21 @@ async fn get_fallback(id: &str, pool: DbPool) -> Result<Response<Body>, HandlerE
247265 match pool. peek_req ( & id) . await {
248266 Some ( result) => match result {
249267 Ok ( buffered_req) => Ok ( Response :: new ( Body :: from ( buffered_req) ) ) ,
250- Err ( _ ) => Err ( HandlerError :: BadRequest ) ,
268+ Err ( e ) => Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
251269 } ,
252270 None => Ok ( Response :: builder ( ) . status ( StatusCode :: ACCEPTED ) . body ( Body :: empty ( ) ) ?) ,
253271 }
254272}
255273
256274async fn post_payjoin ( id : & str , body : Body , pool : DbPool ) -> Result < Response < Body > , HandlerError > {
257275 let id = shorten_string ( id) ;
258- let res = hyper:: body:: to_bytes ( body) . await . map_err ( |_| HandlerError :: InternalServerError ) ?;
276+ let res = hyper:: body:: to_bytes ( body)
277+ . await
278+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
259279
260280 match pool. push_res ( & id, res. into ( ) ) . await {
261281 Ok ( _) => Ok ( Response :: builder ( ) . status ( StatusCode :: NO_CONTENT ) . body ( Body :: empty ( ) ) ?) ,
262- Err ( _ ) => Err ( HandlerError :: BadRequest ) ,
282+ Err ( e ) => Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
263283 }
264284}
265285
0 commit comments