Browse Source

header_pick method

master
nicolas-arnaud 2 years ago
parent
commit
d5b3037e36
  1. 15
      includes/Client.hpp
  2. 15
      includes/Route.hpp
  3. 11
      srcs/load/Route.cpp
  4. 26
      srcs/sock/Client.cpp

15
includes/Client.hpp

@ -13,12 +13,13 @@ class Client {
public: public:
Client(int fd, Master *parent); Client(int fd, Master *parent);
~Client(void); ~Client(void);
bool getRequest(string paquet); bool getRequest(string paquet);
bool parseHeader(); bool parseHeader();
void answer(Env *env); string header_pick(string key, int id);
bool check_method(Server *server, Route *route, string method); void answer(Env *env);
void send_cgi(string cgi, string path); bool check_method(Server *server, Route *route, string method);
void send_error(int error_code); void send_cgi(string cgi, string path);
void send_answer(string msg); void send_error(int error_code);
void send_answer(string msg);
friend class Master; friend class Master;
}; };

15
includes/Route.hpp

@ -16,13 +16,10 @@ class Route {
Route(Server *server, string location, JSONNode *datas); Route(Server *server, string location, JSONNode *datas);
~Route(void); ~Route(void);
string getLocation(void); string getLocation(void);
string getRoot(void); string getRoot(void);
string getReturn(void); string getReturn(void);
std::vector< string > getIndexsLst(void); Server *getServer(void);
std::vector< string > getHeadersLst(void); string getIndex(string uri, string path);
Server *getServer(void); string correctUri(string uri);
string getIndex(string uri, string path);
string correctUri(string uri);
friend class Socket;
}; };

11
srcs/load/Route.cpp

@ -33,11 +33,9 @@ Route::Route(Server *server, string location, JSONNode *datas)
Route::~Route(void) {} Route::~Route(void) {}
string Route::getLocation(void) { return _location; } string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; } string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; } string Route::getReturn(void) { return _ret; }
std::vector< string > Route::getIndexsLst(void) { return _indexs; }
std::vector< string > Route::getHeadersLst(void) { return _headers; }
string Route::getIndex(string uri, string path) { string Route::getIndex(string uri, string path) {
std::stringstream content; std::stringstream content;
@ -45,7 +43,6 @@ string Route::getIndex(string uri, string path) {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
struct stat info; struct stat info;
std::vector< string > indexs = getIndexsLst();
std::vector< string >::iterator it; std::vector< string >::iterator it;
if ((dir = opendir(path.c_str())) == NULL) if ((dir = opendir(path.c_str())) == NULL)
@ -56,7 +53,7 @@ string Route::getIndex(string uri, string path) {
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
continue; continue;
for (it = indexs.begin(); it < indexs.end(); it++) { for (it = _indexs.begin(); it < _indexs.end(); it++) {
if (entry->d_name == *it) if (entry->d_name == *it)
return (read_file(path + "/" + *it)); return (read_file(path + "/" + *it));
} }

26
srcs/sock/Client.cpp

@ -80,21 +80,29 @@ bool Client::check_method(Server *server, Route *route, string method) {
return (false); return (false);
} }
string Client::header_pick(string key, int id) {
if (_request[key].size() == 0)
throw std::runtime_error("The key is not in request's header.");
string ret = _request[key].at(id);
return ret;
}
void Client::answer(Env *env) { void Client::answer(Env *env) {
cout << "Method: " << _request["Method:"].at(0) << "\n"; string method = header_pick("Method:", 0);
cout << "URI: " << _request["Method:"].at(1) << "\n"; string uri = header_pick("Method:", 1);
cout << "Host: " << _request["Host:"].at(0) << "\n"; string host = header_pick("Method:", 0);
cout << "Method: " << method << "\n";
cout << "URI: " << uri << "\n";
cout << "Host: " << host << "\n";
string ret; string ret;
Server *server = _parent->choose_server(env, _request["Host:"].at(0)); Server *server = _parent->choose_server(env, host);
Route *route = server->choose_route(_request["Method:"].at(1)); Route *route = server->choose_route(uri);
string method = _request["Method:"].at(0);
if (check_method(server, route, method)) { if (check_method(server, route, method)) {
string path = route->correctUri(_request["Method:"].at(1)); string path = route->correctUri(uri);
string cgi = route->_cgi.size() ? route->_cgi[get_extension(path)] : ""; string cgi = route->_cgi.size() ? route->_cgi[get_extension(path)] : "";
if (cgi == "") { if (cgi == "") {
if ((ret = route->getIndex(_request["Method:"].at(1), path)) == if ((ret = route->getIndex(uri, path)) == "" &&
"" &&
(ret = read_file(path)) == "") (ret = read_file(path)) == "")
send_error(404); send_error(404);
else else

Loading…
Cancel
Save