wip cgi ouptput
This commit is contained in:
4
srcs/cgi-bin/cgi
Executable file
4
srcs/cgi-bin/cgi
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
echo "status: 100\r\n"
|
||||||
|
echo "\r\n\r\n"
|
||||||
|
echo "hiii"
|
||||||
@@ -1,40 +1,53 @@
|
|||||||
|
|
||||||
# include "cgi_utils.hpp"
|
# include "cgi_utils.hpp"
|
||||||
|
|
||||||
int main ()
|
int main (int ac, char **av, char ** env)
|
||||||
{
|
{
|
||||||
std::vector<std::string> split_str;
|
std::string http_req_body;
|
||||||
std::vector<std::string> sub_split_str;
|
std::string http_resp_header;
|
||||||
std::vector<std::string>::const_iterator it;
|
std::string http_resp_body;
|
||||||
std::string input;
|
|
||||||
std::string http_header;
|
|
||||||
std::string http_body;
|
|
||||||
|
|
||||||
std::cin >> input;
|
std::string tmp;
|
||||||
|
(void)ac;
|
||||||
|
(void)av;
|
||||||
|
(void)env;
|
||||||
|
|
||||||
http_body = HTML_BODY_TOP;
|
/*
|
||||||
|
rq_method = find_method();
|
||||||
|
rq_body = parse_body();
|
||||||
|
rq_query = parse_query();
|
||||||
|
|
||||||
http_body += fill_env("REQUEST_METHOD", "h3");
|
method used : GET
|
||||||
|
form query : key=val&key=val
|
||||||
split_str = split(input, "&");
|
form body : EMPTY
|
||||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
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)
|
||||||
{
|
{
|
||||||
sub_split_str = split(*it, "=");
|
tmp += "<p>";
|
||||||
http_body += "<h3>";
|
tmp += env[i];
|
||||||
http_body += sub_split_str[0];
|
tmp += "</p>";
|
||||||
http_body += "</h3>";
|
|
||||||
http_body += "<p>";
|
|
||||||
http_body += sub_split_str[1];
|
|
||||||
http_body += "</p>";
|
|
||||||
}
|
}
|
||||||
|
tmp += "<p>___________________________</p>";
|
||||||
|
|
||||||
http_body += HTML_BODY_BOTTOM;
|
http_req_body = parse_form_infos();
|
||||||
|
|
||||||
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
http_resp_body = HTML_BODY_TOP;
|
||||||
http_header += "Content-Length: ";
|
http_resp_body += fill_tag("ENV:", "h3") + fill_env("REQUEST_METHOD");
|
||||||
http_header += itos(http_body.size());
|
http_resp_body += fill_form(http_req_body, "h3", "p");
|
||||||
|
http_resp_body += tmp;
|
||||||
|
http_resp_body += HTML_BODY_BOTTOM;
|
||||||
|
|
||||||
std::cout << http_header << CRLF CRLF << http_body;
|
http_resp_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
||||||
|
http_resp_header += "Content-Length: " + itos(http_resp_body.size());
|
||||||
|
|
||||||
|
std::cout << http_resp_header << CRLF CRLF << http_resp_body;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,28 +51,56 @@ std::string itos(int n)
|
|||||||
return ( strs.str() );
|
return ( strs.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string fill_tag(std::string content, std::string tag)
|
||||||
|
{
|
||||||
|
std::string ret;
|
||||||
|
|
||||||
|
ret = "<" + tag + ">" + content + "</" + tag + ">";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
std::string fill_env(std::string env, std::string tag)
|
std::string fill_env(std::string env, std::string tag)
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
char * ret_env;
|
char * ret_env;
|
||||||
|
|
||||||
ret = "<";
|
ret = "<" + tag + ">";
|
||||||
ret += tag;
|
|
||||||
ret += ">";
|
|
||||||
|
|
||||||
ret_env = getenv(env.c_str());
|
ret_env = getenv(env.c_str());
|
||||||
if (ret_env != NULL)
|
if (ret_env != NULL)
|
||||||
ret += ret_env;
|
ret += ret_env;
|
||||||
else
|
else
|
||||||
{
|
ret += env + " not foud";
|
||||||
ret += env;
|
|
||||||
ret += " not foud";
|
|
||||||
}
|
|
||||||
|
|
||||||
ret += "</";
|
ret += "</" + tag + ">";
|
||||||
ret += tag;
|
|
||||||
ret += ">";
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
parse_form_infos()
|
||||||
|
{
|
||||||
|
std::string ret;
|
||||||
|
|
||||||
|
std::cin >> ret;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
fill_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 = "<" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
|
||||||
|
ret = "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,20 @@ std::string
|
|||||||
itos(int n);
|
itos(int n);
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
fill_env(std::string env, std::string tag);
|
fill_env(std::string env, std::string tag = "p");
|
||||||
|
|
||||||
|
std::string
|
||||||
|
fill_tag(std::string env, std::string tag = "p");
|
||||||
|
|
||||||
|
std::string
|
||||||
|
fill_form(
|
||||||
|
std::string form,
|
||||||
|
std::string tag_key = "p",
|
||||||
|
std::string tag_val = "p"
|
||||||
|
);
|
||||||
|
|
||||||
|
std::string
|
||||||
|
parse_form_infos();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user