added script download test
This commit is contained in:
@@ -232,6 +232,10 @@ SERVER_SOFTWARE : the server software you're using (e.g. Apache 1.3)
|
|||||||
REDIRECT_STATUS : for exemple, 200
|
REDIRECT_STATUS : for exemple, 200
|
||||||
```
|
```
|
||||||
|
|
||||||
|
g 50 34 48
|
||||||
|
p 30 23 32
|
||||||
|
l 20 14 20
|
||||||
|
71
|
||||||
|
|
||||||
---
|
---
|
||||||
## http status
|
## http status
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ SRCS_X = \
|
|||||||
cgi_cpp_only_crlf.cpp \
|
cgi_cpp_only_crlf.cpp \
|
||||||
cgi_cpp_sleep.cpp \
|
cgi_cpp_sleep.cpp \
|
||||||
cgi_cpp_status.cpp \
|
cgi_cpp_status.cpp \
|
||||||
|
cgi_cpp_download.cpp \
|
||||||
|
|
||||||
OBJS_D = builds
|
OBJS_D = builds
|
||||||
OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o)
|
OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o)
|
||||||
|
|||||||
@@ -12,9 +12,11 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,11 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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;
|
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)ac;
|
||||||
(void)av;
|
(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;
|
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)ac;
|
||||||
(void)av;
|
(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,13 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,12 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
int main (int ac, char **av, char ** env)
|
int main (int ac, char **av, char ** env)
|
||||||
{
|
{
|
||||||
std::string http_header;
|
std::string http_header;
|
||||||
std::string http_body;
|
|
||||||
std::string http_status;
|
std::string http_status;
|
||||||
std::string rq_body;
|
std::string rq_body;
|
||||||
|
|
||||||
@@ -12,10 +11,11 @@ int main (int ac, char **av, char ** env)
|
|||||||
|
|
||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,11 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,15 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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 << get_value("sleep", rq_body);
|
||||||
ss >> time;
|
ss >> time;
|
||||||
sleep(time);
|
sleep(time);
|
||||||
|
|
||||||
std::cout << http_header << CRLF CRLF << http_body;
|
std::cout << http_header << CRLF << http_body;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,12 @@ int main (int ac, char **av, char ** env)
|
|||||||
(void)ac;
|
(void)ac;
|
||||||
(void)av;
|
(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_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;
|
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
|
void
|
||||||
fill_response_basic(
|
fill_body_basic(char **env, std::string & http_body, const std::string & rq_body)
|
||||||
char **env,
|
|
||||||
std::string & http_body,
|
|
||||||
std::string & http_header,
|
|
||||||
const std::string & rq_body)
|
|
||||||
{
|
{
|
||||||
std::string rq_method = "not found";
|
std::string rq_method = "not found";
|
||||||
std::string rq_query;
|
std::string rq_query;
|
||||||
@@ -183,7 +179,21 @@ void
|
|||||||
http_body += print_env(env, "p");
|
http_body += print_env(env, "p");
|
||||||
|
|
||||||
http_body += HTML_BODY_BOTTOM;
|
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 <iostream>
|
||||||
# include <string>
|
# include <string>
|
||||||
# include <sstream>
|
# include <sstream>
|
||||||
|
# include <fstream>
|
||||||
# include <vector>
|
# include <vector>
|
||||||
# include <stdlib.h> // getenv
|
# include <stdlib.h> // getenv
|
||||||
# include <algorithm> // transform
|
# include <algorithm> // transform
|
||||||
# include <unistd.h> // sleep
|
# include <unistd.h> // sleep, close, access
|
||||||
|
|
||||||
# define CR "\r"
|
# define CR "\r"
|
||||||
# define LF "\n"
|
# define LF "\n"
|
||||||
@@ -58,11 +59,10 @@ std::string
|
|||||||
print_form(std::string form, std::string key = "p", std::string val = "p");
|
print_form(std::string form, std::string key = "p", std::string val = "p");
|
||||||
|
|
||||||
void
|
void
|
||||||
fill_response_basic(
|
fill_body_basic(char **env, std::string & http_body, const std::string & rq_body);
|
||||||
char **env,
|
|
||||||
std::string & http_body,
|
size_t
|
||||||
std::string & http_header,
|
eval_file_read(const std::string &path);
|
||||||
const std::string & rq_body);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
BIN
www/directory/root.png
Normal file
BIN
www/directory/root.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 446 KiB |
1
www/file.md
Normal file
1
www/file.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
some md file to test upload
|
||||||
@@ -208,7 +208,7 @@
|
|||||||
<input type="text" id="sleep" name="sleep" value="5"><br><br>
|
<input type="text" id="sleep" name="sleep" value="5"><br><br>
|
||||||
<input type="submit" value="submit">
|
<input type="submit" value="submit">
|
||||||
<h3>expectation:</h3>
|
<h3>expectation:</h3>
|
||||||
<p>the request will sleep for one minute</p>
|
<p>the request will sleep for chosen seconds</p>
|
||||||
<p>but other request chould not be blocked</p>
|
<p>but other request chould not be blocked</p>
|
||||||
</form>
|
</form>
|
||||||
<br>
|
<br>
|
||||||
@@ -276,8 +276,35 @@
|
|||||||
<h3>expectation:</h3>
|
<h3>expectation:</h3>
|
||||||
<p>error 500</p>
|
<p>error 500</p>
|
||||||
</form>
|
</form>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<form method="get" action="/cgi-bin/cgi_cpp_download.out">
|
||||||
|
<h1>WIP</h1>
|
||||||
|
<p><mark>get</mark> form</p>
|
||||||
|
<p>to <mark>/cgi-bin/cgi_cpp_download.out</mark></p>
|
||||||
|
<label for="fdownload">download file:</label><br>
|
||||||
|
<input type="file" id="fupload" name="fupload">
|
||||||
|
<select name="file">
|
||||||
|
<option value="Cagneyc_intro.gif" >Cagneyc_intro.gif </option>
|
||||||
|
<option value="file.md" >file.md </option>
|
||||||
|
<option value="index.html" >index.html </option>
|
||||||
|
<option value="kermit.ico" >kermit.ico </option>
|
||||||
|
<option value="punpun.png" selected>punpun.png </option>
|
||||||
|
<option value="subject.pdf" >subject.pdf </option>
|
||||||
|
<option value="Van_Eyck_Portrait_Arnolfini.jpg" >Van_Eyck_Portrait_Arnolfini.jpg</option>
|
||||||
|
<option value="directory" >directory </option>
|
||||||
|
<option value="DOESNT_EXIST.BAD" >DOESNT_EXIST.BAD </option>
|
||||||
|
</select>
|
||||||
|
<input type="submit" value="submit">
|
||||||
|
<h3>expectation:</h3>
|
||||||
|
<p>get a file in list</p>
|
||||||
|
<p>from ./www/user_files/</p>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div id="post">
|
<div id="post">
|
||||||
<form method="post" action="/cgi-bin/cgi_cpp.out">
|
<form method="post" action="/cgi-bin/cgi_cpp.out">
|
||||||
<p><mark>post</mark> form</p>
|
<p><mark>post</mark> form</p>
|
||||||
@@ -433,7 +460,7 @@
|
|||||||
<input type="text" id="sleep" name="sleep" value="5"><br><br>
|
<input type="text" id="sleep" name="sleep" value="5"><br><br>
|
||||||
<input type="submit" value="submit">
|
<input type="submit" value="submit">
|
||||||
<h3>expectation:</h3>
|
<h3>expectation:</h3>
|
||||||
<p>the request will sleep for one minute</p>
|
<p>the request will sleep for chosen seconds</p>
|
||||||
<p>but other request chould not be blocked</p>
|
<p>but other request chould not be blocked</p>
|
||||||
</form>
|
</form>
|
||||||
<br>
|
<br>
|
||||||
@@ -501,6 +528,18 @@
|
|||||||
<h3>expectation:</h3>
|
<h3>expectation:</h3>
|
||||||
<p>error 500</p>
|
<p>error 500</p>
|
||||||
</form>
|
</form>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<form method="post" action="/cgi-bin/cgi_cpp_upload.out" enctype="multipart/form-data">
|
||||||
|
<h1>WIP</h1>
|
||||||
|
<p><mark>post</mark> form</p>
|
||||||
|
<p>to <mark>/cgi-bin/cgi_cpp_upload.out</mark></p>
|
||||||
|
<label for="fupload">Upload file:</label><br>
|
||||||
|
<input type="file" id="fupload" name="fupload">
|
||||||
|
<input type="submit" value="submit">
|
||||||
|
<h3>expectation:</h3>
|
||||||
|
<p>upload file in ./www/user_files/</p>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
BIN
www/subject.pdf
Normal file
BIN
www/subject.pdf
Normal file
Binary file not shown.
BIN
www/user_files/duck.jpg
Normal file
BIN
www/user_files/duck.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 171 KiB |
Reference in New Issue
Block a user