Skip to content

Commit

Permalink
Fix header position parsing, fix automatic 502
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <eloi.demolis@clever-cloud.com>
  • Loading branch information
Wonshtrum committed Feb 12, 2025
1 parent c4d45e7 commit e77e822
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
9 changes: 5 additions & 4 deletions command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,14 @@ message RequestHttpFrontend {
repeated Header headers = 15;
}

enum Position {
FRONTEND = 1;
BACKEND = 2;
enum HeaderPosition {
REQUEST = 1;
RESPONSE = 2;
BOTH = 3;
}

message Header {
required Position position = 1;
required HeaderPosition position = 1;
required string key = 2;
required string val = 3;
}
Expand Down
28 changes: 23 additions & 5 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use crate::{
logging::AccessLogFormat,
proto::command::{
request::RequestType, ActivateListener, AddBackend, AddCertificate, CertificateAndKey,
Cluster, Header, HttpListenerConfig, HttpsListenerConfig, ListenerType,
Cluster, Header, HeaderPosition, HttpListenerConfig, HttpsListenerConfig, ListenerType,
LoadBalancingAlgorithms, LoadBalancingParams, LoadMetric, MetricsConfiguration, PathRule,
ProtobufAccessLogFormat, ProxyProtocolConfig, RedirectPolicy, RedirectScheme, Request,
RequestHttpFrontend, RequestTcpFrontend, RulePosition, ServerConfig, ServerMetricsConfig,
Expand Down Expand Up @@ -609,6 +609,14 @@ pub enum PathRuleType {
Equals,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HeaderConfig {
position: HeaderPosition,
key: String,
val: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FileClusterFrontendConfig {
Expand All @@ -635,7 +643,7 @@ pub struct FileClusterFrontendConfig {
pub rewrite_path: Option<String>,
pub rewrite_port: Option<u16>,
#[serde(default)]
pub headers: Vec<Header>,
pub headers: Vec<HeaderConfig>,
}

impl FileClusterFrontendConfig {
Expand Down Expand Up @@ -897,7 +905,7 @@ pub struct HttpFrontendConfig {
pub rewrite_host: Option<String>,
pub rewrite_path: Option<String>,
pub rewrite_port: Option<u16>,
pub headers: Vec<Header>,
pub headers: Vec<HeaderConfig>,
}

fn default_rule_position() -> RulePosition {
Expand All @@ -909,6 +917,16 @@ impl HttpFrontendConfig {
let mut v = Vec::new();

let tags = self.tags.clone().unwrap_or_default();
let headers = self
.headers
.iter()
.cloned()
.map(|h| Header {
position: h.position.into(),
key: h.key,
val: h.val,
})
.collect();

if self.key.is_some() && self.certificate.is_some() {
v.push(
Expand Down Expand Up @@ -946,7 +964,7 @@ impl HttpFrontendConfig {
rewrite_host: self.rewrite_host.clone(),
rewrite_path: self.rewrite_path.clone(),
rewrite_port: self.rewrite_port.map(|x| x as u32),
headers: self.headers.clone(),
headers,
})
.into(),
);
Expand All @@ -968,7 +986,7 @@ impl HttpFrontendConfig {
rewrite_host: self.rewrite_host.clone(),
rewrite_path: self.rewrite_path.clone(),
rewrite_port: self.rewrite_port.map(|x| x as u32),
headers: self.headers.clone(),
headers,
})
.into(),
);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/protocol/kawa_h1/answers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,9 @@ Sozu-Id: %REQUEST_ID\r
\"cluster_id\": \"%CLUSTER_ID\",
\"backend_id\": \"%BACKEND_ID\",
\"parsing_phase\": \"%PHASE\",
\"successfully_parsed\": \"%SUCCESSFULLY_PARSED\",
\"partially_parsed\": \"%PARTIALLY_PARSED\",
\"invalid\": \"%INVALID\"
\"successfully_parsed\": %SUCCESSFULLY_PARSED,
\"partially_parsed\": %PARTIALLY_PARSED,
\"invalid\": %INVALID
}
</pre>
<p>Response could not be parsed. %MESSAGE</p>
Expand Down
23 changes: 12 additions & 11 deletions lib/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use regex::bytes::Regex;
use sozu_command::{
logging::CachedTags,
proto::command::{
PathRule as CommandPathRule, PathRuleKind, Position, RedirectPolicy, RedirectScheme,
HeaderPosition, PathRule as CommandPathRule, PathRuleKind, RedirectPolicy, RedirectScheme,
RulePosition,
},
response::HttpFrontend,
Expand Down Expand Up @@ -852,16 +852,17 @@ impl Frontend {
let mut headers_request = Vec::new();
let mut headers_response = Vec::new();
for header in headers {
if header.position() == Position::Frontend {
headers_request.push(HeaderEdit {
key: header.key.clone().into_bytes().into(),
val: header.val.clone().into_bytes().into(),
});
} else {
headers_response.push(HeaderEdit {
key: header.key.clone().into_bytes().into(),
val: header.val.clone().into_bytes().into(),
});
let edit = HeaderEdit {
key: header.key.clone().into_bytes().into(),
val: header.val.clone().into_bytes().into(),
};
match header.position() {
HeaderPosition::Request => headers_request.push(edit),
HeaderPosition::Response => headers_response.push(edit),
HeaderPosition::Both => {
headers_request.push(edit.clone());
headers_response.push(edit);
}
}
}
Ok(Frontend {
Expand Down

0 comments on commit e77e822

Please sign in to comment.