correction of trim in case of str only fill with delim char
+ added a new split, that also does trim, to split without counting newlines
This commit is contained in:
@@ -200,10 +200,12 @@ std::string Client::get_rq_headers(const std::string & key) const
|
|||||||
void Client::_parse_request_line()
|
void Client::_parse_request_line()
|
||||||
{
|
{
|
||||||
std::vector<std::string> line;
|
std::vector<std::string> line;
|
||||||
|
std::string raw_line;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = ::parse_http_first_line(raw_request, line);
|
raw_line = extract_line();
|
||||||
if (ret != 3)
|
line = ::parse_http_first_line(raw_line);
|
||||||
|
if (line.size() != 3)
|
||||||
{
|
{
|
||||||
std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n";
|
std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n";
|
||||||
status = 400; // "bad request"
|
status = 400; // "bad request"
|
||||||
|
|||||||
@@ -21,21 +21,42 @@ std::vector<std::string> split(std::string input, char delimiter)
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string trim(std::string str, char c)
|
std::vector<std::string>
|
||||||
|
split_trim(std::string input, std::string delim = "\n", char ctrim = '')
|
||||||
|
{
|
||||||
|
std::vector<std::string> split_str;
|
||||||
|
std::string tmp;
|
||||||
|
size_t start = 0;
|
||||||
|
size_t end;
|
||||||
|
|
||||||
|
end = input.find(delim);
|
||||||
|
while (end != -1)
|
||||||
|
{
|
||||||
|
tmp = input.substr(start, end - start);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string trim(std::string str, char del)
|
||||||
{
|
{
|
||||||
size_t pos;
|
size_t pos;
|
||||||
|
|
||||||
// delete leadings c
|
// delete leadings del
|
||||||
pos = str.find_first_not_of(c);
|
pos = str.find_first_not_of(del);
|
||||||
if (pos == std::string::npos)
|
if (pos == std::string::npos)
|
||||||
return str;
|
pos = str.size();
|
||||||
str = str.substr(pos);
|
str = str.substr(pos);
|
||||||
|
|
||||||
// delete endings c
|
// delete trailing del
|
||||||
pos = str.find_last_not_of(c);
|
pos = str.find_last_not_of(del);
|
||||||
if (pos == std::string::npos)
|
if (pos != std::string::npos)
|
||||||
return str;
|
str = str.substr(0, pos + 1);
|
||||||
str = str.substr(0, pos + 1);
|
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@@ -177,14 +198,6 @@ std::string extract_line(std::string * str, size_t pos, std::string delim)
|
|||||||
return del_str;
|
return del_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform a str, like a http header, into a map
|
|
||||||
// with <keys> delim <values>
|
|
||||||
// and perform an action on keys and values
|
|
||||||
// action receives address of keys and values, and return bool error :
|
|
||||||
// bool action(&keys, &values)
|
|
||||||
//std::map<std:string, std::string>
|
|
||||||
// str_to_map(str, delim, action = NULL)
|
|
||||||
|
|
||||||
bool operator==(const listen_socket& lhs, int fd)
|
bool operator==(const listen_socket& lhs, int fd)
|
||||||
{ return lhs.fd == fd; }
|
{ return lhs.fd == fd; }
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ std::vector<std::string> split(std::string input, char delimiter);
|
|||||||
bool isNumeric(std::string str);
|
bool isNumeric(std::string str);
|
||||||
bool isNumeric_btw(int low, int high, std::string str);
|
bool isNumeric_btw(int low, int high, std::string str);
|
||||||
std::string itos(int n);
|
std::string itos(int n);
|
||||||
std::string trim(std::string str, char c);
|
std::string trim(std::string str, char del);
|
||||||
http_method str_to_http_method(std::string &str);
|
http_method str_to_http_method(std::string &str);
|
||||||
std::string http_methods_to_str(unsigned int methods);
|
std::string http_methods_to_str(unsigned int methods);
|
||||||
int path_is_valid(std::string path);
|
int path_is_valid(std::string path);
|
||||||
|
|||||||
@@ -1,29 +1,36 @@
|
|||||||
|
|
||||||
#include "parsing_message_http.hpp"
|
#include "parsing_message_http.hpp"
|
||||||
|
|
||||||
size_t
|
/*
|
||||||
parse_http_first_line(std::string message, std::vector<std::string> &line)
|
- parse_http_first_line() :
|
||||||
|
- copy first line into a vector of 3 strings
|
||||||
|
/ copy line into a vector of 3 strings
|
||||||
|
- parse_http_headers(std::string message)
|
||||||
|
-
|
||||||
|
- parse_http_body(std::string message)
|
||||||
|
*/
|
||||||
|
|
||||||
|
std::vector<std::string>
|
||||||
|
parse_http_first_line(std::string line)
|
||||||
{
|
{
|
||||||
std::vector<std::string> sline;
|
std::vector<std::string> sline;
|
||||||
|
std::vector<std::string> line = "";
|
||||||
std::string sub;
|
std::string sub;
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
size_t ret;
|
|
||||||
|
|
||||||
// TODO: check for err in substr
|
sline = ::split(line, ' ');
|
||||||
pos = message.find(CRLF);
|
if (sline.size() == 3)
|
||||||
sub = message.substr(0, pos);
|
|
||||||
sline = ::split(sub, ' ');
|
|
||||||
ret = sline.size();
|
|
||||||
if (ret != 3)
|
|
||||||
return ret;
|
|
||||||
for (int i = 0; i < 3; i++)
|
|
||||||
{
|
{
|
||||||
tmp = ::trim(sline[i], ' ');
|
for (int i = 0; i < 3; i++)
|
||||||
tmp = ::trim(tmp, '\r');
|
{
|
||||||
line.push_back(tmp);
|
tmp = sline[i];
|
||||||
|
tmp = ::trim(tmp, '\r');
|
||||||
|
tmp = ::trim(tmp, ' ');
|
||||||
|
line.push_back(tmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string>
|
std::map<std::string, std::string>
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
# include <map>
|
# include <map>
|
||||||
# include "utils.hpp"
|
# include "utils.hpp"
|
||||||
|
|
||||||
size_t
|
std::vector<std::string>
|
||||||
parse_http_first_line(std::string message, std::vector<std::string> &line);
|
parse_http_first_line(std::string message);
|
||||||
|
|
||||||
std::map<std::string, std::string>
|
std::map<std::string, std::string>
|
||||||
parse_http_headers(std::string message);
|
parse_http_headers(std::string message);
|
||||||
|
|||||||
Reference in New Issue
Block a user