mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
update Header class v.1.1
This commit is contained in:
@@ -8,7 +8,7 @@ class Header
|
|||||||
private:
|
private:
|
||||||
int _row;
|
int _row;
|
||||||
char *_buff;
|
char *_buff;
|
||||||
std::string _fileName;
|
|
||||||
|
|
||||||
std::string _method;
|
std::string _method;
|
||||||
std::string _version;
|
std::string _version;
|
||||||
@@ -29,7 +29,8 @@ public:
|
|||||||
std::string getMethod(void);
|
std::string getMethod(void);
|
||||||
std::string getURI(void);
|
std::string getURI(void);
|
||||||
void setRawData(char *);
|
void setRawData(char *);
|
||||||
void parseStartLine(std::string);
|
int parseStartLine(std::string);
|
||||||
|
int parseHeaderfield(std::string);
|
||||||
void printHeaderInfo(void);
|
void printHeaderInfo(void);
|
||||||
int parseRequest(void);
|
int parseRequest(void);
|
||||||
void clear(void);
|
void clear(void);
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "Socket.hpp"
|
#include "Socket.hpp"
|
||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
|
|||||||
143
src/Header.cpp
143
src/Header.cpp
@@ -3,53 +3,60 @@
|
|||||||
Header::Header()
|
Header::Header()
|
||||||
{
|
{
|
||||||
this->_row = 0;
|
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)
|
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---------------------------------------
|
||||||
@@ -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];
|
std::string tmp[3];
|
||||||
|
|
||||||
@@ -83,44 +90,56 @@ void Header::parseStartLine(std::string str)
|
|||||||
_URI = str.substr(0, str.find(" "));
|
_URI = str.substr(0, str.find(" "));
|
||||||
str = str.erase(0 , str.find(" ") + 1);
|
str = str.erase(0 , str.find(" ") + 1);
|
||||||
_version = str;
|
_version = str;
|
||||||
|
_version.erase(_version.find_last_not_of(" \n\r\t") + 1);
|
||||||
if (_URI == "/")
|
if (_URI == "/")
|
||||||
_URI = "www/index2.html";
|
_URI = "www/index2.html";
|
||||||
else
|
else
|
||||||
_URI = HOME + _URI;
|
_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)
|
int Header::parseRequest(void)
|
||||||
{
|
{
|
||||||
std::string line;
|
std::string line;
|
||||||
std::string key;
|
int ret;
|
||||||
int distance;
|
|
||||||
std::stringstream buffStream;
|
std::stringstream buffStream;
|
||||||
|
|
||||||
buffStream << _buff;
|
buffStream << _buff;
|
||||||
distance = 0;
|
ret = 200;
|
||||||
while (std::getline(buffStream, line, '\n'))
|
while (std::getline(buffStream, line, '\n') && ret == 200)
|
||||||
{
|
{
|
||||||
if (_row == 0)
|
if (_row == 0)
|
||||||
parseStartLine(line);
|
ret = parseStartLine(line);
|
||||||
else
|
else
|
||||||
{
|
ret = parseHeaderfield(line);
|
||||||
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;
|
|
||||||
}
|
|
||||||
_row++;
|
_row++;
|
||||||
}
|
}
|
||||||
if (_version != "HTTP/1.1")
|
|
||||||
return (505);
|
|
||||||
else if (_method != "GET" || _method != "POST" || _method != "DELETE")
|
|
||||||
return (405);
|
|
||||||
return (200);
|
return (200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user