@@ -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,15 @@ 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+ Some ( body) => body,
187+ None => return Ok ( None ) ,
188+ } ;
189+ match String :: from_utf8 ( body. clone ( ) ) {
194190 // V1 response bodies are utf8 plaintext
195191 Ok ( response) => Ok ( Some ( Receiver { state : self . extract_proposal_from_v1 ( response) ? } ) ) ,
196192 // V2 response bodies are encrypted binary
197- Err ( _) => Ok ( Some ( Receiver {
198- state : self . extract_proposal_from_v2 ( response. body ( ) . to_vec ( ) ) ?,
199- } ) ) ,
193+ Err ( _) => Ok ( Some ( Receiver { state : self . extract_proposal_from_v2 ( body) ? } ) ) ,
200194 }
201195 }
202196
@@ -350,16 +344,7 @@ impl Receiver<UncheckedProposal> {
350344 body : & [ u8 ] ,
351345 context : ohttp:: ClientResponse ,
352346 ) -> 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- }
347+ process_post_res ( body, context) . map_err ( Into :: into)
363348 }
364349}
365350
@@ -648,15 +633,7 @@ impl Receiver<PayjoinProposal> {
648633 res : & [ u8 ] ,
649634 ohttp_context : ohttp:: ClientResponse ,
650635 ) -> 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- }
636+ process_post_res ( res, ohttp_context) . map_err ( Into :: into)
660637 }
661638}
662639
0 commit comments