2019-12-07 23:59:24 +06:00
|
|
|
use std::future::Future;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-09-11 15:12:08 +02:00
|
|
|
use actix_service::{Service, Transform};
|
2019-09-18 06:16:47 +02:00
|
|
|
use actix_web::body::{BodySize, MessageBody, ResponseBody};
|
2019-09-18 04:37:35 +02:00
|
|
|
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
2019-09-18 06:16:47 +02:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-12-07 23:59:24 +06:00
|
|
|
use futures::future::{ok, Ready};
|
2019-09-11 15:12:08 +02:00
|
|
|
|
|
|
|
pub struct Logging;
|
|
|
|
|
|
|
|
impl<S: 'static, B> Transform<S> for Logging
|
|
|
|
where
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-09-18 04:37:35 +02:00
|
|
|
B: MessageBody + 'static,
|
2019-09-11 15:12:08 +02:00
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
2019-09-18 06:16:47 +02:00
|
|
|
type Response = ServiceResponse<BodyLogger<B>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
type Error = Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = LoggingMiddleware<S>;
|
2019-12-07 23:59:24 +06:00
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
2019-12-07 23:59:24 +06:00
|
|
|
ok(LoggingMiddleware { service })
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LoggingMiddleware<S> {
|
2019-09-18 04:37:35 +02:00
|
|
|
service: S,
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service for LoggingMiddleware<S>
|
|
|
|
where
|
2019-09-18 06:16:47 +02:00
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
B: MessageBody,
|
2019-09-11 15:12:08 +02:00
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
2019-09-18 06:16:47 +02:00
|
|
|
type Response = ServiceResponse<BodyLogger<B>>;
|
2019-09-11 15:12:08 +02:00
|
|
|
type Error = Error;
|
2019-09-18 06:16:47 +02:00
|
|
|
type Future = WrapperStream<S, B>;
|
2019-09-11 15:12:08 +02:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(cx)
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 04:37:35 +02:00
|
|
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
2019-09-18 06:16:47 +02:00
|
|
|
WrapperStream {
|
|
|
|
fut: self.service.call(req),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
#[pin_project::pin_project]
|
2019-09-18 06:16:47 +02:00
|
|
|
pub struct WrapperStream<S, B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
S: Service,
|
|
|
|
{
|
2019-12-07 23:59:24 +06:00
|
|
|
#[pin]
|
2019-09-18 06:16:47 +02:00
|
|
|
fut: S::Future,
|
|
|
|
_t: PhantomData<(B,)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Future for WrapperStream<S, B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
{
|
2019-12-07 23:59:24 +06:00
|
|
|
type Output = Result<ServiceResponse<BodyLogger<B>>, Error>;
|
2019-09-18 06:16:47 +02:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let res = futures::ready!(self.project().fut.poll(cx));
|
2019-09-18 06:16:47 +02:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
Poll::Ready(res.map(|res| {
|
|
|
|
res.map_body(move |_, body| {
|
|
|
|
ResponseBody::Body(BodyLogger {
|
|
|
|
body,
|
|
|
|
body_accum: BytesMut::new(),
|
|
|
|
})
|
2019-09-18 06:16:47 +02:00
|
|
|
})
|
2019-12-07 23:59:24 +06:00
|
|
|
}))
|
2019-09-18 06:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BodyLogger<B> {
|
|
|
|
body: ResponseBody<B>,
|
|
|
|
body_accum: BytesMut,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> Drop for BodyLogger<B> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("response body: {:?}", self.body_accum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: MessageBody> MessageBody for BodyLogger<B> {
|
|
|
|
fn size(&self) -> BodySize {
|
|
|
|
self.body.size()
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, Error>>> {
|
|
|
|
match self.body.poll_next(cx) {
|
|
|
|
Poll::Ready(Some(Ok(chunk))) => {
|
2019-09-18 06:16:47 +02:00
|
|
|
self.body_accum.extend_from_slice(&chunk);
|
2019-12-07 23:59:24 +06:00
|
|
|
Poll::Ready(Some(Ok(chunk)))
|
2019-09-18 06:16:47 +02:00
|
|
|
}
|
2019-12-07 23:59:24 +06:00
|
|
|
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
|
|
|
|
Poll::Ready(None) => Poll::Ready(None),
|
|
|
|
Poll::Pending => Poll::Pending,
|
2019-09-18 06:16:47 +02:00
|
|
|
}
|
2019-09-11 15:12:08 +02:00
|
|
|
}
|
|
|
|
}
|