From 4c9bfb73baed5c0cba26f8037b330f3b85ad9695 Mon Sep 17 00:00:00 2001 From: Talyx Date: Thu, 13 Jan 2022 21:37:35 +0300 Subject: [PATCH] update Header class --- includes/Header.hpp | 21 ++++---- src/Header.cpp | 124 ++++++++++++++++++++++++++++---------------- src/Server.cpp | 4 +- 3 files changed, 92 insertions(+), 57 deletions(-) diff --git a/includes/Header.hpp b/includes/Header.hpp index 65d1e1a..7d37da3 100644 --- a/includes/Header.hpp +++ b/includes/Header.hpp @@ -6,11 +6,15 @@ class Header { private: - int _type; int _row; char *_buff; std::string _fileName; - std::map _request; + + std::string _method; + std::string _version; + std::string _URI; + std::map _headerField; + public: enum REQ @@ -22,14 +26,13 @@ public: public: std::map getRequest(void); - int getType(void); - std::string getFileName(void); - void setFile(std::string); - void setRequest(char *); - void identifyType(std::string); + std::string getMethod(void); + std::string getURI(void); + void setRawData(char *); + void parseStartLine(std::string); void printHeaderInfo(void); - void parseRequest(void); - void clearHeader(void); + int parseRequest(void); + void clear(void); Header(); Header(char *); ~Header(); diff --git a/src/Header.cpp b/src/Header.cpp index 7d7d0c5..8e1ca07 100644 --- a/src/Header.cpp +++ b/src/Header.cpp @@ -9,55 +9,88 @@ Header::Header(char *str) { this->_row = 0; 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(); } //-------------------------------------------------GET/SET--------------------------------------- std::map 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; } //-------------------------------------------------- -void Header::setFile(std::string str) +void Header::parseStartLine(std::string str) { - std::string del = " "; - int pos; + std::string tmp[3]; - pos = str.find(del); - str.erase(0, pos + del.length()); - _fileName = str.substr(0, str.find(del)); - if (_fileName == "/") - _fileName = "www/index2.html"; + _method = str.substr(0, str.find(" ")); + str = str.erase(0 , str.find(" ") + 1); + _URI = str.substr(0, str.find(" ")); + str = str.erase(0 , str.find(" ") + 1); + _version = str; + if (_URI == "/") + _URI = "www/index2.html"; else - _fileName.insert(0, HOME); + _URI = HOME + _URI; + } -void Header::identifyType(std::string str) -{ - 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) +int Header::parseRequest(void) { std::string line; std::string key; @@ -69,55 +102,54 @@ void Header::parseRequest(void) while (std::getline(buffStream, line, '\n')) { if (_row == 0) - { - identifyType(line); - setFile(line); - } + parseStartLine(line); else { distance = line.find(":"); key = line.substr(0, distance); 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 << (_request.find(key))->first << (_request.find(key))->second << std::endl; } else - _request[key] = line; + _headerField[key] = line; } _row++; } + if (_version != "HTTP/1.1") + return (505); + else if (_method != "GET" || _method != "POST" || _method != "DELETE") + return (405); + return (200); } void Header::printHeaderInfo(void) { std::map::iterator it; - std::cout << YELLOW << "request type = " << _type << ZERO_C << std::endl; - std::cout << YELLOW << "request rows = " << _row << ZERO_C << std::endl; - std::cout << YELLOW << "request fileName = " << _fileName << ZERO_C << std::endl; + std::cout << PINK << "request method = " << _method << ZERO_C << std::endl; + std::cout << PINK << "request URI = " << _URI << 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 << 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; - } - for ( it = _request.begin(); it != _request.end(); it++) - { - std::cout << PINK << it->second << ZERO_C << std::endl; + std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl; } } -void Header::clearHeader(void) +void Header::clear(void) { - _type = -1; + _method = ""; _row = 0; _buff = NULL; - _fileName = "www/index2.html"; - _request.clear(); + _URI = ""; + _version = ""; + _headerField.clear(); } Header::~Header() diff --git a/src/Server.cpp b/src/Server.cpp index d114e46..fbecc78 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -27,7 +27,7 @@ void Server::sendHeader(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(); std::ifstream file(path); char buff[BUFFSIZE + 1] = {0}; @@ -86,7 +86,7 @@ void Server::start(void) checkError(fd_accept, "Initialize client socket"); checkError(recv(fd_accept, buff, BUFFSIZE, 0), "Receive msg from client"); std::cout << TURGUOISE << "Receive Header" << ZERO_C << std::endl; - header.setRequest(buff); + header.setRawData(buff); header.parseRequest(); header.printHeaderInfo(); sendRespons(header, fd_accept);