Initial commit. Code panics.

This commit is contained in:
uzy lol 2024-04-02 14:15:35 -07:00
commit e81efdd6a9
9 changed files with 166 additions and 0 deletions

10
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

16
.idea/checkstyle-idea.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA" serialisationVersion="2">
<checkstyleVersion>10.14.0</checkstyleVersion>
<scanScope>JavaOnly</scanScope>
<copyLibs>true</copyLibs>
<option name="thirdPartyClasspath" />
<option name="activeLocationIds" />
<option name="locations">
<list>
<ConfigurationLocation id="bundled-sun-checks" type="BUNDLED" scope="All" description="Sun Checks">(bundled)</ConfigurationLocation>
<ConfigurationLocation id="bundled-google-checks" type="BUNDLED" scope="All" description="Google Checks">(bundled)</ConfigurationLocation>
</list>
</option>
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/udemy-course-server.iml" filepath="$PROJECT_DIR$/.idea/udemy-course-server.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.idea/copilot/chatSessions" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

11
src/http/method.rs Normal file
View File

@ -0,0 +1,11 @@
pub enum Method {
GET(String),
DELETE(u64),
POST,
PUT,
HEAD,
CONNECT,
OPTIONS,
TRACE,
PATCH,
}

5
src/http/mod.rs Normal file
View File

@ -0,0 +1,5 @@
pub use request::Request;
pub use method::Method;
pub mod request;
pub mod method;

32
src/http/request.rs Normal file
View File

@ -0,0 +1,32 @@
use super::method::Method;
use std::convert::TryFrom;
use std::io::Error;
use std::fmt::{Display, Debug};
pub struct Request {
path: String,
query_string: Option<String>,
method: Method,
}
impl Request {
}
impl TryFrom<&[u8]> for Request {
type Error = String;
fn try_from(buffer: &[u8]) -> Result<Self, Self::Error> {
todo!()
}
}
pub enum ParseError {
InvalidRequest,
InvalidEncoding,
InvalidProtocol
}
impl Error for ParseError {
}

65
src/server.rs Normal file
View File

@ -0,0 +1,65 @@
use std::io::Read;
use std::net::TcpListener;
use std::process;
use std::ptr::read;
use super::Request;
use std::convert::TryFrom;
use std::convert::TryInto;
/// A sample HTTP request looks like this:
/// ```
/// GET / HTTP/1.1\r\n
/// HEADERS \r\n
/// BODY
/// ```
pub struct Server {
address: String,
}
impl Server {
pub fn new(address: String) -> Self {
Self {
address,
}
}
pub fn run(self) {
let (ip, port) = self.address.split_at(self.address.find(":").unwrap());
let port = &port[1..];
let listener = match TcpListener::bind(&self.address) {
Ok(listener) => listener,
Err(e) => {
println!("Failed to bind to {} <- {}", self.address, e);
process::exit(1);
}
};
println!("Listening on {} at port {}", ip, port);
loop {
match listener.accept() {
Ok((mut stream, _)) => {
let mut buffer = [0; 1024];
match stream.read(&mut buffer) {
Ok(read_bytes) => {
println!("Read {} bytes", read_bytes);
println!("Received a request: {}", String::from_utf8_lossy(&buffer));
match Request::try_from(&buffer[..]){
Ok(request) => {},
Err(e) => println!("Failed to parse a request: {e}")
};
let result: &Result<Request, _> = &buffer[..].try_into();
}
Err(error) => {
println!("Failed to read: {}", error);
process::exit(1);
}
};
}
Err(e) => {
println!("Failed to establish a connection: {}", e);
}
}
}
}
}