@@ -3,15 +3,17 @@ use std::collections::HashMap;
3
3
use std:: io:: { BufRead , BufReader , Read , Write } ;
4
4
use std:: net:: { TcpListener , TcpStream } ;
5
5
use std:: ops:: Add ;
6
+ use crate :: cache:: server_cache;
6
7
use crate :: config:: Config ;
8
+ use crate :: content_manager:: content_parser;
7
9
use crate :: http:: http_status:: StatusCode ;
8
10
use crate :: response:: refera_response:: ReferaResponse ;
9
11
10
12
mod config;
11
- mod content_parser;
12
- mod server_cache;
13
13
mod response;
14
14
mod http;
15
+ mod cache;
16
+ mod content_manager;
15
17
16
18
fn main ( ) {
17
19
let conf = Config :: default_config ( ) ;
@@ -52,8 +54,6 @@ fn handle_http_req(request: &mut TcpStream) {
52
54
53
55
if method_url. get ( 0 ) . unwrap ( ) . contains ( "GET" ) {
54
56
resp = get_reply ( method_url. get ( 1 ) . unwrap ( ) , http_headers. 0 ) ;
55
- } else if method_url. get ( 0 ) . unwrap ( ) . contains ( "POST" ) {
56
- resp = post_reply ( & mut buf_reader, http_headers. 0 ) ;
57
57
} else {
58
58
resp = ReferaResponse :: new ( StatusCode :: BadRequest , None , Vec :: new ( ) )
59
59
}
@@ -64,28 +64,18 @@ fn get_reply(url: &str, headers: HashMap<String, String>) -> ReferaResponse {
64
64
let cached_file = server_cache:: file_lookup ( url) ;
65
65
if cached_file. is_none ( ) {
66
66
let file = content_parser:: get_file ( url) ;
67
- if !file. 0 . is_empty ( ) {
67
+ return if !file. 0 . is_empty ( ) {
68
68
server_cache:: insert_file ( url, & file) ;
69
- return ReferaResponse :: new ( StatusCode :: Ok , None , file. 0 . clone ( ) ) ;
70
- } else {
71
- return ReferaResponse :: new ( StatusCode :: NotFound , None , content_parser:: error_page ( ) ) ;
69
+ ReferaResponse :: new ( StatusCode :: Ok , None , file. 0 . clone ( ) )
70
+ } else {
71
+ ReferaResponse :: new ( StatusCode :: NotFound , None , content_parser:: error_page ( ) )
72
72
}
73
73
}
74
74
75
75
ReferaResponse :: new ( StatusCode :: Ok , None , cached_file. unwrap ( ) . 0 . clone ( ) )
76
76
}
77
77
78
78
79
- fn post_reply ( buf_reader : & mut BufReader < TcpStream > , headers : HashMap < String , String > ) -> ReferaResponse { //WIP - TODO
80
- let content_length_str = headers. get_key_value ( "Content-Length" ) . unwrap ( ) . 1 ;
81
- let mut buffer: Vec < u8 > = vec ! [ 0 ; content_length_str. trim( ) . parse:: <usize >( ) . unwrap( ) ] ;
82
- buf_reader. read_exact ( & mut buffer) . unwrap ( ) ;
83
-
84
- //let result = content_parser::post_content(buffer.clone(), "aa");
85
-
86
- ReferaResponse :: new ( StatusCode :: Ok , None , Vec :: new ( ) )
87
- }
88
-
89
79
fn determine_headers ( vector : & Vec < String > ) -> ( HashMap < String , String > , String ) {
90
80
let mut header_map = HashMap :: new ( ) ;
91
81
@@ -104,16 +94,7 @@ fn determine_headers(vector: &Vec<String>) -> (HashMap<String, String>, String)
104
94
105
95
106
96
107
- /*fn read(stream: &mut TcpStream) {
108
- let mut buf = vec![0; 1024];
109
- println!("Received {} bytes", stream.read(&mut buf).unwrap());
110
- let resp = ReferaResponse::new(StatusCode::Ok, None, Vec::new());
111
- stream.write_all(resp.as_u8().as_slice()).unwrap();
112
- }
113
-
114
-
115
-
116
-
97
+ /*
117
98
118
99
This causes the request to stack. Why?
119
100
According to stackoverflow, "read_to_string reads into String until EOF which will not happen until you close the stream from the writer side."
0 commit comments