#ifndef UTILS_HPP # define UTILS_HPP # include # include # include # include # include # include // strtol, strtoul # include // LONG_MAX # include // errno # include // stat() # include // tolower # include // transform # include // perror, fflush # include // 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 split(std::string input, char delimiter); std::vector 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 & fields ); void str_map_key_tolower(std::map & mp); // debug void throw_test(); void print_special(std::string str); /* Template */ template void print_pair(const std::pair p) { std::cout << p.first << ": "; std::cout << p.second << "\n"; } template void print_map(const std::map& c) { typename std::map::const_iterator it = c.begin(); typename std::map::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