pingora_core/modules/http/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
// Copyright 2024 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Modules for HTTP traffic.
//!
//! [HttpModule]s define request and response filters to use while running an
//! [HttpServer](crate::apps::http_app::HttpServer)
//! application.
//! See the [ResponseCompression](crate::modules::http::compression::ResponseCompression)
//! module for an example of how to implement a basic module.
pub mod compression;
pub mod grpc_web;
use async_trait::async_trait;
use bytes::Bytes;
use http::HeaderMap;
use once_cell::sync::OnceCell;
use pingora_error::Result;
use pingora_http::{RequestHeader, ResponseHeader};
use std::any::Any;
use std::any::TypeId;
use std::collections::HashMap;
use std::sync::Arc;
/// The trait an HTTP traffic module needs to implement
#[async_trait]
pub trait HttpModule {
async fn request_header_filter(&mut self, _req: &mut RequestHeader) -> Result<()> {
Ok(())
}
async fn request_body_filter(
&mut self,
_body: &mut Option<Bytes>,
_end_of_stream: bool,
) -> Result<()> {
Ok(())
}
async fn response_header_filter(
&mut self,
_resp: &mut ResponseHeader,
_end_of_stream: bool,
) -> Result<()> {
Ok(())
}
fn response_body_filter(
&mut self,
_body: &mut Option<Bytes>,
_end_of_stream: bool,
) -> Result<()> {
Ok(())
}
fn response_trailer_filter(
&mut self,
_trailers: &mut Option<Box<HeaderMap>>,
) -> Result<Option<Bytes>> {
Ok(None)
}
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub type Module = Box<dyn HttpModule + 'static + Send + Sync>;
/// Trait to init the http module ctx for each request
pub trait HttpModuleBuilder {
/// The order the module will run
///
/// The lower the value, the later it runs relative to other filters.
/// If the order of the filter is not important, leave it to the default 0.
fn order(&self) -> i16 {
0
}
/// Initialize and return the per request module context
fn init(&self) -> Module;
}
pub type ModuleBuilder = Box<dyn HttpModuleBuilder + 'static + Send + Sync>;
/// The object to hold multiple http modules
pub struct HttpModules {
modules: Vec<ModuleBuilder>,
module_index: OnceCell<Arc<HashMap<TypeId, usize>>>,
}
impl HttpModules {
/// Create a new [HttpModules]
pub fn new() -> Self {
HttpModules {
modules: vec![],
module_index: OnceCell::new(),
}
}
/// Add a new [ModuleBuilder] to [HttpModules]
///
/// Each type of [HttpModule] can be only added once.
/// # Panic
/// Panic if any [HttpModule] is added more than once.
pub fn add_module(&mut self, builder: ModuleBuilder) {
if self.module_index.get().is_some() {
// We use a shared module_index the index would be out of sync if we
// add more modules.
panic!("cannot add module after ctx is already built")
}
self.modules.push(builder);
// not the most efficient way but should be fine
// largest order first
self.modules.sort_by_key(|m| -m.order());
}
/// Build the contexts of all the modules added to this [HttpModules]
pub fn build_ctx(&self) -> HttpModuleCtx {
let module_ctx: Vec<_> = self.modules.iter().map(|b| b.init()).collect();
let module_index = self
.module_index
.get_or_init(|| {
let mut module_index = HashMap::with_capacity(self.modules.len());
for (i, c) in module_ctx.iter().enumerate() {
let exist = module_index.insert(c.as_any().type_id(), i);
if exist.is_some() {
panic!("duplicated filters found")
}
}
Arc::new(module_index)
})
.clone();
HttpModuleCtx {
module_ctx,
module_index,
}
}
}
/// The Contexts of multiple modules
///
/// This is the object that will apply all the included modules to a certain HTTP request.
/// The modules are ordered according to their `order()`.
pub struct HttpModuleCtx {
// the modules in the order of execution
module_ctx: Vec<Module>,
// find the module in the vec with its type ID
module_index: Arc<HashMap<TypeId, usize>>,
}
impl HttpModuleCtx {
/// Create a placeholder empty [HttpModuleCtx].
///
/// [HttpModules] should be used to create nonempty [HttpModuleCtx].
pub fn empty() -> Self {
HttpModuleCtx {
module_ctx: vec![],
module_index: Arc::new(HashMap::new()),
}
}
/// Get a ref to [HttpModule] if any.
pub fn get<T: 'static>(&self) -> Option<&T> {
let idx = self.module_index.get(&TypeId::of::<T>())?;
let ctx = &self.module_ctx[*idx];
Some(
ctx.as_any()
.downcast_ref::<T>()
.expect("type should always match"),
)
}
/// Get a mut ref to [HttpModule] if any.
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
let idx = self.module_index.get(&TypeId::of::<T>())?;
let ctx = &mut self.module_ctx[*idx];
Some(
ctx.as_any_mut()
.downcast_mut::<T>()
.expect("type should always match"),
)
}
/// Run the `request_header_filter` for all the modules according to their orders.
pub async fn request_header_filter(&mut self, req: &mut RequestHeader) -> Result<()> {
for filter in self.module_ctx.iter_mut() {
filter.request_header_filter(req).await?;
}
Ok(())
}
/// Run the `request_body_filter` for all the modules according to their orders.
pub async fn request_body_filter(
&mut self,
body: &mut Option<Bytes>,
end_of_stream: bool,
) -> Result<()> {
for filter in self.module_ctx.iter_mut() {
filter.request_body_filter(body, end_of_stream).await?;
}
Ok(())
}
/// Run the `response_header_filter` for all the modules according to their orders.
pub async fn response_header_filter(
&mut self,
req: &mut ResponseHeader,
end_of_stream: bool,
) -> Result<()> {
for filter in self.module_ctx.iter_mut() {
filter.response_header_filter(req, end_of_stream).await?;
}
Ok(())
}
/// Run the `response_body_filter` for all the modules according to their orders.
pub fn response_body_filter(
&mut self,
body: &mut Option<Bytes>,
end_of_stream: bool,
) -> Result<()> {
for filter in self.module_ctx.iter_mut() {
filter.response_body_filter(body, end_of_stream)?;
}
Ok(())
}
/// Run the `response_trailer_filter` for all the modules according to their orders.
///
/// Returns an `Option<Bytes>` which can be used to write response trailers into
/// the response body. Note, if multiple modules attempt to write trailers into
/// the body the last one will be used.
///
/// Implementors that intend to write trailers into the body need to ensure their filter
/// is using an encoding that supports this.
pub fn response_trailer_filter(
&mut self,
trailers: &mut Option<Box<HeaderMap>>,
) -> Result<Option<Bytes>> {
let mut encoded = None;
for filter in self.module_ctx.iter_mut() {
if let Some(buf) = filter.response_trailer_filter(trailers)? {
encoded = Some(buf);
}
}
Ok(encoded)
}
}
#[cfg(test)]
mod tests {
use super::*;
struct MyModule;
#[async_trait]
impl HttpModule for MyModule {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
async fn request_header_filter(&mut self, req: &mut RequestHeader) -> Result<()> {
req.insert_header("my-filter", "1")
}
}
struct MyModuleBuilder;
impl HttpModuleBuilder for MyModuleBuilder {
fn order(&self) -> i16 {
1
}
fn init(&self) -> Module {
Box::new(MyModule)
}
}
struct MyOtherModule;
#[async_trait]
impl HttpModule for MyOtherModule {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
async fn request_header_filter(&mut self, req: &mut RequestHeader) -> Result<()> {
if req.headers.get("my-filter").is_some() {
// if this MyOtherModule runs after MyModule
req.insert_header("my-filter", "2")
} else {
// if this MyOtherModule runs before MyModule
req.insert_header("my-other-filter", "1")
}
}
}
struct MyOtherModuleBuilder;
impl HttpModuleBuilder for MyOtherModuleBuilder {
fn order(&self) -> i16 {
-1
}
fn init(&self) -> Module {
Box::new(MyOtherModule)
}
}
#[test]
fn test_module_get() {
let mut http_module = HttpModules::new();
http_module.add_module(Box::new(MyModuleBuilder));
http_module.add_module(Box::new(MyOtherModuleBuilder));
let mut ctx = http_module.build_ctx();
assert!(ctx.get::<MyModule>().is_some());
assert!(ctx.get::<MyOtherModule>().is_some());
assert!(ctx.get::<usize>().is_none());
assert!(ctx.get_mut::<MyModule>().is_some());
assert!(ctx.get_mut::<MyOtherModule>().is_some());
assert!(ctx.get_mut::<usize>().is_none());
}
#[tokio::test]
async fn test_module_filter() {
let mut http_module = HttpModules::new();
http_module.add_module(Box::new(MyOtherModuleBuilder));
http_module.add_module(Box::new(MyModuleBuilder));
let mut ctx = http_module.build_ctx();
let mut req = RequestHeader::build("Get", b"/", None).unwrap();
ctx.request_header_filter(&mut req).await.unwrap();
// MyModule runs before MyOtherModule
assert_eq!(req.headers.get("my-filter").unwrap(), "2");
assert!(req.headers.get("my-other-filter").is_none());
}
}