modif and added several scipts for testing cgi

+ modif html page for script tests
+ script output not added to response in case of http error
This commit is contained in:
Hugo LAMY
2022-08-16 01:39:46 +02:00
parent c05536ca01
commit 8c2aff6c6b
14 changed files with 482 additions and 186 deletions

View File

@@ -1,5 +1,11 @@
#include "cgi_utils.hpp"
std::string str_tolower(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
std::string trim(std::string str, char del)
{
size_t pos;
@@ -91,14 +97,99 @@ std::string
std::vector<std::string> sub_split_str;
std::vector<std::string>::const_iterator it;
std::string ret = "";
std::string key;
split_str = split(form, "&");
for (it = split_str.begin(); it != split_str.end(); ++it)
{
sub_split_str = split(*it, "=");
ret += "<br><" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
key = sub_split_str[0];
if (key == "fname")
key = "first name";
else if (key == "lname")
key = "last name";
ret += "<br><" + tag_key + ">" + key + ": </" + tag_key + ">";
ret += "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
}
return ret;
}
std::string get_form_infos()
{
std::string form_infos;
std::string method;
method = parse_env("REQUEST_METHOD");
if (method == "POST")
form_infos = parse_body();
else if (method == "GET")
form_infos = parse_env("QUERY_STRING");
return form_infos;
}
std::string get_value(std::string key)
{
std::string infos;
std::string ret;
size_t pos;
size_t end;
size_t len;
infos = get_form_infos();
pos = str_tolower(infos).find(str_tolower(key));
if (pos == NPOS)
return "";
pos = infos.find_first_of("=", pos);
if (pos == NPOS)
return "";
pos++;
end = infos.find_first_of("&", pos);
if (end == NPOS)
end = infos.size();
len = end - pos;
ret = infos.substr(pos, len);
return ret;
}
void
fill_response_basic(char **env, std::string & http_body, std::string & http_header)
{
std::string rq_method = "not found";
std::string rq_body;
std::string rq_query;
std::string form_infos;
rq_method = parse_env("REQUEST_METHOD");
rq_body = parse_body();
rq_query = parse_env("QUERY_STRING");
if (rq_method == "POST")
form_infos = rq_body;
else if (rq_method == "GET")
form_infos = rq_query;
http_body = HTML_BODY_TOP;
http_body += "<br><h3>method used: </h3>";
http_body += "<p>" + rq_method + "</p>";
http_body += "<br><h3>form body: </h3>";
http_body += "<p>" + rq_body + "</p>";
http_body += "<br><h3>form query: </h3>";
http_body += "<p>" + rq_query + "</p>";
http_body += "<br><br><h3>output:</h3><br>";
http_body += print_form(form_infos, "p", "p");
http_body += "<br><br><h3>cgi_env_variables:</h3><br>";
http_body += print_env(env, "p");
http_body += HTML_BODY_BOTTOM;
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
}