Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: header in http processor #1788

Merged
merged 10 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions fastn-core/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@
}

#[cfg(test)]
mod test {

Check failure on line 911 in fastn-core/src/http.rs

View workflow job for this annotation

GitHub Actions / Rust/JS Checks/Formatting

items after a test module
use actix_web::body::MessageBody;
use pretty_assertions::assert_eq;

Expand Down Expand Up @@ -1016,3 +1016,14 @@
Ok(())
}
}

static HEADER_KEY_PATTERN: once_cell::sync::Lazy<regex::Regex> =
once_cell::sync::Lazy::new(|| regex::Regex::new(r"\$header-(\w+)\$").unwrap());
harshdoesdev marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) fn match_header_key(header_key: &str) -> Option<String> {
if let Some(captures) = HEADER_KEY_PATTERN.captures(header_key) {
return captures.get(1).map(|c| c.as_str().to_string());
}

None
}
4 changes: 2 additions & 2 deletions fastn-core/src/library2022/processor/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn process(
.get_value(header.line_number, value.as_str())?
.to_json_string(doc, true)?
{
if let Some(key) = header.key.strip_prefix("header-") {
if let Some(key) = fastn_core::http::match_header_key(header.key.as_str()) {
conf.insert(key.to_string(), value);
continue;
}
Expand All @@ -96,7 +96,7 @@ pub async fn process(
.append_pair(header.key.as_str(), value.trim_matches('"'));
}
} else {
if let Some(key) = header.key.strip_prefix("header-") {
if let Some(key) = fastn_core::http::match_header_key(header.key.as_str()) {
conf.insert(key.to_string(), value);
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion ftd/src/ast/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,9 @@ impl VariableValue {
})
.map(|header| {
let key = header.get_key();
let header_key = if ftd::ast::utils::is_variable_mutable(key.as_str()) {
let header_key = if ftd::ast::utils::is_variable_mutable(key.as_str())
&& !ftd::ast::utils::is_header_key(key.as_str())
{
key.trim_start_matches(ftd::ast::utils::REFERENCE)
} else {
key.as_str()
Expand Down
5 changes: 5 additions & 0 deletions ftd/src/ast/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub(crate) fn get_css_and_fields_from_headers(
Ok((css, fields))
}

pub(crate) fn is_header_key(key: &str) -> bool {
key.starts_with(HEADER_KEY_START) && key.ends_with('$')
}

pub const REFERENCE: &str = "$";
pub const CLONE: &str = "*$";
pub const LOOP: &str = "$loop$";
Expand All @@ -82,3 +86,4 @@ pub const IN: &str = " in ";
pub const IF: &str = "if";
pub const FOR: &str = "for";
pub const PROCESSOR: &str = "$processor$";
pub const HEADER_KEY_START: &str = "$header-";
Loading