Skip to content

Commit 6fa93b1

Browse files
committed
Multi-domain fix #502
1 parent db114d2 commit 6fa93b1

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

server/src/config.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,30 @@ impl Config {
209209
/// Returns the server URL for a given request.
210210
/// If multi-tenancy is enabled and the host matches a subdomain of the base domain, it returns the host URL.
211211
pub fn get_server_url_for_request(&self, req: &actix_web::HttpRequest) -> String {
212-
if let Some(base) = &self.base_domain {
213-
if let Some(host) = req.head().headers.get("Host") {
214-
if let Ok(host_str) = host.to_str() {
215-
// Remove port if present
216-
let domain = host_str.split(':').next().unwrap_or(host_str);
217-
if domain.ends_with(base) {
218-
let schema =
219-
if let Some(proto) = req.head().headers.get("X-Forwarded-Proto") {
220-
proto.to_str().unwrap_or("http")
221-
} else if self.opts.https {
222-
"https"
223-
} else {
224-
"http"
225-
};
226-
return format!("{}://{}", schema, host_str);
227-
}
212+
let host_header = req
213+
.head()
214+
.headers
215+
.get("X-Forwarded-Host")
216+
.or_else(|| req.head().headers.get("Host"));
217+
218+
if let Some(host) = host_header {
219+
if let Ok(host_str) = host.to_str() {
220+
let domain = host_str.split(':').next().unwrap_or(host_str);
221+
let allowed = if let Some(base) = &self.base_domain {
222+
domain.ends_with(base)
223+
} else {
224+
true
225+
};
226+
227+
if allowed {
228+
let schema = if let Some(proto) = req.head().headers.get("X-Forwarded-Proto") {
229+
proto.to_str().unwrap_or("http")
230+
} else if self.opts.https {
231+
"https"
232+
} else {
233+
"http"
234+
};
235+
return format!("{}://{}", schema, host_str);
228236
}
229237
}
230238
}

server/src/handlers/single_page_app.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ use actix_web::HttpResponse;
99
pub async fn single_page(
1010
appstate: actix_web::web::Data<AppState>,
1111
path: actix_web::web::Path<String>,
12+
req: actix_web::HttpRequest,
1213
) -> AtomicServerResult<HttpResponse> {
14+
let server_url = appstate.config.get_server_url_for_request(&req);
15+
let store = appstate.store.clone_with_url(server_url.clone());
1316
let template = include_str!("../../assets_tmp/index.html");
1417
let csp_nonce = generate_nonce().map_err(|_e| "Failed to generate nonce")?;
15-
let subject = format!("{}/{}", appstate.store.get_server_url()?, path);
18+
let subject = format!("{}/{}", server_url, path);
1619
let meta_tags: MetaTags = if let Ok(resource_response) =
17-
appstate
18-
.store
19-
.get_resource_extended(&subject, true, &ForAgent::Public)
20+
store.get_resource_extended(&subject, true, &ForAgent::Public)
2021
{
2122
resource_response.into()
2223
} else {

server/src/handlers/web_sockets.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ pub async fn web_socket_handler(
4141
)?;
4242
tracing::debug!("Starting websocket for {}", for_agent);
4343

44+
let server_url = appstate.config.get_server_url_for_request(&req);
45+
let store = appstate.store.clone_with_url(server_url);
46+
4447
let result = ws::start(
4548
WebSocketConnection::new(
4649
appstate.commit_monitor.clone(),
4750
appstate.y_sync_broadcaster.clone(),
4851
for_agent,
4952
// We need to make sure this is easily clone-able
50-
appstate.store.clone(),
53+
store,
5154
),
5255
&req,
5356
stream,

0 commit comments

Comments
 (0)