makefile for cgi_scripts
This commit is contained in:
@@ -1,91 +1,20 @@
|
||||
# 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
|
||||
# include "cgi_utils.hpp"
|
||||
|
||||
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)
|
||||
int main ()
|
||||
{
|
||||
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 = HTML_BODY_TOP;
|
||||
|
||||
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>";
|
||||
http_body += fill_env("REQUEST_METHOD", "h3");
|
||||
|
||||
split_str = split(input, "&");
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
@@ -99,14 +28,11 @@ int main (int ac, char **av, char **en)
|
||||
http_body += "</p>";
|
||||
}
|
||||
|
||||
http_body += "\
|
||||
</body>\
|
||||
</html>\
|
||||
";
|
||||
http_body += HTML_BODY_BOTTOM;
|
||||
|
||||
strs << http_body.size();
|
||||
http_header += strs.str();
|
||||
http_header += CRLF CRLF;
|
||||
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
||||
http_header += "Content-Length: ";
|
||||
http_header += itos(http_body.size());
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user