return http error from handshake

This commit is contained in:
Nikita Galaiko 2023-04-06 16:22:36 +02:00
parent 27fdd591d7
commit 57dfd89a28
3 changed files with 33 additions and 13 deletions

1
src-tauri/Cargo.lock generated
View File

@ -1296,6 +1296,7 @@ dependencies = [
"futures",
"futures-util",
"git2",
"http",
"log",
"md5",
"notify",

View File

@ -45,6 +45,7 @@ bytes = "1.1.0"
futures = "0.3"
futures-util = "0.3.8"
timed = "0.2.1"
http = "0.2.9"
[features]
# by default Tauri runs in production mode

View File

@ -7,8 +7,7 @@ use serde::Deserialize;
use std::env;
use std::io::{Read, Write};
use tokio::net;
use tokio_tungstenite;
use tokio_tungstenite::tungstenite::handshake::server::{Request, Response};
use tokio_tungstenite::tungstenite::handshake::server::{ErrorResponse, Request, Response};
const TERM: &str = "xterm-256color";
@ -31,19 +30,38 @@ pub async fn accept_connection(
stream: net::TcpStream,
) -> Result<()> {
let mut project = None;
let copy_uri_callback = |req: &Request, response: Response| {
let path = req.uri().path().to_string();
if let Some(project_id) = path.split("/").last() {
project = match projects_store.get_project(project_id) {
Ok(p) => p,
Err(e) => {
log::error!("failed to get project: {}", e);
None
let copy_uri_callback =
|req: &Request, response: Response| -> Result<Response, ErrorResponse> {
let path = req.uri().path().to_string();
let project_id = match path.split("/").last() {
Some(project_id) => project_id,
None => {
return Err(http::response::Response::builder()
.status(http::StatusCode::NOT_FOUND)
.body(None)
.unwrap());
}
};
}
Ok(response)
};
project = match projects_store.get_project(project_id) {
Ok(Some(p)) => Some(p),
Ok(None) => {
return Err(http::response::Response::builder()
.status(http::StatusCode::NOT_FOUND)
.body(None)
.unwrap());
}
Err(e) => {
log::error!("failed to get project: {}", e);
return Err(http::response::Response::builder()
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
.body(None)
.unwrap());
}
};
Ok(response)
};
let mut ws_stream = tokio_tungstenite::accept_hdr_async(stream, copy_uri_callback)
.await