Browse Source

Some error fixing, parse of uri/methods

master
Walid Bekkal 2 years ago
parent
commit
3ebda14f2d
  1. 4
      includes/Socket.hpp
  2. 2
      srcs/load/Server.cpp
  3. 37
      srcs/load/Socket.cpp

4
includes/Socket.hpp

@ -10,6 +10,10 @@ class Socket {
string _tmp; string _tmp;
string _header; string _header;
string _content; string _content;
string _method;
string _uri;
string _host;
string _extension;
int answer(Env *env, string request); int answer(Env *env, string request);
void send_answer(string msg); void send_answer(string msg);

2
srcs/load/Server.cpp

@ -51,7 +51,7 @@ std::vector<Socket *> Server::get_sockets(JSONNode *server) {
} }
Route *Server::get_route(string uri) { Route *Server::get_route(string uri) {
cout << uri << "\n"; // cout << uri << "\n";
std::vector<string> req = split(uri, '/'); std::vector<string> req = split(uri, '/');
std::vector<string> root; std::vector<string> root;
for (std::map<string, Route *>::iterator rit = _routes.begin(); for (std::map<string, Route *>::iterator rit = _routes.begin();

37
srcs/load/Socket.cpp

@ -100,10 +100,6 @@ void Socket::refresh(Env *env) {
} }
bool Socket::waitHeader() { bool Socket::waitHeader() {
if (_header != "")
return true;
string header;
string content;
if (_tmp.length() < 1) if (_tmp.length() < 1)
return false; return false;
std::vector<string> lines = split(_tmp, '\n'); std::vector<string> lines = split(_tmp, '\n');
@ -112,35 +108,36 @@ bool Socket::waitHeader() {
it++) { it++) {
if (*it == "\r") if (*it == "\r")
is_valid = true; is_valid = true;
else if (!is_valid)
header += *it + "\n";
else
content += *it + "\n";
} }
if (!is_valid) if (!is_valid)
return false; return false;
if (content.length() > 0) _header = _tmp;
content.at(content.length() - 1) = '\0'; _tmp = "";
_header = header;
_tmp = content;
return true; return true;
} }
int Socket::answer(Env *env, string request) { int Socket::answer(Env *env, string request) {
_tmp += request; _tmp += request;
cout << "|===|request|===>" << _tmp << "|===||\n"; cout << "|===|request|===>" << _tmp << "|===||\n";
if (!waitHeader()) if (_header == "") {
return EXIT_FAILURE; waitHeader();
}
std::vector<string> lines = split(_header, '\n'); std::vector<string> lines = split(_header, '\n');
std::vector<string> head = split(lines.at(0), ' '); std::vector<string> head = split(lines.at(0), ' ');
string uri = head.at(1); this->_method = head.at(0);
cout << uri << "\n"; this->_uri = head.at(1);
for (std::vector<string>::iterator it = lines.begin(); it < lines.end(); it++)
if (it->find("Host:") != string::npos)
this->_host = it->substr(6);
cout << "Method: " << this->_method << "\n";
cout << "URI: " << this->_uri << "\n";
cout << "Host: " << this->_host << "\n";
string ret; string ret;
std::stringstream answer; std::stringstream answer;
answer << "HTTP/1.1"; answer << "HTTP/1.1";
Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1)); Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri); Route *route = server->get_route(this->_uri);
std::vector<string> headers; std::vector<string> headers;
if ((head.at(0) != "GET" && head.at(0) != "POST" && if ((head.at(0) != "GET" && head.at(0) != "POST" &&
@ -160,9 +157,9 @@ int Socket::answer(Env *env, string request) {
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); "HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
} }
string path = route->correctUri(uri); string path = route->correctUri(this->_uri);
cout << "Path: " << path << "\n"; cout << "Path: " << path << "\n";
ret = route->getIndex(uri, path); ret = route->getIndex(this->_uri, path);
if (ret == "") { if (ret == "") {
cout << "No index: lf file\n"; cout << "No index: lf file\n";
ret = read_file(path); ret = read_file(path);
@ -170,7 +167,7 @@ int Socket::answer(Env *env, string request) {
answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n" answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n"
: " 200 OK\r\n") : " 200 OK\r\n")
<< ret; << ret;
cout << "|===|Answer|===>" << answer.str() << "|===||\n"; cout << "|===|Answer|===|\n" << answer.str() << "|===|End of answer|===|\n";
send_answer(answer.str()); send_answer(answer.str());
_content = ""; _content = "";
_header = ""; _header = "";

Loading…
Cancel
Save