From 99e3faf5b5ee44f3dcf49350a2a1109c2de2bbcc Mon Sep 17 00:00:00 2001 From: Talyx Date: Sat, 15 Jan 2022 23:05:55 +0300 Subject: [PATCH] update Header class v.1.1 --- includes/Header.hpp | 5 +- includes/webserv.hpp | 1 + src/Header.cpp | 145 ++++++++++++++++++++++++------------------- 3 files changed, 86 insertions(+), 65 deletions(-) diff --git a/includes/Header.hpp b/includes/Header.hpp index 7d37da3..c2c4f85 100644 --- a/includes/Header.hpp +++ b/includes/Header.hpp @@ -8,7 +8,7 @@ class Header private: int _row; char *_buff; - std::string _fileName; + std::string _method; std::string _version; @@ -29,7 +29,8 @@ public: std::string getMethod(void); std::string getURI(void); void setRawData(char *); - void parseStartLine(std::string); + int parseStartLine(std::string); + int parseHeaderfield(std::string); void printHeaderInfo(void); int parseRequest(void); void clear(void); diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 0e0eb3f..a6f886c 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "Socket.hpp" #include "Server.hpp" diff --git a/src/Header.cpp b/src/Header.cpp index 8e1ca07..94cc4b6 100644 --- a/src/Header.cpp +++ b/src/Header.cpp @@ -3,53 +3,60 @@ Header::Header() { this->_row = 0; + } +// std::string** Header::initErrorCode(void) +// { +// 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"}}; +// return (HttpCode); +// } + 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--------------------------------------- @@ -74,7 +81,7 @@ void Header::setRawData(char *str) } //-------------------------------------------------- -void Header::parseStartLine(std::string str) +int Header::parseStartLine(std::string str) { std::string tmp[3]; @@ -83,44 +90,56 @@ void Header::parseStartLine(std::string str) _URI = str.substr(0, str.find(" ")); str = str.erase(0 , str.find(" ") + 1); _version = str; + _version.erase(_version.find_last_not_of(" \n\r\t") + 1); if (_URI == "/") _URI = "www/index2.html"; else _URI = HOME + _URI; - + if (_version != "HTTP/1.1") + return (505); + else if (_method != "GET" && _method != "POST" && _method != "DELETE") + return (405); + return 200; +} + +int Header::parseHeaderfield(std::string str) +{ + int distance; + std::string key; + std::string value; + + distance = str.find(":"); + if (distance < 0 && str != "\r") + return 400; + key = str.substr(0, distance); + value = str.erase(0, distance + 1); + if (_headerField.find(key) != _headerField.end()) + { + std::cout << RED << "ERROR: double header-field" << ZERO_C << std::endl; + } + else + { + _headerField[key] = value.erase(value.find_last_not_of(" \n\r\t") + 1); + } + return 200; } int Header::parseRequest(void) { std::string line; - std::string key; - int distance; + int ret; std::stringstream buffStream; buffStream << _buff; - distance = 0; - while (std::getline(buffStream, line, '\n')) + ret = 200; + while (std::getline(buffStream, line, '\n') && ret == 200) { if (_row == 0) - parseStartLine(line); + ret = parseStartLine(line); else - { - distance = line.find(":"); - key = line.substr(0, distance); - line = line.erase(0, distance + 1); - if (_headerField.find(key) != _headerField.end()) - { - std::cout << RED << "ERROR: double header-field" << ZERO_C << std::endl; - } - else - _headerField[key] = line; - } + ret = parseHeaderfield(line); _row++; } - if (_version != "HTTP/1.1") - return (505); - else if (_method != "GET" || _method != "POST" || _method != "DELETE") - return (405); return (200); }