@@ -5,17 +5,17 @@ use bitcoin::consensus::encode::Decodable;
55use bitcoin:: consensus:: Encodable ;
66use url:: Url ;
77
8- use super :: error:: ParseReceiverPubkeyError ;
8+ use super :: error:: { ParseExpError , ParseOhttpError , ParseReceiverPubkeyError } ;
99use crate :: hpke:: HpkePublicKey ;
1010use crate :: OhttpKeys ;
1111
1212/// Parse and set fragment parameters from `&pj=` URI parameter URLs
1313pub ( crate ) trait UrlExt {
1414 fn receiver_pubkey ( & self ) -> Result < HpkePublicKey , ParseReceiverPubkeyError > ;
1515 fn set_receiver_pubkey ( & mut self , exp : HpkePublicKey ) ;
16- fn ohttp ( & self ) -> Option < OhttpKeys > ;
16+ fn ohttp ( & self ) ->Result < OhttpKeys , ParseOhttpError > ;
1717 fn set_ohttp ( & mut self , ohttp : OhttpKeys ) ;
18- fn exp ( & self ) -> Option < std:: time:: SystemTime > ;
18+ fn exp ( & self ) -> Result < std:: time:: SystemTime , ParseExpError > ;
1919 fn set_exp ( & mut self , exp : std:: time:: SystemTime ) ;
2020}
2121
@@ -50,8 +50,10 @@ impl UrlExt for Url {
5050 }
5151
5252 /// Retrieve the ohttp parameter from the URL fragment
53- fn ohttp ( & self ) -> Option < OhttpKeys > {
54- get_param ( self , "OH1" , |value| OhttpKeys :: from_str ( value) . ok ( ) )
53+ fn ohttp ( & self ) -> Result < OhttpKeys , ParseOhttpError > {
54+ let value = get_param ( self , "ohttp=" , |v| Some ( v. to_owned ( ) ) )
55+ . ok_or ( ParseOhttpError :: MissingOhttp ) ?;
56+ OhttpKeys :: from_str ( & value) . ok_or ( ParseOhttpError :: InvalidOhttpKeys )
5557 }
5658
5759 /// Set the ohttp parameter in the URL fragment
@@ -142,7 +144,16 @@ mod tests {
142144 url. set_ohttp ( ohttp_keys. clone ( ) ) ;
143145
144146 assert_eq ! ( url. fragment( ) , Some ( serialized) ) ;
145- assert_eq ! ( url. ohttp( ) , Some ( ohttp_keys) ) ;
147+ assert_eq ! ( url. ohttp( ) , Ok ( ohttp_keys) ) ;
148+ }
149+
150+ #[ test]
151+ fn test_errors_when_parsing_ohttp ( ) {
152+ let missing_ohttp_url = Url :: parse ( "https://example.com" ) . unwrap ( ) ;
153+ assert ! ( matches!( missing_ohttp_url. ohttp( ) , Err ( ParseOhttpError :: MissingOhttp ) ) ) ;
154+
155+ let invalid_ohttp_url = Url :: parse ( "https://example.com?ohttp=@@@@@@@" ) . unwrap ( ) ;
156+ assert ! ( matches!( invalid_ohttp_url. ohttp( ) , Err ( ParseOhttpError :: InvalidOhttpKeys ) ) ) ;
146157 }
147158
148159 #[ test]
0 commit comments