Skip to main content

pingora_core/modules/http/
grpc_web.rs

1// Copyright 2026 Cloudflare, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16use crate::protocols::http::bridge::grpc_web::GrpcWebCtx;
17use std::ops::{Deref, DerefMut};
18
19/// gRPC-web bridge module, this will convert
20/// HTTP/1.1 gRPC-web requests to H2 gRPC requests
21#[derive(Default)]
22pub struct GrpcWebBridge(GrpcWebCtx);
23
24impl Deref for GrpcWebBridge {
25    type Target = GrpcWebCtx;
26
27    fn deref(&self) -> &Self::Target {
28        &self.0
29    }
30}
31
32impl DerefMut for GrpcWebBridge {
33    fn deref_mut(&mut self) -> &mut Self::Target {
34        &mut self.0
35    }
36}
37
38#[async_trait]
39impl HttpModule for GrpcWebBridge {
40    fn as_any(&self) -> &dyn std::any::Any {
41        self
42    }
43
44    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
45        self
46    }
47
48    async fn request_header_filter(&mut self, req: &mut RequestHeader) -> Result<()> {
49        self.0.request_header_filter(req);
50        Ok(())
51    }
52
53    async fn response_header_filter(
54        &mut self,
55        resp: &mut ResponseHeader,
56        _end_of_stream: bool,
57    ) -> Result<()> {
58        self.0.response_header_filter(resp);
59        Ok(())
60    }
61
62    fn response_trailer_filter(
63        &mut self,
64        trailers: &mut Option<Box<HeaderMap>>,
65    ) -> Result<Option<Bytes>> {
66        if let Some(trailers) = trailers {
67            return self.0.response_trailer_filter(trailers);
68        }
69        Ok(None)
70    }
71}
72
73/// The builder for gRPC-web bridge module
74pub struct GrpcWeb;
75
76impl HttpModuleBuilder for GrpcWeb {
77    fn init(&self) -> Module {
78        Box::new(GrpcWebBridge::default())
79    }
80}