support for new data types
@@ -31,17 +31,11 @@ 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);
|
||||
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();
|
||||
}
|
||||
@@ -61,7 +55,44 @@ 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 == "jpeg" || type == "jpg")
|
||||
return ("image/jpeg");
|
||||
else if (type == "png")
|
||||
return ("image/png");
|
||||
else if (type == "bmp")
|
||||
return ( "image/bmp");
|
||||
else
|
||||
return ("text/plain");
|
||||
|
||||
}
|
||||
|
||||
void Response::invalidClient(void)
|
||||
{
|
||||
@@ -82,6 +113,8 @@ void Response::generateBody(void)
|
||||
_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);
|
||||
|
||||
@@ -92,11 +125,14 @@ void Response::generateHeader(void)
|
||||
std::stringstream ss;
|
||||
std::string tmp;
|
||||
|
||||
ss << "HTTP/1.1" << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode())
|
||||
<< "\r\nContent-Type: text/html"
|
||||
<< "\r\nContent-Length: " << _body.size()
|
||||
<< "\r\nServer: poheck"
|
||||
<< "\r\n\r\n";
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,10 @@ private:
|
||||
// void methodPost(void);
|
||||
// void methodDelete(void);
|
||||
void invalidClient(void);
|
||||
void generateHeader(void);
|
||||
void generateBody(void);
|
||||
void generateHeader(void);
|
||||
void generateBody(void);
|
||||
std::string getTime(void);
|
||||
std::string getContentType(void);
|
||||
public:
|
||||
std::string getHeader(void);
|
||||
std::string getBody(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 |