Skip to content

Commit

Permalink
Merge main into issue267-handle_no_host_header
Browse files Browse the repository at this point in the history
  • Loading branch information
ayase-mstk committed Jul 22, 2024
2 parents 350a4ed + d5043d4 commit f83323d
Show file tree
Hide file tree
Showing 22 changed files with 68 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ logs
*tmp*
.vscode
.DS_Store
*.cgi
*.log
__pycache__
.clangd
.clangd
28 changes: 7 additions & 21 deletions conf/webserv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ events {
error_log logs/error.log error;

http {
root html;
autoindex on;
root .;
client_max_body_size 5k;
keepalive_timeout 60s;
receive_timeout 60s;
send_timeout 60s;
server {
listen 127.0.0.1:4242 default_server;
listen 127.0.0.1:8005;
server_name localhost;
index /index.html;

location / {
root html;
index index.html;

# delete method 禁止
Expand Down Expand Up @@ -44,23 +47,8 @@ http {
try_files /internal_redirect.html /;
}

# 無限リダイレクト
error_page 405 /code-error-page/;
location /code-error-page/ {
try_files /nothing.html /no.html =405;
}

}

server { # timeout確認用
listen 127.0.0.1:9090;
server_name test;
keepalive_timeout 10s;
receive_timeout 5s;
send_timeout 5s;

location / {
index index.html;
location /test/ {
autoindex on;
}
}

Expand All @@ -72,7 +60,5 @@ http {
location / {
index hello.html;
}

}

}
24 changes: 19 additions & 5 deletions src/cgi/CgiExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ void cgi::CgiExecutor::executeCgiScript(const HttpRequest& request, const HttpRe
void cgi::CgiExecutor::prepareCgiExecution(const HttpRequest& request, const HttpResponse& response,
const std::string& full_path, int cgi_sock, int cli_sock) {
if (!redirectStdIOToSocket(request, cgi_sock)) std::exit(EXIT_FAILURE);
this->script_path_ = full_path;
createArgv(full_path);
createMetaVars(request, response, cli_sock);
// scriptが存在するdirに移動する
if (syscall_wrapper::Chdir(createCgiDirPath(full_path).c_str()) == -1) std::exit(EXIT_FAILURE);
}

void cgi::CgiExecutor::createArgv(const std::string& script_path) {
const std::string::size_type n = script_path.rfind("/");
const std::string cgi_script = n == std::string::npos ? script_path : script_path.substr(n + 1);
this->argv_.push_back(strdupFromString(cgi_script));
/**
* @brief script pathとargvを生成する
* scriptのpathを指定するが、pathは固定で、["./" + script_name]
* なぜなら、execveでスクリプトを実行する前に、chdir()でcur dirをscriptのpathに変更するから
*/
void cgi::CgiExecutor::createArgv(const std::string& full_path) {
std::string::size_type n = full_path.rfind("/");
std::string script_name = n == std::string::npos ? full_path : full_path.substr(n + 1);
this->script_path_ = "./" + script_name;
this->argv_.push_back(strdupFromString(script_name));
this->argv_.push_back(NULL);
}

Expand Down Expand Up @@ -124,6 +131,13 @@ void cgi::CgiExecutor::createMetaVars(const HttpRequest& request, const HttpResp
this->meta_vars_.push_back(NULL);
}

/**
* '/'が見つからない場合は、文字列そのままを返す
*/
std::string cgi::CgiExecutor::createCgiDirPath(const std::string& script_path) const {
return script_path.substr(0, script_path.rfind("/"));
}

std::vector<std::string> cgi::CgiExecutor::split(const std::string& s, char delimiter) const {
std::vector<std::string> tokens;
std::string token;
Expand Down
1 change: 1 addition & 0 deletions src/cgi/CgiExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CgiExecutor {
const std::string& full_path, int cgi_sock, int cli_sock);
void createArgv(const std::string& script_path);
void createMetaVars(const HttpRequest& request, const HttpResponse& response, int cli_sock);
std::string createCgiDirPath(const std::string& script_path) const;
std::vector<std::string> split(const std::string& s, char delimiter) const;
bool isExecutableFile(const std::string&) const;
char* strdupFromString(const std::string&) const;
Expand Down
4 changes: 2 additions & 2 deletions src/server/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void EventHandler::handleCgiReadEvent(NetworkIOHandler &io_handler, ConnectionMa
IServer *server, TimerTree &timer_tree, int sock) const {
const ConfigHandler &config_handler = WebServer::getConfigHandler();
ssize_t re = io_handler.receiveCgiResponse(conn_manager, sock);
int status = -1;
int status = 0;
// recv error
if (re == -1) return;
cgi::CgiHandler &cgi_handler = conn_manager.getCgiHandler(sock);
Expand Down Expand Up @@ -232,7 +232,7 @@ void EventHandler::handleCgiWriteEvent(NetworkIOHandler &io_handler, ConnectionM

const std::string &body = conn_manager.getRequest(sock).body_;
const cgi::CgiHandler &cgi_handler = conn_manager.getCgiHandler(sock);
int status = -1;
int status = 0;
if (conn_manager.getSentBytes(sock) != body.size() && // bodyをまだ送る必要がある
!cgiProcessExited(cgi_handler.getCgiProcessId(), status)) { // cgi processが生きている
addTimerByType(conn_manager, config_handler, timer_tree, sock,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/syscall_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ int syscall_wrapper::Fcntl(int fd, int cmd, int flags) {
if (re == -1) WebServer::writeErrorlog(error::strSysCallError("fctnl"), config::EMERG);
return re;
}

int syscall_wrapper::Chdir(const char *path) {
int re = chdir(path);
if (re == -1) WebServer::writeErrorlog(error::strSysCallError("chdir"), config::EMERG);
return re;
}
1 change: 1 addition & 0 deletions src/utils/syscall_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int Pipe(int fildes[2]);
pid_t Fork(void);
int Dup2(int fildes, int fildes2);
int Fcntl(int fd, int cmd, int flags);
int Chdir(const char *path);
} // namespace syscall_wrapper

#endif
7 changes: 3 additions & 4 deletions test/cgi/CgiExecutor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ TEST(cgi_executor, path_info_GET) {
ConnectionData cd;
cd.request_ = test::initRequest(config::REQUEST_METHOD::GET, "/path/uri/", "HTTP/1.1",
"one=1&two=2&three=3", "", {{"Host", "tt"}, {"content-type", "text/html"}});
cd.response_ = test::initResponse("./", "test/cgi/cgi_files/executor/path_info.py",
"/test/cgi/cgi_files/executor/path_info_dir/");
cd.response_ = test::initResponse("./", "test/cgi/cgi_files/executor/path_info.py", "/path_info_dir/");

const std::string expect_header = "Content-Type: text/html\r\nStatus: 200 OK\r\n\r\n";
const std::string expect = expect_header + "<ul>\r\n" + "<li><a href=\"a\">a</a></li>\r\n" +
Expand All @@ -218,8 +217,8 @@ TEST(cgi_executor, path_info_POST) {
ConnectionData cd;
cd.request_ = test::initRequest(config::REQUEST_METHOD::POST, "/path/uri/", "HTTP/1.1", "", "name=mahayase",
{{"Host", "tt"}, {"content-type", "text/html"}, {"Content-Length", "13"}});
cd.response_ = test::initResponse("./", "test/cgi/cgi_files/executor/post_and_pathinfo.py",
"/test/cgi/cgi_files/executor/path_info_dir/");
cd.response_ =
test::initResponse("./", "test/cgi/cgi_files/executor/post_and_pathinfo.py", "/path_info_dir/");

const std::string expect_header = "Content-Type: text/html\r\nStatus: 200 OK\r\n\r\n";
const std::string expect =
Expand Down
3 changes: 3 additions & 0 deletions test/integration_test/server_res_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function runTest {
assert "${root}/dynamic/body_res.py" "200" "GET" ""
assert "${root}/dynamic/post_cgi.py?key=value" "200" "GET" ""
assert "${root}/dynamic/body_res.py" "413" "GET" "-d \"${random_string}\""
assert "${root}/../cgi_test/check_cur_dir.py" "200" "GET" ""

# HEAD
assert "${root}/static/index.html" "200" "HEAD" ""
Expand Down Expand Up @@ -133,6 +134,7 @@ function runTest {
assert "${root}/dynamic/client_redirect_res_doc.cgi" "302" "POST" ""
assert "${root}/dynamic/body_res.py" "200" "POST" ""
assert "${root}/dynamic/body_res.py" "413" "POST" "-d \"${random_string}\""
assert "${root}/../cgi_test/check_cur_dir.py" "200" "POST" ""

# DELETE
assert "${root}/dynamic/post_cgi.py" "405" "DELETE" ""
Expand All @@ -146,6 +148,7 @@ function runTest {
assert "${root}/dynamic/client_redirect_res_doc.cgi" "302" "DELETE" ""
assert "${root}/dynamic/body_res.py" "200" "DELETE" ""
assert "${root}/dynamic/body_res.py" "413" "DELETE" "-d \"${random_string}\""
assert "${root}/../cgi_test/check_cur_dir.py" "200" "DELETE" ""

# UNKNOWN
assert "${root}/static/index.html" "400" "UNKNOWN" ""
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions test/integration_test/test_files/cgi_test/check_cur_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/python3
import os

current_dir = os.getcwd()
dir_name = os.path.basename(current_dir)

assert dir_name == "cgi_test", f"Expected 'cgi_test', but got '{dir_name}'"

print("\n\n", end="")
13 changes: 13 additions & 0 deletions test/integration_test/test_files/cgi_test/print_cur_dir_files.cgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /bin/bash

main() {
echo

for file in *; do
echo $file
done

exit 0
}

main "$@"
2 changes: 1 addition & 1 deletion test/integration_test/test_files/delete_test/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cgi

# 許可されたディレクトリ
ALLOWED_DIRECTORY = "test/integration_test/test_files/delete_test/files"
ALLOWED_DIRECTORY = "files"

# CGIヘッダーを出力
headers = []
Expand Down
3 changes: 2 additions & 1 deletion test/integration_test/test_files/upload_test/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# アップロードを許可する拡張子
ALLOWED_EXTENSIONS = ["html", "txt", "jpg", "jpeg"]

save_dir = "uploads"

# CGIヘッダーを出力
header = "Content-Type: text/html\n"
body = "<html><body>\n"
Expand All @@ -28,7 +30,6 @@
# 許可する拡張子かどうかチェック
if extension in ALLOWED_EXTENSIONS:
try:
save_dir = "test/integration_test/test_files/upload_test/uploads"
if os.path.exists(save_dir):
# ファイルを保存
save_path = f"{save_dir}/{fn}"
Expand Down

0 comments on commit f83323d

Please sign in to comment.