Files
42_INT_12_webserv/srcs/utils.hpp
LuckyLaszlo 3fe37ea451 DELETE method tested and fixed
+ added 404 for directories without index and autoindex
+ minors adjustements
2022-08-15 18:16:11 +02:00

108 lines
2.8 KiB
C++

#ifndef UTILS_HPP
# define UTILS_HPP
# include <vector>
# include <map>
# include <string>
# include <sstream>
# include <iostream>
# include <cstdlib> // strtol, strtoul
# include <climits> // LONG_MAX
# include <cerrno> // errno
# include <sys/stat.h> // stat()
# include <cctype> // tolower
# include <algorithm> // transform
# include <cstdio> // perror, fflush
# include <unistd.h> // close, access
# include "colors.h" // for debug print_special
# define CR "\r"
# define LF "\n"
# define CRLF CR LF
# define CRLF_SIZE 2
# define NPOS std::string::npos
# define KB 1024
# define MB 1048576
/* Equivalent for end of http header size :
** std::string(CRLF CRLF).size();
** sizeof(CRLF CRLF) - 1;
** CRLF_SIZE*2
*/
enum file_type
{
IS_OTHER,
IS_FILE,
IS_DIR
};
enum http_method
{
UNKNOWN = 0b0,
GET = 1 << 0,
POST = 1 << 1,
DELETE = 1 << 2,
ANY_METHODS = 0b11111111,
// ALL_METHODS = 0b11111111,
// i would prefer this...
};
struct listen_socket
{
int fd;
std::string host;
std::string port;
};
bool operator==(const listen_socket& lhs, int fd);
bool operator==(int fd, const listen_socket& rhs);
std::vector<std::string> split(std::string input, char delimiter);
std::vector<std::string> split_trim(std::string input, std::string delim = "\n", char ctrim = '\0');
bool isNumeric(std::string str);
bool isNumeric_btw(int low, int high, std::string str);
std::string itos(int n);
std::string trim(std::string str, char del);
http_method str_to_http_method(std::string &str);
std::string http_methods_to_str(unsigned int methods);
file_type eval_file_type(const std::string &path);
size_t eval_file_mode(const std::string &path, int mode);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
std::string str_tolower(std::string str);
std::string extract_line(std::string & str, size_t pos = 0, std::string delim = "\n");
std::string get_line (std::string str, size_t pos = 0, std::string delim = "\n");
size_t parse_http_headers (std::string headers, std::map<std::string, std::string> & fields );
void str_map_key_tolower(std::map<std::string, std::string> & mp);
// debug
void throw_test();
void print_special(std::string str);
/* Template */
template <typename T1, typename T2 >
void print_pair(const std::pair<T1,T2> p)
{
std::cout << p.first << ": ";
std::cout << p.second << "\n";
}
template <typename Key, typename T >
void print_map(const std::map<Key,T>& c)
{
typename std::map<Key,T>::const_iterator it = c.begin();
typename std::map<Key,T>::const_iterator it_end = c.end();
std::cout << " --print_map():\n";
std::cout << "map.size() = " << c.size() << "\n";
while (it != it_end)
{
print_pair(*it);
++it;
}
std::cout << " --\n";
}
#endif