pdk_core/host/context/
http.rs

1// Copyright (c) 2025, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5//! Middleware to fix missing metadata propagation in the filter context.
6
7use crate::policy_context::metadata::PolicyMetadata;
8use crate::policy_context::static_policy_context_cache::StaticPolicyContextCache;
9use classy::proxy_wasm::traits::{Context, HttpContext};
10use classy::proxy_wasm::types::Action;
11use std::rc::Rc;
12
13pub struct HttpContextAdapter {
14    http_context: Box<dyn HttpContext>,
15    policy_metadata: Rc<PolicyMetadata>,
16    plugin_name_api_id: Rc<String>,
17}
18
19impl HttpContextAdapter {
20    pub fn new(
21        http_context: Box<dyn HttpContext>,
22        policy_metadata: Rc<PolicyMetadata>,
23        plugin_name_api_id: Rc<String>,
24    ) -> Self {
25        Self {
26            http_context,
27            policy_metadata,
28            plugin_name_api_id,
29        }
30    }
31
32    pub fn boxed(self) -> Box<dyn HttpContext> {
33        Box::new(self)
34    }
35
36    fn fix_current_context(&self) {
37        StaticPolicyContextCache::fix_metadata(&self.policy_metadata);
38        StaticPolicyContextCache::fix_plugin_name_api_id(&self.plugin_name_api_id);
39    }
40}
41
42impl Context for HttpContextAdapter {
43    fn on_http_call_response(
44        &mut self,
45        token_id: u32,
46        num_headers: usize,
47        body_size: usize,
48        num_trailers: usize,
49    ) {
50        self.fix_current_context();
51        self.http_context
52            .on_http_call_response(token_id, num_headers, body_size, num_trailers)
53    }
54
55    fn on_grpc_call_response(&mut self, token_id: u32, status_code: u32, response_size: usize) {
56        self.fix_current_context();
57        self.http_context
58            .on_grpc_call_response(token_id, status_code, response_size)
59    }
60
61    fn on_done(&mut self) -> bool {
62        self.fix_current_context();
63        self.http_context.on_done()
64    }
65}
66
67impl HttpContext for HttpContextAdapter {
68    fn on_http_request_headers(&mut self, num_headers: usize, end_of_stream: bool) -> Action {
69        self.fix_current_context();
70        self.http_context
71            .on_http_request_headers(num_headers, end_of_stream)
72    }
73
74    fn on_http_request_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
75        self.fix_current_context();
76        self.http_context
77            .on_http_request_body(body_size, end_of_stream)
78    }
79
80    fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
81        self.fix_current_context();
82        self.http_context
83            .on_http_response_body(body_size, end_of_stream)
84    }
85
86    fn on_http_response_headers(&mut self, num_headers: usize, end_of_stream: bool) -> Action {
87        self.fix_current_context();
88        self.http_context
89            .on_http_response_headers(num_headers, end_of_stream)
90    }
91}