11use std:: env;
22use std:: net:: SocketAddr ;
3- use std:: str:: FromStr ;
43use std:: sync:: Arc ;
54
65use anyhow:: Result ;
@@ -73,7 +72,7 @@ fn init_ohttp() -> Result<ohttp::Server> {
7372 let server_config = ohttp:: KeyConfig :: new ( KEY_ID , KEM , Vec :: from ( SYMMETRIC ) ) ?;
7473 let encoded_config = server_config. encode ( ) ?;
7574 let b64_config = base64:: encode_config (
76- encoded_config,
75+ & encoded_config,
7776 base64:: Config :: new ( base64:: CharacterSet :: UrlSafe , false ) ,
7877 ) ;
7978 info ! ( "ohttp server config base64 UrlSafe: {:?}" , b64_config) ;
@@ -119,29 +118,36 @@ async fn handle_ohttp(
119118 let ( bhttp_req, res_ctx) =
120119 ohttp_locked. decapsulate ( & ohttp_body) . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
121120 let mut cursor = std:: io:: Cursor :: new ( bhttp_req) ;
122- let req = bhttp:: Message :: read_bhttp ( & mut cursor) . unwrap ( ) ;
121+ let req =
122+ bhttp:: Message :: read_bhttp ( & mut cursor) . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
123123 let uri = Uri :: builder ( )
124- . scheme ( req. control ( ) . scheme ( ) . unwrap ( ) )
125- . authority ( req. control ( ) . authority ( ) . unwrap ( ) )
126- . path_and_query ( req. control ( ) . path ( ) . unwrap ( ) )
127- . build ( )
128- . unwrap ( ) ;
124+ . scheme ( req. control ( ) . scheme ( ) . unwrap_or_default ( ) )
125+ . authority ( req. control ( ) . authority ( ) . unwrap_or_default ( ) )
126+ . path_and_query ( req. control ( ) . path ( ) . unwrap_or_default ( ) )
127+ . build ( ) ?;
129128 let body = req. content ( ) . to_vec ( ) ;
130- let mut http_req = Request :: builder ( ) . uri ( uri) . method ( req. control ( ) . method ( ) . unwrap ( ) ) ;
129+ let mut http_req =
130+ Request :: builder ( ) . uri ( uri) . method ( req. control ( ) . method ( ) . unwrap_or_default ( ) ) ;
131131 for header in req. header ( ) . fields ( ) {
132132 http_req = http_req. header ( header. name ( ) , header. value ( ) )
133133 }
134- let request = http_req. body ( Body :: from ( body) ) . unwrap ( ) ;
134+ let request = http_req. body ( Body :: from ( body) ) ? ;
135135
136136 let response = handle_v2 ( pool, request) . await ?;
137137
138138 let ( parts, body) = response. into_parts ( ) ;
139139 let mut bhttp_res = bhttp:: Message :: response ( parts. status . as_u16 ( ) ) ;
140- let full_body = hyper:: body:: to_bytes ( body) . await . unwrap ( ) ;
140+ let full_body = hyper:: body:: to_bytes ( body)
141+ . await
142+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
141143 bhttp_res. write_content ( & full_body) ;
142144 let mut bhttp_bytes = Vec :: new ( ) ;
143- bhttp_res. write_bhttp ( bhttp:: Mode :: KnownLength , & mut bhttp_bytes) . unwrap ( ) ;
144- let ohttp_res = res_ctx. encapsulate ( & bhttp_bytes) . unwrap ( ) ;
145+ bhttp_res
146+ . write_bhttp ( bhttp:: Mode :: KnownLength , & mut bhttp_bytes)
147+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
148+ let ohttp_res = res_ctx
149+ . encapsulate ( & bhttp_bytes)
150+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
145151 Ok ( Response :: new ( Body :: from ( ohttp_res) ) )
146152}
147153
@@ -162,16 +168,22 @@ async fn handle_v2(pool: DbPool, req: Request<Body>) -> Result<Response<Body>, H
162168
163169enum HandlerError {
164170 PayloadTooLarge ,
165- InternalServerError ,
166- BadRequest ,
171+ InternalServerError ( Box < dyn std :: error :: Error > ) ,
172+ BadRequest ( Box < dyn std :: error :: Error > ) ,
167173}
168174
169175impl HandlerError {
170176 fn to_response ( & self ) -> Response < Body > {
171177 let status = match self {
172178 HandlerError :: PayloadTooLarge => StatusCode :: PAYLOAD_TOO_LARGE ,
173- HandlerError :: BadRequest => StatusCode :: BAD_REQUEST ,
174- _ => StatusCode :: INTERNAL_SERVER_ERROR ,
179+ Self :: InternalServerError ( e) => {
180+ error ! ( "Internal server error: {}" , e) ;
181+ StatusCode :: INTERNAL_SERVER_ERROR
182+ }
183+ Self :: BadRequest ( e) => {
184+ error ! ( "Bad request: {}" , e) ;
185+ StatusCode :: BAD_REQUEST
186+ }
175187 } ;
176188
177189 let mut res = Response :: new ( Body :: empty ( ) ) ;
@@ -181,17 +193,19 @@ impl HandlerError {
181193}
182194
183195impl From < hyper:: http:: Error > for HandlerError {
184- fn from ( _ : hyper:: http:: Error ) -> Self { HandlerError :: InternalServerError }
196+ fn from ( e : hyper:: http:: Error ) -> Self { HandlerError :: InternalServerError ( e . into ( ) ) }
185197}
186198
187199async fn post_enroll ( body : Body ) -> Result < Response < Body > , HandlerError > {
188200 let b64_config = base64:: Config :: new ( base64:: CharacterSet :: UrlSafe , false ) ;
189- let bytes = hyper:: body:: to_bytes ( body) . await . map_err ( |_| HandlerError :: BadRequest ) ?;
190- let base64_id = String :: from_utf8 ( bytes. to_vec ( ) ) . map_err ( |_| HandlerError :: BadRequest ) ?;
191- let pubkey_bytes: Vec < u8 > =
192- base64:: decode_config ( base64_id, b64_config) . map_err ( |_| HandlerError :: BadRequest ) ?;
201+ let bytes =
202+ hyper:: body:: to_bytes ( body) . await . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
203+ let base64_id =
204+ String :: from_utf8 ( bytes. to_vec ( ) ) . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
205+ let pubkey_bytes: Vec < u8 > = base64:: decode_config ( base64_id, b64_config)
206+ . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
193207 let pubkey = bitcoin:: secp256k1:: PublicKey :: from_slice ( & pubkey_bytes)
194- . map_err ( |_ | HandlerError :: BadRequest ) ?;
208+ . map_err ( |e | HandlerError :: BadRequest ( e . into ( ) ) ) ?;
195209 tracing:: info!( "Enrolled valid pubkey: {:?}" , pubkey) ;
196210 Ok ( Response :: builder ( ) . status ( StatusCode :: NO_CONTENT ) . body ( Body :: empty ( ) ) ?)
197211}
@@ -226,20 +240,23 @@ async fn post_fallback(
226240) -> Result < Response < Body > , HandlerError > {
227241 tracing:: trace!( "Post fallback" ) ;
228242 let id = shorten_string ( id) ;
229- let req = hyper:: body:: to_bytes ( body) . await . map_err ( |_| HandlerError :: InternalServerError ) ?;
243+ let req = hyper:: body:: to_bytes ( body)
244+ . await
245+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
246+
230247 if req. len ( ) > MAX_BUFFER_SIZE {
231248 return Err ( HandlerError :: PayloadTooLarge ) ;
232249 }
233250
234251 match pool. push_req ( & id, req. into ( ) ) . await {
235252 Ok ( _) => ( ) ,
236- Err ( _ ) => return Err ( HandlerError :: BadRequest ) ,
253+ Err ( e ) => return Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
237254 } ;
238255
239256 match pool. peek_res ( & id) . await {
240257 Some ( result) => match result {
241258 Ok ( buffered_res) => Ok ( Response :: new ( Body :: from ( buffered_res) ) ) ,
242- Err ( _ ) => Err ( HandlerError :: BadRequest ) ,
259+ Err ( e ) => Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
243260 } ,
244261 None => Ok ( none_response) ,
245262 }
@@ -250,19 +267,21 @@ async fn get_fallback(id: &str, pool: DbPool) -> Result<Response<Body>, HandlerE
250267 match pool. peek_req ( & id) . await {
251268 Some ( result) => match result {
252269 Ok ( buffered_req) => Ok ( Response :: new ( Body :: from ( buffered_req) ) ) ,
253- Err ( _ ) => Err ( HandlerError :: BadRequest ) ,
270+ Err ( e ) => Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
254271 } ,
255272 None => Ok ( Response :: builder ( ) . status ( StatusCode :: ACCEPTED ) . body ( Body :: empty ( ) ) ?) ,
256273 }
257274}
258275
259276async fn post_payjoin ( id : & str , body : Body , pool : DbPool ) -> Result < Response < Body > , HandlerError > {
260277 let id = shorten_string ( id) ;
261- let res = hyper:: body:: to_bytes ( body) . await . map_err ( |_| HandlerError :: InternalServerError ) ?;
278+ let res = hyper:: body:: to_bytes ( body)
279+ . await
280+ . map_err ( |e| HandlerError :: InternalServerError ( e. into ( ) ) ) ?;
262281
263282 match pool. push_res ( & id, res. into ( ) ) . await {
264283 Ok ( _) => Ok ( Response :: builder ( ) . status ( StatusCode :: NO_CONTENT ) . body ( Body :: empty ( ) ) ?) ,
265- Err ( _ ) => Err ( HandlerError :: BadRequest ) ,
284+ Err ( e ) => Err ( HandlerError :: BadRequest ( e . into ( ) ) ) ,
266285 }
267286}
268287
0 commit comments