diff --git a/srcs/cgi-bin/cgi_cpp.cpp b/srcs/cgi-bin/cgi_cpp.cpp index 7d7ffaa..10c475a 100644 --- a/srcs/cgi-bin/cgi_cpp.cpp +++ b/srcs/cgi-bin/cgi_cpp.cpp @@ -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 = "

PRINT ENV _________________

"; - for (int i = 0; env[i] != NULL; ++i) - { - tmp += "

"; - tmp += env[i]; - tmp += "

"; - } - tmp += "

___________________________

"; + 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 += "

method used:

"; + http_body += "

" + rq_method + "

"; - http_resp_header = "Content-Type: text/html; charset=UTF-8" CRLF; - http_resp_header += "Content-Length: " + itos(http_resp_body.size()); + http_body += "

form body:

"; + http_body += "

" + rq_body + "

"; + + http_body += "

form query:

"; + http_body += "

" + rq_query + "

"; + + http_body += "

output:


"; + http_body += print_form(form_infos, "p", "p"); + + http_body += "

cgi_env_variables:


"; + 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; } diff --git a/srcs/cgi-bin/cgi_style.css b/srcs/cgi-bin/cgi_style.css new file mode 100644 index 0000000..52c981f --- /dev/null +++ b/srcs/cgi-bin/cgi_style.css @@ -0,0 +1,3 @@ +h1, h2, h3, p { + display: inline; +} diff --git a/srcs/cgi-bin/cgi_utils.cpp b/srcs/cgi-bin/cgi_utils.cpp index 2d5dcb6..cea2ca9 100644 --- a/srcs/cgi-bin/cgi_utils.cpp +++ b/srcs/cgi-bin/cgi_utils.cpp @@ -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 + ""; - 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 += ""; + 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 += "
"; + } + 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 split_str; std::vector sub_split_str; std::vector::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] + ": "; - ret = "<" + tag_val + ">" + sub_split_str[1] + ""; + ret += "
<" + tag_key + ">" + sub_split_str[0] + ": "; + ret += "<" + tag_val + ">" + sub_split_str[1] + ""; } return ret; } diff --git a/srcs/cgi-bin/cgi_utils.hpp b/srcs/cgi-bin/cgi_utils.hpp index a491d39..00abe7b 100644 --- a/srcs/cgi-bin/cgi_utils.hpp +++ b/srcs/cgi-bin/cgi_utils.hpp @@ -17,10 +17,13 @@ # define HTML_BODY_TOP ""\ ""\ " "\ + " "\ + " "\ " CGI"\ + " "\ " "\ " "\ - "

cgi

" + "

cgi


" # define HTML_BODY_BOTTOM " "\ "" @@ -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 diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index e572749..20e9003 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -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); diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 556ab4d..88906ad 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -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; }