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:
133
srcs/cgi-bin/cgi_cpp_content_length.cpp
Normal file
133
srcs/cgi-bin/cgi_cpp_content_length.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
# 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 output;
|
||||
std::ostringstream strs;
|
||||
size_t pos;
|
||||
|
||||
std::cout << "Content-Type: text/html; charset=UTF-8" << CRLF CRLF;
|
||||
|
||||
std::cout
|
||||
<< "<!DOCTYPE html>"
|
||||
<< "<html>"
|
||||
<< "<head>"
|
||||
<< " <title>CGI</title>"
|
||||
<< "</head>"
|
||||
<< "<body>"
|
||||
<< " <h2>cgi</h2>"
|
||||
<< " <h3>";
|
||||
|
||||
tmp = getenv("REQUEST_METHOD");
|
||||
if (tmp != NULL)
|
||||
output = tmp;
|
||||
else
|
||||
output = "method not foud";
|
||||
|
||||
std::cout
|
||||
<< output
|
||||
<< " </h3>"
|
||||
<< " <h3>http-request-body-message content :</h3>";
|
||||
|
||||
|
||||
std::cin >> output;
|
||||
split_str = split(output, "&");
|
||||
output.clear();
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
{
|
||||
sub_split_str = split(*it, "=");
|
||||
|
||||
std::cout
|
||||
<< "<p>"
|
||||
<< sub_split_str[0]
|
||||
<< " : "
|
||||
<< sub_split_str[1]
|
||||
<< "</p>";
|
||||
}
|
||||
|
||||
tmp = getenv("QUERY_STRING");
|
||||
if (tmp == NULL)
|
||||
std::cout << "query not foud";
|
||||
|
||||
std::cout
|
||||
<< " <h3>http-uri-query content :</h3>";
|
||||
|
||||
output = tmp;
|
||||
split_str = split(output, "&");
|
||||
output.clear();
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
{
|
||||
sub_split_str = split(*it, "=");
|
||||
|
||||
std::cout
|
||||
<< "<h3>"
|
||||
<< sub_split_str[0]
|
||||
<< "</h3>"
|
||||
<< "<p>"
|
||||
<< sub_split_str[1]
|
||||
<< "</p>";
|
||||
}
|
||||
|
||||
|
||||
std::cout
|
||||
<< "</body>"
|
||||
<< "</html>";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user