correct extract_line to avoid duplicate with erase or substr, and to resolve pbm of returning trailing delim
This commit is contained in:
@@ -78,9 +78,13 @@ Client & Client::operator=( Client const & rhs )
|
||||
void Client::parse_request(std::vector<ServerConfig> &servers)
|
||||
{
|
||||
clear_request(); // not mandatory
|
||||
// debug
|
||||
print_client("before");
|
||||
|
||||
_parse_request_line();
|
||||
// debug
|
||||
print_client("first line");
|
||||
|
||||
if (status)
|
||||
return;
|
||||
_parse_request_headers();
|
||||
@@ -167,25 +171,26 @@ void Client::clear_script()
|
||||
// debug
|
||||
void Client::print_client(std::string message)
|
||||
{
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
|
||||
std::cout << "\n=== DEBUG PRINT CLIENT ===\n";
|
||||
std::cout << "\n" << message << ":\n----------\n"
|
||||
<< "raw_request:\n__\n" << raw_request << "\n__\n"
|
||||
<< "get_cl_fd() : " << get_cl_fd() << "\n"
|
||||
<< "get_cl_port() : " << get_cl_port() << "\n"
|
||||
<< "get_cl_ip() : " << get_cl_ip() << "\n"
|
||||
<< "get_rq_method_str() : " << get_rq_method_str() << "\n"
|
||||
<< "get_rq_uri() : " << get_rq_uri() << "\n"
|
||||
<< "get_rq_abs_path() : " << get_rq_abs_path() << "\n"
|
||||
<< "get_rq_query() : " << get_rq_query() << "\n"
|
||||
<< "get_rq_version() : " << get_rq_version() << "\n"
|
||||
<< "get_rq_body() : " << get_rq_body() << "\n"
|
||||
<< "get_rq_port() : " << get_rq_port() << "\n"
|
||||
<< "get_rq_hostname() : " << get_rq_hostname() << "\n"
|
||||
<< "get_rq_script_path() : " << get_rq_script_path() << "\n"
|
||||
<< "get_rq_script_info() : " << get_rq_script_info() << "\n"
|
||||
<< "headers : " << "\n";
|
||||
std::cout << "\n" << message << ":\n----------\n" << "raw_request:\n__\n";
|
||||
::print_special(raw_request);
|
||||
std::cout << "\n__\n"
|
||||
<< "get_cl_fd() : [" << get_cl_fd() << "]\n"
|
||||
<< "get_cl_port() : [" << get_cl_port() << "]\n"
|
||||
<< "get_cl_ip() : [" << get_cl_ip() << "]\n"
|
||||
<< "get_rq_method_str() : [" << get_rq_method_str() << "]\n"
|
||||
<< "get_rq_uri() : [" << get_rq_uri() << "]\n"
|
||||
<< "get_rq_abs_path() : [" << get_rq_abs_path() << "]\n"
|
||||
<< "get_rq_query() : [" << get_rq_query() << "]\n"
|
||||
<< "get_rq_version() : [" << get_rq_version() << "]\n"
|
||||
<< "get_rq_body() : [" << get_rq_body() << "]\n"
|
||||
<< "get_rq_port() : [" << get_rq_port() << "]\n"
|
||||
<< "get_rq_hostname() : [" << get_rq_hostname() << "]\n"
|
||||
<< "get_rq_script_path() : [" << get_rq_script_path() << "]\n"
|
||||
<< "get_rq_script_info() : [" << get_rq_script_info() << "]\n"
|
||||
<< "headers :\n";
|
||||
for (it = _request.headers.begin(); it != _request.headers.end(); it++)
|
||||
std::cout << " " << it->first << ": " << it->second << "\n";
|
||||
std::cout << "\n=== END DEBUG ===\n\n";
|
||||
@@ -235,7 +240,13 @@ void Client::_parse_request_line()
|
||||
std::string raw_line;
|
||||
|
||||
raw_line = ::get_line(raw_request, 0, CRLF);
|
||||
line = ::split_trim(raw_line, " ", ' ');
|
||||
|
||||
// DEBUG
|
||||
std::cout << "raw_line:[";
|
||||
::print_special(raw_line);
|
||||
std::cout << "]\n";
|
||||
|
||||
line = ::split_trim(raw_line, " ");
|
||||
if (line.size() != 3)
|
||||
{
|
||||
std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n";
|
||||
|
||||
@@ -30,25 +30,27 @@ 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')
|
||||
split_trim(std::string input, std::string delim, char ctrim)
|
||||
{
|
||||
std::vector<std::string> split_str;
|
||||
std::string tmp;
|
||||
int start = 0;
|
||||
int end;
|
||||
size_t start = 0;
|
||||
size_t end = 0;
|
||||
size_t len = 0;
|
||||
|
||||
end = input.find(delim);
|
||||
while (end != -1)
|
||||
while (end != std::string::npos)
|
||||
{
|
||||
tmp = input.substr(start, end - start);
|
||||
end = input.find(delim, start);
|
||||
len = end - start;
|
||||
if (end == std::string::npos)
|
||||
len = end;
|
||||
tmp = input.substr(start, len);
|
||||
if (ctrim != '\0')
|
||||
tmp = trim(tmp, ctrim);
|
||||
if (tmp.size() != 0)
|
||||
split_str.push_back( tmp );
|
||||
start = end + delim.size();
|
||||
end = input.find(delim, start);
|
||||
}
|
||||
split_str.push_back( input.substr(start, end - start) );
|
||||
return split_str;
|
||||
}
|
||||
|
||||
@@ -70,6 +72,19 @@ std::string trim(std::string str, char del)
|
||||
return str;
|
||||
}
|
||||
|
||||
//// trim a set of char
|
||||
//std::string trim(std::string str, std::string del)
|
||||
//{
|
||||
// std::string new_str;
|
||||
//
|
||||
// while (new_str.compare(str) != 0)
|
||||
// {
|
||||
// for (size_t i = 0; i < del.size(); i++)
|
||||
// trim(str, del[i]);
|
||||
// }
|
||||
// return str;
|
||||
//}
|
||||
|
||||
std::string itos(int n)
|
||||
{
|
||||
std::stringstream strs;
|
||||
@@ -160,7 +175,11 @@ int path_is_valid(std::string path)
|
||||
}
|
||||
|
||||
|
||||
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr)
|
||||
void
|
||||
replace_all_substr(
|
||||
std::string &str,
|
||||
const std::string &ori_substr,
|
||||
const std::string &new_substr)
|
||||
{
|
||||
if (ori_substr.empty())
|
||||
return;
|
||||
@@ -184,13 +203,13 @@ std::string str_tolower(std::string str)
|
||||
// identify a line in a string, by delim (ex. '\n')
|
||||
// delete this line from the string
|
||||
// and return the deleted line
|
||||
// if delim is empty, the extraction begin exactly at pos, and end at npos
|
||||
std::string
|
||||
extract_line(std::string & str, size_t pos = 0, std::string delim = "")
|
||||
extract_line(std::string & str, size_t pos, std::string delim)
|
||||
{
|
||||
std::string del_str;
|
||||
size_t begin;
|
||||
size_t end;
|
||||
size_t len;
|
||||
|
||||
begin = str.rfind(delim, pos);
|
||||
if (begin == std::string::npos)
|
||||
@@ -199,38 +218,23 @@ std::string
|
||||
begin += delim.size();
|
||||
|
||||
end = str.find(delim, pos);
|
||||
len = end;
|
||||
if (end != std::string::npos)
|
||||
end += delim.size();
|
||||
if (delim.empty())
|
||||
end = std::string::npos;
|
||||
len = end - begin;
|
||||
|
||||
del_str = str.substr(begin, end - begin);
|
||||
str.erase(begin, end - begin);
|
||||
del_str = str.substr(begin, len);
|
||||
str.erase(begin, len);
|
||||
return del_str;
|
||||
}
|
||||
|
||||
// get a line in a string, by delim
|
||||
// same as extract, except it doesn't delete it
|
||||
std::string get_line(std::string str, size_t pos = 0, std::string delim = "")
|
||||
std::string get_line(std::string str, size_t pos, std::string delim)
|
||||
{
|
||||
std::string ret_str;
|
||||
size_t begin;
|
||||
size_t end;
|
||||
std::string ret;
|
||||
|
||||
begin = str.rfind(delim, pos);
|
||||
if (begin == std::string::npos)
|
||||
begin = 0;
|
||||
else
|
||||
begin += delim.size();
|
||||
|
||||
end = str.find(delim, pos);
|
||||
if (end != std::string::npos)
|
||||
end += delim.size();
|
||||
if (delim.empty())
|
||||
end = std::string::npos;
|
||||
|
||||
ret_str = str.substr(begin, end - begin);
|
||||
return ret_str;
|
||||
ret = ::extract_line(str, pos, delim);
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t
|
||||
@@ -271,6 +275,24 @@ size_t
|
||||
return err;
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
void print_special(std::string str)
|
||||
{
|
||||
char c;
|
||||
|
||||
for (size_t i = 0; i < str.size(); i++)
|
||||
{
|
||||
c = str[i];
|
||||
if (c == '\r')
|
||||
std::cout << "\\r";
|
||||
else if (c == '\n')
|
||||
std::cout << "\\n" << "\n";
|
||||
else
|
||||
std::cout << c;
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const listen_socket& lhs, int fd)
|
||||
{ return lhs.fd == fd; }
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
# include <sys/stat.h> // stat()
|
||||
# include <cctype> // tolower
|
||||
# include <algorithm> // transform
|
||||
# include <cstdio> // fflush
|
||||
|
||||
# define CR "\r"
|
||||
# define LF "\n"
|
||||
@@ -36,7 +37,7 @@ 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 s, std::string d, char c);
|
||||
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);
|
||||
@@ -46,9 +47,11 @@ std::string http_methods_to_str(unsigned int methods);
|
||||
int path_is_valid(std::string path);
|
||||
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 & s, size_t p, std::string d);
|
||||
std::string get_line(std::string str, size_t pos, std::string del);
|
||||
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 throw_test();
|
||||
// debug
|
||||
void print_special(std::string str);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user