mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
update Header class v.1.1
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "Socket.hpp"
|
||||
#include "Server.hpp"
|
||||
|
||||
145
src/Header.cpp
145
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user