dataframe struc definition
This commit is contained in:
parent
145cbb9243
commit
8f12a03e46
5 changed files with 78 additions and 7 deletions
11
src/main.rs
11
src/main.rs
|
|
@ -37,9 +37,10 @@ async fn handle_connection(stream: TcpStream) -> tokio::io::Result<()> {
|
||||||
async fn handle_http_connection(
|
async fn handle_http_connection(
|
||||||
mut stream: TcpStream,
|
mut stream: TcpStream,
|
||||||
) -> tokio::io::Result<Option<websoket_connection::WebsocketConnection>> {
|
) -> tokio::io::Result<Option<websoket_connection::WebsocketConnection>> {
|
||||||
|
let mut timeout = 500;
|
||||||
loop {
|
loop {
|
||||||
let req = match time::timeout(
|
let req = match time::timeout(
|
||||||
Duration::from_millis(500),
|
Duration::from_millis(timeout),
|
||||||
request::Request::from_bufreader(&mut stream),
|
request::Request::from_bufreader(&mut stream),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
@ -65,9 +66,9 @@ async fn handle_http_connection(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
["websocket"] => {
|
["websocket"] => {
|
||||||
return Ok(Some(WebsocketConnection::initialize_connection(
|
return Ok(Some(
|
||||||
req, stream,
|
WebsocketConnection::initialize_connection(req, stream).await?,
|
||||||
)?));
|
));
|
||||||
}
|
}
|
||||||
[] => Response::new()
|
[] => Response::new()
|
||||||
.with_code(ResponseCode::PermanentRedirect)
|
.with_code(ResponseCode::PermanentRedirect)
|
||||||
|
|
@ -83,6 +84,8 @@ async fn handle_http_connection(
|
||||||
|
|
||||||
stream.flush().await?;
|
stream.flush().await?;
|
||||||
|
|
||||||
|
timeout = 5000;
|
||||||
|
|
||||||
if req.headers.contains(&request::RequestHeader::Connection(
|
if req.headers.contains(&request::RequestHeader::Connection(
|
||||||
request::Connection::Close,
|
request::Connection::Close,
|
||||||
)) {
|
)) {
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,15 @@ pub struct Upgrade {
|
||||||
pub version: Box<str>,
|
pub version: Box<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Upgrade {
|
||||||
|
pub fn to_str(&self) -> Box<str> {
|
||||||
|
match self.version.as_ref() {
|
||||||
|
"" => format!("{}", self.protocol.to_str()).into(),
|
||||||
|
_ => format!("{}/{}", self.protocol.to_str(), self.version).into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for Upgrade {
|
impl FromStr for Upgrade {
|
||||||
type Err = tokio::io::Error;
|
type Err = tokio::io::Error;
|
||||||
fn from_str(_s: &str) -> Result<Self, Self::Err> {
|
fn from_str(_s: &str) -> Result<Self, Self::Err> {
|
||||||
|
|
@ -67,6 +76,15 @@ pub enum Protocol {
|
||||||
Websocket,
|
Websocket,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Protocol {
|
||||||
|
pub fn to_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::HTTP => "HTTP",
|
||||||
|
Self::Websocket => "websocket",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for RequestHeader {
|
impl FromStr for RequestHeader {
|
||||||
type Err = tokio::io::Error;
|
type Err = tokio::io::Error;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{ffi::OsStr, path::Path};
|
use std::{ffi::OsStr, path::Path};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
request::{Connection, ServerPath},
|
request::{Connection, ServerPath, Upgrade},
|
||||||
shared_enums::{Content, ContentType},
|
shared_enums::{Content, ContentType},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -249,6 +249,11 @@ pub enum ResponseHeader {
|
||||||
CacheControl(CacheControl),
|
CacheControl(CacheControl),
|
||||||
Connection(Connection),
|
Connection(Connection),
|
||||||
Location(ServerPath),
|
Location(ServerPath),
|
||||||
|
Upgrade(Upgrade),
|
||||||
|
Other {
|
||||||
|
header_name: Box<str>,
|
||||||
|
header_value: Box<str>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResponseHeader {
|
impl ResponseHeader {
|
||||||
|
|
@ -259,6 +264,11 @@ impl ResponseHeader {
|
||||||
R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into(),
|
R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into(),
|
||||||
R::Connection(c) => format!("Connection: {}", c.to_str()).into(),
|
R::Connection(c) => format!("Connection: {}", c.to_str()).into(),
|
||||||
R::Location(l) => format!("Location: /{}", l.path.join("/")).into(),
|
R::Location(l) => format!("Location: /{}", l.path.join("/")).into(),
|
||||||
|
R::Other {
|
||||||
|
header_name,
|
||||||
|
header_value,
|
||||||
|
} => format!("{header_name}: {header_value}").into(),
|
||||||
|
R::Upgrade(upg) => format!("Upgrade: {}", upg.to_str()).into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -233,6 +233,7 @@ impl FromStr for ContentType {
|
||||||
["image", "webp"] => Ok(ContentType::Image(Image::Webp)),
|
["image", "webp"] => Ok(ContentType::Image(Image::Webp)),
|
||||||
["image", "svg"] | ["image", "svg+xml"] => Ok(ContentType::Image(Image::Svg)),
|
["image", "svg"] | ["image", "svg+xml"] => Ok(ContentType::Image(Image::Svg)),
|
||||||
["image", "*"] => Ok(ContentType::Image(Image::Any)),
|
["image", "*"] => Ok(ContentType::Image(Image::Any)),
|
||||||
|
["image", "jxl"] => Ok(ContentType::Image(Image::JpegXL)),
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
println!("{parts:?}");
|
println!("{parts:?}");
|
||||||
|
|
@ -250,6 +251,7 @@ pub enum Image {
|
||||||
Webp,
|
Webp,
|
||||||
Svg,
|
Svg,
|
||||||
Any,
|
Any,
|
||||||
|
JpegXL,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Image {
|
impl Image {
|
||||||
|
|
@ -260,6 +262,7 @@ impl Image {
|
||||||
Image::Jpeg => "jpeg",
|
Image::Jpeg => "jpeg",
|
||||||
Image::Webp => "webp",
|
Image::Webp => "webp",
|
||||||
Image::Svg => "svg",
|
Image::Svg => "svg",
|
||||||
|
Image::JpegXL => "jxl",
|
||||||
Image::Any => "*",
|
Image::Any => "*",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
request::{Connection, Protocol, Request, RequestHeader},
|
request::{Connection, Protocol, Request, RequestHeader, Upgrade},
|
||||||
response::Response,
|
response::Response,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -13,7 +13,30 @@ pub struct WebsocketConnection {
|
||||||
stream: TcpStream,
|
stream: TcpStream,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DataFrame {
|
||||||
|
is_final: bool,
|
||||||
|
extension_1: bool,
|
||||||
|
extension_2: bool,
|
||||||
|
extension_3: bool,
|
||||||
|
|
||||||
|
frame_type: FrameType,
|
||||||
|
|
||||||
|
is_masked: bool,
|
||||||
|
|
||||||
|
data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum FrameType {}
|
||||||
|
|
||||||
impl WebsocketConnection {
|
impl WebsocketConnection {
|
||||||
|
pub async fn send_message() -> io::Result<()> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_next_message() {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn initialize_connection(
|
pub async fn initialize_connection(
|
||||||
req: Request,
|
req: Request,
|
||||||
mut stream: TcpStream,
|
mut stream: TcpStream,
|
||||||
|
|
@ -54,7 +77,21 @@ impl WebsocketConnection {
|
||||||
let result = hasher.finalize();
|
let result = hasher.finalize();
|
||||||
let result = BASE64_STANDARD.encode(result);
|
let result = BASE64_STANDARD.encode(result);
|
||||||
|
|
||||||
Response::new().with_code(200).with_header(crate::response::ResponseHeader::Location)
|
Response::new()
|
||||||
|
.with_code(crate::response::ResponseCode::SwitchingProtocols)
|
||||||
|
.with_header(crate::response::ResponseHeader::Upgrade(Upgrade {
|
||||||
|
protocol: Protocol::Websocket,
|
||||||
|
version: "".into(),
|
||||||
|
}))
|
||||||
|
.with_header(crate::response::ResponseHeader::Connection(
|
||||||
|
Connection::Upgrade,
|
||||||
|
))
|
||||||
|
.with_header(crate::response::ResponseHeader::Other {
|
||||||
|
header_name: "Sec-WebSocket-Accept".into(),
|
||||||
|
header_value: result.into(),
|
||||||
|
})
|
||||||
|
.respond(&mut stream)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(Self { stream })
|
Ok(Self { stream })
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue