added script download test
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
55
srcs/cgi-bin/cgi_cpp_download.cpp
Normal file
55
srcs/cgi-bin/cgi_cpp_download.cpp
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <sstream>
|
||||
# include <fstream>
|
||||
# include <vector>
|
||||
# include <stdlib.h> // getenv
|
||||
# include <algorithm> // transform
|
||||
# include <unistd.h> // sleep
|
||||
# include <unistd.h> // 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user