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:
Client(int fd, Master *parent);
~Client(void);
bool getRequest(string paquet);
bool parseHeader();
void answer(Env *env);
bool check_method(Server *server, Route *route, string method);
void send_cgi(string cgi, string path);
void send_error(int error_code);
void send_answer(string msg);
bool getRequest(string paquet);
bool parseHeader();
string header_pick(string key, int id);
void answer(Env *env);
bool check_method(Server *server, Route *route, string method);
void send_cgi(string cgi, string path);
void send_error(int error_code);
void send_answer(string msg);
friend class Master;
};

15
includes/Route.hpp

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

11
srcs/load/Route.cpp

@ -33,11 +33,9 @@ Route::Route(Server *server, string location, JSONNode *datas)
Route::~Route(void) {}
string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; }
std::vector< string > Route::getIndexsLst(void) { return _indexs; }
std::vector< string > Route::getHeadersLst(void) { return _headers; }
string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; }
string Route::getIndex(string uri, string path) {
std::stringstream content;
@ -45,7 +43,6 @@ string Route::getIndex(string uri, string path) {
DIR *dir;
struct dirent *entry;
struct stat info;
std::vector< string > indexs = getIndexsLst();
std::vector< string >::iterator it;
if ((dir = opendir(path.c_str())) == NULL)
@ -56,7 +53,7 @@ string Route::getIndex(string uri, string path) {
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.')
continue;
for (it = indexs.begin(); it < indexs.end(); it++) {
for (it = _indexs.begin(); it < _indexs.end(); it++) {
if (entry->d_name == *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);
}
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) {
cout << "Method: " << _request["Method:"].at(0) << "\n";
cout << "URI: " << _request["Method:"].at(1) << "\n";
cout << "Host: " << _request["Host:"].at(0) << "\n";
string method = header_pick("Method:", 0);
string uri = header_pick("Method:", 1);
string host = header_pick("Method:", 0);
cout << "Method: " << method << "\n";
cout << "URI: " << uri << "\n";
cout << "Host: " << host << "\n";
string ret;
Server *server = _parent->choose_server(env, _request["Host:"].at(0));
Route *route = server->choose_route(_request["Method:"].at(1));
string method = _request["Method:"].at(0);
Server *server = _parent->choose_server(env, host);
Route *route = server->choose_route(uri);
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)] : "";
if (cgi == "") {
if ((ret = route->getIndex(_request["Method:"].at(1), path)) ==
"" &&
if ((ret = route->getIndex(uri, path)) == "" &&
(ret = read_file(path)) == "")
send_error(404);
else

Loading…
Cancel
Save