-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for gRPC-web module to bridge gRPC-web client requests to…
… gRPC server requests
- Loading branch information
1 parent
3d42cf0
commit b7ea433
Showing
11 changed files
with
623 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
9b92c0fed7b703c61415414c69ff196e9deb11eb | ||
761f676b044dcf0d34205f96921e3385ffac7810 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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. | ||
|
||
use super::*; | ||
use crate::protocols::http::bridge::grpc_web::GrpcWebCtx; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
/// gRPC-web bridge module, this will convert | ||
/// HTTP/1.1 gRPC-web requests to H2 gRPC requests | ||
#[derive(Default)] | ||
pub struct GrpcWebBridge(GrpcWebCtx); | ||
|
||
impl Deref for GrpcWebBridge { | ||
type Target = GrpcWebCtx; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for GrpcWebBridge { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl HttpModule for GrpcWebBridge { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any { | ||
self | ||
} | ||
|
||
async fn request_header_filter(&mut self, req: &mut RequestHeader) -> Result<()> { | ||
self.0.request_header_filter(req); | ||
Ok(()) | ||
} | ||
|
||
async fn response_header_filter( | ||
&mut self, | ||
resp: &mut ResponseHeader, | ||
_end_of_stream: bool, | ||
) -> Result<()> { | ||
self.0.response_header_filter(resp); | ||
Ok(()) | ||
} | ||
|
||
fn response_trailer_filter( | ||
&mut self, | ||
trailers: &mut Option<Box<HeaderMap>>, | ||
) -> Result<Option<Bytes>> { | ||
if let Some(trailers) = trailers { | ||
return self.0.response_trailer_filter(trailers); | ||
} | ||
Ok(None) | ||
} | ||
} | ||
|
||
/// The builder for gRPC-web bridge module | ||
pub struct GrpcWeb; | ||
|
||
impl HttpModuleBuilder for GrpcWeb { | ||
fn init(&self) -> Module { | ||
Box::new(GrpcWebBridge::default()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.