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

fix(cli,serverless): send 404 if favicon doesn't exists #564

Merged
merged 1 commit into from
Feb 4, 2023
Merged
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
6 changes: 6 additions & 0 deletions .changeset/curvy-cameras-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/cli': patch
'@lagon/serverless': patch
---

Send 404 if favicon doesn't exists
13 changes: 11 additions & 2 deletions crates/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request as HyperRequest, Response as HyperResponse, Server};
use lagon_runtime::{options::RuntimeOptions, Runtime};
use lagon_runtime_http::{Request, RunResult};
use lagon_runtime_http::{Request, Response, RunResult};
use lagon_runtime_isolate::{options::IsolateOptions, Isolate};
use lagon_runtime_utils::assets::{find_asset, handle_asset};
use lagon_runtime_utils::response::{handle_response, ResponseEvent};
use lagon_runtime_utils::response::{handle_response, ResponseEvent, FAVICON_URL};
use log::{
set_boxed_logger, set_max_level, Level, LevelFilter, Log, Metadata, Record, SetLoggerError,
};
Expand Down Expand Up @@ -93,6 +93,8 @@ async fn handle_request(
let (tx, rx) = flume::unbounded();
let (index, assets) = content.lock().await.to_owned();

let is_favicon = url == FAVICON_URL;

if let Some(asset) = find_asset(url, &assets.keys().cloned().collect()) {
println!(" {}", input("Asset found"));

Expand All @@ -102,6 +104,13 @@ async fn handle_request(
};

tx.send_async(run_result).await.unwrap_or(());
} else if is_favicon {
tx.send_async(RunResult::Response(Response {
status: 404,
..Default::default()
}))
.await
.unwrap_or(());
} else {
match Request::from_hyper(req).await {
Ok(mut request) => {
Expand Down
4 changes: 4 additions & 0 deletions crates/runtime_utils/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub const PAGE_404: &str = include_str!("../public/404.html");
pub const PAGE_502: &str = include_str!("../public/502.html");
pub const PAGE_500: &str = include_str!("../public/500.html");

pub const FAVICON_URL: &str = "/favicon.ico";

pub enum ResponseEvent {
StreamData(usize),
StreamDoneNoDataError,
Expand Down Expand Up @@ -57,6 +59,8 @@ pub async fn handle_response<T: Send + Clone + 'static>(
response_tx.send_async(response).await.unwrap_or(());
}
RunResult::Stream(StreamResult::Data(bytes)) => {
on_event(ResponseEvent::StreamData(bytes.len()), data.clone());

if done {
on_event(ResponseEvent::StreamDoneDataError, data.clone());

Expand Down
11 changes: 9 additions & 2 deletions crates/serverless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request as HyperRequest, Response as HyperResponse, Server};
use lagon_runtime::{options::RuntimeOptions, Runtime};
use lagon_runtime_http::{Request, RunResult};
use lagon_runtime_http::{Request, Response, RunResult};
use lagon_runtime_isolate::{options::IsolateOptions, Isolate};
use lagon_runtime_utils::response::{handle_response, ResponseEvent, PAGE_404};
use lagon_runtime_utils::response::{handle_response, ResponseEvent, FAVICON_URL, PAGE_404};
use lagon_runtime_utils::{
assets::{find_asset, handle_asset},
Deployment,
Expand Down Expand Up @@ -153,6 +153,8 @@ async fn handle_request(
async move {
increment_counter!("lagon_requests", &thread_labels);

let is_favicon = url == FAVICON_URL;

if let Some(asset) = find_asset(url, &deployment.assets) {
let root = Path::new(env::current_dir().unwrap().as_path())
.join("deployments")
Expand All @@ -168,6 +170,11 @@ async fn handle_request(
};

tx.send_async(run_result).await.unwrap_or(());
} else if is_favicon {
tx.send_async(RunResult::Response(Response {
status: 404,
..Default::default()
})).await.unwrap_or(());
} else {
last_requests.write().await.insert(hostname.clone(), Instant::now());
increment_counter!("lagon_isolate_requests", &thread_labels);
Expand Down