use std::fmt;
use std::future::{Future, poll_fn};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use bytes::Bytes;
use http::header::{
ACCEPT_ENCODING, CACHE_CONTROL, CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE, HeaderValue,
};
use http::{Method, Request, Response, StatusCode};
use tower_service::Service;
use super::body::DevBody;
use super::reload::Shared;
pub const RELOAD_POLL_PATH: &str = "/__fetchlet/reload";
pub const RELOAD_SCRIPT_PATH: &str = "/__fetchlet/reload.js";
const RELOAD_SCRIPT: &str = include_str!("../../js/dev-reload.js");
const POLL_TIMEOUT: Duration = Duration::from_secs(25);
pub struct DevService<S> {
shared: Arc<Shared<S>>,
}
impl<S> Clone for DevService<S> {
fn clone(&self) -> Self {
Self {
shared: self.shared.clone(),
}
}
}
impl<S> fmt::Debug for DevService<S> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("DevService").finish_non_exhaustive()
}
}
impl<S> DevService<S> {
pub(super) fn new(shared: Arc<Shared<S>>) -> Self {
Self { shared }
}
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for DevService<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + Sync + 'static,
S::Future: Send + 'static,
S::Error: Send + 'static,
ReqBody: Send + 'static,
ResBody: http_body::Body<Data = Bytes>,
{
type Response = Response<DevBody<ResBody>>;
type Error = S::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, S::Error>> + Send>>;
fn poll_ready(&mut self, _context: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, mut request: Request<ReqBody>) -> Self::Future {
let get = request.method() == Method::GET;
if get && request.uri().path() == RELOAD_SCRIPT_PATH {
return Box::pin(async {
Ok(fixed_response(
Bytes::from_static(RELOAD_SCRIPT.as_bytes()),
"text/javascript",
))
});
}
if get && request.uri().path() == RELOAD_POLL_PATH {
let seen = seen_token(request.uri().query());
let mut tokens = self.shared.token.subscribe();
return Box::pin(async move {
let wait = tokens.wait_for(|token| Some(token.as_str()) != seen.as_deref());
let _ = tokio::time::timeout(POLL_TIMEOUT, wait).await;
let token = tokens.borrow().clone();
Ok(fixed_response(Bytes::from(token), "text/plain"))
});
}
let (mut service, token) = self.shared.current();
request.headers_mut().remove(ACCEPT_ENCODING);
let head = request.method() == Method::HEAD;
Box::pin(async move {
poll_fn(|context| service.poll_ready(context)).await?;
let response = service.call(request).await?;
Ok(inject(response, &token, head))
})
}
}
fn seen_token(query: Option<&str>) -> Option<String> {
query?
.split('&')
.find_map(|pair| pair.strip_prefix("token="))
.and_then(percent_decode)
}
fn percent_decode(value: &str) -> Option<String> {
let bytes = value.as_bytes();
let mut decoded = Vec::with_capacity(bytes.len());
let mut index = 0;
while index < bytes.len() {
if bytes[index] == b'%' {
let hex = std::str::from_utf8(bytes.get(index + 1..index + 3)?).ok()?;
decoded.push(u8::from_str_radix(hex, 16).ok()?);
index += 3;
} else {
decoded.push(bytes[index]);
index += 1;
}
}
String::from_utf8(decoded).ok()
}
fn percent_encode(value: &str) -> String {
let mut encoded = String::with_capacity(value.len());
for byte in value.bytes() {
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
encoded.push(char::from(byte));
} else {
encoded.push_str(&format!("%{byte:02X}"));
}
}
encoded
}
fn escape_attribute(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for character in value.chars() {
match character {
'&' => escaped.push_str("&"),
'"' => escaped.push_str("""),
'<' => escaped.push_str("<"),
'>' => escaped.push_str(">"),
other => escaped.push(other),
}
}
escaped
}
fn fixed_response<B>(body: Bytes, content_type: &'static str) -> Response<DevBody<B>> {
let mut response = Response::new(DevBody::full(body));
let headers = response.headers_mut();
headers.insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
headers.insert(CACHE_CONTROL, HeaderValue::from_static("no-store"));
response
}
fn inject<B>(response: Response<B>, token: &str, head: bool) -> Response<DevBody<B>> {
let (mut parts, body) = response.into_parts();
let html = parts
.headers
.get(CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.is_some_and(|value| {
value
.split(';')
.next()
.is_some_and(|kind| kind.trim().eq_ignore_ascii_case("text/html"))
});
let injectable = html
&& !head
&& parts.status != StatusCode::NOT_MODIFIED
&& !parts.headers.contains_key(CONTENT_ENCODING);
if !injectable {
return Response::from_parts(parts, DevBody::plain(body));
}
parts.headers.remove(CONTENT_LENGTH);
Response::from_parts(
parts,
DevBody::appended(body, Bytes::from(script_element(token))),
)
}
fn script_element(token: &str) -> String {
let source = format!("{RELOAD_SCRIPT_PATH}?token={}", percent_encode(token));
format!(
"<script type=\"module\" src=\"{}\"></script>",
escape_attribute(&source)
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_seen_token_comes_from_the_query() {
assert_eq!(seen_token(Some("token=ab-1")).as_deref(), Some("ab-1"));
assert_eq!(seen_token(Some("x=1&token=ab-2")).as_deref(), Some("ab-2"));
assert_eq!(seen_token(Some("x=1")), None);
assert_eq!(seen_token(None), None);
assert_eq!(
seen_token(Some("token=a%22%3C%20b")).as_deref(),
Some("a\"< b")
);
assert_eq!(
seen_token(Some("token=%E3%81%82")).as_deref(),
Some("\u{3042}")
);
for broken in ["token=%", "token=%4", "token=%zz", "token=%FF"] {
assert_eq!(seen_token(Some(broken)), None, "{broken}");
}
}
#[test]
fn tokens_round_trip_through_the_query() {
for token in ["ab-1", "a\"<b>&c", "x y/z?#", "\u{3042}"] {
assert_eq!(
seen_token(Some(&format!("token={}", percent_encode(token)))).as_deref(),
Some(token)
);
}
}
#[test]
fn the_element_escapes_the_token() {
let element = script_element("\"></script><script>alert(1)</script>&");
assert_eq!(
element,
"<script type=\"module\" src=\"/__fetchlet/reload.js?token=\
%22%3E%3C%2Fscript%3E%3Cscript%3Ealert%281%29%3C%2Fscript%3E%26\"></script>"
);
assert_eq!(escape_attribute("a&\"<>b"), "a&"<>b");
}
#[test]
fn the_element_loads_the_script_with_the_token() {
assert_eq!(
script_element("ab-3"),
"<script type=\"module\" src=\"/__fetchlet/reload.js?token=ab-3\"></script>"
);
}
#[test]
fn the_script_polls_the_poll_path() {
assert!(RELOAD_SCRIPT.contains(&format!("`{RELOAD_POLL_PATH}?token=")));
}
}