diff --git a/README.md b/README.md index 12d13c5..2750f40 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,10 @@ SERVER_SOFTWARE : the server software you're using (e.g. Apache 1.3) REDIRECT_STATUS : for exemple, 200 ``` +g 50 34 48 +p 30 23 32 +l 20 14 20 + 71 --- ## http status diff --git a/srcs/cgi-bin/Makefile b/srcs/cgi-bin/Makefile index 24dfe80..a32e969 100644 --- a/srcs/cgi-bin/Makefile +++ b/srcs/cgi-bin/Makefile @@ -57,6 +57,7 @@ SRCS_X = \ cgi_cpp_only_crlf.cpp \ cgi_cpp_sleep.cpp \ cgi_cpp_status.cpp \ + cgi_cpp_download.cpp \ OBJS_D = builds OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o) diff --git a/srcs/cgi-bin/cgi_cpp.cpp b/srcs/cgi-bin/cgi_cpp.cpp index 0182dd1..9910adb 100644 --- a/srcs/cgi-bin/cgi_cpp.cpp +++ b/srcs/cgi-bin/cgi_cpp.cpp @@ -12,9 +12,11 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; - std::cout << http_header << CRLF CRLF << http_body; + fill_body_basic(env, http_body, rq_body); + + std::cout << http_header << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_bad_headers.cpp b/srcs/cgi-bin/cgi_cpp_bad_headers.cpp index efe12ed..73846ee 100644 --- a/srcs/cgi-bin/cgi_cpp_bad_headers.cpp +++ b/srcs/cgi-bin/cgi_cpp_bad_headers.cpp @@ -13,9 +13,11 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Bad-Headers: wrong"; - std::cout << "Bad-Headers: wrong" << CRLF CRLF << http_body; + fill_body_basic(env, http_body, rq_body); + + std::cout << http_header << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_download.cpp b/srcs/cgi-bin/cgi_cpp_download.cpp new file mode 100644 index 0000000..b0238e8 --- /dev/null +++ b/srcs/cgi-bin/cgi_cpp_download.cpp @@ -0,0 +1,55 @@ + +# include "cgi_utils.hpp" + +int main (int ac, char **av, char ** env) +{ + std::string http_header; + std::string http_body; + std::string rq_body; + std::string form_infos; + std::string path; + std::ifstream ifd; + std::stringstream buf; + size_t status; + + std::cin >> rq_body; + + (void)ac; + (void)av; + (void)env; + + http_header = "Content-Type: image/jpeg" CRLF; + + form_infos = get_form_infos(rq_body); + path = get_value("file", rq_body); + path = "./www/" + path; + + status = ::eval_file_read(path); + if (status) + { + std::cout << "Status: " << status << CRLF CRLF; + return 0; + } + + ifd.open(path.c_str()); + if (!ifd) + { + std::cout << "Status: " << 500 << CRLF CRLF; + return 0; + } + else + { + buf << ifd.rdbuf(); + if (!ifd || !buf) + { + std::cout << "Status: " << 500 << CRLF CRLF; + return 0; + } + } + + std::cout << http_header << CRLF << buf.str(); + + return 0; +} + + diff --git a/srcs/cgi-bin/cgi_cpp_empty_lines.cpp b/srcs/cgi-bin/cgi_cpp_empty_lines.cpp index 14a1361..62376b9 100644 --- a/srcs/cgi-bin/cgi_cpp_empty_lines.cpp +++ b/srcs/cgi-bin/cgi_cpp_empty_lines.cpp @@ -12,7 +12,9 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; + + fill_body_basic(env, http_body, rq_body); std::cout << http_header << CRLF CRLF CRLF CRLF CRLF << http_body; diff --git a/srcs/cgi-bin/cgi_cpp_len.cpp b/srcs/cgi-bin/cgi_cpp_len.cpp index 30f5991..641d083 100644 --- a/srcs/cgi-bin/cgi_cpp_len.cpp +++ b/srcs/cgi-bin/cgi_cpp_len.cpp @@ -12,11 +12,13 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; + http_header += "Content-Length: " + itos(http_body.size()) + CRLF; - http_header += "Content-Length: " + itos(http_body.size()); + fill_body_basic(env, http_body, rq_body); - std::cout << http_header << CRLF CRLF << http_body; + + std::cout << http_header << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_len_big.cpp b/srcs/cgi-bin/cgi_cpp_len_big.cpp index 3b6e330..a006e78 100644 --- a/srcs/cgi-bin/cgi_cpp_len_big.cpp +++ b/srcs/cgi-bin/cgi_cpp_len_big.cpp @@ -12,11 +12,13 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; + http_header += "Content-Length: " + itos(http_body.size() + 100) + CRLF; - http_header += "Content-Length: " + itos(http_body.size() + 100); + fill_body_basic(env, http_body, rq_body); - std::cout << http_header << CRLF CRLF << http_body; + + std::cout << http_header << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_len_small.cpp b/srcs/cgi-bin/cgi_cpp_len_small.cpp index 98372e4..3e72d8c 100644 --- a/srcs/cgi-bin/cgi_cpp_len_small.cpp +++ b/srcs/cgi-bin/cgi_cpp_len_small.cpp @@ -12,11 +12,12 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; + http_header += "Content-Length: " + itos(http_body.size() - 100) + CRLF; - http_header += "Content-Length: " + itos(http_body.size() - 100); + fill_body_basic(env, http_body, rq_body); - std::cout << http_header << CRLF CRLF << http_body; + std::cout << http_header << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_no_body.cpp b/srcs/cgi-bin/cgi_cpp_no_body.cpp index 70c57f5..e97d6b3 100644 --- a/srcs/cgi-bin/cgi_cpp_no_body.cpp +++ b/srcs/cgi-bin/cgi_cpp_no_body.cpp @@ -4,7 +4,6 @@ int main (int ac, char **av, char ** env) { std::string http_header; - std::string http_body; std::string http_status; std::string rq_body; @@ -12,10 +11,11 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; + (void)env; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; - std::cout << http_header << CRLF CRLF; + std::cout << http_header << CRLF; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_no_headers.cpp b/srcs/cgi-bin/cgi_cpp_no_headers.cpp index beb591d..b563a71 100644 --- a/srcs/cgi-bin/cgi_cpp_no_headers.cpp +++ b/srcs/cgi-bin/cgi_cpp_no_headers.cpp @@ -13,9 +13,11 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = CRLF; - std::cout << CRLF CRLF << http_body; + fill_body_basic(env, http_body, rq_body); + + std::cout << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_sleep.cpp b/srcs/cgi-bin/cgi_cpp_sleep.cpp index 0910855..0948606 100644 --- a/srcs/cgi-bin/cgi_cpp_sleep.cpp +++ b/srcs/cgi-bin/cgi_cpp_sleep.cpp @@ -14,13 +14,15 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; + + fill_body_basic(env, http_body, rq_body); ss << get_value("sleep", rq_body); ss >> time; sleep(time); - std::cout << http_header << CRLF CRLF << http_body; + std::cout << http_header << CRLF << http_body; return 0; } diff --git a/srcs/cgi-bin/cgi_cpp_status.cpp b/srcs/cgi-bin/cgi_cpp_status.cpp index bbcf103..1fee0a8 100644 --- a/srcs/cgi-bin/cgi_cpp_status.cpp +++ b/srcs/cgi-bin/cgi_cpp_status.cpp @@ -13,10 +13,12 @@ int main (int ac, char **av, char ** env) (void)ac; (void)av; - fill_response_basic(env, http_body, http_header, rq_body); + http_header = "Content-Type: text/html; charset=UTF-8" CRLF; + + fill_body_basic(env, http_body, rq_body); http_status = get_value("Status", rq_body); - http_header += "Status: " + http_status; + http_header += "Status: " + http_status + CRLF; std::cout << http_header << CRLF CRLF << http_body; diff --git a/srcs/cgi-bin/cgi_utils.cpp b/srcs/cgi-bin/cgi_utils.cpp index 128ab99..9e2e7b7 100644 --- a/srcs/cgi-bin/cgi_utils.cpp +++ b/srcs/cgi-bin/cgi_utils.cpp @@ -147,11 +147,7 @@ std::string get_value(const std::string & key, const std::string & rq_body) } void - fill_response_basic( - char **env, - std::string & http_body, - std::string & http_header, - const std::string & rq_body) + fill_body_basic(char **env, std::string & http_body, const std::string & rq_body) { std::string rq_method = "not found"; std::string rq_query; @@ -183,7 +179,21 @@ void http_body += print_env(env, "p"); http_body += HTML_BODY_BOTTOM; - - http_header = "Content-Type: text/html; charset=UTF-8" CRLF; +} + +size_t eval_file_read(const std::string &path) +{ + if (::access(path.c_str(), F_OK) == -1) + { + std::perror("err access()"); + return 404; // NOT_FOUND, file doesn't exist + } + + if (::access(path.c_str(), R_OK) == -1) + { + std::perror("err access()"); + return 403; // FORBIDDEN, file doesn't have access permission + } + return 0; } diff --git a/srcs/cgi-bin/cgi_utils.hpp b/srcs/cgi-bin/cgi_utils.hpp index 1d15391..6ba5a7a 100644 --- a/srcs/cgi-bin/cgi_utils.hpp +++ b/srcs/cgi-bin/cgi_utils.hpp @@ -5,10 +5,11 @@ # include # include # include +# include # include # include // getenv # include // transform -# include // sleep +# include // sleep, close, access # define CR "\r" # define LF "\n" @@ -58,11 +59,10 @@ std::string print_form(std::string form, std::string key = "p", std::string val = "p"); void -fill_response_basic( - char **env, - std::string & http_body, - std::string & http_header, - const std::string & rq_body); +fill_body_basic(char **env, std::string & http_body, const std::string & rq_body); + +size_t +eval_file_read(const std::string &path); #endif diff --git a/www/directory/root.png b/www/directory/root.png new file mode 100644 index 0000000..ebd1548 Binary files /dev/null and b/www/directory/root.png differ diff --git a/www/file.md b/www/file.md new file mode 100644 index 0000000..a8b8ab6 --- /dev/null +++ b/www/file.md @@ -0,0 +1 @@ +some md file to test upload diff --git a/www/form_cgi.html b/www/form_cgi.html index c77148e..2e73f05 100644 --- a/www/form_cgi.html +++ b/www/form_cgi.html @@ -208,7 +208,7 @@

expectation:

-

the request will sleep for one minute

+

the request will sleep for chosen seconds

but other request chould not be blocked


@@ -276,8 +276,35 @@

expectation:

error 500

+
+ +
+

WIP

+

get form

+

to /cgi-bin/cgi_cpp_download.out

+
+ + + +

expectation:

+

get a file in list

+

from ./www/user_files/

+
+ + +

post form

@@ -433,7 +460,7 @@

expectation:

-

the request will sleep for one minute

+

the request will sleep for chosen seconds

but other request chould not be blocked


@@ -501,6 +528,18 @@

expectation:

error 500

+
+ +
+

WIP

+

post form

+

to /cgi-bin/cgi_cpp_upload.out

+
+ + +

expectation:

+

upload file in ./www/user_files/

+
diff --git a/www/subject.pdf b/www/subject.pdf new file mode 100644 index 0000000..005f6c2 Binary files /dev/null and b/www/subject.pdf differ diff --git a/www/user_files/duck.jpg b/www/user_files/duck.jpg new file mode 100644 index 0000000..1a7c38d Binary files /dev/null and b/www/user_files/duck.jpg differ