@@ -23,32 +23,27 @@ int Respond::ft_check_file()
23
23
return (0 );
24
24
}
25
25
26
- void Respond::ft_handle_file ()
26
+ void Respond::ft_handle_file ()
27
27
{
28
28
std::ifstream file;
29
- if (strcmp (_rooted_path.c_str (), " " ))
29
+ if (! strcmp (_rooted_path.c_str (), " " ))
30
30
{
31
- file.open (_rooted_path.c_str ());
32
- if (file.is_open ())
33
- {
34
- _response_body = std::string ((std::istreambuf_iterator<char >(file)), std::istreambuf_iterator<char >());
35
- _status_code = 200 ;
36
- _status_message = " OK" ;
37
- _headers[" Content-Type" ] = " text/html" ;
38
- _headers[" Content-Length" ] = std::to_string (_response_body.length ());
39
- _headers[" Connection" ] = " keep-alive" ;
40
- }
41
- else
42
- {
43
- _status_code = 404 ;
44
- _status_message = " Not Found" ;
45
- }
31
+ handle_error_response (404 );
32
+ return ;
46
33
}
47
- else
34
+
35
+ file.open (_rooted_path.c_str (), std::ifstream::in);
36
+ if (file.is_open ())
48
37
{
49
- _status_code = 404 ;
50
- _status_message = " Not Found" ;
38
+ std::stringstream buffer;
39
+ buffer << file.rdbuf ();
40
+ _response_body = buffer.str ();
41
+ file.close ();
42
+ set_status_code (200 );
43
+ set_status_message (get_response_status (200 ));
51
44
}
45
+ else
46
+ handle_error_response (403 );
52
47
}
53
48
/*
54
49
Here's how it works:
@@ -58,32 +53,34 @@ Here's how it works:
58
53
In other words, the std::string constructor reads the entire contents of the file into a std::string object. The resulting std::string object contains all the characters from the file, including any whitespace characters and newline characters.
59
54
*/
60
55
61
- void Respond::ft_handle_index (std::vector<server> server)
56
+ int Respond::ft_handle_index (std::vector<server> server)
62
57
{
63
58
std::string index ;
64
-
65
- for (size_t i = 0 ; i < server.size (); i++)
66
- {
67
- for (size_t j = 0 ; j < server[i]._location .size (); j++)
68
- {
69
- if (_path_found == server[i]._location [j].location_name )
59
+
60
+ if (_path_found == server[_server_index]._location [_location_index].location_name )
70
61
{
71
- if (server[i ]._location [j ].get_index ().empty ())
62
+ if (server[_server_index ]._location [_location_index ].get_index ().empty ())
72
63
{
73
- if (server[i ].get_index ().empty ())
74
- handle_error_response (403 );
64
+ if (server[_server_index ].get_index ().empty ())
65
+ return ( handle_error_response (403 ) );
75
66
else
76
67
{
77
- index = server[i].get_index ();
78
- _rooted_path = server[i].get_root () + _removed_path + index ;
79
- ft_handle_index_2 ();
68
+ index = server[_server_index].get_index ();
69
+ _rooted_path = server[_server_index].get_root () + _removed_path + index ;
70
+ if (ft_handle_index_2 ())
71
+ return (1 );
72
+ else
73
+ return (0 );
80
74
}
81
75
}
82
76
else
83
77
{
84
- index = server[i]._location [j].get_index ();
85
- _rooted_path = server[i].get_root () + _removed_path + index ;
86
- ft_handle_index_2 ();
78
+ index = server[_server_index]._location [_location_index].get_index ();
79
+ _rooted_path = server[_location_index].get_root () + _removed_path + index ;
80
+ if (ft_handle_index_2 ())
81
+ return (1 );
82
+ else
83
+ return (0 );
87
84
}
88
85
}
89
86
}
@@ -100,21 +97,23 @@ void Respond::ft_handle_index_2()
100
97
{
101
98
_response_body = std::string ((std::istreambuf_iterator<char >(file)), std::istreambuf_iterator<char >());
102
99
set_status_code (200 );
103
- set_status_message (get_response_status (get_status_code () ));
100
+ set_status_message (get_response_status (200 ));
104
101
_headers[" Content-Type" ] = " text/html" ;
105
102
_headers[" Content-Length" ] = std::to_string (_response_body.length ());
106
103
_headers[" Connection" ] = " keep-alive" ;
104
+ set_date ();
105
+ set_cache_control (" no cache" );
107
106
}
108
107
else
109
108
{
110
- _status_code = 404 ;
111
- _status_message = " Not Found " ;
109
+ handle_error_response ( 403 ) ;
110
+ return ( 1 ) ;
112
111
}
113
112
}
114
113
else
115
114
{
116
- _status_code = 404 ;
117
- _status_message = " Not Found " ;
115
+ handle_error_response ( 404 ) ;
116
+ return ( 1 ) ;
118
117
}
119
118
}
120
119
@@ -151,6 +150,8 @@ void Respond::ft_handle_error(int error_code)
151
150
_headers[" Content-Type" ] = " text/html" ;
152
151
_headers[" Content-Length" ] = _response_body.length ();
153
152
_headers[" Connection" ] = " keep-alive" ;
153
+ set_date ();
154
+ set_cache_control (" no cache" );
154
155
}
155
156
156
157
void Respond::ft_show_autoindex ()
0 commit comments