basic cgi script test working

This commit is contained in:
hugogogo
2022-08-15 18:26:41 +02:00
parent 6ad6ec7d63
commit 1c13e254d5
6 changed files with 74 additions and 75 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,3 @@
h1, h2, h3, p {
display: inline;
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -187,9 +187,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
void Webserv::_check_script_output(Client *client, std::string & 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);
/*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);
/*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);
/*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_space_colons(client, output);
// _check_script_new_lines(client, output);

View File

@@ -85,15 +85,12 @@ void Webserv::_construct_response(Client *client)
if (_is_cgi(client, path))
{
script_output = _exec_cgi(client);
///*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";
/*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";
_check_script_output(client, script_output);
client->response += script_output;
/*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 << 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";
return;
}