Basic server

This commit is contained in:
CatAClock 2025-06-17 19:43:56 -07:00
parent 771dca26f2
commit 11e8cc5ad5
4 changed files with 64 additions and 3 deletions

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "FediverseServer"
version = "0.1.0"

11
src/404.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ouch!</title>
</head>
<body>
<h1>404</h1>
<p>Page not found.</p>
</body>
</html>

11
src/index.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>

View file

@ -1,5 +1,37 @@
use std::net;
#![allow(non_snake_case)]
use std::net::{TcpListener, TcpStream};
use std::io::{BufReader, prelude::*};
use std::fs;
fn main() {
println!("Hello, world!");
fn main() -> std::io::Result<()> {
let TCPlistener = TcpListener::bind("127.0.0.1:8080").unwrap();
for Stream in TCPlistener.incoming() {
ServePage(Stream?);
}
Ok(())
}
fn ServePage(mut Stream: TcpStream) {
// Identifying information.
let Buffer = BufReader::new(&Stream);
let HTTPrequest: Vec<_> = Buffer.lines().map(|result| result.unwrap()).take_while(|line| !line.is_empty()).collect();
println!("{HTTPrequest:#?}");
// Response.
#[allow(unused_assignments)]
let mut Status : &str = "";
#[allow(unused_assignments)]
let mut Contents : String = "".to_string();
if HTTPrequest[0] == "GET / HTTP/1.1" {
Status = "HTTP/1.1 200 OK";
Contents = fs::read_to_string("src/index.html").unwrap();
} else {
Status = "HTTP/1.1 404 Not Found";
Contents = fs::read_to_string("src/404.html").unwrap();
}
let Length = Contents.len();
let Response : String = format!("{Status}\r\nContent-Length: {Length}\r\n\r\n{Contents}");
Stream.write_all(Response.as_bytes()).unwrap();
}