basic cgi script test working
This commit is contained in:
@@ -3,51 +3,49 @@
|
||||
|
||||
int main (int ac, char **av, char ** env)
|
||||
{
|
||||
std::string http_req_body;
|
||||
std::string http_resp_header;
|
||||
std::string http_resp_body;
|
||||
std::string rq_method = "not found";
|
||||
std::string rq_body = "";
|
||||
std::string rq_query = "";
|
||||
std::string form_infos = "";
|
||||
std::string http_header = "";
|
||||
std::string http_body = "";
|
||||
|
||||
std::string tmp;
|
||||
(void)ac;
|
||||
(void)av;
|
||||
(void)env;
|
||||
|
||||
/*
|
||||
rq_method = find_method();
|
||||
rq_body = parse_body();
|
||||
rq_query = parse_query();
|
||||
rq_method = parse_env("REQUEST_METHOD");
|
||||
rq_body = parse_body();
|
||||
rq_query = parse_env("QUERY_STRING");
|
||||
|
||||
method used : GET
|
||||
form query : key=val&key=val
|
||||
form body : EMPTY
|
||||
output :
|
||||
first name: John
|
||||
last name: Doe
|
||||
cgi env variables :
|
||||
CONTENT_TYPE: value
|
||||
...
|
||||
*/
|
||||
tmp = "<p>PRINT ENV _________________</p>";
|
||||
for (int i = 0; env[i] != NULL; ++i)
|
||||
{
|
||||
tmp += "<p>";
|
||||
tmp += env[i];
|
||||
tmp += "</p>";
|
||||
}
|
||||
tmp += "<p>___________________________</p>";
|
||||
if (rq_method == "POST")
|
||||
form_infos = rq_body;
|
||||
else if (rq_method == "GET")
|
||||
form_infos = rq_query;
|
||||
|
||||
http_req_body = parse_form_infos();
|
||||
http_body = HTML_BODY_TOP;
|
||||
|
||||
http_resp_body = HTML_BODY_TOP;
|
||||
http_resp_body += fill_tag("ENV:", "h3") + fill_env("REQUEST_METHOD");
|
||||
http_resp_body += fill_form(http_req_body, "h3", "p");
|
||||
http_resp_body += tmp;
|
||||
http_resp_body += HTML_BODY_BOTTOM;
|
||||
http_body += "<br><h3>method used: </h3>";
|
||||
http_body += "<p>" + rq_method + "</p>";
|
||||
|
||||
http_resp_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
||||
http_resp_header += "Content-Length: " + itos(http_resp_body.size());
|
||||
http_body += "<br><h3>form body: </h3>";
|
||||
http_body += "<p>" + rq_body + "</p>";
|
||||
|
||||
http_body += "<br><h3>form query: </h3>";
|
||||
http_body += "<p>" + rq_query + "</p>";
|
||||
|
||||
http_body += "<br><br><h3>output:</h3><br>";
|
||||
http_body += print_form(form_infos, "p", "p");
|
||||
|
||||
http_body += "<br><br><h3>cgi_env_variables:</h3><br>";
|
||||
http_body += print_env(env, "p");
|
||||
|
||||
http_body += HTML_BODY_BOTTOM;
|
||||
|
||||
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
||||
http_header += "Content-Length: " + itos(http_body.size() - 10);
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
|
||||
std::cout << http_resp_header << CRLF CRLF << http_resp_body;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
3
srcs/cgi-bin/cgi_style.css
Normal file
3
srcs/cgi-bin/cgi_style.css
Normal file
@@ -0,0 +1,3 @@
|
||||
h1, h2, h3, p {
|
||||
display: inline;
|
||||
}
|
||||
@@ -51,34 +51,19 @@ std::string itos(int n)
|
||||
return ( strs.str() );
|
||||
}
|
||||
|
||||
std::string fill_tag(std::string content, std::string tag)
|
||||
std::string parse_env(const std::string & env)
|
||||
{
|
||||
std::string ret;
|
||||
|
||||
ret = "<" + tag + ">" + content + "</" + tag + ">";
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string fill_env(std::string env, std::string tag)
|
||||
{
|
||||
std::string ret;
|
||||
std::string ret = "";
|
||||
char * ret_env;
|
||||
|
||||
ret = "<" + tag + ">";
|
||||
|
||||
ret_env = getenv(env.c_str());
|
||||
if (ret_env != NULL)
|
||||
ret += ret_env;
|
||||
else
|
||||
ret += env + " not foud";
|
||||
|
||||
ret += "</" + tag + ">";
|
||||
ret = ret_env;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string
|
||||
parse_form_infos()
|
||||
std::string parse_body()
|
||||
{
|
||||
std::string ret;
|
||||
|
||||
@@ -86,20 +71,33 @@ std::string
|
||||
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
|
||||
fill_form(std::string form, std::string tag_key, std::string tag_val)
|
||||
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;
|
||||
std::string ret = "";
|
||||
|
||||
split_str = split(form, "&");
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
{
|
||||
sub_split_str = split(*it, "=");
|
||||
ret = "<" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
|
||||
ret = "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
||||
ret += "<br><" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
|
||||
ret += "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -17,10 +17,13 @@
|
||||
# define HTML_BODY_TOP "<!DOCTYPE html>"\
|
||||
"<html>"\
|
||||
" <head>"\
|
||||
" <meta charset=\"UTF-8\">"\
|
||||
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"\
|
||||
" <title>CGI</title>"\
|
||||
" <link href=\"./cgi_style.css\" type=\"text/css\" rel=\"stylesheet\">"\
|
||||
" </head>"\
|
||||
" <body>"\
|
||||
" <h2>cgi</h2>"
|
||||
" <h1>cgi</h1><br>"
|
||||
# define HTML_BODY_BOTTOM " </body>"\
|
||||
"</html>"
|
||||
|
||||
@@ -34,20 +37,16 @@ std::string
|
||||
itos(int n);
|
||||
|
||||
std::string
|
||||
fill_env(std::string env, std::string tag = "p");
|
||||
parse_env(const std::string & env);
|
||||
|
||||
std::string
|
||||
fill_tag(std::string env, std::string tag = "p");
|
||||
parse_body();
|
||||
|
||||
std::string
|
||||
fill_form(
|
||||
std::string form,
|
||||
std::string tag_key = "p",
|
||||
std::string tag_val = "p"
|
||||
);
|
||||
print_env(char **env, std::string tag = "p");
|
||||
|
||||
std::string
|
||||
parse_form_infos();
|
||||
print_form(std::string form, std::string key = "p", std::string val = "p");
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user