# include # include # include # include # include // 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 split(const std::string & input, std::string delim, char ctrim = '\0') { std::vector 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 split_str; std::vector sub_split_str; std::vector::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 = "\ \ \ \ CGI\ \ \

cgi

\ "; http_body += "

"; tmp = getenv("REQUEST_METHOD"); if (tmp != NULL) http_body += tmp; else http_body = "method not foud"; http_body += "

"; split_str = split(input, "&"); for (it = split_str.begin(); it != split_str.end(); ++it) { sub_split_str = split(*it, "="); http_body += "

"; http_body += sub_split_str[0]; http_body += "

"; http_body += "

"; http_body += sub_split_str[1]; http_body += "

"; } http_body += "\ \ \ "; strs << http_body.size(); http_header += strs.str(); http_header += CRLF CRLF; std::cout << http_header << CRLF CRLF << http_body; return 0; }