NPOS macro

This commit is contained in:
LuckyLaszlo
2022-08-12 18:16:49 +02:00
parent b44acafefe
commit c7bbf29a1b
9 changed files with 43 additions and 43 deletions

View File

@@ -9,7 +9,6 @@ IN 42 SUBJECT AND/OR PRIORITY :
- Ecrire des tests ! - Ecrire des tests !
- handle redirection (Work, but weird behavior need deeper test) - handle redirection (Work, but weird behavior need deeper test)
- _determine_location() review (New version to complete and test) - _determine_location() review (New version to complete and test)
- replace std::string::npos with macro NPOS ?
----------------------------- -----------------------------
Si ce n'est pas deja fait : Si ce n'est pas deja fait :
- dans config, check erreur si port > 16bits - dans config, check erreur si port > 16bits

View File

@@ -352,7 +352,7 @@ void Client::_parse_request_target( std::string target )
size_t pos; size_t pos;
pos = target.find("?"); pos = target.find("?");
if (pos != std::string::npos) if (pos != NPOS)
_request.query = target.substr(pos + 1); _request.query = target.substr(pos + 1);
else else
_request.query = ""; _request.query = "";
@@ -368,11 +368,11 @@ void Client::_parse_request_fields()
headers = raw_request; headers = raw_request;
// delete first line // delete first line
pos = headers.find(CRLF); pos = headers.find(CRLF);
if (pos != std::string::npos) if (pos != NPOS)
headers.erase(0, pos + std::string(CRLF).size()); headers.erase(0, pos + std::string(CRLF).size());
// delete body part // delete body part
pos = headers.find(CRLF CRLF); pos = headers.find(CRLF CRLF);
if (pos != std::string::npos) if (pos != NPOS)
headers.erase(pos); headers.erase(pos);
else { else {
std::cerr << "err _parse_request_fields(): request header doesn't end with empty line\n"; std::cerr << "err _parse_request_fields(): request header doesn't end with empty line\n";
@@ -396,7 +396,7 @@ void Client::_parse_port_hostname(std::string host)
pos = host.find(':'); pos = host.find(':');
// port : // port :
if (pos == std::string::npos) if (pos == NPOS)
_request.port = "4040"; // TODO: make equal to default port in config _request.port = "4040"; // TODO: make equal to default port in config
else else
_request.port = host.substr(pos); _request.port = host.substr(pos);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 16:08:00 by me #+# #+# */ /* Created: 2022/07/23 16:08:00 by me #+# #+# */
/* Updated: 2022/08/04 19:32:40 by erlazo ### ########.fr */ /* Updated: 2022/08/12 18:12:23 by lperrey ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -65,7 +65,7 @@ public:
int comp_rhs = 0; int comp_rhs = 0;
size_t tmp = 0; size_t tmp = 0;
while ((tmp = this->path.find_first_of("/", tmp)) != std::string::npos) while ((tmp = this->path.find_first_of("/", tmp)) != NPOS)
{ {
++tmp; ++tmp;
++comp_lhs; ++comp_lhs;
@@ -73,7 +73,7 @@ public:
if (path[path.find_last_of("/") + 1] != '\0') if (path[path.find_last_of("/") + 1] != '\0')
++comp_lhs; ++comp_lhs;
tmp = 0; tmp = 0;
while ((tmp = rhs.path.find_first_of("/", tmp)) != std::string::npos) while ((tmp = rhs.path.find_first_of("/", tmp)) != NPOS)
{ {
++tmp; ++tmp;
++comp_rhs; ++comp_rhs;

View File

@@ -13,10 +13,10 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
// check values for ; at end and right number of words depending on key // check values for ; at end and right number of words depending on key
// std::cout << "pre check\n"; // std::cout << "pre check\n";
if (key.find_first_of(";") != std::string::npos) if (key.find_first_of(";") != NPOS)
throw std::invalid_argument("bad config file arguments 2"); throw std::invalid_argument("bad config file arguments 2");
if (value.find_first_of("\t") != std::string::npos) if (value.find_first_of("\t") != NPOS)
throw std::invalid_argument("why would you put tabs between values"); throw std::invalid_argument("why would you put tabs between values");
size_t i = value.find_first_of(";"); size_t i = value.find_first_of(";");
@@ -24,7 +24,7 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
// you can't have just ; // you can't have just ;
// and you can't have a ; not at the end or several ; // and you can't have a ; not at the end or several ;
// in theory value_find_last_of should find the only ; // in theory value_find_last_of should find the only ;
if (i == std::string::npos || (value.find_last_not_of(" \n")) != i \ if (i == NPOS || (value.find_last_not_of(" \n")) != i \
|| value.compare(";") == 0) || value.compare(";") == 0)
throw std::invalid_argument("bad config file arguments 4"); throw std::invalid_argument("bad config file arguments 4");
@@ -38,9 +38,9 @@ std::string ConfigParser::_get_first_word(size_t *curr)
{ {
size_t start; size_t start;
if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos) if ((start = _content.find_first_not_of(" \t\n", *curr)) == NPOS)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
if ((*curr = _content.find_first_of(" \t\n", start)) == std::string::npos) if ((*curr = _content.find_first_of(" \t\n", start)) == NPOS)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
std::string key = _content.substr(start, *curr - start); std::string key = _content.substr(start, *curr - start);
@@ -53,9 +53,9 @@ std::string ConfigParser::_get_rest_of_line(size_t *curr)
{ {
size_t start; size_t start;
if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos) if ((start = _content.find_first_not_of(" \t\n", *curr)) == NPOS)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
if ((*curr = _content.find_first_of("\n", start)) == std::string::npos) if ((*curr = _content.find_first_of("\n", start)) == NPOS)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
std::string values = _content.substr(start, *curr - start); std::string values = _content.substr(start, *curr - start);

View File

@@ -24,10 +24,10 @@ ConfigParser::ConfigParser(const char* path)
{ {
getline(file, buf); getline(file, buf);
// remove # comments here. // remove # comments here.
if ((comment = buf.find_first_of("#")) == std::string::npos) if ((comment = buf.find_first_of("#")) == NPOS)
{ {
// remove empty lines, i think... // remove empty lines, i think...
if ((buf.find_first_not_of(" \t")) != std::string::npos) if ((buf.find_first_not_of(" \t")) != NPOS)
_content.append(buf + '\n'); _content.append(buf + '\n');
} }
else if (comment > 0 && (buf.find_first_not_of(" \t")) < comment) else if (comment > 0 && (buf.find_first_not_of(" \t")) < comment)
@@ -67,14 +67,14 @@ std::vector<ServerConfig> * ConfigParser::parse()
size_t start = 0; size_t start = 0;
size_t curr = _content.find_first_not_of(" \t\n", 0); size_t curr = _content.find_first_not_of(" \t\n", 0);
if (curr == std::string::npos) if (curr == NPOS)
throw std::invalid_argument("empty config file"); throw std::invalid_argument("empty config file");
while (curr != std::string::npos) while (curr != NPOS)
{ {
if ((start = _content.find_first_not_of(" \t\n", curr)) == std::string::npos) if ((start = _content.find_first_not_of(" \t\n", curr)) == NPOS)
throw std::invalid_argument("empty config file"); throw std::invalid_argument("empty config file");
if ((curr = _content.find_first_of(" \t\n", start)) == std::string::npos) if ((curr = _content.find_first_of(" \t\n", start)) == NPOS)
throw std::invalid_argument("empty config file"); throw std::invalid_argument("empty config file");
std::string key = _content.substr(start, curr - start); std::string key = _content.substr(start, curr - start);
if (key != "server") if (key != "server")
@@ -91,13 +91,13 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
size_t curr = _content.find_first_not_of(" \t\n", *start); size_t curr = _content.find_first_not_of(" \t\n", *start);
ret.client_body_limit = 0; ret.client_body_limit = 0;
if (curr == std::string::npos || _content[curr] != '{') if (curr == NPOS || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 1"); throw std::invalid_argument("bad config file syntax 1");
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == std::string::npos) if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax"); throw std::invalid_argument("bad config file syntax");
// are there other things to check for? // are there other things to check for?
while (curr != std::string::npos) // here curr == { + 1 while (curr != NPOS) // here curr == { + 1
{ {
// so this moves curr to past the word... // so this moves curr to past the word...
std::string key = _get_first_word(&curr); std::string key = _get_first_word(&curr);
@@ -140,12 +140,12 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
curr = _content.find_first_not_of(" \t\n", curr); curr = _content.find_first_not_of(" \t\n", curr);
if (curr == std::string::npos || _content[curr] != '{') if (curr == NPOS || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 2"); throw std::invalid_argument("bad config file syntax 2");
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == std::string::npos) if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax"); throw std::invalid_argument("bad config file syntax");
while (curr != std::string::npos) while (curr != NPOS)
{ {
// so this moves curr to past the word... // so this moves curr to past the word...
std::string key = _get_first_word(&curr); std::string key = _get_first_word(&curr);
@@ -192,7 +192,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
else if (key == "listen" && size == 1 && server->host == "" \ else if (key == "listen" && size == 1 && server->host == "" \
&& server->port == "") && server->port == "")
{ {
if (tmp_val[0].find_first_of(":") == std::string::npos) if (tmp_val[0].find_first_of(":") == NPOS)
{ {
if (!::isNumeric(tmp_val[0])) if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("bad port number"); throw std::invalid_argument("bad port number");

View File

@@ -38,11 +38,11 @@ std::vector<std::string>
size_t end = 0; size_t end = 0;
size_t len = 0; size_t len = 0;
while (end != std::string::npos) while (end != NPOS)
{ {
end = input.find(delim, start); end = input.find(delim, start);
len = end - start; len = end - start;
if (end == std::string::npos) if (end == NPOS)
len = end; len = end;
tmp = input.substr(start, len); tmp = input.substr(start, len);
if (ctrim != '\0') if (ctrim != '\0')
@@ -60,13 +60,13 @@ std::string trim(std::string str, char del)
// delete leadings del // delete leadings del
pos = str.find_first_not_of(del); pos = str.find_first_not_of(del);
if (pos == std::string::npos) if (pos == NPOS)
pos = str.size(); pos = str.size();
str = str.substr(pos); str = str.substr(pos);
// delete trailing del // delete trailing del
pos = str.find_last_not_of(del); pos = str.find_last_not_of(del);
if (pos != std::string::npos) if (pos != NPOS)
str = str.substr(0, pos + 1); str = str.substr(0, pos + 1);
return str; return str;
@@ -184,7 +184,7 @@ void
while (1) while (1)
{ {
pos = str.find(ori_substr, pos); pos = str.find(ori_substr, pos);
if (pos == std::string::npos) if (pos == NPOS)
break; break;
str.replace(pos, ori_substr.size(), new_substr); str.replace(pos, ori_substr.size(), new_substr);
pos += new_substr.size(); pos += new_substr.size();
@@ -209,14 +209,14 @@ std::string
size_t len; size_t len;
begin = str.rfind(delim, pos); begin = str.rfind(delim, pos);
if (begin == std::string::npos) if (begin == NPOS)
begin = 0; begin = 0;
else else
begin += delim.size(); begin += delim.size();
end = str.find(delim, pos); end = str.find(delim, pos);
len = end; len = end;
if (end != std::string::npos) if (end != NPOS)
len = end - begin; len = end - begin;
del_str = str.substr(begin, len); del_str = str.substr(begin, len);
@@ -253,13 +253,13 @@ size_t
for (it = list.begin(); it != it_end; it++) for (it = list.begin(); it != it_end; it++)
{ {
pos = (*it).find(':'); pos = (*it).find(':');
if (pos == std::string::npos) if (pos == NPOS)
{ {
err++; err++;
continue; continue;
} }
key = (*it).substr(0, pos); key = (*it).substr(0, pos);
if ( key.find(' ') != std::string::npos ) if ( key.find(' ') != NPOS )
{ {
err++; err++;
continue; continue;

View File

@@ -157,7 +157,7 @@ void Webserv::_check_script_status(Client *client, std::string & output)
int status_pos; int status_pos;
pos = output.find("Status:"); pos = output.find("Status:");
if (pos != std::string::npos) if (pos != NPOS)
{ {
status_pos = pos + std::string("Status:").size(); status_pos = pos + std::string("Status:").size();
client->status = std::strtoul(output.c_str() + status_pos, NULL, 10); client->status = std::strtoul(output.c_str() + status_pos, NULL, 10);
@@ -179,13 +179,13 @@ void Webserv::_check_script_fields(Client *client, std::string & output)
// put server headers in map // put server headers in map
tmp = client->response; tmp = client->response;
pos = tmp.find(CRLF CRLF); pos = tmp.find(CRLF CRLF);
if (pos != std::string::npos) if (pos != NPOS)
tmp.erase(pos); tmp.erase(pos);
::parse_http_headers(tmp, srv_fld); ::parse_http_headers(tmp, srv_fld);
// put script headers in map // put script headers in map
tmp = output; tmp = output;
pos = tmp.find(CRLF CRLF); pos = tmp.find(CRLF CRLF);
if (pos != std::string::npos) if (pos != NPOS)
tmp.erase(pos); tmp.erase(pos);
::parse_http_headers(tmp, scr_fld); ::parse_http_headers(tmp, scr_fld);
// compare both map to supress duplicates // compare both map to supress duplicates

View File

@@ -61,7 +61,8 @@ int Webserv::_read_request(Client *client)
{ {
if (client->get_rq_headers("Content-Type").empty() if (client->get_rq_headers("Content-Type").empty()
&& client->get_rq_headers("Content-Length").empty() && client->get_rq_headers("Content-Length").empty()
&& client->get_rq_headers("Transfer-Encoding").empty()) // && client->get_rq_headers("Transfer-Encoding").empty()
)
return READ_COMPLETE; // No body case return READ_COMPLETE; // No body case
} }
else if (client->raw_request.size() > MAX_HEADER_SIZE) else if (client->raw_request.size() > MAX_HEADER_SIZE)

View File

@@ -133,7 +133,7 @@ void Webserv::_append_body(Client *client, const std::string &body, const std::s
else else
{ {
client->response.append(mime_type); client->response.append(mime_type);
if (mime_type.find("text/") != std::string::npos) if (mime_type.find("text/") != NPOS)
client->response.append("; charset=UTF-8"); client->response.append("; charset=UTF-8");
} }
client->response.append(CRLF); client->response.append(CRLF);
@@ -251,7 +251,7 @@ If we get a url that ends in / ignore the last /
std::string Webserv::_determine_file_extension(const std::string &path) const std::string Webserv::_determine_file_extension(const std::string &path) const
{ {
size_t dot_pos = path.rfind("."); size_t dot_pos = path.rfind(".");
if (dot_pos != std::string::npos && dot_pos + 1 < path.size()) if (dot_pos != NPOS && dot_pos + 1 < path.size())
return ( path.substr(dot_pos + 1) ); return ( path.substr(dot_pos + 1) );
return (std::string("")); return (std::string(""));
} }