Skip to content

Commit

Permalink
refactor: adjust add request header and response header
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Mar 30, 2024
1 parent 4c2bca6 commit 0d55a5b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "pingap"
version = "0.1.2"
version = "0.1.3"
authors = ["Tree Xie <[email protected]>"]
edition = "2021"
categories = ["http-server"]
categories = ["network-programming", "web-programming::http-server"]
keywords = ["proxy", "http", "gateway"]
description = "A reverse proxy like nginx"
license = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
- [ ] status:499 for client abort
- [x] support get pingap start time
- [ ] custom error for pingora error
- [ ] authentication for admin page
25 changes: 13 additions & 12 deletions docs/introduction_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,25 @@ Location支持配置对应的host与path规则,path支持以下的规则,权
在server中会根据所添加的所有location列表,计算对应的权重重新排序,location的计算权限逻辑如下:

```rust
pub fn get_weight(conf: &LocationConf) -> u8 {
pub fn get_weight(&self) -> u32 {
// path starts with
// = 8
// prefix(default) 4
// ~ 2
// host exist 1
let mut weighted: u8 = 0;
if let Some(path) = &conf.path {
// = 65536
// prefix(default) 32768
// ~ 16384
// host exist 8192
let mut weighted: u32 = 0;
if let Some(path) = &self.path {
if path.starts_with('=') {
weighted += 8;
weighted += 65536;
} else if path.starts_with('~') {
weighted += 2;
weighted += 16384;
} else {
weighted += 4;
weighted += 32768;
}
weighted += path.len() as u32;
};
if conf.host.is_some() {
weighted += 1;
if self.host.is_some() {
weighted += 8192;
}
weighted
}
Expand Down
19 changes: 15 additions & 4 deletions src/proxy/location.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::Upstream;
use crate::cache::{convert_headers, HttpHeader};
use crate::config::LocationConf;
use pingora::http::{RequestHeader, ResponseHeader};
use regex::Regex;
use snafu::{ResultExt, Snafu};
use std::sync::Arc;
Expand Down Expand Up @@ -134,12 +135,22 @@ impl Location {
None
}
#[inline]
pub fn get_proxy_headers(&self) -> Option<Vec<HttpHeader>> {
self.proxy_headers.clone()
pub fn insert_proxy_headers(&self, header: &mut RequestHeader) {
if let Some(arr) = &self.proxy_headers {
for (k, v) in arr {
// v validate for HeaderValue, so always no error
let _ = header.insert_header(k, v);
}
}
}
#[inline]
pub fn get_header(&self) -> Option<Vec<HttpHeader>> {
self.headers.clone()
pub fn insert_headers(&self, header: &mut ResponseHeader) {
if let Some(arr) = &self.headers {
for (k, v) in arr {
// v validate for HeaderValue, so always no error
let _ = header.insert_header(k, v);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/proxy/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl Parser {
#[cfg(test)]
mod tests {
use super::Parser;
use crate::proxy::state::State;
use crate::state::State;
use pingora::http::RequestHeader;
use pretty_assertions::assert_eq;
use std::collections::HashMap;
Expand Down
24 changes: 6 additions & 18 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,9 @@ impl ProxyHttp for Server {
new_path = format!("{new_path}?{query}");
}
// TODO parse error
if let Ok(uri) = new_path.parse::<http::Uri>() {
header.set_uri(uri);
}
}
if let Some(arr) = lo.get_proxy_headers() {
for (k, v) in arr {
// v validate for HeaderValue, so always no error
let _ = header.insert_header(k, v);
}
let _ = new_path.parse::<http::Uri>().map(|uri| header.set_uri(uri));
}
lo.insert_proxy_headers(header);
let peer = lo
.upstream
.new_http_peer(ctx, header)
Expand Down Expand Up @@ -425,23 +418,18 @@ impl ProxyHttp for Server {
}
if let Some(index) = ctx.location_index {
if let Some(lo) = self.locations.get(index) {
if let Some(arr) = lo.get_header() {
for (k, v) in arr {
// v validate for HeaderValue, so always no error
let _ = upstream_response.insert_header(k, v);
}
}
lo.insert_headers(upstream_response)
}
}
if let Some(p) = &self.log_parser {
let mut m = HashMap::new();
for key in p.response_headers.iter() {
if let Some(value) = upstream_response.headers.get(key) {
upstream_response.headers.get(key).map(|value| {
m.insert(
key.to_string(),
value.to_str().unwrap_or_default().to_string(),
);
}
)
});
}
ctx.response_headers = Some(m);
}
Expand Down

0 comments on commit 0d55a5b

Please sign in to comment.