Skip to content

Commit 79aa226

Browse files
authored
cookie packing for backwards compat for < http2 (#27)
* cookie packing for backwards compat for < http2 * Increasing timeout
1 parent e1dca26 commit 79aa226

7 files changed

Lines changed: 103 additions & 17 deletions

File tree

src/web/helper.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use actix_web::web;
22
use base64::{Engine, prelude::BASE64_STANDARD};
3-
use mongodb::bson::{Bson, to_bson};
43
#[cfg(feature = "observe")]
54
use mongodb::bson;
5+
use mongodb::bson::{Bson, to_bson};
66
use rand::{Rng, rng};
77
use serde::Deserialize;
88
use tera::Context;
@@ -218,21 +218,34 @@ pub mod forwarding {
218218

219219
use crate::errors::{BridgeError, Result};
220220

221+
/// Configuration that is passed to the forward function takes the following parameters.
222+
/// inference filters out Auth specific headers to the proxy forward
223+
/// pack_cookies packs cookies to be compatible for http protocol older than 2
224+
/// updated_cookie will forward back to client any updated cookies
225+
#[derive(Default)]
226+
pub struct Config<'a> {
227+
pub inference: bool,
228+
pub pack_cookies: bool,
229+
pub updated_cookie: Option<Cookie<'a>>,
230+
}
231+
221232
// No inline needed... generic are inherently inlined
222-
#[allow(clippy::too_many_arguments)]
223233
pub async fn forward<T>(
224234
req: HttpRequest,
225235
mut payload: web::Payload,
226236
method: Method,
227237
peer_addr: Option<PeerAddr>,
228238
client: web::Data<reqwest::Client>,
229239
new_url: T,
230-
updated_cookie: Option<Cookie<'_>>,
231-
inference: bool,
240+
config: Config<'_>,
232241
) -> Result<HttpResponse>
233242
where
234243
T: AsRef<str> + Send + Sync,
235244
{
245+
let inference = config.inference;
246+
let pack_cookies = config.pack_cookies;
247+
let updated_cookie = config.updated_cookie;
248+
236249
let (tx, rx) = mpsc::channel(128);
237250

238251
actix_web::rt::spawn(async move {
@@ -292,6 +305,27 @@ pub mod forwarding {
292305
);
293306
}
294307

308+
// find all the cookie header and pack them delimited by ";"
309+
if pack_cookies {
310+
let mut cookies = String::new();
311+
for (header_name, header_value) in req.headers().iter() {
312+
if header_name.as_str().to_lowercase() == "cookie" {
313+
if let Ok(value) = header_value.to_str() {
314+
if !cookies.is_empty() {
315+
cookies.push(';');
316+
}
317+
cookies.push_str(value);
318+
}
319+
}
320+
}
321+
if !cookies.is_empty() {
322+
headers.insert(
323+
ReqwestHeaderName::from_static("cookie"),
324+
ReqwestHeaderValue::from_str(&cookies).unwrap(),
325+
);
326+
}
327+
}
328+
295329
let forwarded_req = forwarded_req.headers(headers);
296330

297331
let res = forwarded_req.send().await.map_err(|e| {

src/web/route/health/inference_services.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> InferenceServicesHealth<'a> {
6767
let now = Instant::now();
6868

6969
let fut = client.get(url.as_str()).send();
70-
let response = timeout(Duration::from_secs(1), fut).await.map_err(|_| {
70+
let response = timeout(Duration::from_secs(10), fut).await.map_err(|_| {
7171
BridgeError::GeneralError("Call to inference service timed out".to_string())
7272
})??;
7373

src/web/route/mcp/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
auth::jwt::validate_token,
1111
config::CONFIG,
1212
errors::{BridgeError, Result},
13-
web::{helper, services::CATALOG},
13+
web::{helper::{self, forwarding::Config}, services::CATALOG},
1414
};
1515

1616
const MCP_PREFIX: &str = "/mcp/";
@@ -68,7 +68,7 @@ async fn forward(
6868
}
6969
new_url.set_query(req.uri().query());
7070

71-
helper::forwarding::forward(req, payload, method, peer_addr, client, new_url, None, true)
71+
helper::forwarding::forward(req, payload, method, peer_addr, client, new_url, Config{inference: true, ..Default::default()})
7272
.await
7373
} else {
7474
warn!("MCP service not found in url request");

src/web/route/notebook/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::{
3737
kube::{KubeAPI, NOTEBOOK_NAMESPACE, Notebook, NotebookSpec, PVCSpec},
3838
web::{
3939
bridge_middleware::{CookieCheck, Htmx, NotebookCookieCheck},
40-
helper::{self, bson},
40+
helper::{self, bson, forwarding},
4141
},
4242
};
4343

@@ -672,7 +672,15 @@ async fn notebook_forward(
672672
new_url.set_query(req.uri().query());
673673

674674
helper::forwarding::forward(
675-
req, payload, method, peer_addr, client, new_url, None, false,
675+
req,
676+
payload,
677+
method,
678+
peer_addr,
679+
client,
680+
new_url,
681+
forwarding::Config {
682+
..Default::default()
683+
},
676684
)
677685
.await
678686
}

src/web/route/openwebui/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
config::CONFIG,
1515
db::models::OWUICookie,
1616
errors::{BridgeError, Result},
17-
web::helper,
17+
web::helper::{self, forwarding},
1818
};
1919

2020
const OWUI_PORT: &str = "8080";
@@ -90,7 +90,18 @@ async fn openwebui_forward(
9090

9191
url.set_query(req.uri().query());
9292

93-
helper::forwarding::forward(req, payload, method, peer_addr, client, url, None, false).await
93+
helper::forwarding::forward(
94+
req,
95+
payload,
96+
method,
97+
peer_addr,
98+
client,
99+
url,
100+
forwarding::Config {
101+
..Default::default()
102+
},
103+
)
104+
.await
94105
}
95106

96107
#[instrument(skip(payload))]
@@ -116,7 +127,18 @@ async fn moleviewer_forward(
116127
url.set_path(path);
117128
url.set_query(req.uri().query());
118129

119-
helper::forwarding::forward(req, payload, method, peer_addr, client, url, None, false).await
130+
helper::forwarding::forward(
131+
req,
132+
payload,
133+
method,
134+
peer_addr,
135+
client,
136+
url,
137+
forwarding::Config {
138+
..Default::default()
139+
},
140+
)
141+
.await
120142
}
121143

122144
#[inline]

src/web/route/proxy/mod.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use tracing::{error, instrument, warn};
44

55
use crate::{
66
errors::{BridgeError, Result},
7-
web::{bridge_middleware::validator, helper},
7+
web::{
8+
bridge_middleware::validator,
9+
helper::{self, forwarding::Config},
10+
},
811
};
912

1013
use self::services::CATALOG;
@@ -47,7 +50,19 @@ async fn forward(
4750
new_url.set_path(path);
4851
new_url.set_query(req.uri().query());
4952

50-
helper::forwarding::forward(req, payload, method, peer_addr, client, new_url, None, true).await
53+
helper::forwarding::forward(
54+
req,
55+
payload,
56+
method,
57+
peer_addr,
58+
client,
59+
new_url,
60+
Config {
61+
inference: true,
62+
..Default::default()
63+
},
64+
)
65+
.await
5166
}
5267

5368
pub fn config_proxy(cfg: &mut web::ServiceConfig) {

src/web/route/resource/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ use crate::{
1919
mongo::DB,
2020
},
2121
errors::{BridgeError, Result},
22-
web::{bridge_middleware::ResourceCookieCheck, helper, services::CATALOG},
22+
web::{
23+
bridge_middleware::ResourceCookieCheck,
24+
helper::{self, forwarding::Config},
25+
services::CATALOG,
26+
},
2327
};
2428

2529
static TOKEN_LIFETIME: usize = 60 * 60 * 24; // 24 hours
@@ -88,8 +92,11 @@ async fn resource_http(
8892
peer_addr,
8993
client,
9094
new_url,
91-
updated_cookie,
92-
false,
95+
Config {
96+
updated_cookie,
97+
pack_cookies: true,
98+
..Default::default()
99+
},
93100
)
94101
.await
95102
}

0 commit comments

Comments
 (0)