35 lines
862 B
Rust
35 lines
862 B
Rust
use crate::handler::FileHandler;
|
|||
|
|
use common::{Request, RESPONSE_NO, RESPONSE_OK};
|
||
|
|
use std::io::{Error, Write};
|
||
|
|
use std::os::unix::net::UnixStream;
|
||
|
|
|
||
|
|
pub struct Server {
|
||
|
|
stream: UnixStream,
|
||
|
|
handler: FileHandler,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Server {
|
||
|
|
pub fn new(stream: UnixStream, handler: FileHandler) -> Result<Server, Error> {
|
||
|
|
Ok(Server { stream, handler })
|
||
|
|
}
|
||
|
|
|
||
|
|
fn communicate(&mut self) -> Result<(), Error> {
|
||
|
|
let request = Request::from_stream(&mut self.stream)?;
|
||
|
|
|
||
|
|
println!("userid: {}", request.userid);
|
||
|
|
if self.handler.check_auth(&request) {
|
||
|
|
self.stream.write_all(&RESPONSE_OK)?
|
||
|
|
} else {
|
||
|
|
self.stream.write_all(&RESPONSE_NO)?
|
||
|
|
}
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn handle_client(&mut self) {
|
||
|
|
if let Err(error) = self.communicate() {
|
||
|
|
eprint!("{error}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|