mirror of
https://github.com/dadada/portfs.git
synced 2025-06-08 01:53:56 +02:00
If file exists for port name, serve file
This commit is contained in:
parent
97ee529bc7
commit
38e6841f53
1 changed files with 26 additions and 2 deletions
28
src/main.rs
28
src/main.rs
|
@ -1,8 +1,32 @@
|
||||||
use std::net::{TcpListener, TcpStream, SocketAddr};
|
use std::net::{TcpListener, TcpStream, SocketAddr};
|
||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::io;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
fn handle_client(stream: TcpStream, addr: SocketAddr) {
|
fn handle_client(mut stream: TcpStream, addr: SocketAddr) {
|
||||||
println!("Hello, world!");
|
println!("Received connection from {:?}", addr.port());
|
||||||
|
let mut filename = addr.port().to_string();
|
||||||
|
if Path::new(&filename).exists() {
|
||||||
|
let mut buffer = File::open(&filename).unwrap();
|
||||||
|
match io::copy(&mut buffer, &mut stream) {
|
||||||
|
Ok(_written) => (),
|
||||||
|
Err(e) => println!("Failed to file {:?} to TCP stream: {:?}", filename, e),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut buffer = File::create(&filename).unwrap();
|
||||||
|
match io::copy(&mut stream, &mut buffer) {
|
||||||
|
Ok(_written) => {
|
||||||
|
filename.push('\n');
|
||||||
|
match stream.write_all(filename.as_bytes()) {
|
||||||
|
Err(e) => println!("Error writing to socket {:?}", e),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => println!("Failed to write TCP stream to file {:?} failed with {:?}", filename, e),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue