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: use request host for hot-reload instead of window.location.host #500

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
- Updated CLI argument parser to clap v0.4.
- Reduce error to warning when processing a project without Cargo.toml and no `<link rel="rust"/>` (fixes #487)
- Add wasm-bindgen URLs for all supported architectures
- Changed hot-reload to use request host instead of `window.location.host`

### fixed
- Nested WS proxies - if `backend=ws://localhost:8000/ws` is set, queries for `ws://localhost:8080/ws/entityX` will be linked with `ws://localhost:8000/ws/entityX`
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ reqwest = { version = "0.11", default-features = false, features = [
"stream",
"trust-dns",
] }
http-body = "0.4"
hyper = "0.14"
seahash = "4"
serde = { version = "1", features = ["derive"] }
tar = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion src/autoreload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function () {
var protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
var url = protocol + '//' + window.location.host + '/_trunk/ws';
var url = protocol + '//' + '{{__TRUNK_ADDRESS__}}' + '/_trunk/ws';
var poll_interval = 5000;
var reload_upon_connect = () => {
window.setTimeout(
Expand Down
51 changes: 48 additions & 3 deletions src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use std::path::PathBuf;
use std::sync::Arc;

use anyhow::{Context, Result};
use axum::body::{self, Body};
use axum::body::{self, Body, Bytes};
use axum::extract::ws::{WebSocket, WebSocketUpgrade};
use axum::http::StatusCode;
use axum::http::response::Parts;
use axum::http::{
header::{CONTENT_LENGTH, CONTENT_TYPE, HOST},
Request, StatusCode,
};
use axum::middleware::Next;
use axum::response::Response;
use axum::routing::{get, get_service, Router};
use axum::Server;
Expand Down Expand Up @@ -227,7 +232,8 @@ fn router(state: Arc<State>, cfg: Arc<RtcServe>) -> Router {
tracing::error!(?error, "failed serving static file");
StatusCode::INTERNAL_SERVER_ERROR
})
.layer(TraceLayer::new_for_http()),
.layer(TraceLayer::new_for_http())
.layer(axum::middleware::from_fn(html_address_middleware)),
),
)
.route(
Expand Down Expand Up @@ -303,6 +309,45 @@ fn router(state: Arc<State>, cfg: Arc<RtcServe>) -> Router {
router
}

async fn html_address_middleware<B: std::fmt::Debug>(
request: Request<B>,
next: Next<B>,
) -> (Parts, Bytes) {
let uri = request.headers().get(HOST).cloned();
let response = next.run(request).await;
let (parts, body) = response.into_parts();

match hyper::body::to_bytes(body).await {
Err(_) => (parts, Bytes::default()),
Ok(bytes) => {
let (mut parts, mut bytes) = (parts, bytes);

if let Some(uri) = uri {
if parts
.headers
.get(CONTENT_TYPE)
.map(|t| t == "text/html")
.unwrap_or(false)
{
if let Ok(data_str) = std::str::from_utf8(&bytes) {
let data_str = data_str.replace(
"'{{__TRUNK_ADDRESS__}}'",
&uri.to_str()
.map(|s| format!("'{}'", s))
.unwrap_or_else(|_| "window.location.href".into()),
);
let bytes_vec = data_str.as_bytes().to_vec();
parts.headers.insert(CONTENT_LENGTH, bytes_vec.len().into());
bytes = Bytes::from(bytes_vec);
}
}
}

(parts, bytes)
}
}
}

async fn handle_ws(mut ws: WebSocket, state: Arc<State>) {
let mut rx = state.build_done_chan.subscribe();
tracing::debug!("autoreload websocket opened");
Expand Down
Loading