Skip to content

Commit

Permalink
[refactor] Calling directly hostcalls.add_map_value
Browse files Browse the repository at this point in the history
* Instead of passing the Filter instance of `response_headers_to_add`
* Matching the Auth Service

Signed-off-by: dd di cesare <didi@posteo.net>
  • Loading branch information
didierofrivia committed Oct 7, 2024
1 parent b115154 commit 725d043
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
6 changes: 1 addition & 5 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ impl Context for Filter {
let some_op = self.operation_dispatcher.borrow().get_operation(token_id);

if let Some(operation) = some_op {
GrpcService::process_grpc_response(
operation,
resp_size,
&mut self.response_headers_to_add,
);
GrpcService::process_grpc_response(operation, resp_size);
self.operation_dispatcher.borrow_mut().next();

if let Some(_op) = self.operation_dispatcher.borrow_mut().next() {
Expand Down
14 changes: 4 additions & 10 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ impl GrpcService {
&self.extension.failure_mode
}

pub fn process_grpc_response(
operation: Rc<Operation>,
resp_size: usize,
response_headers_to_add: &mut Vec<(String, String)>,
) {
pub fn process_grpc_response(operation: Rc<Operation>, resp_size: usize) {
let failure_mode = operation.get_failure_mode();
let res_body_bytes =
match hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, 0, resp_size).unwrap() {
Expand All @@ -79,11 +75,9 @@ impl GrpcService {
};
match operation.get_extension_type() {
ExtensionType::Auth => AuthService::process_auth_grpc_response(res, failure_mode),
ExtensionType::RateLimit => RateLimitService::process_ratelimit_grpc_response(
res,
failure_mode,
response_headers_to_add,
),
ExtensionType::RateLimit => {
RateLimitService::process_ratelimit_grpc_response(res, failure_mode)
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/service/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::service::grpc_message::{GrpcMessageResponse, GrpcMessageResult};
use crate::service::GrpcService;
use protobuf::{Message, RepeatedField};
use proxy_wasm::hostcalls;
use proxy_wasm::types::Bytes;
use proxy_wasm::types::{Bytes, MapType};

pub const RATELIMIT_SERVICE_NAME: &str = "envoy.service.ratelimit.v3.RateLimitService";
pub const RATELIMIT_METHOD_NAME: &str = "ShouldRateLimit";
Expand Down Expand Up @@ -37,7 +37,6 @@ impl RateLimitService {
pub fn process_ratelimit_grpc_response(
rl_resp: GrpcMessageResponse,
failure_mode: &FailureMode,
response_headers_to_add: &mut Vec<(String, String)>,
) {
match rl_resp {
GrpcMessageResponse::RateLimit(RateLimitResponse {
Expand All @@ -63,9 +62,14 @@ impl RateLimitService {
response_headers_to_add: additional_headers,
..
}) => {
for header in additional_headers {
response_headers_to_add.push((header.key, header.value));
}
additional_headers.iter().for_each(|header| {
hostcalls::add_map_value(
MapType::HttpResponseHeaders,
header.get_key(),
header.get_value(),
)
.unwrap()
});
}
_ => {}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/rate_limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@ fn it_passes_additional_headers() {
)
.expect_get_buffer_bytes(Some(BufferType::GrpcReceiveBuffer))
.returning(Some(&grpc_response))
.execute_and_expect(ReturnType::None)
.unwrap();

module
.call_proxy_on_response_headers(http_context, 0, false)
.expect_log(Some(LogLevel::Debug), Some("#2 on_http_response_headers"))
.expect_add_header_map_value(
Some(MapType::HttpResponseHeaders),
Some("test"),
Expand All @@ -393,6 +387,12 @@ fn it_passes_additional_headers() {
Some("other"),
Some("header value"),
)
.execute_and_expect(ReturnType::None)
.unwrap();

module
.call_proxy_on_response_headers(http_context, 0, false)
.expect_log(Some(LogLevel::Debug), Some("#2 on_http_response_headers"))
.execute_and_expect(ReturnType::Action(Action::Continue))
.unwrap();
}
Expand Down

0 comments on commit 725d043

Please sign in to comment.