@@ -17,7 +17,9 @@ use super::{
1717 v1, InternalPayloadError , JsonReply , OutputSubstitutionError , ReplyableError , SelectionError ,
1818} ;
1919use crate :: hpke:: { decrypt_message_a, encrypt_message_b, HpkeKeyPair , HpkePublicKey } ;
20- use crate :: ohttp:: { ohttp_decapsulate, ohttp_encapsulate, OhttpEncapsulationError , OhttpKeys } ;
20+ use crate :: ohttp:: {
21+ ohttp_encapsulate, process_get_res, process_post_res, OhttpEncapsulationError , OhttpKeys ,
22+ } ;
2123use crate :: output_substitution:: OutputSubstitution ;
2224use crate :: persist:: Persister ;
2325use crate :: receive:: { parse_payload, InputPair } ;
@@ -180,23 +182,17 @@ impl Receiver<WithContext> {
180182 body : & [ u8 ] ,
181183 context : ohttp:: ClientResponse ,
182184 ) -> Result < Option < Receiver < UncheckedProposal > > , Error > {
183- let response_array: & [ u8 ; crate :: directory:: ENCAPSULATED_MESSAGE_BYTES ] =
184- body. try_into ( )
185- . map_err ( |_| InternalSessionError :: UnexpectedResponseSize ( body. len ( ) ) ) ?;
186- log:: trace!( "decapsulating directory response" ) ;
187- let response = ohttp_decapsulate ( context, response_array)
188- . map_err ( InternalSessionError :: OhttpEncapsulation ) ?;
189- if response. body ( ) . is_empty ( ) {
190- log:: debug!( "response is empty" ) ;
191- return Ok ( None ) ;
192- }
193- match String :: from_utf8 ( response. body ( ) . to_vec ( ) ) {
185+ let body = match process_get_res ( body, context)
186+ . map_err ( InternalSessionError :: DirectoryResponse ) ?
187+ {
188+ Some ( body) => body,
189+ None => return Ok ( None ) ,
190+ } ;
191+ match String :: from_utf8 ( body. clone ( ) ) {
194192 // V1 response bodies are utf8 plaintext
195193 Ok ( response) => Ok ( Some ( Receiver { state : self . extract_proposal_from_v1 ( response) ? } ) ) ,
196194 // V2 response bodies are encrypted binary
197- Err ( _) => Ok ( Some ( Receiver {
198- state : self . extract_proposal_from_v2 ( response. body ( ) . to_vec ( ) ) ?,
199- } ) ) ,
195+ Err ( _) => Ok ( Some ( Receiver { state : self . extract_proposal_from_v2 ( body) ? } ) ) ,
200196 }
201197 }
202198
@@ -350,16 +346,8 @@ impl Receiver<UncheckedProposal> {
350346 body : & [ u8 ] ,
351347 context : ohttp:: ClientResponse ,
352348 ) -> Result < ( ) , SessionError > {
353- let response_array: & [ u8 ; crate :: directory:: ENCAPSULATED_MESSAGE_BYTES ] =
354- body. try_into ( )
355- . map_err ( |_| InternalSessionError :: UnexpectedResponseSize ( body. len ( ) ) ) ?;
356- let response = ohttp_decapsulate ( context, response_array)
357- . map_err ( InternalSessionError :: OhttpEncapsulation ) ?;
358-
359- match response. status ( ) {
360- http:: StatusCode :: OK => Ok ( ( ) ) ,
361- _ => Err ( InternalSessionError :: UnexpectedStatusCode ( response. status ( ) ) . into ( ) ) ,
362- }
349+ process_post_res ( body, context)
350+ . map_err ( |e| InternalSessionError :: DirectoryResponse ( e) . into ( ) )
363351 }
364352}
365353
@@ -648,15 +636,8 @@ impl Receiver<PayjoinProposal> {
648636 res : & [ u8 ] ,
649637 ohttp_context : ohttp:: ClientResponse ,
650638 ) -> Result < ( ) , Error > {
651- let response_array: & [ u8 ; crate :: directory:: ENCAPSULATED_MESSAGE_BYTES ] =
652- res. try_into ( ) . map_err ( |_| InternalSessionError :: UnexpectedResponseSize ( res. len ( ) ) ) ?;
653- let res = ohttp_decapsulate ( ohttp_context, response_array)
654- . map_err ( InternalSessionError :: OhttpEncapsulation ) ?;
655- if res. status ( ) . is_success ( ) {
656- Ok ( ( ) )
657- } else {
658- Err ( InternalSessionError :: UnexpectedStatusCode ( res. status ( ) ) . into ( ) )
659- }
639+ process_post_res ( res, ohttp_context)
640+ . map_err ( |e| InternalSessionError :: DirectoryResponse ( e) . into ( ) )
660641 }
661642}
662643
0 commit comments