mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
add HeaderHandl class
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
#ifndef HEADER
|
||||
# define HEADER
|
||||
#ifndef HEADER_HPP
|
||||
# define HEADER_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
|
||||
class Header
|
||||
{
|
||||
private:
|
||||
HeaderHandl _request;
|
||||
HeaderHandl _respons;
|
||||
|
||||
private:
|
||||
int _row;
|
||||
int _ret;
|
||||
int _autoIndex;
|
||||
int _fd;
|
||||
char *_buff;
|
||||
|
||||
|
||||
std::string _method;
|
||||
std::string _version;
|
||||
std::string _URI;
|
||||
std::string _host;
|
||||
std::string _query;
|
||||
std::string _fileToSend;
|
||||
std::map<std::string, std::string> _headerField;
|
||||
std::map<std::string, std::string> _errorCode;
|
||||
|
||||
public:
|
||||
std::map<std::string , std::string> getRequest(void);
|
||||
std::string getMethod(void);
|
||||
std::string getURI(void);
|
||||
std::map<std::string , std::string> getHeaderField(void);
|
||||
HeaderHandl getRequest(void);
|
||||
HeaderHandl getRespons(void);
|
||||
std::string getReasonPhrase(std::string);
|
||||
std::string getReasonPhrase(int);
|
||||
std::string getErrorPage(int code);
|
||||
@@ -33,16 +33,12 @@ public:
|
||||
int getFd(void);
|
||||
|
||||
void initErrorCode(void);
|
||||
int checkURI(void);
|
||||
int isFile(std::string);
|
||||
int isDir(std::string);
|
||||
void OpenResponsFile(const char *path);
|
||||
|
||||
std::string ltrim(std::string);
|
||||
std::string rtrim(std::string);
|
||||
std::string trim(std::string);
|
||||
|
||||
int parseStartLine(std::string);
|
||||
void parseURI(std::string);
|
||||
int parseHeaderfield(std::string);
|
||||
void printHeaderInfo(void);
|
||||
int parseRequest(void);
|
||||
|
||||
37
includes/HeaderHandl.hpp
Normal file
37
includes/HeaderHandl.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef HEADERHANDL_HPP
|
||||
#define HEADERHANDL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
class HeaderHandl
|
||||
{
|
||||
public:
|
||||
std::string _method;
|
||||
std::string _URI;
|
||||
std::string _fullURI;
|
||||
std::string _version;
|
||||
std::string _query;
|
||||
public:
|
||||
// std::string _allow;
|
||||
// std::string _contentLenght;
|
||||
std::string _contentType;
|
||||
std::string _host;
|
||||
// std::string _contentLanguage;
|
||||
std::string _contentLocation;
|
||||
// std::string _date;
|
||||
// std::string _lastModified;
|
||||
// std::string _transferEncoding;
|
||||
// std::string _server;
|
||||
// std::string _location;
|
||||
public:
|
||||
HeaderHandl();
|
||||
|
||||
void copyData(std::map<std::string, std::string>);
|
||||
|
||||
~HeaderHandl();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -34,7 +34,9 @@
|
||||
#include <dirent.h>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "HeaderHandl.hpp"
|
||||
#include "Autoindex.hpp"
|
||||
#include "Server.hpp"
|
||||
#include "Header.hpp"
|
||||
|
||||
125
src/Header.cpp
125
src/Header.cpp
@@ -6,6 +6,7 @@ Header::Header()
|
||||
{
|
||||
this->_row = 0;
|
||||
this->_fd = -1;
|
||||
this->_autoIndex = 0;
|
||||
initErrorCode();
|
||||
|
||||
}
|
||||
@@ -15,6 +16,7 @@ Header::Header(char *str)
|
||||
this->_row = 0;
|
||||
this->_fd = -1;
|
||||
this->_buff = str;
|
||||
this->_autoIndex = 1;
|
||||
|
||||
initErrorCode();
|
||||
parseRequest();
|
||||
@@ -35,19 +37,19 @@ std::string Header::getErrorPage(int code)
|
||||
return (Page);
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> Header::getRequest(void)
|
||||
std::map<std::string, std::string> Header::getHeaderField(void)
|
||||
{
|
||||
return (this->_headerField);
|
||||
}
|
||||
|
||||
std::string Header::getMethod(void)
|
||||
HeaderHandl Header::getRequest(void)
|
||||
{
|
||||
return (this->_method);
|
||||
return (_request);
|
||||
}
|
||||
|
||||
std::string Header::getURI(void)
|
||||
HeaderHandl Header::getRespons(void)
|
||||
{
|
||||
return _URI;
|
||||
return (_respons);
|
||||
}
|
||||
|
||||
int Header::getFd(void)
|
||||
@@ -67,28 +69,52 @@ void Header::setFd(int fd)
|
||||
|
||||
//-------------------------------------------------Parsing---------------------------------------
|
||||
|
||||
void Header::parseURI(std::string str)
|
||||
{
|
||||
std::string tmp;
|
||||
int pos;
|
||||
|
||||
tmp = str;
|
||||
pos = str.find("?");
|
||||
if (pos > 0)
|
||||
{
|
||||
_request._URI = tmp.substr(0, pos);
|
||||
_request._query = tmp.erase(0, pos + 1);
|
||||
}
|
||||
else
|
||||
_request._URI = str;
|
||||
_request._fullURI = HOME + _request._URI;
|
||||
}
|
||||
|
||||
int Header::parseStartLine(std::string str)
|
||||
{
|
||||
std::string tmp[3];
|
||||
|
||||
_method = str.substr(0, str.find(" "));
|
||||
tmp[0] = str.substr(0, str.find(" "));
|
||||
str = str.erase(0 , str.find(" ") + 1);
|
||||
_URI = str.substr(0, str.find(" "));
|
||||
tmp[1] = 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 (_version != "HTTP/1.1")
|
||||
return (505);
|
||||
else if (_method != "GET" && _method != "POST" && _method != "DELETE")
|
||||
return (405);
|
||||
else if (checkURI() < 0)
|
||||
tmp[2] = str;
|
||||
tmp[2].erase(tmp[2].find_last_not_of(" \n\r\t") + 1);
|
||||
_request._method = tmp[0];
|
||||
parseURI(tmp[1]);
|
||||
_request._version = tmp[2];
|
||||
|
||||
if (_request._version != "HTTP/1.1")
|
||||
_ret = 505;
|
||||
else if (_request._method != "GET" && _request._method != "POST"
|
||||
&& _request._method != "DELETE")
|
||||
_ret = 405;
|
||||
else if (isFile(_request._fullURI) != 0)
|
||||
{
|
||||
if (isDir(HOME + _URI) == 0)
|
||||
return (200);
|
||||
if (isDir(_request._fullURI) == 0 && _autoIndex)
|
||||
_ret = 200;
|
||||
else if (_autoIndex)
|
||||
_ret = 404;
|
||||
else
|
||||
return (404);
|
||||
_ret = 403;
|
||||
}
|
||||
return 200;
|
||||
return (_ret);
|
||||
}
|
||||
|
||||
int Header::parseHeaderfield(std::string str)
|
||||
@@ -113,8 +139,6 @@ int Header::parseHeaderfield(std::string str)
|
||||
value = value.substr(0, value.find_last_not_of(WHITESPACE) + 1);
|
||||
_headerField[key] = value;
|
||||
}
|
||||
if (key == "host")
|
||||
_host = value;
|
||||
return 200;
|
||||
}
|
||||
|
||||
@@ -133,32 +157,13 @@ int Header::parseRequest(void)
|
||||
_ret = parseHeaderfield(line);
|
||||
_row++;
|
||||
}
|
||||
if (_ret == 200)
|
||||
_request.copyData(_headerField);
|
||||
return (_ret);
|
||||
}
|
||||
|
||||
//-------------------------------------------------FILE---------------------------------------
|
||||
|
||||
int Header::checkURI(void)
|
||||
{
|
||||
const char *path;
|
||||
std::string tmp;
|
||||
std::string str;
|
||||
int pos;
|
||||
|
||||
tmp = _URI;
|
||||
pos = _URI.find("?");
|
||||
if (pos > 0)
|
||||
{
|
||||
_URI = tmp.substr(0, pos);
|
||||
_query = tmp.erase(0, pos + 1);
|
||||
}
|
||||
str = HOME + _URI;
|
||||
path = str.c_str();
|
||||
if (isFile(str) < 0)
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int Header::isFile(std::string path)
|
||||
{
|
||||
struct stat s;
|
||||
@@ -220,7 +225,7 @@ int Header::sendHeader(int fd)
|
||||
std::string tmp;
|
||||
const char *header;
|
||||
|
||||
ss << _version << " " << _ret << " " << getReasonPhrase(_ret) << "\r\nContent-Type: text/html\r\n\r\n";
|
||||
ss << _request._version << " " << _ret << " " << getReasonPhrase(_ret) << "\r\nContent-Type: text/html\r\n\r\n";
|
||||
tmp = ss.str();
|
||||
header = tmp.c_str();
|
||||
std::cout << TURGUOISE << "Send Header\n" << YELLOW << tmp << ZERO_C;
|
||||
@@ -232,10 +237,10 @@ int Header::sendRespons(int fd)
|
||||
{
|
||||
std::string path;
|
||||
|
||||
path = HOME + _URI;
|
||||
path = _request._fullURI;
|
||||
sendHeader(fd);
|
||||
if (_ret == 200 && isDir(path) == 0)
|
||||
_fileToSend = Autoindex::getPage(_URI, _host);
|
||||
_fileToSend = Autoindex::getPage(_request._URI, _request._host);
|
||||
else if (_ret == 200)
|
||||
OpenResponsFile(path.c_str());
|
||||
else
|
||||
@@ -314,31 +319,15 @@ std::string Header::getReasonPhrase(int code)
|
||||
|
||||
//-------------------------------------------------Other---------------------------------------
|
||||
|
||||
std::string ltrim(std::string s)
|
||||
{
|
||||
size_t start = s.find_first_not_of(WHITESPACE);
|
||||
return (start == std::string::npos) ? "" : s.substr(start);
|
||||
}
|
||||
|
||||
std::string rtrim(std::string s)
|
||||
{
|
||||
size_t end = s.find_last_not_of(WHITESPACE);
|
||||
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
|
||||
}
|
||||
|
||||
std::string trim(std::string s) {
|
||||
return rtrim(ltrim(s));
|
||||
}
|
||||
|
||||
void Header::printHeaderInfo(void)
|
||||
{
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
|
||||
std::cout << PINK << "request method = " << _method << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request URI = " << _URI << ZERO_C << std::endl;
|
||||
std::cout << PINK << "host = " << _host << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request query = " << _query << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request http versioin = " << _version << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request method = " << _request._method << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request URI = " << _request._URI << ZERO_C << std::endl;
|
||||
std::cout << PINK << "host = " << _request._host << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request query = " << _request._query << ZERO_C << std::endl;
|
||||
std::cout << PINK << "request http versioin = " << _request._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;
|
||||
|
||||
@@ -353,11 +342,11 @@ void Header::printHeaderInfo(void)
|
||||
void Header::clear(void)
|
||||
{
|
||||
_fd = -1;
|
||||
_method = "";
|
||||
_request._method = "";
|
||||
_row = 0;
|
||||
_buff = NULL;
|
||||
_URI = "";
|
||||
_version = "";
|
||||
_request._URI = "";
|
||||
_request._URI = "";
|
||||
_headerField.clear();
|
||||
}
|
||||
|
||||
|
||||
17
src/HeaderHandl.cpp
Normal file
17
src/HeaderHandl.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "HeaderHandl.hpp"
|
||||
|
||||
|
||||
HeaderHandl::HeaderHandl()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
HeaderHandl::~HeaderHandl()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void HeaderHandl::copyData(std::map<std::string, std::string> map)
|
||||
{
|
||||
_host = map.find("host")->second;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "webserv.hpp"
|
||||
#include "Socket.hpp"
|
||||
|
||||
Socket::Socket()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user