11//! Method Logic module is response for structs and methods that define how parsed HTTP methods should be handled
22use std:: collections:: HashMap ;
3- use std:: hash:: Hash ;
43use std:: str;
54
65use super :: file_reader;
@@ -78,7 +77,7 @@ impl MethodLogic {
7877 Some ( host) => host,
7978 None => {
8079 return Response {
81- status : ResponseStatusCode :: NotFound ,
80+ status : ResponseStatusCode :: ImATeapot ,
8281 meta_data : HashMap :: new ( ) ,
8382 body : None ,
8483 }
@@ -167,7 +166,7 @@ impl MethodLogic {
167166 "woff" => ContentType :: Application ( Application :: woff) ,
168167 _ => {
169168 return Response {
170- status : ResponseStatusCode :: NotFound ,
169+ status : ResponseStatusCode :: UnsupportedMediaType ,
171170 meta_data : HashMap :: new ( ) ,
172171 body : None ,
173172 }
@@ -186,7 +185,115 @@ impl MethodLogic {
186185 }
187186 }
188187 _ => panic ! (
189- "default_get_logic logic should only used to handle Method::Get requests"
188+ "default_get_logic should only used to handle Method::Get requests"
189+ ) ,
190+ }
191+ }
192+ }
193+
194+ pub fn default_head_logic ( ) -> LogicFunc {
195+ |request : Method , server_settings : & ServerSetting , meta_data : & HashMap < String , String > | {
196+ match request {
197+ Method :: Get { file } => {
198+ let host: & str = match meta_data. get ( "host" ) {
199+ Some ( host) => host,
200+ None => {
201+ return Response {
202+ status : ResponseStatusCode :: ImATeapot ,
203+ meta_data : HashMap :: new ( ) ,
204+ body : None ,
205+ }
206+ }
207+ } ;
208+
209+ let ( host_path, allowed_extension) = match server_settings. paths . get ( host) {
210+ Some ( host_path) => ( & host_path. path , & host_path. allow ) ,
211+ None => {
212+ return Response {
213+ status : ResponseStatusCode :: NotFound ,
214+ meta_data : HashMap :: new ( ) ,
215+ body : None ,
216+ }
217+ }
218+ } ;
219+
220+ let path_buf = match file_reader:: parse ( & file, & host_path, allowed_extension) {
221+ Some ( path_buf) => path_buf,
222+ None => {
223+ match file_reader:: parse (
224+ "error\\ 404.html" ,
225+ & host_path,
226+ & allowed_extension,
227+ ) {
228+ Some ( file) => file,
229+ None => {
230+ return Response {
231+ status : ResponseStatusCode :: NotFound ,
232+ meta_data : HashMap :: new ( ) ,
233+ body : None ,
234+ }
235+ }
236+ }
237+ }
238+ } ;
239+
240+ let file_name = match path_buf. file_name ( ) {
241+ Some ( name) => name. to_str ( ) . unwrap ( ) ,
242+ None => panic ! (
243+ "FileReader::parse does not return file name. {:?}" ,
244+ path_buf
245+ ) ,
246+ } ;
247+
248+ match file_name {
249+ "404.html" => {
250+ return Response {
251+ status : ResponseStatusCode :: NotFound ,
252+ meta_data : HashMap :: new ( ) ,
253+ body : None ,
254+ }
255+ }
256+ _ => {
257+ let content_type = path_buf. extension ( ) . unwrap ( ) ;
258+ let content_type = content_type. to_str ( ) . unwrap ( ) ;
259+
260+ let content_type: ContentType = match content_type {
261+ "gif" => ContentType :: Image ( Image :: gif) ,
262+ "jpg" | "jpeg" | "jpe" | "jfif" => ContentType :: Image ( Image :: jpeg) ,
263+ "png" => ContentType :: Image ( Image :: png) ,
264+ "tif" | "tiff" => ContentType :: Image ( Image :: tiff) ,
265+ "css" => ContentType :: Text ( Text :: css) ,
266+ "csv" => ContentType :: Text ( Text :: csv) ,
267+ "html" => ContentType :: Text ( Text :: html) ,
268+ "js" => ContentType :: Text ( Text :: javascript) ,
269+ "xml" => ContentType :: Text ( Text :: xml) ,
270+ "mpeg" => ContentType :: Video ( Video :: mpeg) ,
271+ "mp4" => ContentType :: Video ( Video :: mp4) ,
272+ "webm" => ContentType :: Video ( Video :: webm) ,
273+ "svg" => ContentType :: Image ( Image :: svg_xml) ,
274+ "woff" => ContentType :: Application ( Application :: woff) ,
275+ _ => {
276+ return Response {
277+ status : ResponseStatusCode :: UnsupportedMediaType ,
278+ meta_data : HashMap :: new ( ) ,
279+ body : None ,
280+ }
281+ }
282+ } ;
283+
284+ Response {
285+ status : ResponseStatusCode :: Ok ,
286+ meta_data : HashMap :: from ( [
287+ ( "Cache-Control" . to_string ( ) , "private" . to_string ( ) ) ,
288+ ( "content-type" . to_string ( ) , content_type. to_string ( ) ) ,
289+ ] ) ,
290+ body : None ,
291+ }
292+ }
293+ }
294+ }
295+ _ => panic ! (
296+ "default_head_logic should only used to handle Method::Head requests"
190297 ) ,
191298 }
192299 }
0 commit comments