@@ -7,13 +7,13 @@ use url::Url;
77
88use super :: error:: ParseReceiverPubkeyError ;
99use crate :: hpke:: HpkePublicKey ;
10- use crate :: OhttpKeys ;
10+ use crate :: ohttp :: { OhttpKeys , ParseOhttpKeysError } ;
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 , ParseOhttpKeysError > ;
1717 fn set_ohttp ( & mut self , ohttp : OhttpKeys ) ;
1818 fn exp ( & self ) -> Option < std:: time:: SystemTime > ;
1919 fn set_exp ( & mut self , exp : std:: time:: SystemTime ) ;
@@ -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 , ParseOhttpKeysError > {
54+ let value = get_param ( self , "ohttp=" , |v| Some ( v. to_owned ( ) ) )
55+ . ok_or ( ParseOhttpKeysError :: MissingOhttpKeys ) ?;
56+ OhttpKeys :: from_str ( & value)
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 ( ParseOhttpKeysError :: MissingOhttpKeys ) ) ) ;
154+
155+ let invalid_ohttp_url = Url :: parse ( "https://example.com?ohttp=@@@@@@@" ) . unwrap ( ) ;
156+ assert ! ( matches!( invalid_ohttp_url. ohttp( ) , Err ( ParseOhttpKeysError :: InvalidFormat ) ) ) ;
146157 }
147158
148159 #[ test]
0 commit comments