martinthomson / ohttp

Rust library for encapsulating HTTP messages in a cryptographic wrapper
Apache License 2.0
22 stars 16 forks source link

Add support for custom request and response labels #25

Open chris-wood opened 1 year ago

chris-wood commented 1 year ago

For folks that want to reuse the encapsulation format it would be useful to be able to compile this crate with custom request and response labels. Right now, the crate mandates BHTTP as the underlying content. As a proof-of-concept, I implemented a protobuf-based encoding for HTTP messages and plumbed it into this crate in this change. It's not great... I basically want the equivalent of compile-time strings, but I don't know the best way to do that in Rust.

martinthomson commented 1 year ago

So the way to do this is to define a new trait on which you have a const, call it const INFO_BASE: &str. You implement that trait for OHTTP as follows:

pub trait InfoStr {
  const BASE: &'static str;
}
pub struct Bhttp();
impl InfoStr for Bhttp {
  const BASE: &'static str = "message/bhttp"
}

Then you make all of the implementation pieces generic over T: InfoStr.

The usage turns from new Client(...) to new Client<Bhttp>, which isn't a big change. The performance cost is manageable (I think that concatenating &'static str instances can be done by the compiler), so there is only a tiny compile time cost involved for monomorphism and no runtime cost.

Caveat: I haven't tested any of this by writing it. If you want to give it a spin, I am happy to review. Otherwise, I'll probably try this out at some later time.

chris-wood commented 1 year ago

Thanks @martinthomson. This sounds like a reasonable approach to me. If I get some time, I'll give it a shot.