105 lines
2.0 KiB
C++
105 lines
2.0 KiB
C++
#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)
|
|
{
|
|
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;
|
|
}
|
|
|
|
std::string itos(int n)
|
|
{
|
|
std::stringstream strs;
|
|
|
|
strs << n;
|
|
return ( strs.str() );
|
|
}
|
|
|
|
std::string parse_env(const std::string & env)
|
|
{
|
|
std::string ret = "";
|
|
char * ret_env;
|
|
|
|
ret_env = getenv(env.c_str());
|
|
if (ret_env != NULL)
|
|
ret = ret_env;
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::string parse_body()
|
|
{
|
|
std::string ret;
|
|
|
|
std::cin >> ret;
|
|
return ret;
|
|
}
|
|
|
|
std::string print_env(char **env, std::string tag)
|
|
{
|
|
std::string ret = "";
|
|
|
|
for (int i = 0; env[i] != NULL; ++i)
|
|
{
|
|
ret += "<" + tag + ">";
|
|
ret += env[i];
|
|
ret += "</" + tag + "><br>";
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string
|
|
print_form(std::string form, std::string tag_key, std::string tag_val)
|
|
{
|
|
std::vector<std::string> split_str;
|
|
std::vector<std::string> sub_split_str;
|
|
std::vector<std::string>::const_iterator it;
|
|
std::string ret = "";
|
|
|
|
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 + ">";
|
|
ret += "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
|
}
|
|
return ret;
|
|
}
|
|
|