update Header class

This commit is contained in:
Talyx
2022-01-13 21:37:35 +03:00
parent 9285dafe99
commit 4c9bfb73ba
3 changed files with 92 additions and 57 deletions

View File

@@ -6,11 +6,15 @@
class Header class Header
{ {
private: private:
int _type;
int _row; int _row;
char *_buff; char *_buff;
std::string _fileName; std::string _fileName;
std::map<std::string, std::string> _request;
std::string _method;
std::string _version;
std::string _URI;
std::map<std::string, std::string> _headerField;
public: public:
enum REQ enum REQ
@@ -22,14 +26,13 @@ public:
public: public:
std::map<std::string, std::string> getRequest(void); std::map<std::string, std::string> getRequest(void);
int getType(void); std::string getMethod(void);
std::string getFileName(void); std::string getURI(void);
void setFile(std::string); void setRawData(char *);
void setRequest(char *); void parseStartLine(std::string);
void identifyType(std::string);
void printHeaderInfo(void); void printHeaderInfo(void);
void parseRequest(void); int parseRequest(void);
void clearHeader(void); void clear(void);
Header(); Header();
Header(char *); Header(char *);
~Header(); ~Header();

View File

@@ -9,55 +9,88 @@ Header::Header(char *str)
{ {
this->_row = 0; this->_row = 0;
this->_buff = str; this->_buff = str;
std::string HttpCode [41][2]= {{"100", "Continue"},
{"101", "Switching Protocols"},
{"200", "OK"},
{"201", "Created"},
{"202", "Accepted"},
{"203", "Non-Authoritative Information"},
{"204", "No Content"},
{"205", "Reset Content"},
{"206", "Partial Content"},
{"300", "Multiple Choices"},
{"301", "Moved Permanently"},
{"302", "Found"},
{"303", "See Other"},
{"304", "Not Modified"},
{"305", "Use Proxy"},
{"306", "(Unused)"},
{"307", "Temporary Redirect"},
{"400", "Bad Request"},
{"401", "Unauthorized"},
{"402", "Payment Required"},
{"403", "Forbidden"},
{"404", "Not Found"},
{"405", "Method Not Allowed"},
{"406", "Not Acceptable"},
{"407", "Proxy Authentication Required"},
{"408", "Request Timeout"},
{"409", "Conflict"},
{"410", "Gone"},
{"411", "Length Required"},
{"412", "Precondition Failed"},
{"413", "Request Entity Too Large"},
{"414", "Request-URI Too Long"},
{"415", "Unsupported Media Type"},
{"416", "Requested Range Not Satisfiable"},
{"417", "Expectation Failed"},
{"500", "Internal Server Error"},
{"501", "Not Implemented"},
{"502", "Bad Gateway"},
{"503", "Service Unavailable"},
{"504", "Gateway Timeout"},
{"505", "HTTP Version Not Supported"}};
parseRequest(); parseRequest();
} }
//-------------------------------------------------GET/SET--------------------------------------- //-------------------------------------------------GET/SET---------------------------------------
std::map<std::string, std::string> Header::getRequest(void) std::map<std::string, std::string> Header::getRequest(void)
{ {
return (this->_request); return (this->_headerField);
} }
int Header::getType(void) std::string Header::getMethod(void)
{ {
return (this->_type); return (this->_method);
} }
std::string Header::getFileName(void) std::string Header::getURI(void)
{ {
return _fileName; return _URI;
} }
void Header::setRequest(char *str) void Header::setRawData(char *str)
{ {
this->_buff = str; this->_buff = str;
} }
//-------------------------------------------------- //--------------------------------------------------
void Header::setFile(std::string str) void Header::parseStartLine(std::string str)
{ {
std::string del = " "; std::string tmp[3];
int pos;
pos = str.find(del); _method = str.substr(0, str.find(" "));
str.erase(0, pos + del.length()); str = str.erase(0 , str.find(" ") + 1);
_fileName = str.substr(0, str.find(del)); _URI = str.substr(0, str.find(" "));
if (_fileName == "/") str = str.erase(0 , str.find(" ") + 1);
_fileName = "www/index2.html"; _version = str;
if (_URI == "/")
_URI = "www/index2.html";
else else
_fileName.insert(0, HOME); _URI = HOME + _URI;
} }
void Header::identifyType(std::string str) int Header::parseRequest(void)
{
if (str.compare(0,3, "GET") == 0)
_type = GET;
else if (str.compare(0,4, "POST") == 0)
_type = POST;
else if (str.compare(0,6, "DELETE") == 0)
_type = DELETE;
}
void Header::parseRequest(void)
{ {
std::string line; std::string line;
std::string key; std::string key;
@@ -69,55 +102,54 @@ void Header::parseRequest(void)
while (std::getline(buffStream, line, '\n')) while (std::getline(buffStream, line, '\n'))
{ {
if (_row == 0) if (_row == 0)
{ parseStartLine(line);
identifyType(line);
setFile(line);
}
else else
{ {
distance = line.find(":"); distance = line.find(":");
key = line.substr(0, distance); key = line.substr(0, distance);
line = line.erase(0, distance + 1); line = line.erase(0, distance + 1);
if (_request.find(key) != _request.end()) if (_headerField.find(key) != _headerField.end())
{ {
std::cout << RED << "ERROR: double header-field" << ZERO_C << std::endl; std::cout << RED << "ERROR: double header-field" << ZERO_C << std::endl;
std::cout << RED << (_request.find(key))->first << (_request.find(key))->second << std::endl;
} }
else else
_request[key] = line; _headerField[key] = line;
} }
_row++; _row++;
} }
if (_version != "HTTP/1.1")
return (505);
else if (_method != "GET" || _method != "POST" || _method != "DELETE")
return (405);
return (200);
} }
void Header::printHeaderInfo(void) void Header::printHeaderInfo(void)
{ {
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
std::cout << YELLOW << "request type = " << _type << ZERO_C << std::endl; std::cout << PINK << "request method = " << _method << ZERO_C << std::endl;
std::cout << YELLOW << "request rows = " << _row << ZERO_C << std::endl; std::cout << PINK << "request URI = " << _URI << ZERO_C << std::endl;
std::cout << YELLOW << "request fileName = " << _fileName << ZERO_C << std::endl; std::cout << PINK << "request http versioin = " << _version << ZERO_C << std::endl;
std::cout << PINK << "request rows = " << _row << ZERO_C << std::endl;
std::cout << YELLOW << "request header:\n" << _buff << ZERO_C << std::endl; std::cout << YELLOW << "request header:\n" << _buff << ZERO_C << std::endl;
std::cout << TURGUOISE << "HEADER MAP" << ZERO_C << std::endl; std::cout << TURGUOISE << "HEADER MAP" << ZERO_C << std::endl;
for ( it = _request.begin(); it != _request.end(); it++) for ( it = _headerField.begin(); it != _headerField.end(); it++)
{ {
std::cout << PINK << it->first << ZERO_C << std::endl; std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
}
for ( it = _request.begin(); it != _request.end(); it++)
{
std::cout << PINK << it->second << ZERO_C << std::endl;
} }
} }
void Header::clearHeader(void) void Header::clear(void)
{ {
_type = -1; _method = "";
_row = 0; _row = 0;
_buff = NULL; _buff = NULL;
_fileName = "www/index2.html"; _URI = "";
_request.clear(); _version = "";
_headerField.clear();
} }
Header::~Header() Header::~Header()

View File

@@ -27,7 +27,7 @@ void Server::sendHeader(Header head, int fd)
void Server::sendRespons(Header head, int fd) void Server::sendRespons(Header head, int fd)
{ {
std::string str = head.getFileName(); std::string str = head.getURI();
const char *path = str.c_str(); const char *path = str.c_str();
std::ifstream file(path); std::ifstream file(path);
char buff[BUFFSIZE + 1] = {0}; char buff[BUFFSIZE + 1] = {0};
@@ -86,7 +86,7 @@ void Server::start(void)
checkError(fd_accept, "Initialize client socket"); checkError(fd_accept, "Initialize client socket");
checkError(recv(fd_accept, buff, BUFFSIZE, 0), "Receive msg from client"); checkError(recv(fd_accept, buff, BUFFSIZE, 0), "Receive msg from client");
std::cout << TURGUOISE << "Receive Header" << ZERO_C << std::endl; std::cout << TURGUOISE << "Receive Header" << ZERO_C << std::endl;
header.setRequest(buff); header.setRawData(buff);
header.parseRequest(); header.parseRequest();
header.printHeaderInfo(); header.printHeaderInfo();
sendRespons(header, fd_accept); sendRespons(header, fd_accept);