diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index 7ab8fb2..b7594d0 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -176,7 +176,7 @@ std::string Client::generateRespons(std::vector &configs) _response.generate2(connected_to); _toSend = new std::string; - *_toSend = _response.getHeader() + _response.getBody(); + *_toSend = (_response.getBody().empty()) ? _response.getHeader() : _response.getHeader() + _response.getBody(); len = _toSend->size(); @@ -184,7 +184,10 @@ std::string Client::generateRespons(std::vector &configs) _to_send_char = new char[len + 1]; std::memcpy(_to_send_char, _toSend->c_str(), len + 1); - + std::cout << PINK << "\n[["<< YELLOW << "Request header\n{" << ENDL; + std::cout << BLUE << _request.getHeader() << YELLOW << "},\n" << ENDL; + std::cout << GREEN << "Response Header\n{" << ENDL; + std::cout << BLUE << _response.getHeader() << GREEN << "}" << PINK << "]]\n"<< ENDL; delete _toSend; if (_request.getBody() != NULL) _request.freeData(); diff --git a/src/Client/Request.cpp b/src/Client/Request.cpp index 7005019..cf74ab5 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -108,6 +108,10 @@ ssize_t Request::getRecved(void) const { return (_received); } +std::string &Request::getHeader(void) +{ + return _head; +} void Request::setData(char *str) { this->_data = str; @@ -226,7 +230,6 @@ void Request::splitData(std::string &data) str = _head.substr(_headerSize); _head = tmp; _head_ok = true; - parseHeader(); if ((_contentLength == 0 && !_chunked) || (_method == "GET" || _method == "DELETE" || _method == "HEAD")) diff --git a/src/Client/Request.hpp b/src/Client/Request.hpp index 45f4160..3754b07 100644 --- a/src/Client/Request.hpp +++ b/src/Client/Request.hpp @@ -82,6 +82,7 @@ public: void clear(void); void splitData(std::string &); void freeData(void); + std::string &getHeader(void); void increaseRecvCounter(unsigned int n); ~Request(); diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index ade0982..470ecff 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -6,8 +6,8 @@ Response::Response() { initErrorCode(); _Autoindex = true; - _body = new std::string; - _header = new std::string; + _body = NULL; + _header = NULL; _code = 200; } @@ -15,7 +15,8 @@ Response::Response() void Response::freeData(void) { - delete _body; + if (_body != NULL) + delete _body; delete _header; } @@ -290,6 +291,7 @@ bool Response::allowedMethod(std::string &method) } void Response::generateHeader(void) { + _header = new std::string; std::stringstream ss; std::string tmp; @@ -298,8 +300,8 @@ void Response::generateHeader(void) ss << "Content-Type: " << _contentType << "\r\n"; ss << "Content-Length: " << _contentLength << "\r\n"; ss << "Server: " << _server << "\r\n"; - if (!_keepAlive.empty()) - ss << "Keep-Alive: " <<_keepAlive << "\r\n"; + // if (!_keepAlive.empty()) + // ss << "Keep-Alive: " <<_keepAlive << "\r\n"; ss << "Date: " << _date << "\r\n"; if (!_cacheControl.empty()) ss << "Cache-Control: " << _cacheControl << "\r\n"; @@ -374,6 +376,7 @@ bool Response::isRedirect() void Response::invalidClient(void) { + _body = new std::string; if (!isRedirect()) OpenErrorFile(_code); setHeaderBlocks(); @@ -385,6 +388,7 @@ void Response::invalidClient(void) void Response::methodGet(void) { + _body = new std::string; if (!_location->cgi_pass.empty()) { CgiHandle cgi(_request, *this); @@ -408,12 +412,13 @@ void Response::methodGet(void) generateBody(); setHeaderBlocks(); generateHeader(); - std::cout << GREEN << "GET method called\n" << ZERO_C; + DBOUT << GREEN << "GET method called\n" << ZERO_C; } void Response::methodPost(void) { if (!_location->cgi_pass.empty()) { + _body = new std::string; CgiHandle cgi(_request, *this); *_body = cgi.executeCgi(); diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index e7ae52f..0096fa0 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -132,6 +132,7 @@ void Server::readSocket(Client &client, int fd) // client.setRawData(buf); client.increaseRecvCounter(bytes_read); status = client.parseRequest(); + (void)status; // client_map[fd].printClientInfo(); if (client.allRecved()) @@ -252,7 +253,7 @@ void Server::start(void) { ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 5000); - // DBOUT << TURQ << "ready_num " << ready_num << ENDL;F + // DBOUT << TURQ << "ready_num " << ready_num << ENDL; if (ready_num < 0) throw std::logic_error("epoll_ret"); diff --git a/src/Server/ServerConfig.cpp b/src/Server/ServerConfig.cpp index 0fcc7bc..7c2b61b 100644 --- a/src/Server/ServerConfig.cpp +++ b/src/Server/ServerConfig.cpp @@ -383,6 +383,8 @@ void ServerConfig::checkConfig(void) it = _locations.begin(); location *tmp; + if (it == _locations.end()) + throw ConfigException("Required routing settings are missing!"); while (it != _locations.end()) { tmp = *it; @@ -402,43 +404,43 @@ void ServerConfig::printFields(void) it1 = _errorPages.begin(); it = _locations.begin(); - std::cout << RED << "-------------------------Server-Start----------------------------------\n" << ZERO_C; - std::cout << GREEN << "name" << " " << BLUE << _serverName << std::endl; - std::cout << GREEN << "host" << " " << BLUE << _host << std::endl; - std::cout << GREEN << "port" << " " << BLUE << _port << std::endl; - std::cout << GREEN << "client_body_size" << " " << BLUE << _clientBodySize << std::endl; - std::cout << GREEN << "location" << std::endl; + DBOUT << RED << "-------------------------Server-Start----------------------------------\n" << ZERO_C; + DBOUT << GREEN << "name" << " " << BLUE << _serverName << std::endl; + DBOUT << GREEN << "host" << " " << BLUE << _host << std::endl; + DBOUT << GREEN << "port" << " " << BLUE << _port << std::endl; + DBOUT << GREEN << "client_body_size" << " " << BLUE << _clientBodySize << std::endl; + DBOUT << GREEN << "location" << std::endl; while (it != _locations.end()) { - std::cout << PINK << "------------------------------------------------\n"; - std::cout << YELLOW << "location " << BLUE << (*it)->location <root <directoryFile <uploadDir <autoindex <uploadAccept <cgi_pass <clientBodySize <location <root <directoryFile <uploadDir <autoindex <uploadAccept <cgi_pass <clientBodySize <methods.begin(); while (it2 != (*it)->methods.end()) { - std::cout << BLUE << *it2 << " "; + DBOUT << BLUE << *it2 << " "; it2++; } - std::cout << std::endl; + DBOUT << std::endl; it3 = (*it)->redirect.begin(); - std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl; + DBOUT << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl; ++it; - std::cout << PINK << "------------------------------------------------\n"; + DBOUT << PINK << "------------------------------------------------\n"; } - std::cout << GREEN << "error pages" << std::endl; + DBOUT << GREEN << "error pages" << std::endl; while (it1 != _errorPages.end()) { - std::cout << YELLOW << it1->first << " " << BLUE << it1->second << std::endl; + DBOUT << YELLOW << it1->first << " " << BLUE << it1->second << std::endl; ++it1; } - std::cout << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C; + DBOUT << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C; } ServerConfig::~ServerConfig() diff --git a/src/main.cpp b/src/main.cpp index ec0d119..4bcb4af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ int main(int argc, char **argv) catch(const ConfigException& e) { server.end(); - std::cerr << RED << e.getMessage() << '\n' << ENDL; + std::cerr << RED << "\n" << e.getMessage() << '\n' << ENDL; } }