basic cgi script test working
This commit is contained in:
@@ -3,51 +3,49 @@
|
|||||||
|
|
||||||
int main (int ac, char **av, char ** env)
|
int main (int ac, char **av, char ** env)
|
||||||
{
|
{
|
||||||
std::string http_req_body;
|
std::string rq_method = "not found";
|
||||||
std::string http_resp_header;
|
std::string rq_body = "";
|
||||||
std::string http_resp_body;
|
std::string rq_query = "";
|
||||||
|
std::string form_infos = "";
|
||||||
|
std::string http_header = "";
|
||||||
|
std::string http_body = "";
|
||||||
|
|
||||||
std::string tmp;
|
|
||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(void)av;
|
||||||
(void)env;
|
|
||||||
|
|
||||||
/*
|
rq_method = parse_env("REQUEST_METHOD");
|
||||||
rq_method = find_method();
|
rq_body = parse_body();
|
||||||
rq_body = parse_body();
|
rq_query = parse_env("QUERY_STRING");
|
||||||
rq_query = parse_query();
|
|
||||||
|
|
||||||
method used : GET
|
if (rq_method == "POST")
|
||||||
form query : key=val&key=val
|
form_infos = rq_body;
|
||||||
form body : EMPTY
|
else if (rq_method == "GET")
|
||||||
output :
|
form_infos = rq_query;
|
||||||
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>";
|
|
||||||
|
|
||||||
http_req_body = parse_form_infos();
|
http_body = HTML_BODY_TOP;
|
||||||
|
|
||||||
http_resp_body = HTML_BODY_TOP;
|
http_body += "<br><h3>method used: </h3>";
|
||||||
http_resp_body += fill_tag("ENV:", "h3") + fill_env("REQUEST_METHOD");
|
http_body += "<p>" + rq_method + "</p>";
|
||||||
http_resp_body += fill_form(http_req_body, "h3", "p");
|
|
||||||
http_resp_body += tmp;
|
|
||||||
http_resp_body += HTML_BODY_BOTTOM;
|
|
||||||
|
|
||||||
http_resp_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
http_body += "<br><h3>form body: </h3>";
|
||||||
http_resp_header += "Content-Length: " + itos(http_resp_body.size());
|
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;
|
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() );
|
return ( strs.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string fill_tag(std::string content, std::string tag)
|
std::string parse_env(const std::string & env)
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret = "";
|
||||||
|
|
||||||
ret = "<" + tag + ">" + content + "</" + tag + ">";
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string fill_env(std::string env, std::string tag)
|
|
||||||
{
|
|
||||||
std::string ret;
|
|
||||||
char * ret_env;
|
char * ret_env;
|
||||||
|
|
||||||
ret = "<" + tag + ">";
|
|
||||||
|
|
||||||
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
|
|
||||||
ret += env + " not foud";
|
|
||||||
|
|
||||||
ret += "</" + tag + ">";
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string parse_body()
|
||||||
parse_form_infos()
|
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
|
|
||||||
@@ -86,20 +71,33 @@ std::string
|
|||||||
return 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
|
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> split_str;
|
||||||
std::vector<std::string> sub_split_str;
|
std::vector<std::string> sub_split_str;
|
||||||
std::vector<std::string>::const_iterator it;
|
std::vector<std::string>::const_iterator it;
|
||||||
std::string ret;
|
std::string ret = "";
|
||||||
|
|
||||||
split_str = split(form, "&");
|
split_str = split(form, "&");
|
||||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||||
{
|
{
|
||||||
sub_split_str = split(*it, "=");
|
sub_split_str = split(*it, "=");
|
||||||
ret = "<" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
|
ret += "<br><" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
|
||||||
ret = "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
ret += "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,13 @@
|
|||||||
# define HTML_BODY_TOP "<!DOCTYPE html>"\
|
# define HTML_BODY_TOP "<!DOCTYPE html>"\
|
||||||
"<html>"\
|
"<html>"\
|
||||||
" <head>"\
|
" <head>"\
|
||||||
|
" <meta charset=\"UTF-8\">"\
|
||||||
|
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"\
|
||||||
" <title>CGI</title>"\
|
" <title>CGI</title>"\
|
||||||
|
" <link href=\"./cgi_style.css\" type=\"text/css\" rel=\"stylesheet\">"\
|
||||||
" </head>"\
|
" </head>"\
|
||||||
" <body>"\
|
" <body>"\
|
||||||
" <h2>cgi</h2>"
|
" <h1>cgi</h1><br>"
|
||||||
# define HTML_BODY_BOTTOM " </body>"\
|
# define HTML_BODY_BOTTOM " </body>"\
|
||||||
"</html>"
|
"</html>"
|
||||||
|
|
||||||
@@ -34,20 +37,16 @@ std::string
|
|||||||
itos(int n);
|
itos(int n);
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
fill_env(std::string env, std::string tag = "p");
|
parse_env(const std::string & env);
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
fill_tag(std::string env, std::string tag = "p");
|
parse_body();
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
fill_form(
|
print_env(char **env, std::string tag = "p");
|
||||||
std::string form,
|
|
||||||
std::string tag_key = "p",
|
|
||||||
std::string tag_val = "p"
|
|
||||||
);
|
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
parse_form_infos();
|
print_form(std::string form, std::string key = "p", std::string val = "p");
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -187,9 +187,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
|||||||
void Webserv::_check_script_output(Client *client, std::string & output)
|
void Webserv::_check_script_output(Client *client, std::string & output)
|
||||||
{
|
{
|
||||||
_check_script_status(client, output);
|
_check_script_status(client, output);
|
||||||
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_check_script_fields(client, output);
|
_check_script_fields(client, output);
|
||||||
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_remove_body_leading_empty_lines(output);
|
_remove_body_leading_empty_lines(output);
|
||||||
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_add_script_body_length_header(output);
|
_add_script_body_length_header(output);
|
||||||
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
// _check_script_empty_lines(client, output);
|
// _check_script_empty_lines(client, output);
|
||||||
// _check_script_space_colons(client, output);
|
// _check_script_space_colons(client, output);
|
||||||
// _check_script_new_lines(client, output);
|
// _check_script_new_lines(client, output);
|
||||||
|
|||||||
@@ -85,15 +85,12 @@ void Webserv::_construct_response(Client *client)
|
|||||||
if (_is_cgi(client, path))
|
if (_is_cgi(client, path))
|
||||||
{
|
{
|
||||||
script_output = _exec_cgi(client);
|
script_output = _exec_cgi(client);
|
||||||
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[response]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(script_output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(script_output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[response]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
|
||||||
|
|
||||||
_check_script_output(client, script_output);
|
_check_script_output(client, script_output);
|
||||||
client->response += script_output;
|
client->response += script_output;
|
||||||
|
///*DEBUG*/ std::cout << B_YELLOW "inside cgi" RESET "\n";
|
||||||
/*DEBUG*/ std::cout << B_YELLOW "inside cgi" RESET "\n";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user