Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df24de46c7 |
23
42.config
23
42.config
@@ -1,23 +0,0 @@
|
|||||||
|
|
||||||
server {
|
|
||||||
|
|
||||||
|
|
||||||
server_name server1;
|
|
||||||
listen 0.0.0.0:4040;
|
|
||||||
client_body_limit 1000;
|
|
||||||
|
|
||||||
index youpi.bla; # this is another comment
|
|
||||||
|
|
||||||
root ./YoupiBanane/;
|
|
||||||
|
|
||||||
error_page 404 ./www/error_pages/error_404.html;
|
|
||||||
|
|
||||||
location / {
|
|
||||||
allow_methods GET;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /directory {
|
|
||||||
index youpi.bad_extention;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,7 @@ server {
|
|||||||
|
|
||||||
server_name server1;
|
server_name server1;
|
||||||
|
|
||||||
listen 0.0.0.0:8080;
|
listen 0.0.0.0:4040;
|
||||||
|
|
||||||
client_body_limit 1;
|
client_body_limit 1;
|
||||||
# Max == 18446744073709551615 / 1024 == 18014398509481984
|
# Max == 18446744073709551615 / 1024 == 18014398509481984
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
|
|
||||||
server {
|
|
||||||
|
|
||||||
# this is a comment
|
|
||||||
|
|
||||||
server_name server1;
|
|
||||||
|
|
||||||
listen 8080;
|
|
||||||
# listen 70000;
|
|
||||||
|
|
||||||
# client_body_limit asdfa;
|
|
||||||
client_body_limit 1000;
|
|
||||||
# Max == 18446744073709551615 / 1024 == 18014398509481984
|
|
||||||
|
|
||||||
index index.html; # this is another comment
|
|
||||||
#index mdr.html; # this is another comment
|
|
||||||
|
|
||||||
root ./Tester/www/;
|
|
||||||
|
|
||||||
error_page 404 ./Tester/www/error_pages/error_404.html;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
location / {
|
|
||||||
allow_methods GET;
|
|
||||||
root ./Tester/www/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /srcs/cgi-bin/ {
|
|
||||||
root ./srcs/cgi-bin/;
|
|
||||||
allow_methods POST;
|
|
||||||
cgi_ext php;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /list {
|
|
||||||
autoindex on;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /cgi-bin {
|
|
||||||
root ./srcs/cgi-bin/;
|
|
||||||
cgi_ext out php sh;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /upload {
|
|
||||||
allow_methods POST;
|
|
||||||
autoindex on;
|
|
||||||
upload_dir ./Tester/www/user_files/;
|
|
||||||
# root doesn’t matter if used only with POST and no CGI
|
|
||||||
}
|
|
||||||
|
|
||||||
location /the_dump {
|
|
||||||
allow_methods GET DELETE;
|
|
||||||
root ./Tester/www/user_files;
|
|
||||||
autoindex on;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /redirect {
|
|
||||||
redirect 307 https://fr.wikipedia.org/wiki/Ketchup;
|
|
||||||
# redirect 307 https://www.youtube.com/watch?v=rG6b8gjMEkw;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /test {
|
|
||||||
index index1.html subdex.html;
|
|
||||||
root ./Tester/www/test/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /stylesheet {
|
|
||||||
root ./stylesheet/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /test/index1.html {
|
|
||||||
root ./Tester/www/test/index1.html;
|
|
||||||
index index1.html subdex.html;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /hilarious_404/ {
|
|
||||||
redirect 301 https://berniesanders.com/404/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /test/something.html {
|
|
||||||
# allow_methods DELETE;
|
|
||||||
root ./Tester/www/test/something.html;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /test/test_deeper/ {
|
|
||||||
# allow_methods
|
|
||||||
root ./Tester/www/test/test_deeper/;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /test/test_deeper/super_deep {
|
|
||||||
root ./Tester/www/test/test_deeper/super_deep/;
|
|
||||||
}
|
|
||||||
|
|
||||||
# location /test/test_deeper/something.html {
|
|
||||||
# allow_methods DELETE;
|
|
||||||
# }
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
test_name="Body Test"
|
test_name="Body Test"
|
||||||
|
|
||||||
config_file="./Tester/test2.config"
|
config_file="./Tester/test2.config"
|
||||||
port=8080
|
port=4040
|
||||||
host="localhost"
|
host="localhost"
|
||||||
|
|
||||||
#paths=("/" "/test" "/test/" "/list" "list" "/wrong") # you can add many
|
#paths=("/" "/test" "/test/" "/list" "list" "/wrong") # you can add many
|
||||||
@@ -17,19 +17,17 @@ httpz=("HTTP/1.1")
|
|||||||
l1="${methods[0]} ${paths[0]} ${httpz[0]}"
|
l1="${methods[0]} ${paths[0]} ${httpz[0]}"
|
||||||
|
|
||||||
header="Host: $host\n"
|
header="Host: $host\n"
|
||||||
body="this is a message \n"
|
body="this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message \n"
|
||||||
body+="this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message this is a message \n"
|
|
||||||
|
|
||||||
# turns out this does nothing...
|
# turns out this does nothing...
|
||||||
#header_cycle=("Content-Length: 17\n" "Content-Length: 14" "Content-Length: 25")
|
#header_cycle=("Content-Length: 17" "Content-Length: 14" "Content-Length: 25")
|
||||||
#header_cycle=("Content-Length: 17\n" "\n")
|
#header_cycle=("Content-Length: 17" "")
|
||||||
#header_cycle=("Content-Length: 17\n")
|
#header_cycle=("Content-Length: 17")
|
||||||
#header_cycle=()
|
#header_cycle=()
|
||||||
#header+="Content-Length: 8\n"
|
header+="Content-Length: 15"
|
||||||
|
|
||||||
run_this_test()
|
run_this_test()
|
||||||
{
|
{
|
||||||
echo size is ${#body} >> telnet.log
|
|
||||||
|
|
||||||
# for i in "${header_cycle[@]}"
|
# for i in "${header_cycle[@]}"
|
||||||
for i in "${paths[@]}"
|
for i in "${paths[@]}"
|
||||||
@@ -38,9 +36,7 @@ run_this_test()
|
|||||||
l1="${methods[0]} $i ${httpz[0]}"
|
l1="${methods[0]} $i ${httpz[0]}"
|
||||||
# header_send="$header$i"
|
# header_send="$header$i"
|
||||||
header_send="$header"
|
header_send="$header"
|
||||||
## if you add an extra \n after header send you get a status code 200 OK and 400 bad request, but like both
|
request="$l1\n$header_send\n\n$body\n"
|
||||||
# request="$l1\n$header_send\n$body\n"
|
|
||||||
request="$l1\n$header_send\n$body"
|
|
||||||
{
|
{
|
||||||
echo "----- $test_name -----"
|
echo "----- $test_name -----"
|
||||||
echo -e "$_RED$request$_END"
|
echo -e "$_RED$request$_END"
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
#! /bin/bash
|
|
||||||
|
|
||||||
test_name="Port Test"
|
|
||||||
|
|
||||||
config_file="./Tester/test3.config"
|
|
||||||
port=8080
|
|
||||||
host="localhost"
|
|
||||||
|
|
||||||
#paths=("/" "/test" "/test/" "/list" "list" "/wrong") # you can add many
|
|
||||||
paths=("/") # you can add many
|
|
||||||
#methods=("GET" "POST" "DELETE")
|
|
||||||
methods=("GET")
|
|
||||||
httpz=("HTTP/1.1")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
l1="${methods[0]} ${paths[0]} ${httpz[0]}"
|
|
||||||
|
|
||||||
header="Host: $host"
|
|
||||||
body=
|
|
||||||
|
|
||||||
|
|
||||||
run_this_test()
|
|
||||||
{
|
|
||||||
|
|
||||||
for i in "${paths[@]}"
|
|
||||||
do
|
|
||||||
l1="${methods[0]} $i ${httpz[0]}"
|
|
||||||
request="$l1\n$header\n$body\n"
|
|
||||||
{
|
|
||||||
echo "----- $test_name -----"
|
|
||||||
echo -e "$_RED$request$_END"
|
|
||||||
} >> telnet.log
|
|
||||||
echo -e "$request"
|
|
||||||
sleep 1
|
|
||||||
echo -e "\n" >> telnet.log
|
|
||||||
done
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# expected result...
|
|
||||||
|
|
||||||
|
|
||||||
files=()
|
|
||||||
|
|
||||||
file="expected_path_test.txt"
|
|
||||||
files+=("expected_path_test.txt")
|
|
||||||
#files+=("expected_path_root_test.txt")
|
|
||||||
#files+=("expected_path_testnoslash_test.txt")
|
|
||||||
#files+=("expected_path_testslash_test.txt")
|
|
||||||
#files+=("expected_path_list_test.txt")
|
|
||||||
#files+=("expected_path_badlist_test.txt")
|
|
||||||
#files+=("expected_path_wrong_test.txt")
|
|
||||||
|
|
||||||
|
|
||||||
#local_expected_test_file=$file
|
|
||||||
local_expected_test_files=()
|
|
||||||
test_path=""
|
|
||||||
|
|
||||||
add_path()
|
|
||||||
{
|
|
||||||
for i in "${files[@]}"
|
|
||||||
do
|
|
||||||
local_expected_test_files+=("$test_path$i")
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
#add_path
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
12
compare.txt
12
compare.txt
@@ -1,4 +1,10 @@
|
|||||||
telnet> Trying 127.0.0.1...
|
telnet> Trying 127.0.0.1...
|
||||||
telnet> ?Invalid command
|
Connected to localhost.
|
||||||
telnet> ?Invalid command
|
Escape character is '^]'.
|
||||||
telnet> telnet> telnet>
|
HTTP/1.1 413 Content Too Large
|
||||||
|
Server: Webserv/0.1
|
||||||
|
Connection: close
|
||||||
|
Content-Type: text/html; charset=UTF-8
|
||||||
|
Content-Length: 197
|
||||||
|
|
||||||
|
<!DOCTYPE html><html><head><title>413 Content Too Large</title></head><body><h1 style="text-align:center">413 Content Too Large</h1><hr><p style="text-align:center">Le Webserv/0.1</p></body></html>
|
||||||
@@ -42,19 +42,8 @@ server {
|
|||||||
|
|
||||||
location /upload {
|
location /upload {
|
||||||
allow_methods POST;
|
allow_methods POST;
|
||||||
# autoindex on;
|
|
||||||
# root ./www/;
|
|
||||||
# index upload_form_single.html;
|
|
||||||
|
|
||||||
# upload_dir ./www/user_files/;
|
|
||||||
# root doesn’t matter if used only with POST and no CGI
|
|
||||||
}
|
|
||||||
|
|
||||||
location /uploaded {
|
|
||||||
allow_methods GET;
|
|
||||||
autoindex on;
|
autoindex on;
|
||||||
# upload_dir ./www/user_files/;
|
upload_dir ./www/user_files/;
|
||||||
root ./www/user_files;
|
|
||||||
# root doesn’t matter if used only with POST and no CGI
|
# root doesn’t matter if used only with POST and no CGI
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,12 +84,10 @@ server {
|
|||||||
location /test/test_deeper/ {
|
location /test/test_deeper/ {
|
||||||
# allow_methods
|
# allow_methods
|
||||||
root ./www/test/test_deeper/;
|
root ./www/test/test_deeper/;
|
||||||
index index1.html;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
location /test/test_deeper/super_deep {
|
location /test/test_deeper/super_deep {
|
||||||
root ./www/test/test_deeper/super_deep/;
|
root ./www/test/test_deeper/super_deep/;
|
||||||
index something.html;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# location /test/test_deeper/something.html {
|
# location /test/test_deeper/something.html {
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ _END='\033[0m'
|
|||||||
|
|
||||||
#test_file_names=("test_template.sh" "test_header.sh" "test_path.sh")
|
#test_file_names=("test_template.sh" "test_header.sh" "test_path.sh")
|
||||||
#test_file_names=("test_method.sh" "test_header.sh" "test_path.sh")
|
#test_file_names=("test_method.sh" "test_header.sh" "test_path.sh")
|
||||||
test_file_names=("test_port.sh")
|
test_file_names=("test_body.sh")
|
||||||
#test_file_names=("test_body.sh")
|
|
||||||
#test_file_names=("test_valid_uri.sh")
|
#test_file_names=("test_valid_uri.sh")
|
||||||
#test_file_names=("test_path.sh")
|
#test_file_names=("test_path.sh")
|
||||||
|
|
||||||
|
|||||||
@@ -137,18 +137,16 @@ void Client::parse_request_body()
|
|||||||
}
|
}
|
||||||
else if (raw_request.size() - pos >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10))
|
else if (raw_request.size() - pos >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10))
|
||||||
{
|
{
|
||||||
std::cerr << "Content-Length = " << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
|
|
||||||
if (get_rq_headers("Content-Type").find("multipart/form-data") != NPOS)
|
if (get_rq_headers("Content-Type").find("multipart/form-data") != NPOS)
|
||||||
_parse_multipart_body(pos);
|
_parse_multipart_body(pos);
|
||||||
else
|
else
|
||||||
_request.body = raw_request.substr(pos + CRLF_SIZE*2);
|
_request.body = raw_request.substr(pos + CRLF_SIZE*2);
|
||||||
body_complete = true;
|
body_complete = true;
|
||||||
}
|
}
|
||||||
|
// std::cerr << "Content-Length = " << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
|
||||||
std::cerr << "Content-Length = " << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
|
// std::cerr << "raw_request.size() - pos = " << raw_request.size() - pos << "\n";
|
||||||
std::cerr << "raw_request.size() - pos = " << raw_request.size() - pos << "\n";
|
// _request.body = raw_request.substr(pos);
|
||||||
_request.body = raw_request.substr(pos);
|
// std::cerr << "_request.body.size() = " << _request.body.size() << "\n";
|
||||||
std::cerr << "_request.body.size() = " << _request.body.size() << "\n";
|
|
||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
// Body checks
|
// Body checks
|
||||||
@@ -341,7 +339,6 @@ void Client::_parse_request_line()
|
|||||||
{
|
{
|
||||||
std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n";
|
std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n";
|
||||||
status = 400; // "bad request"
|
status = 400; // "bad request"
|
||||||
// if the header is fucked up, then this will be triggered, but for some reason in test_body.sh case another response 200 OK is sent first?
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -416,9 +413,9 @@ void Client::_parse_port_hostname(std::string host)
|
|||||||
|
|
||||||
void Client::_check_request_errors()
|
void Client::_check_request_errors()
|
||||||
{
|
{
|
||||||
/* Debug */ std::cerr << "Content-Length=" << get_rq_headers("Content-Length") << "\n";
|
// /* Debug */ std::cerr << "Content-Length=" << get_rq_headers("Content-Length") << "\n";
|
||||||
/* Debug */ std::cerr << "strtoul=" << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
|
// /* Debug */ std::cerr << "strtoul=" << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
|
||||||
/* Debug */ std::cerr << "client_body_limit=" << assigned_server->client_body_limit << "\n";
|
// /* Debug */ std::cerr << "client_body_limit=" << assigned_server->client_body_limit << "\n";
|
||||||
///////////////////////
|
///////////////////////
|
||||||
// Request line checks
|
// Request line checks
|
||||||
if (_request.method == UNKNOWN)
|
if (_request.method == UNKNOWN)
|
||||||
@@ -510,7 +507,6 @@ If we get a url that ends in / ignore the last /
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// TODO: technically i think this is useless...
|
|
||||||
std::string uri = path;
|
std::string uri = path;
|
||||||
if (uri[uri.size() - 1] == '/' && uri.size() != 1)
|
if (uri[uri.size() - 1] == '/' && uri.size() != 1)
|
||||||
uri.erase(uri.size() - 1);
|
uri.erase(uri.size() - 1);
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
#! /bin/bash
|
|
||||||
echo "status: 100\r\n"
|
|
||||||
echo "\r\n\r\n"
|
|
||||||
echo "hiii"
|
|
||||||
@@ -24,7 +24,7 @@ int main (int ac, char **av, char ** env)
|
|||||||
path = get_value("file", rq_body);
|
path = get_value("file", rq_body);
|
||||||
path = "./www/" + path;
|
path = "./www/" + path;
|
||||||
|
|
||||||
status = ::eval_file_read(path);
|
status = ::eval_file_access(path, R_OK);
|
||||||
if (status)
|
if (status)
|
||||||
{
|
{
|
||||||
std::cout << "Status: " << status << CRLF CRLF;
|
std::cout << "Status: " << status << CRLF CRLF;
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ void
|
|||||||
http_body += HTML_BODY_BOTTOM;
|
http_body += HTML_BODY_BOTTOM;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t eval_file_read(const std::string &path)
|
size_t eval_file_access(const std::string &path, int mode)
|
||||||
{
|
{
|
||||||
if (::access(path.c_str(), F_OK) == -1)
|
if (::access(path.c_str(), F_OK) == -1)
|
||||||
{
|
{
|
||||||
@@ -189,7 +189,7 @@ size_t eval_file_read(const std::string &path)
|
|||||||
return 404; // NOT_FOUND, file doesn't exist
|
return 404; // NOT_FOUND, file doesn't exist
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::access(path.c_str(), R_OK) == -1)
|
if (::access(path.c_str(), mode) == -1)
|
||||||
{
|
{
|
||||||
std::perror("err access()");
|
std::perror("err access()");
|
||||||
return 403; // FORBIDDEN, file doesn't have access permission
|
return 403; // FORBIDDEN, file doesn't have access permission
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ void
|
|||||||
fill_body_basic(char **env, std::string & http_body, const std::string & rq_body);
|
fill_body_basic(char **env, std::string & http_body, const std::string & rq_body);
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
eval_file_read(const std::string &path);
|
eval_file_access(const std::string &path, int mode);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
#! /usr/bin/php
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
if(isset($_FILE["fileToUpload"]))
|
|
||||||
echo "there is a file";
|
|
||||||
else
|
|
||||||
echo "\n\nHeader: something\r\n\r\nno file\r\n";
|
|
||||||
|
|
||||||
?>
|
|
||||||
49
srcs/cgi-bin/upload_file.php
Executable file → Normal file
49
srcs/cgi-bin/upload_file.php
Executable file → Normal file
@@ -1,26 +1,34 @@
|
|||||||
#! /usr/bin/php
|
#! /bin/bash/php
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
// $FileName=$_FILES['myFile']['filename'];
|
||||||
|
# $TmpName=$_FILES['myFile']['tmp_name'];
|
||||||
|
# move_uploaded_file($TmpName, $FileName);
|
||||||
|
# echo("File was uploaded successfully!");
|
||||||
|
|
||||||
|
// this part needs to be grabed from POST uri
|
||||||
|
$target_dir = "./www/uploaded/";
|
||||||
$target_dir = "user_files/";
|
//$target_dir = $_POST["upload_dir"];
|
||||||
|
//$target_dir = $_GET["upload_dir"];
|
||||||
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
|
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
|
||||||
$uploadOk = 1;
|
$uploadOk = 1;
|
||||||
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
|
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
|
||||||
|
//$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
|
||||||
|
|
||||||
// Check if image file is a actual image or fake image
|
// Check if image file is a actual image or fake image
|
||||||
if(isset($_POST["submit"])) {
|
//if(isset($_POST["submit"])) {
|
||||||
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
|
// $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
|
||||||
if($check !== false) {
|
// if($check !== false) {
|
||||||
echo "File is an image - " . $check["mime"] . ".";
|
// echo "File is an image - " . $check["mime"] . ".";
|
||||||
$uploadOk = 1;
|
// $uploadOk = 1;
|
||||||
} else {
|
// } else {
|
||||||
echo "File is not an image.";
|
// echo "File is not an image.";
|
||||||
$uploadOk = 0;
|
// $uploadOk = 0;
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
// Check if file already exists
|
// Check if file already exists
|
||||||
if (file_exists($target_file)) {
|
if (file_exists($target_file)) {
|
||||||
@@ -35,11 +43,11 @@ if ($_FILES["fileToUpload"]["size"] > 500000) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allow certain file formats
|
// Allow certain file formats
|
||||||
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
|
//if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
|
||||||
&& $imageFileType != "gif" ) {
|
//&& $imageFileType != "gif" ) {
|
||||||
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
|
// echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
|
||||||
$uploadOk = 0;
|
// $uploadOk = 0;
|
||||||
}
|
//}
|
||||||
|
|
||||||
// Check if $uploadOk is set to 0 by an error
|
// Check if $uploadOk is set to 0 by an error
|
||||||
if ($uploadOk == 0) {
|
if ($uploadOk == 0) {
|
||||||
@@ -54,3 +62,4 @@ if ($uploadOk == 0) {
|
|||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
#! /usr/bin/php
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<?php
|
|
||||||
// $FileName=$_FILES['myFile']['filename'];
|
|
||||||
# $TmpName=$_FILES['myFile']['tmp_name'];
|
|
||||||
# move_uploaded_file($TmpName, $FileName);
|
|
||||||
# echo("File was uploaded successfully!");
|
|
||||||
|
|
||||||
// this part needs to be grabed from POST uri
|
|
||||||
$target_dir = "./www/user_files/";
|
|
||||||
//$target_dir = $_POST["upload_dir"];
|
|
||||||
//$target_dir = $_GET["upload_dir"];
|
|
||||||
//$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
|
|
||||||
$target_file = $target_dir . basename($_FILES["myFile"]["name"]);
|
|
||||||
|
|
||||||
$uploadOk = 1;
|
|
||||||
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
|
|
||||||
//$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
|
|
||||||
|
|
||||||
// Check if image file is a actual image or fake image
|
|
||||||
//if(isset($_POST["submit"])) {
|
|
||||||
// $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
|
|
||||||
// if($check !== false) {
|
|
||||||
// echo "File is an image - " . $check["mime"] . ".";
|
|
||||||
// $uploadOk = 1;
|
|
||||||
// } else {
|
|
||||||
// echo "File is not an image.";
|
|
||||||
// $uploadOk = 0;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Check if file already exists
|
|
||||||
if (file_exists($target_file)) {
|
|
||||||
echo "Sorry, file already exists.";
|
|
||||||
$uploadOk = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check file size
|
|
||||||
if ($_FILES["fileToUpload"]["size"] > 500000) {
|
|
||||||
echo "Sorry, your file is too large.";
|
|
||||||
$uploadOk = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow certain file formats
|
|
||||||
//if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
|
|
||||||
//&& $imageFileType != "gif" ) {
|
|
||||||
// echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
|
|
||||||
// $uploadOk = 0;
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Check if $uploadOk is set to 0 by an error
|
|
||||||
if ($uploadOk == 0) {
|
|
||||||
echo "Sorry, your file was not uploaded.";
|
|
||||||
// if everything is ok, try to upload file
|
|
||||||
} else {
|
|
||||||
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
|
|
||||||
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
|
|
||||||
} else {
|
|
||||||
echo "Sorry, there was an error uploading your file.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
|
|
||||||
|
|
||||||
@@ -241,14 +241,15 @@ void Webserv::_check_script_output(Client *client, std::string & output)
|
|||||||
_check_script_status(client, output);
|
_check_script_status(client, output);
|
||||||
if (client->status >= 400 && client->status < 600)
|
if (client->status >= 400 && client->status < 600)
|
||||||
return;
|
return;
|
||||||
//*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
client->status = _check_script_fields(output, client->status);
|
client->status = _check_script_fields(output, client->status);
|
||||||
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_check_fields_duplicates(client, output);
|
_check_fields_duplicates(client, output);
|
||||||
//*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[fields duplicates]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_remove_body_leading_empty_lines(output);
|
_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";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_add_script_body_length_header(output);
|
_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";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Webserv::_check_script_status(Client *client, std::string & output)
|
void Webserv::_check_script_status(Client *client, std::string & output)
|
||||||
@@ -273,9 +274,11 @@ size_t Webserv::_check_script_fields(const std::string & output, size_t status)
|
|||||||
std::string body;
|
std::string body;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
|
|
||||||
|
std::cerr << "0\n";
|
||||||
pos = output.find(CRLF CRLF);
|
pos = output.find(CRLF CRLF);
|
||||||
if (pos == NPOS) // there is not empty line
|
if (pos == NPOS) // there is not empty line
|
||||||
return 500;
|
return 500;
|
||||||
|
std::cerr << "1\n";
|
||||||
headers = output.substr(0, pos);
|
headers = output.substr(0, pos);
|
||||||
body = output.substr(pos + CRLF_SIZE * 2);
|
body = output.substr(pos + CRLF_SIZE * 2);
|
||||||
headers = str_tolower(headers);
|
headers = str_tolower(headers);
|
||||||
@@ -284,14 +287,18 @@ size_t Webserv::_check_script_fields(const std::string & output, size_t status)
|
|||||||
{
|
{
|
||||||
if (!body.empty()) // there is body
|
if (!body.empty()) // there is body
|
||||||
return 500;
|
return 500;
|
||||||
|
std::cerr << "2\n";
|
||||||
if (headers.find("location") == NPOS) // there is no location field
|
if (headers.find("location") == NPOS) // there is no location field
|
||||||
return 500;
|
return 500;
|
||||||
|
std::cerr << "3\n";
|
||||||
}
|
}
|
||||||
else if (headers.find("location") != NPOS) // there is a location field
|
else if (headers.find("location") != NPOS) // there is a location field
|
||||||
{
|
{
|
||||||
if (body.empty()) // there is no body
|
if (body.empty()) // there is no body
|
||||||
return 500;
|
return 500;
|
||||||
|
std::cerr << "4\n";
|
||||||
}
|
}
|
||||||
|
std::cerr << "5\n";
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,13 +53,10 @@ int Webserv::_read_request(Client *client)
|
|||||||
// ::print_special(client->raw_request);
|
// ::print_special(client->raw_request);
|
||||||
// std::cerr << "__raw_request__\n" << client->raw_request << "\n______\n"; // DEBUG
|
// std::cerr << "__raw_request__\n" << client->raw_request << "\n______\n"; // DEBUG
|
||||||
|
|
||||||
print_special(client->raw_request);
|
// print_special(client->raw_request);
|
||||||
|
|
||||||
std::cerr << "client header complete: " << client->header_complete << "\n"; // DEBUG
|
|
||||||
|
|
||||||
if (!client->header_complete)
|
if (!client->header_complete)
|
||||||
{
|
{
|
||||||
std::cout << "Header not complete\n"; // debug
|
|
||||||
client->parse_request_headers(_servers);
|
client->parse_request_headers(_servers);
|
||||||
if (client->status)
|
if (client->status)
|
||||||
return READ_COMPLETE;
|
return READ_COMPLETE;
|
||||||
@@ -78,7 +75,6 @@ int Webserv::_read_request(Client *client)
|
|||||||
}
|
}
|
||||||
if (client->header_complete)
|
if (client->header_complete)
|
||||||
{
|
{
|
||||||
std::cerr << "Client header Complete\n";
|
|
||||||
// client->read_body_size += ret; // Not accurate, part of body could have been read with headers, unused for now
|
// client->read_body_size += ret; // Not accurate, part of body could have been read with headers, unused for now
|
||||||
client->parse_request_body();
|
client->parse_request_body();
|
||||||
if (client->status || client->body_complete)
|
if (client->status || client->body_complete)
|
||||||
|
|||||||
@@ -52,12 +52,12 @@ int Webserv::_send_response(Client *client)
|
|||||||
}
|
}
|
||||||
else if (!client->cgi_output.empty())
|
else if (!client->cgi_output.empty())
|
||||||
{
|
{
|
||||||
// /*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";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(client->cgi_output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
_check_script_output(client, client->cgi_output); // FD_CGI : adjust for client->cgi_output;
|
_check_script_output(client, client->cgi_output); // FD_CGI : adjust for client->cgi_output;
|
||||||
if (client->status < 400)
|
if (client->status < 400)
|
||||||
client->response += client->cgi_output;
|
client->response += client->cgi_output;
|
||||||
// /*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
/*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
_insert_status_line(client);
|
_insert_status_line(client);
|
||||||
@@ -201,3 +201,4 @@ void Webserv::_append_body(Client *client, const std::string &body, const std::s
|
|||||||
client->response.append(CRLF);
|
client->response.append(CRLF);
|
||||||
client->response.append(body);
|
client->response.append(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,7 @@ void Webserv::_read_cgi_output(Client *client)
|
|||||||
else if (ret == 0)
|
else if (ret == 0)
|
||||||
std::cerr << "Madame s'il vous plait, du Ketchup pour mon hamburger" << " (AKA:ret=0)" << "\n";
|
std::cerr << "Madame s'il vous plait, du Ketchup pour mon hamburger" << " (AKA:ret=0)" << "\n";
|
||||||
else
|
else
|
||||||
{
|
|
||||||
std::cerr << "NORMAL BEHAVIOR I THINK!\n"; // debug
|
|
||||||
client->cgi_output.append(buf, ret);
|
client->cgi_output.append(buf, ret);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Webserv::_handle_epoll_error_cgi_fd(uint32_t events, Client *client)
|
void Webserv::_handle_epoll_error_cgi_fd(uint32_t events, Client *client)
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Webserv test Something</title>
|
||||||
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<h1 style="text-align:center">Webserv in Test</h1>
|
||||||
<!--- <form action="/cgi-bin/tmp.php" method="post" enctype="multipart/form-data"> -->
|
<hr>
|
||||||
<form action="/cgi-bin/upload_file.php" method="post" enctype="multipart/form-data">
|
<p style="text-align:center">Time to submit something:</p>
|
||||||
Select image to upload:
|
<!-- <form action="./srcs/cgi-bin/upload_file.php" method="post" enctype="multipart/form-data"> -->
|
||||||
<input type="file" name="fileToUpload" id="fileToUpload">
|
<form action="/uploaded" method="post" enctype="multipart/form-data">
|
||||||
<input type="submit" value="Upload Image" name="submit">
|
<!-- <input type="hidden" name="upload_dir" value="./www/uploaded/"> -->
|
||||||
</form>
|
<input type="file" id="fileToUpload" name="myFile">
|
||||||
|
<input type="submit" value="Upload File">
|
||||||
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Webserv test Something</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1 style="text-align:center">Webserv in Test</h1>
|
|
||||||
<hr>
|
|
||||||
<p style="text-align:center">Time to submit something:</p>
|
|
||||||
<!-- <form action="./srcs/cgi-bin/upload_file.php" method="post" enctype="multipart/form-data"> -->
|
|
||||||
<form action="/cgi-bin/upload_file.php" method="post" enctype="multipart/form-data">
|
|
||||||
<!-- <input type="hidden" name="upload_dir" value="./www/uploaded/"> -->
|
|
||||||
<input type="file" id="fileToUpload" name="fileToUpload">
|
|
||||||
<input type="submit" value="Upload File">
|
|
||||||
</form>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
https://www.rfc-editor.org/rfc/rfc9110#name-multipart-types
|
https://www.rfc-editor.org/rfc/rfc9110#name-multipart-types
|
||||||
https://www.rfc-editor.org/rfc/rfc2046#section-5.1.1
|
https://www.rfc-editor.org/rfc/rfc2046#section-5.1.1
|
||||||
-->
|
-->
|
||||||
<form action="/cgi-bin/upload_file.php" method="post" enctype="multipart/form-data">
|
<form action="/upload" method="post" enctype="multipart/form-data">
|
||||||
<input type="file" name="upload_file1">
|
<input type="file" name="upload_file1">
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user