litle fix in utils trim

+ added 2 functions to check script output
+ created a html page with 4 forms for tests
+ created 2 cpp forms, with and without creating header content-length
This commit is contained in:
Hugo LAMY
2022-08-15 00:24:08 +02:00
parent d663a4c7e6
commit f6f63931ad
12 changed files with 385 additions and 73 deletions

114
srcs/cgi-bin/cgi_cpp.cpp Normal file
View File

@@ -0,0 +1,114 @@
# include <iostream>
# include <string>
# include <sstream>
# include <vector>
# include <stdlib.h> // getenv
# define CR "\r"
# define LF "\n"
# define CRLF CR LF
# define NPOS std::string::npos
std::string trim(std::string str, char del)
{
size_t pos;
// delete leadings del
pos = str.find_first_not_of(del);
if (pos == NPOS)
pos = str.size();
str = str.substr(pos);
// delete trailing del
pos = str.find_last_not_of(del);
if (pos != NPOS)
str = str.substr(0, pos + 1);
return str;
}
std::vector<std::string>
split(const std::string & input, std::string delim, char ctrim = '\0')
{
std::vector<std::string> split_str;
std::string tmp;
size_t start = 0;
size_t end = 0;
size_t len = 0;
while (end != NPOS)
{
end = input.find(delim, start);
len = end - start;
if (end == 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();
}
return split_str;
}
int main (int ac, char **av, char **en)
{
std::vector<std::string> split_str;
std::vector<std::string> sub_split_str;
std::vector<std::string>::const_iterator it;
char * tmp;
std::string input;
std::string http_header;
std::string http_body;
std::ostringstream strs;
size_t pos;
std::cin >> input;
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
http_header += "Content-Length: ";
http_body = "\
<!DOCTYPE html>\
<html>\
<head>\
<title>CGI</title>\
</head>\
<body>\
<h2>cgi</h2>\
";
http_body += "<h3>";
tmp = getenv("REQUEST_METHOD");
if (tmp != NULL)
http_body += tmp;
else
http_body = "method not foud";
http_body += "</h3>";
split_str = split(input, "&");
for (it = split_str.begin(); it != split_str.end(); ++it)
{
sub_split_str = split(*it, "=");
http_body += "<h3>";
http_body += sub_split_str[0];
http_body += "</h3>";
http_body += "<p>";
http_body += sub_split_str[1];
http_body += "</p>";
}
http_body += "\
</body>\
</html>\
";
strs << http_body.size();
http_header += strs.str();
http_header += CRLF CRLF;
std::cout << http_header << CRLF CRLF << http_body;
return 0;
}