Merge remote-tracking branch 'origin/fara' into roman
2
Makefile
@@ -6,7 +6,7 @@ SANFLAGS = -fsanitize=address
|
||||
|
||||
# SANFLAGS = -fsanitize=leak
|
||||
|
||||
CXXFLAGS = -Wall -Wextra -Werror -g -std=c++98 $(SANFLAGS) -DDEBUG
|
||||
CXXFLAGS = -Wall -Wextra -Werror -g -std=c++98 -fno-limit-debug-info $(SANFLAGS) -DDEBUG
|
||||
|
||||
CPPFLAGS += -MD -MP
|
||||
|
||||
|
||||
70
config/simple.toml.tmp1
Normal file
@@ -0,0 +1,70 @@
|
||||
[[server]]
|
||||
name = "Edu"
|
||||
host = "127.0.0.1"
|
||||
port = 8080
|
||||
body_size_limit = 100000
|
||||
[server.error_page]
|
||||
400 = "/var/www/poheck/Edu/errorPages/error_400.html"
|
||||
403 = "/var/www/poheck/Edu/errorPages/error_403.html"
|
||||
404 = "/var/www/poheck/Edu/errorPages/error_404.html"
|
||||
[[server.location]]
|
||||
location = "/"
|
||||
root = "/var/www/poheck/Edu/"
|
||||
methods = ["GET"]
|
||||
autoindex = false
|
||||
directory_file = "index.html"
|
||||
[[server.location]]
|
||||
location = "/errorlist"
|
||||
root = "/var/www/poheck/Edu/errorPages/"
|
||||
methods = ["GET"]
|
||||
autoindex = false
|
||||
directory_file = "this.html"
|
||||
[[server.location]]
|
||||
location = "/images"
|
||||
root = "/var/www/poheck/Edu/images/"
|
||||
methods = ["GET", "POST", "DELETE"]
|
||||
autoindex = true
|
||||
directory_file = "oops.html"
|
||||
[[server.location]]
|
||||
location = "/cats"
|
||||
root = "/var/www/poheck/Edu/images/cats/"
|
||||
methods = ["GET", "POST", "DELETE"]
|
||||
autoindex = true
|
||||
[[server.location]]
|
||||
location = "/dogs"
|
||||
root = "/var/www/poheck/Edu/images/dogs/"
|
||||
methods = ["GET", "POST", "DELETE"]
|
||||
autoindex = true
|
||||
[[server.location]]
|
||||
location = "/docs"
|
||||
root = "/var/www/poheck/Edu/docs/"
|
||||
redirect = ["301","http::/127.0.0.1::8080/documents"]
|
||||
[[server.location]]
|
||||
location = "/documents"
|
||||
root = "/var/www/poheck/Edu/documents/"
|
||||
methods = ["GET", "DELETE"]
|
||||
autoindex = true
|
||||
upload_accept = true
|
||||
upload_dir = "/var/www/poheck/Edu/upload/"
|
||||
[[server.location]]
|
||||
location = "/upload"
|
||||
root = "/var/www/poheck/Edu/upload/"
|
||||
methods = ["POST", "DELETE"]
|
||||
autoindex = true
|
||||
upload_accept = true
|
||||
upload_dir = "/var/www/poheck/Edu/upload/"
|
||||
[[server]]
|
||||
name = "2222"
|
||||
host = "localhost"
|
||||
port = 8081
|
||||
body_size_limit = 65356
|
||||
[server.error_page]
|
||||
404 = "/var/www/poheck/errorPages/error_404.html"
|
||||
[[server.location]]
|
||||
location = "/root2/"
|
||||
root = "/var/www/html/jopa.html"
|
||||
methods = ["GET", "POST"]
|
||||
autoindex = true
|
||||
directory_file = "its_a_directory.html"
|
||||
upload_accept = false
|
||||
upload_dir = "/var/www/html/upload"
|
||||
@@ -27,6 +27,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
Client::Client()
|
||||
{
|
||||
allRead = false;
|
||||
_received = 0;
|
||||
this->_fd = -1;
|
||||
this->_sended = 0;
|
||||
}
|
||||
@@ -13,7 +12,6 @@ Client::Client()
|
||||
Client::Client(char *str)
|
||||
{
|
||||
allRead = false;
|
||||
_received = 0;
|
||||
this->_fd = -1;
|
||||
this->_buff = str;
|
||||
this->_sended = 0;
|
||||
@@ -23,7 +21,6 @@ Client::Client(char *str)
|
||||
Client::Client(char *str, ServerConfig *config)
|
||||
{
|
||||
allRead = false;
|
||||
_received = 0;
|
||||
this->_fd = -1;
|
||||
this->_config = config;
|
||||
this->_buff = str;
|
||||
@@ -60,7 +57,7 @@ unsigned int Client::getCounter(void) const
|
||||
|
||||
unsigned int Client::getRecvCounter(void) const
|
||||
{
|
||||
return _received;
|
||||
return _request.getRecved();
|
||||
}
|
||||
|
||||
void Client::setRawData(char *str)
|
||||
@@ -107,14 +104,8 @@ bool Client::allSended(void)
|
||||
|
||||
bool Client::allRecved(void)
|
||||
{
|
||||
if (_request.getContentLength() == _received)
|
||||
{
|
||||
std::cout << "contentLength, _received "
|
||||
<< _request.getContentLength()
|
||||
<< " " <<
|
||||
_received << std::endl;
|
||||
if (_request.getContentLength() == (_request.getRecved() - _request.getHeaderSize()))
|
||||
return (true);
|
||||
}
|
||||
else
|
||||
return (false);
|
||||
}
|
||||
@@ -126,9 +117,7 @@ void Client::increaseCounter(void)
|
||||
|
||||
void Client::increaseRecvCounter(unsigned int n)
|
||||
{
|
||||
if (_received == 0)
|
||||
_received -= _request.getHeaderSize();
|
||||
_received += n;
|
||||
_request.increaseRecvCounter(n);
|
||||
}
|
||||
|
||||
//Генерирует response. Далее респонс можно получить через функцию getStrToSend()
|
||||
@@ -136,7 +125,7 @@ int Client::sendResponse(int fd)
|
||||
{
|
||||
_response.setData(_request, _config);
|
||||
_response.generate();
|
||||
_headerToSend = _response.getClient();
|
||||
_headerToSend = _response.getHeader();
|
||||
_bodyToSend = _response.getBody();
|
||||
_ret = sendData(fd, _headerToSend + _bodyToSend);
|
||||
|
||||
@@ -147,7 +136,7 @@ std::string Client::generateRespons(void)
|
||||
{
|
||||
_response.setData(_request, _config);
|
||||
_response.generate();
|
||||
_headerToSend = _response.getClient();
|
||||
_headerToSend = _response.getHeader();
|
||||
_bodyToSend = _response.getBody();
|
||||
_toSend = _headerToSend + _bodyToSend;
|
||||
|
||||
@@ -160,7 +149,10 @@ std::string Client::generateRespons(void)
|
||||
|
||||
bool Client::readyToSend(void)
|
||||
{
|
||||
return(_request.ok());
|
||||
if (_request.ok())
|
||||
return(_request.ok());
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void Client::printClientInfo(void)
|
||||
@@ -170,24 +162,55 @@ void Client::printClientInfo(void)
|
||||
std::map<std::string, std::string>::iterator it1;
|
||||
|
||||
map = _request.getClientFields();
|
||||
std::cout << PINK << "request method = " << _request.getMethod() << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request URI = " << _request.getURI() << ZERO_C << std::endl;
|
||||
std::cout << PINK << "host = " << _request.getHost() << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request query = " << _request.getQuery() << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request http versioin = " << _request.getVersion() << ZERO_C << std::endl;
|
||||
// std::cout << YELLOW << "request Client:\n" << _buff << ZERO_C << std::endl;
|
||||
std::cout << BLUE << std::endl << "PARSED REQUEST" << ZERO_C << std::endl << std::endl;
|
||||
std::cout << GREEN << "request method = " << _request.getMethod() << ZERO_C << std::endl;
|
||||
std::cout << GREEN << "request URI = " << _request.getURI() << ZERO_C << std::endl;
|
||||
std::cout << GREEN << "host = " << _request.getHost() << ZERO_C << std::endl;
|
||||
std::cout << GREEN << "request query = " << _request.getQuery() << ZERO_C << std::endl;
|
||||
std::cout << GREEN << "request http versioin = " << _request.getVersion() << ZERO_C << std::endl;
|
||||
std::cout << GREEN << "content-length = " << _request.getContentLength() << ZERO_C << std::endl;
|
||||
std::cout << GREEN << "connection type = " << _request.getConnection() << ZERO_C << std::endl;
|
||||
|
||||
std::cout << BLUE << std::endl << "RESPONSE" << ZERO_C << std::endl << std::endl;
|
||||
std::cout << GREEN << _response.getHeader() << ZERO_C << std::endl;
|
||||
|
||||
std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl;
|
||||
for ( it = map.begin(); it != map.end() ; it++)
|
||||
{
|
||||
std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
|
||||
}
|
||||
std::cout << TURGUOISE << "Client BODY" << ZERO_C << std::endl;
|
||||
std::cout << GREEN << _request.getBody().size() << ZERO_C << std::endl;
|
||||
// std::cout << YELLOW << "request Client:\n" << _buff << ZERO_C << std::endl;
|
||||
// std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl;
|
||||
// for ( it = map.begin(); it != map.end() ; it++)
|
||||
// {
|
||||
// std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
|
||||
// }
|
||||
// std::cout << TURGUOISE << "Client BODY" << ZERO_C << std::endl;
|
||||
// std::cout << GREEN << _request.getBody().size() << ZERO_C << std::endl;
|
||||
/* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */
|
||||
|
||||
}
|
||||
|
||||
bool Client::isEmpty(void)
|
||||
{
|
||||
if (!_request.ok() && _request.getHeaderSize() == 0
|
||||
&& _request.getContentLength() == 0)
|
||||
return (true);
|
||||
else
|
||||
return (false);
|
||||
}
|
||||
|
||||
bool Client::TimeToDie(void)
|
||||
{
|
||||
struct timeval curTime;
|
||||
|
||||
gettimeofday(&curTime, NULL);
|
||||
if ((curTime.tv_sec - _time.tv_sec) >= _request.getLifeTime())
|
||||
return (true);
|
||||
else
|
||||
return (false);
|
||||
}
|
||||
|
||||
void Client::updateTimeToDie(void)
|
||||
{
|
||||
gettimeofday(&_time, NULL);
|
||||
}
|
||||
|
||||
void Client::clear(void)
|
||||
{
|
||||
_fd = -1;
|
||||
|
||||
@@ -17,8 +17,8 @@ private:
|
||||
private:
|
||||
int _ret;
|
||||
int _fd;
|
||||
struct timeval _time;
|
||||
unsigned int _sended;
|
||||
unsigned int _received;
|
||||
char *_buff;
|
||||
|
||||
|
||||
@@ -38,9 +38,10 @@ public:
|
||||
void setRawData(char *);
|
||||
void setFd(int);
|
||||
int getFd(void);
|
||||
unsigned int getRecvCounter(void) const;
|
||||
|
||||
|
||||
unsigned int getRecvCounter(void) const;
|
||||
bool isEmpty(void);
|
||||
bool TimeToDie(void);
|
||||
void updateTimeToDie(void);
|
||||
public:
|
||||
int parseRequest(void);
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ Request::Request()
|
||||
_chunked = false;
|
||||
_head_ok = false;
|
||||
_body_ok = false;
|
||||
_received = 0;
|
||||
_headerSize = 0;
|
||||
_lifeTime = 5;
|
||||
|
||||
}
|
||||
|
||||
Request::Request(char *str)
|
||||
@@ -19,8 +23,11 @@ Request::Request(char *str)
|
||||
_data = str;
|
||||
_head_ok = false;
|
||||
_body_ok = false;
|
||||
_received = 0;
|
||||
_chunked = false;
|
||||
_contentLength = 0;
|
||||
_headerSize = 0;
|
||||
_lifeTime = 5;
|
||||
}
|
||||
|
||||
//-------------------------------------------------Get/Set---------------------------------------
|
||||
@@ -57,6 +64,10 @@ std::string Request::getLocation(void)
|
||||
{
|
||||
return (_location);
|
||||
}
|
||||
std::string Request::getConnection(void)
|
||||
{
|
||||
return (_connection);
|
||||
}
|
||||
ServerConfig *Request::getConfig(void)
|
||||
{
|
||||
return (_config);
|
||||
@@ -65,6 +76,10 @@ int Request::getCode(void)
|
||||
{
|
||||
return (_ret);
|
||||
}
|
||||
int Request::getLifeTime(void)
|
||||
{
|
||||
return (_lifeTime);
|
||||
}
|
||||
std::map<std::string, std::string> Request::getClientFields(void)
|
||||
{
|
||||
return (_headerField);
|
||||
@@ -82,7 +97,10 @@ unsigned int Request::getHeaderSize(void) const
|
||||
{
|
||||
return (_headerSize);
|
||||
}
|
||||
|
||||
unsigned int Request::getRecved(void) const
|
||||
{
|
||||
return (_received);
|
||||
}
|
||||
void Request::setData(char *str)
|
||||
{
|
||||
this->_data = str;
|
||||
@@ -99,7 +117,10 @@ void Request::setConfig(ServerConfig *config)
|
||||
|
||||
//-------------------------------------------------Parsing---------------------------------------
|
||||
|
||||
|
||||
void Request::increaseRecvCounter(unsigned int n)
|
||||
{
|
||||
_received += n;
|
||||
}
|
||||
void Request::parseURI(std::string str)
|
||||
{
|
||||
std::string tmp;
|
||||
@@ -142,9 +163,10 @@ int Request::parseStartLine(std::string str)
|
||||
void Request::splitData(char *data)
|
||||
{
|
||||
int pos;
|
||||
std::stringstream ss;
|
||||
std::string str;
|
||||
|
||||
str = data;
|
||||
str = std::string(data);
|
||||
if (!_head_ok)
|
||||
{
|
||||
pos = str.find("\r\n\r\n");
|
||||
@@ -165,9 +187,11 @@ void Request::splitData(char *data)
|
||||
return ;
|
||||
else if (!_body_ok)
|
||||
{
|
||||
_body += str.substr(0, str.size());
|
||||
if (_body.size() >= _contentLength)
|
||||
_body += str;
|
||||
if ((_received - _headerSize) == _contentLength)
|
||||
{
|
||||
_body_ok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,8 +255,11 @@ void Request::copyFromMap()
|
||||
{
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
int pos;
|
||||
|
||||
//host
|
||||
_host = _headerField.find("host")->second;
|
||||
it = _headerField.find("host");
|
||||
if (it != _headerField.end())
|
||||
_host = it->second;
|
||||
|
||||
//content-lenght
|
||||
it = _headerField.find("content-length");
|
||||
@@ -246,6 +273,10 @@ void Request::copyFromMap()
|
||||
if ( pos != -1)
|
||||
_chunked = true;
|
||||
}
|
||||
//connection
|
||||
it = _headerField.find("connection");
|
||||
if (it != _headerField.end())
|
||||
_connection = it->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ private:
|
||||
|
||||
int _ret;
|
||||
int _row;
|
||||
int _lifeTime;
|
||||
unsigned int _contentLength;
|
||||
unsigned int _received;
|
||||
unsigned int _headerSize;
|
||||
|
||||
std::string _URI;
|
||||
@@ -24,6 +26,7 @@ private:
|
||||
std::string _fullURI;
|
||||
std::string _version;
|
||||
std::string _location;
|
||||
std::string _connection;
|
||||
std::map<std::string, std::string> _headerField;
|
||||
|
||||
ServerConfig *_config;
|
||||
@@ -39,10 +42,13 @@ public:
|
||||
std::string getFullUri(void);
|
||||
std::string getVersion(void);
|
||||
std::string getLocation(void);
|
||||
std::string getConnection(void);
|
||||
ServerConfig *getConfig(void);
|
||||
int getCode(void);
|
||||
int getLifeTime(void);
|
||||
unsigned int getContentLength(void) const;
|
||||
unsigned int getHeaderSize(void) const;
|
||||
unsigned int getRecved(void)const;
|
||||
std::map<std::string, std::string> getClientFields(void);
|
||||
bool getChunked(void);
|
||||
|
||||
@@ -71,6 +77,7 @@ public:
|
||||
void clear(void);
|
||||
void splitData(char *);
|
||||
|
||||
void increaseRecvCounter(unsigned int n);
|
||||
~Request();
|
||||
};
|
||||
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
Response::Response()
|
||||
{
|
||||
initErrorCode();
|
||||
_code = 200;
|
||||
}
|
||||
|
||||
//-------------------------------------------------GET/SET---------------------------------------
|
||||
|
||||
std::string Response::getClient(void)
|
||||
std::string Response::getHeader(void)
|
||||
{
|
||||
return (_Client);
|
||||
return (_header);
|
||||
}
|
||||
std::string Response::getBody(void)
|
||||
{
|
||||
@@ -21,6 +22,7 @@ std::string Response::getBody(void)
|
||||
void Response::setData(Request request, ServerConfig *config)
|
||||
{
|
||||
_request = request;
|
||||
_code = request.getCode();
|
||||
_config = config;
|
||||
}
|
||||
|
||||
@@ -29,17 +31,12 @@ void Response::setData(Request request, ServerConfig *config)
|
||||
void Response::OpenResponseFile(const char *path)
|
||||
{
|
||||
std::stringstream ss;
|
||||
char buf[BUFFSIZE + 1] = {0};
|
||||
std::ifstream file(path);
|
||||
// char buf[BUFFSIZE + 1] = {0};
|
||||
std::ifstream file(path, std::ifstream::in);
|
||||
|
||||
if (file.is_open())
|
||||
{
|
||||
while (!file.eof())
|
||||
{
|
||||
file.read(buf, BUFFSIZE);
|
||||
ss << buf;
|
||||
memset(buf, 0, BUFFSIZE + 1);
|
||||
}
|
||||
ss << file.rdbuf();
|
||||
_body = ss.str();
|
||||
file.close();
|
||||
}
|
||||
@@ -59,37 +56,104 @@ void Response::generate()
|
||||
// methodDelete();
|
||||
}
|
||||
|
||||
//-------------------------------------------------GET/SET---------------------------------------
|
||||
//-------------------------------------------------HEADER/BODY---------------------------------------
|
||||
|
||||
std::string Response::getTime(void)
|
||||
{
|
||||
char buff[1337] = {0};
|
||||
struct timeval currTime;
|
||||
struct tm *t;
|
||||
|
||||
gettimeofday(&currTime, NULL);
|
||||
t = gmtime(&currTime.tv_sec);
|
||||
strftime(buff, 1337, "%a, %d %b %Y %H:%M:%S GTM", t);
|
||||
|
||||
return (buff);
|
||||
}
|
||||
|
||||
std::string Response::getContentType(void)
|
||||
{
|
||||
std::string path = _request.getFullUri();
|
||||
std::string type = path.substr(path.rfind(".") + 1, path.size() - path.rfind("."));
|
||||
|
||||
if (_request.isDir(path) == 0)
|
||||
return ("text/html");
|
||||
else if (type == "html")
|
||||
return ("text/html");
|
||||
else if (type == "css")
|
||||
return ("text/css");
|
||||
else if (type == "js")
|
||||
return ("text/javascript");
|
||||
else if (type == "json")
|
||||
return ("application/json");
|
||||
else if (type == "woff")
|
||||
return ("application/x-font-woff");
|
||||
else if (type == "mp4" || type == "m4v" || type == " f4v")
|
||||
return ("video/mp4");
|
||||
else if (type == "ico")
|
||||
return ("image/x-icon");
|
||||
else if (type == "jpeg" || type == "jpg")
|
||||
return ("image/jpeg");
|
||||
else if (type == "png")
|
||||
return ("image/png");
|
||||
else if (type == "bmp")
|
||||
return ("image/bmp");
|
||||
else if (type == "pdf")
|
||||
return ("application/pdf");
|
||||
else
|
||||
return ("text/plain");
|
||||
|
||||
}
|
||||
|
||||
void Response::invalidClient(void)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string tmp;
|
||||
//Client
|
||||
ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n";
|
||||
_Client = ss.str();
|
||||
|
||||
//body
|
||||
_body = getErrorPage(_request.getCode());
|
||||
std::cout << RED << "Invalid Client method called\nCODE: " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << ZERO_C << std::endl;
|
||||
generateBody();
|
||||
//Header
|
||||
generateHeader();
|
||||
}
|
||||
|
||||
void Response::generateBody(void)
|
||||
{
|
||||
if (!_request.badCode(_code) && _request.isDir(_request.getFullUri()) == 0)
|
||||
_body = Autoindex::getPage(_request.getURI(), _request.getFullUri(), _request.getHost());
|
||||
else if (!_request.badCode(_code) && _request.isFile(_request.getFullUri()) == 0)
|
||||
OpenResponseFile(_request.getFullUri().c_str());
|
||||
else if (_request.isFile(_request.getFullUri()) == -1)
|
||||
_body = getErrorPage(404);
|
||||
else
|
||||
_body = getErrorPage(_code);
|
||||
|
||||
}
|
||||
|
||||
void Response::generateHeader(void)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string tmp;
|
||||
|
||||
ss << "HTTP/1.1" << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\n";
|
||||
ss << "Content-Type: " << getContentType() << "\r\n";
|
||||
ss << "Content-Length: " << _body.size() << "\r\n";
|
||||
ss << "Server: poheck\r\n";
|
||||
if (_request.getConnection() == "keep-alive")
|
||||
ss << "Keep-Alive: timeout=" << _request.getLifeTime() << "\r\n";
|
||||
ss << "Date: " << getTime() << "\r\n";
|
||||
ss << "\r\n";
|
||||
_header = ss.str();
|
||||
}
|
||||
|
||||
void Response::methodGet(void)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string tmp;
|
||||
//Client
|
||||
ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n";
|
||||
_Client = ss.str();
|
||||
//body
|
||||
if (!_request.badCode(_request.getCode()) && _request.isDir(_request.getFullUri()) == 0)
|
||||
_body = Autoindex::getPage(_request.getURI(), _request.getFullUri(), _request.getHost());
|
||||
else if (!_request.badCode(_request.getCode()))
|
||||
OpenResponseFile(_request.getFullUri().c_str());
|
||||
else
|
||||
_body = getErrorPage(_request.getCode());
|
||||
|
||||
generateBody();
|
||||
generateHeader();
|
||||
std::cout << GREEN << "GET method called\n" << ZERO_C;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
//-------------------------------------------------GET/SET---------------------------------------
|
||||
|
||||
@@ -9,9 +9,10 @@ class Response
|
||||
{
|
||||
private:
|
||||
std::string _body;
|
||||
std::string _Client;
|
||||
std::string _header;
|
||||
Request _request;
|
||||
ServerConfig *_config;
|
||||
int _code;
|
||||
|
||||
private:
|
||||
static std::map<std::string, std::string> _errorCode;
|
||||
@@ -21,9 +22,12 @@ private:
|
||||
// void methodPost(void);
|
||||
// void methodDelete(void);
|
||||
void invalidClient(void);
|
||||
|
||||
void generateHeader(void);
|
||||
void generateBody(void);
|
||||
std::string getTime(void);
|
||||
std::string getContentType(void);
|
||||
public:
|
||||
std::string getClient(void);
|
||||
std::string getHeader(void);
|
||||
std::string getBody(void);
|
||||
static std::string getReasonPhrase(std::string);
|
||||
static std::string getReasonPhrase(int);
|
||||
|
||||
@@ -82,19 +82,18 @@ void Server::readSocket(int fd, std::map<int, Client> &client_map)
|
||||
return;
|
||||
}
|
||||
client_map[fd].setRawData(buf);
|
||||
status = client_map[fd].parseRequest();
|
||||
client_map[fd].increaseRecvCounter(bytes_read);
|
||||
client_map[fd].printClientInfo();
|
||||
status = client_map[fd].parseRequest();
|
||||
// client_map[fd].printClientInfo();
|
||||
|
||||
if ((bytes_read < BUFFSIZE) && client_map[fd].allRecved())
|
||||
{
|
||||
client_map[fd].allRead = true;
|
||||
}
|
||||
|
||||
std::cerr << "bytes_read " << bytes_read << std::endl;;
|
||||
std::cerr << "recvCounter " << client_map[fd].getRecvCounter() << std::endl;;
|
||||
std::cerr << "recvCounter " << client_map[fd].getRecvCounter() << std::endl;
|
||||
std::cerr << "contentLength " << client_map[fd].getRequest().getContentLength() << std::endl;
|
||||
std::cerr << "allRead " << client_map[fd].allRead << std::endl;;
|
||||
std::cerr << "allRead " << client_map[fd].allRead << std::endl;
|
||||
|
||||
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
|
||||
bzero(buf, BUFFSIZE);
|
||||
@@ -155,7 +154,7 @@ void Server::start(void)
|
||||
std::cout << TURQ << "IN SEND LOOP" << RESET << std::endl;
|
||||
Client &client = client_it->second;
|
||||
|
||||
if (!client.allRead)
|
||||
if (!client.allRead && !client.isEmpty())
|
||||
{
|
||||
readSocket(client_it->first, client_map);
|
||||
}
|
||||
@@ -169,8 +168,9 @@ void Server::start(void)
|
||||
|
||||
}
|
||||
|
||||
if (client.readyToSend() && client.allSended())
|
||||
if ((client.readyToSend() && client.allSended()) || client.isEmpty())
|
||||
{
|
||||
client_map[fd].printClientInfo();
|
||||
close(client_it->first);
|
||||
std::cerr << RED <<
|
||||
"deleting client "
|
||||
@@ -219,15 +219,25 @@ void Server::start(void)
|
||||
void Server::end(void)
|
||||
{
|
||||
std::vector<ServerConfig *>::iterator pri;
|
||||
std::vector<location *>::iterator loc;
|
||||
std::vector< location *> l;
|
||||
|
||||
pri = _configs.begin();
|
||||
while (pri != _configs.end())
|
||||
{
|
||||
(*pri)->printFields();
|
||||
l = (*pri)->getLocations();
|
||||
loc = l.begin();
|
||||
while (loc != l.end())
|
||||
{
|
||||
delete *loc;
|
||||
loc++;
|
||||
}
|
||||
delete *pri;
|
||||
pri++;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------Other------------------------------------------------------------------------------------------------
|
||||
void Server::checkError(int fd, std::string str)
|
||||
{
|
||||
|
||||
@@ -33,7 +33,7 @@ int ServerConfig::getClientBodySize(void)
|
||||
return (_clientBodySize);
|
||||
}
|
||||
|
||||
std::vector<location> ServerConfig::getLocations(void)
|
||||
std::vector<location *> ServerConfig::getLocations(void)
|
||||
{
|
||||
return (_locations);
|
||||
}
|
||||
@@ -73,7 +73,7 @@ void ServerConfig::setErrorPages(std::map<int, std::string> pages)
|
||||
_errorPages = pages;
|
||||
}
|
||||
|
||||
void ServerConfig::setLocations(std::vector<location> locations)
|
||||
void ServerConfig::setLocations(std::vector<location *> locations)
|
||||
{
|
||||
_locations = locations;
|
||||
}
|
||||
@@ -142,7 +142,7 @@ int ServerConfig::putLocation(toml_node *node)
|
||||
TOMLMapArray::iterator it = arr->begin();
|
||||
TOMLMap *map;
|
||||
TOMLMap::iterator it1;
|
||||
location tmp;
|
||||
location *tmp;
|
||||
TOMLArray::iterator it2;
|
||||
TOMLArray Array;
|
||||
|
||||
@@ -151,43 +151,44 @@ int ServerConfig::putLocation(toml_node *node)
|
||||
{
|
||||
map = *it;
|
||||
it1 = map->begin();
|
||||
tmp = new location;
|
||||
while (it1 != map->end())
|
||||
{
|
||||
if (it1->first == "location")
|
||||
{
|
||||
if (it1->second->get_type() != toml_node::STRING)
|
||||
return (1);
|
||||
tmp.location = *it1->second->getString();
|
||||
tmp->location = *it1->second->getString();
|
||||
}
|
||||
else if (it1->first == "root")
|
||||
{
|
||||
if (it1->second->get_type() != toml_node::STRING)
|
||||
return (1);
|
||||
tmp.root = *it1->second->getString();
|
||||
tmp->root = *it1->second->getString();
|
||||
}
|
||||
else if (it1->first == "autoindex")
|
||||
{
|
||||
if (it1->second->get_type() != toml_node::BOOL)
|
||||
return (1);
|
||||
tmp.autoindex = it1->second->getBool();
|
||||
tmp->autoindex = it1->second->getBool();
|
||||
}
|
||||
else if (it1->first == "upload_accept")
|
||||
{
|
||||
if (it1->second->get_type() != toml_node::BOOL)
|
||||
return (1);
|
||||
tmp.uploadAccept = it1->second->getBool();
|
||||
tmp->uploadAccept = it1->second->getBool();
|
||||
}
|
||||
else if (it1->first == "upload_dir")
|
||||
{
|
||||
if (it1->second->get_type() != toml_node::STRING)
|
||||
return (1);
|
||||
tmp.uploadDir = *it1->second->getString();
|
||||
tmp->uploadDir = *it1->second->getString();
|
||||
}
|
||||
else if (it1->first == "directory_file")
|
||||
{
|
||||
if (it1->second->get_type() != toml_node::STRING)
|
||||
return (1);
|
||||
tmp.directoryFile = *it1->second->getString();
|
||||
tmp->directoryFile = *it1->second->getString();
|
||||
}
|
||||
else if (it1->first == "methods")
|
||||
{
|
||||
@@ -199,7 +200,7 @@ int ServerConfig::putLocation(toml_node *node)
|
||||
{
|
||||
if ((*it2)->get_type() != toml_node::STRING)
|
||||
return (1);
|
||||
tmp.methods.push_back(*((*it2)->getString()));
|
||||
tmp->methods.push_back(*((*it2)->getString()));
|
||||
++it2;
|
||||
}
|
||||
|
||||
@@ -212,14 +213,13 @@ int ServerConfig::putLocation(toml_node *node)
|
||||
it2 = Array.begin();
|
||||
str = *(*it2)->getString();
|
||||
++it2;
|
||||
tmp.redirect[atoi(str.c_str())] = *(*it2)->getString();
|
||||
tmp->redirect.insert(std::make_pair(atoi(str.c_str()), *(*it2)->getString()));
|
||||
}
|
||||
else
|
||||
std::cout << RED << it1->first << ZERO_C << std::endl;
|
||||
it1++;
|
||||
}
|
||||
_locations.push_back(tmp);
|
||||
memset(&tmp, 0, sizeof(tmp));
|
||||
it++;
|
||||
}
|
||||
return (0);
|
||||
@@ -259,7 +259,7 @@ void ServerConfig::fillFields(void)
|
||||
|
||||
void ServerConfig::printFields(void)
|
||||
{
|
||||
std::vector<location>::iterator it;
|
||||
std::vector<location *>::iterator it;
|
||||
std::map<int, std::string>::iterator it1;
|
||||
std::vector<std::string>::iterator it2;
|
||||
std::map<int, std::string>::iterator it3;
|
||||
@@ -275,21 +275,21 @@ void ServerConfig::printFields(void)
|
||||
while (it != _locations.end())
|
||||
{
|
||||
std::cout << PINK << "------------------------------------------------\n";
|
||||
std::cout << YELLOW << "location " << BLUE << (*it).location <<std::endl;
|
||||
std::cout << YELLOW << "root " << BLUE << (*it).root <<std::endl;
|
||||
std::cout << YELLOW << "directoryFile " << BLUE << (*it).directoryFile <<std::endl;
|
||||
std::cout << YELLOW << "uploadDir " << BLUE << (*it).uploadDir <<std::endl;
|
||||
std::cout << YELLOW << "autoindex " << BLUE << (*it).autoindex <<std::endl;
|
||||
std::cout << YELLOW << "uploadAccept " << BLUE << (*it).uploadAccept <<std::endl;
|
||||
std::cout << YELLOW << "location " << BLUE << (*it)->location <<std::endl;
|
||||
std::cout << YELLOW << "root " << BLUE << (*it)->root <<std::endl;
|
||||
std::cout << YELLOW << "directoryFile " << BLUE << (*it)->directoryFile <<std::endl;
|
||||
std::cout << YELLOW << "uploadDir " << BLUE << (*it)->uploadDir <<std::endl;
|
||||
std::cout << YELLOW << "autoindex " << BLUE << (*it)->autoindex <<std::endl;
|
||||
std::cout << YELLOW << "uploadAccept " << BLUE << (*it)->uploadAccept <<std::endl;
|
||||
std::cout << YELLOW << "methods " << std::endl;
|
||||
it2 = (*it).methods.begin();
|
||||
while (it2 != (*it).methods.end())
|
||||
it2 = (*it)->methods.begin();
|
||||
while (it2 != (*it)->methods.end())
|
||||
{
|
||||
std::cout << BLUE << *it2 << " ";
|
||||
it2++;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
it3 = (*it).redirect.begin();
|
||||
it3 = (*it)->redirect.begin();
|
||||
std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl;
|
||||
++it;
|
||||
std::cout << PINK << "------------------------------------------------\n";
|
||||
|
||||
@@ -28,7 +28,7 @@ private:
|
||||
int _clientBodySize;
|
||||
|
||||
std::map<int, std::string> _errorPages;
|
||||
std::vector<location> _locations;
|
||||
std::vector<location *> _locations;
|
||||
|
||||
public:
|
||||
void setServerName(std::string);
|
||||
@@ -36,14 +36,14 @@ public:
|
||||
void setPort(int);
|
||||
void setClientBodySize(int);
|
||||
void setErrorPages(std::map<int, std::string>);
|
||||
void setLocations(std::vector<location>);
|
||||
void setLocations(std::vector<location *>);
|
||||
void setRoot(TOMLMap *);
|
||||
|
||||
std::string getServerName(void);
|
||||
std::string getHost(void);
|
||||
int getPort(void);
|
||||
int getClientBodySize(void);
|
||||
std::vector<location> getLocations(void);
|
||||
std::vector<location *> getLocations(void);
|
||||
std::map<int, std::string> getErrorPages(void);
|
||||
TOMLMap *getRoot(void);
|
||||
|
||||
|
||||
BIN
www/images/images.jpeg
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
www/images/images1.jpeg
Normal file
|
After Width: | Height: | Size: 9.6 KiB |
BIN
www/images/images2.jpeg
Normal file
|
After Width: | Height: | Size: 6.0 KiB |
BIN
www/images/images3.jpeg
Normal file
|
After Width: | Height: | Size: 9.6 KiB |
BIN
www/images/images4.jpeg
Normal file
|
After Width: | Height: | Size: 6.2 KiB |
BIN
www/images/images5.jpeg
Normal file
|
After Width: | Height: | Size: 6.8 KiB |
BIN
www/images/index.jpeg
Normal file
|
After Width: | Height: | Size: 5.5 KiB |