Skip to content

Commit

Permalink
Add headers from the rlp call to the response
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Mar 27, 2023
1 parent 03d0009 commit 0962430
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const RATELIMIT_METHOD_NAME: &str = "ShouldRateLimit";
pub struct Filter {
pub context_id: u32,
pub config: Rc<FilterConfig>,
pub headers: Vec<(String, String)>,
}

impl Filter {
Expand Down Expand Up @@ -304,6 +305,13 @@ impl HttpContext for Filter {
Some(rlp) => self.process_rate_limit_policy(rlp),
}
}

fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
for (name, value) in &self.headers {
self.add_http_response_header(name, value);
}
Action::Continue
}
}

impl Context for Filter {
Expand Down Expand Up @@ -334,16 +342,35 @@ impl Context for Filter {
}
};

match rl_resp.get_overall_code() {
RateLimitResponse_Code::UNKNOWN => {
match rl_resp {
RateLimitResponse {
overall_code: RateLimitResponse_Code::UNKNOWN,
..
} => {
request_process_failure(self.config.failure_mode_deny);
return;
}
RateLimitResponse_Code::OVER_LIMIT => {
self.send_http_response(429, vec![], Some(b"Too Many Requests\n"));
RateLimitResponse {
overall_code: RateLimitResponse_Code::OVER_LIMIT,
response_headers_to_add: rl_headers,
..
} => {
let mut headers = vec![];
for header in &rl_headers {
headers.push((header.get_key(), header.get_value()));
}
self.send_http_response(429, headers, Some(b"Too Many Requests\n"));
return;
}
RateLimitResponse_Code::OK => {}
RateLimitResponse {
overall_code: RateLimitResponse_Code::OK,
response_headers_to_add: headers,
..
} => {
for header in headers {
self.headers.push((header.key, header.value));
}
}
}
self.resume_http_request();
}
Expand Down
1 change: 1 addition & 0 deletions src/filter/root_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl RootContext for FilterRoot {
Some(Box::new(Filter {
context_id,
config: Rc::clone(&self.config),
headers: Vec::default(),
}))
}

Expand Down

0 comments on commit 0962430

Please sign in to comment.