a few lines in between classes
This commit is contained in:
parent
bb9fe7aec0
commit
a4b1902fa5
3 changed files with 43 additions and 7 deletions
|
|
@ -65,7 +65,9 @@ async fn handle_http_connection(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
["websocket"] => {
|
["websocket"] => {
|
||||||
return Ok(Some(WebsocketConnection::initialize_connection(req)?));
|
return Ok(Some(WebsocketConnection::initialize_connection(
|
||||||
|
req, stream,
|
||||||
|
)?));
|
||||||
}
|
}
|
||||||
[] => Response::new()
|
[] => Response::new()
|
||||||
.with_code(ResponseCode::PermanentRedirect)
|
.with_code(ResponseCode::PermanentRedirect)
|
||||||
|
|
|
||||||
|
|
@ -50,8 +50,8 @@ impl Connection {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Upgrade {
|
pub struct Upgrade {
|
||||||
protocol: Protocol,
|
pub protocol: Protocol,
|
||||||
version: Box<str>,
|
pub version: Box<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Upgrade {
|
impl FromStr for Upgrade {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,47 @@
|
||||||
use crate::request::Request;
|
use crate::{
|
||||||
|
request::{Connection, Protocol, Request, RequestHeader},
|
||||||
|
response::Response,
|
||||||
|
};
|
||||||
|
|
||||||
|
use tokio::io;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
pub struct WebsocketConnection {
|
pub struct WebsocketConnection {
|
||||||
steam: TcpStream,
|
stream: TcpStream,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebsocketConnection {
|
impl WebsocketConnection {
|
||||||
pub fn initialize_connection(req: Request) -> tokio::io::Result<Self> {
|
pub fn initialize_connection(req: Request, stream: TcpStream) -> tokio::io::Result<Self> {
|
||||||
todo!()
|
let (mut upgrade, mut connection, mut key_exists) = (false, false, false);
|
||||||
|
let mut key_val;
|
||||||
|
|
||||||
|
for i in req.headers {
|
||||||
|
match i {
|
||||||
|
RequestHeader::Upgrade(upgrad) => {
|
||||||
|
if let Some(upg) = upgrad.first()
|
||||||
|
&& upg.protocol == Protocol::Websocket
|
||||||
|
{
|
||||||
|
upgrade = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RequestHeader::Connection(con) => {
|
||||||
|
if con == Connection::Upgrade {
|
||||||
|
connection = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RequestHeader::Other { name, value } => {
|
||||||
|
if name == "Sec-WebSocket-Key".into() {
|
||||||
|
key_val = value.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !upgrade || !connection || !key_exists {
|
||||||
|
Ok(Self { stream })
|
||||||
|
} else {
|
||||||
|
Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong request"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue