use std::fmt;
use crate::{
header::{HeaderName, HeaderValue},
Request, Response,
};
use rama_core::{Context, Layer, Service};
use rama_utils::macros::define_inner_service_accessors;
use uuid::Uuid;
pub(crate) const X_REQUEST_ID: &str = "x-request-id";
pub trait MakeRequestId: Send + Sync + 'static {
fn make_request_id<B>(&self, request: &Request<B>) -> Option<RequestId>;
}
#[derive(Debug, Clone)]
pub struct RequestId(HeaderValue);
impl RequestId {
pub const fn new(header_value: HeaderValue) -> Self {
Self(header_value)
}
pub fn header_value(&self) -> &HeaderValue {
&self.0
}
pub fn into_header_value(self) -> HeaderValue {
self.0
}
}
impl From<HeaderValue> for RequestId {
fn from(value: HeaderValue) -> Self {
Self::new(value)
}
}
pub struct SetRequestIdLayer<M> {
header_name: HeaderName,
make_request_id: M,
}
impl<M: fmt::Debug> fmt::Debug for SetRequestIdLayer<M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("SetRequestIdLayer")
.field("header_name", &self.header_name)
.field("make_request_id", &self.make_request_id)
.finish()
}
}
impl<M: Clone> Clone for SetRequestIdLayer<M> {
fn clone(&self) -> Self {
Self {
header_name: self.header_name.clone(),
make_request_id: self.make_request_id.clone(),
}
}
}
impl<M> SetRequestIdLayer<M> {
pub const fn new(header_name: HeaderName, make_request_id: M) -> Self
where
M: MakeRequestId,
{
SetRequestIdLayer {
header_name,
make_request_id,
}
}
pub const fn x_request_id(make_request_id: M) -> Self
where
M: MakeRequestId,
{
SetRequestIdLayer::new(HeaderName::from_static(X_REQUEST_ID), make_request_id)
}
}
impl<S, M> Layer<S> for SetRequestIdLayer<M>
where
M: Clone + MakeRequestId,
{
type Service = SetRequestId<S, M>;
fn layer(&self, inner: S) -> Self::Service {
SetRequestId::new(
inner,
self.header_name.clone(),
self.make_request_id.clone(),
)
}
}
pub struct SetRequestId<S, M> {
inner: S,
header_name: HeaderName,
make_request_id: M,
}
impl<S: fmt::Debug, M: fmt::Debug> fmt::Debug for SetRequestId<S, M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SetRequestId")
.field("inner", &self.inner)
.field("header_name", &self.header_name)
.field("make_request_id", &self.make_request_id)
.finish()
}
}
impl<S: Clone, M: Clone> Clone for SetRequestId<S, M> {
fn clone(&self) -> Self {
SetRequestId {
inner: self.inner.clone(),
header_name: self.header_name.clone(),
make_request_id: self.make_request_id.clone(),
}
}
}
impl<S, M> SetRequestId<S, M> {
pub const fn new(inner: S, header_name: HeaderName, make_request_id: M) -> Self
where
M: MakeRequestId,
{
Self {
inner,
header_name,
make_request_id,
}
}
pub const fn x_request_id(inner: S, make_request_id: M) -> Self
where
M: MakeRequestId,
{
Self::new(
inner,
HeaderName::from_static(X_REQUEST_ID),
make_request_id,
)
}
define_inner_service_accessors!();
}
impl<State, S, M, ReqBody, ResBody> Service<State, Request<ReqBody>> for SetRequestId<S, M>
where
State: Clone + Send + Sync + 'static,
S: Service<State, Request<ReqBody>, Response = Response<ResBody>>,
M: MakeRequestId,
ReqBody: Send + 'static,
ResBody: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
async fn serve(
&self,
ctx: Context<State>,
mut req: Request<ReqBody>,
) -> Result<Self::Response, Self::Error> {
if let Some(request_id) = req.headers().get(&self.header_name) {
if req.extensions().get::<RequestId>().is_none() {
let request_id = request_id.clone();
req.extensions_mut().insert(RequestId::new(request_id));
}
} else if let Some(request_id) = self.make_request_id.make_request_id(&req) {
req.extensions_mut().insert(request_id.clone());
req.headers_mut()
.insert(self.header_name.clone(), request_id.0);
}
self.inner.serve(ctx, req).await
}
}
#[derive(Debug, Clone)]
pub struct PropagateRequestIdLayer {
header_name: HeaderName,
}
impl PropagateRequestIdLayer {
pub const fn new(header_name: HeaderName) -> Self {
PropagateRequestIdLayer { header_name }
}
pub const fn x_request_id() -> Self {
Self::new(HeaderName::from_static(X_REQUEST_ID))
}
}
impl<S> Layer<S> for PropagateRequestIdLayer {
type Service = PropagateRequestId<S>;
fn layer(&self, inner: S) -> Self::Service {
PropagateRequestId::new(inner, self.header_name.clone())
}
}
pub struct PropagateRequestId<S> {
inner: S,
header_name: HeaderName,
}
impl<S> PropagateRequestId<S> {
pub const fn new(inner: S, header_name: HeaderName) -> Self {
Self { inner, header_name }
}
pub const fn x_request_id(inner: S) -> Self {
Self::new(inner, HeaderName::from_static(X_REQUEST_ID))
}
define_inner_service_accessors!();
}
impl<S: fmt::Debug> fmt::Debug for PropagateRequestId<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PropagateRequestId")
.field("inner", &self.inner)
.field("header_name", &self.header_name)
.finish()
}
}
impl<S: Clone> Clone for PropagateRequestId<S> {
fn clone(&self) -> Self {
PropagateRequestId {
inner: self.inner.clone(),
header_name: self.header_name.clone(),
}
}
}
impl<State, S, ReqBody, ResBody> Service<State, Request<ReqBody>> for PropagateRequestId<S>
where
State: Clone + Send + Sync + 'static,
S: Service<State, Request<ReqBody>, Response = Response<ResBody>>,
ReqBody: Send + 'static,
ResBody: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
async fn serve(
&self,
ctx: Context<State>,
req: Request<ReqBody>,
) -> Result<Self::Response, Self::Error> {
let request_id = req
.headers()
.get(&self.header_name)
.cloned()
.map(RequestId::new);
let mut response = self.inner.serve(ctx, req).await?;
if let Some(current_id) = response.headers().get(&self.header_name) {
if response.extensions().get::<RequestId>().is_none() {
let current_id = current_id.clone();
response.extensions_mut().insert(RequestId::new(current_id));
}
} else if let Some(request_id) = request_id {
response
.headers_mut()
.insert(self.header_name.clone(), request_id.0.clone());
response.extensions_mut().insert(request_id);
}
Ok(response)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct MakeRequestUuid;
impl MakeRequestId for MakeRequestUuid {
fn make_request_id<B>(&self, _request: &Request<B>) -> Option<RequestId> {
let request_id = Uuid::new_v4().to_string().parse().unwrap();
Some(RequestId::new(request_id))
}
}
#[cfg(test)]
mod tests {
use crate::layer::set_header;
use crate::{Body, Response};
use rama_core::service::service_fn;
use rama_core::Layer;
use std::{
convert::Infallible,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
};
#[allow(unused_imports)]
use super::*;
#[tokio::test]
async fn basic() {
let svc = (
SetRequestIdLayer::x_request_id(Counter::default()),
PropagateRequestIdLayer::x_request_id(),
)
.layer(service_fn(handler));
let req = Request::builder().body(Body::empty()).unwrap();
let res = svc.serve(Context::default(), req).await.unwrap();
assert_eq!(res.headers()["x-request-id"], "0");
let req = Request::builder().body(Body::empty()).unwrap();
let res = svc.serve(Context::default(), req).await.unwrap();
assert_eq!(res.headers()["x-request-id"], "1");
let req = Request::builder()
.header("x-request-id", "foo")
.body(Body::empty())
.unwrap();
let res = svc.serve(Context::default(), req).await.unwrap();
assert_eq!(res.headers()["x-request-id"], "foo");
let req = Request::builder().body(Body::empty()).unwrap();
let res = svc.serve(Context::default(), req).await.unwrap();
assert_eq!(res.extensions().get::<RequestId>().unwrap().0, "2");
}
#[tokio::test]
async fn other_middleware_setting_request_id_on_response() {
let svc = (
SetRequestIdLayer::x_request_id(Counter::default()),
PropagateRequestIdLayer::x_request_id(),
set_header::SetResponseHeaderLayer::overriding(
HeaderName::from_static("x-request-id"),
HeaderValue::from_str("foo").unwrap(),
),
)
.layer(service_fn(handler));
let req = Request::builder()
.header("x-request-id", "foo")
.body(Body::empty())
.unwrap();
let res = svc.serve(Context::default(), req).await.unwrap();
assert_eq!(res.headers()["x-request-id"], "foo");
assert_eq!(res.extensions().get::<RequestId>().unwrap().0, "foo");
}
#[derive(Clone, Default)]
struct Counter(Arc<AtomicU64>);
impl MakeRequestId for Counter {
fn make_request_id<B>(&self, _request: &Request<B>) -> Option<RequestId> {
let id =
HeaderValue::from_str(&self.0.fetch_add(1, Ordering::AcqRel).to_string()).unwrap();
Some(RequestId::new(id))
}
}
async fn handler(_: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::empty()))
}
#[tokio::test]
async fn uuid() {
let svc = (
SetRequestIdLayer::x_request_id(MakeRequestUuid),
PropagateRequestIdLayer::x_request_id(),
)
.layer(service_fn(handler));
let req = Request::builder().body(Body::empty()).unwrap();
let mut res = svc.serve(Context::default(), req).await.unwrap();
let id = res.headers_mut().remove("x-request-id").unwrap();
id.to_str().unwrap().parse::<Uuid>().unwrap();
}
}