reading lines
This commit is contained in:
parent
07f8985f19
commit
320a93118f
2 changed files with 56 additions and 5 deletions
|
|
@ -3,7 +3,7 @@ mod response;
|
|||
mod shared_enums;
|
||||
|
||||
use std::{
|
||||
io::{BufRead, BufReader, Write},
|
||||
io::{BufReader, Write},
|
||||
net::TcpListener,
|
||||
};
|
||||
|
||||
|
|
@ -12,8 +12,9 @@ fn main() -> std::io::Result<()> {
|
|||
for incoming_stream in listener.incoming() {
|
||||
let stream = incoming_stream?;
|
||||
|
||||
let mut reader = BufReader::new(&stream);
|
||||
let mut line = Vec::<u8>::new();
|
||||
let reader = BufReader::new(&stream);
|
||||
|
||||
let req = request::Request::from_bufreader(reader)?;
|
||||
|
||||
let mut writer = stream;
|
||||
writer.write_all("Ok".as_bytes())?;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,16 @@
|
|||
use crate::shared_enums::ContentType;
|
||||
use std::{
|
||||
io::{self, BufRead, BufReader, Read},
|
||||
net::TcpStream,
|
||||
};
|
||||
|
||||
const MAX_LINE_WIDTH: u64 = 4096; // 4 KiB
|
||||
const MAX_BODY_LENGTH: u64 = 8388608; // 5 MiB
|
||||
|
||||
pub struct Request {
|
||||
first_line: String,
|
||||
method: Method,
|
||||
http_version: String,
|
||||
path: ServerPath,
|
||||
headers: Vec<RequestHeader>,
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
|
@ -15,5 +24,46 @@ pub enum RequestHeader {
|
|||
}
|
||||
|
||||
impl Request {
|
||||
fn from_bufreader
|
||||
pub fn from_bufreader(buffer: BufReader<&TcpStream>) -> io::Result<Self> {
|
||||
let mut first_line = Vec::<u8>::new();
|
||||
|
||||
let mut limited_buffer = buffer.take(MAX_LINE_WIDTH);
|
||||
limited_buffer.read_until(b'\n', &mut first_line)?;
|
||||
|
||||
println!("{}", String::from_utf8_lossy(&first_line));
|
||||
first_line.clear();
|
||||
|
||||
limited_buffer.set_limit(MAX_LINE_WIDTH);
|
||||
limited_buffer.read_until(b'\n', &mut first_line)?;
|
||||
|
||||
println!("{}", String::from_utf8_lossy(&first_line));
|
||||
|
||||
Ok(Self {
|
||||
method: Method::Get,
|
||||
http_version: "HTTP/1.1".to_string(),
|
||||
path: ServerPath {
|
||||
path: vec![],
|
||||
options: vec![],
|
||||
},
|
||||
headers: vec![],
|
||||
data: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ServerPath {
|
||||
path: Vec<String>,
|
||||
options: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
pub enum Method {
|
||||
Get,
|
||||
Post,
|
||||
Connect,
|
||||
Delete,
|
||||
Head,
|
||||
Options,
|
||||
Patch,
|
||||
Put,
|
||||
Trace,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue