From 2ef8197a75c3d7605279b6c41c2eb3e1643d5374 Mon Sep 17 00:00:00 2001 From: cflip Date: Mon, 19 Sep 2022 13:47:51 -0600 Subject: Refactor the CGIScript class a bit This allows the user of the class to set environment variables before opening the pipe to the script. --- src/CGIScript.cpp | 33 +++++++++++++++++---------------- src/CGIScript.h | 7 +++---- src/main.cpp | 9 +++++++-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/CGIScript.cpp b/src/CGIScript.cpp index 6bc62ce..c630459 100644 --- a/src/CGIScript.cpp +++ b/src/CGIScript.cpp @@ -4,24 +4,13 @@ #include #include -CGIScript::CGIScript(const std::string& path, const HttpRequest& request) +CGIScript::CGIScript(const std::string& script_path) + : m_script_path(script_path) { - set_environment("CONTENT_LENGTH", "0"); - set_environment("REQUEST_URI", request.uri().c_str()); - set_environment("PATH_INFO", request.uri().c_str()); - set_environment("SCRIPT_NAME", path.c_str()); - set_environment("SCRIPT_FILENAME", path.c_str()); - set_environment("REQUEST_METHOD", "GET"); + set_environment("SCRIPT_NAME", script_path.c_str()); + set_environment("SCRIPT_FILENAME", script_path.c_str()); set_environment("SERVER_PROTOCOL", "HTTP/1.1"); - set_environment("SERVER_SOFTWARE", "cfws/1.0-dev"); - - m_pipe = popen(path.c_str(), "r"); - if (!m_pipe) { - perror("cfws: popen"); - return; - } - - m_is_open = true; + set_environment("SERVER_SOFTWARE", "cfws"); } CGIScript::~CGIScript() @@ -38,6 +27,18 @@ void CGIScript::set_environment(const char* key, const char* value) setenv(key, value, true); } +bool CGIScript::open() +{ + m_pipe = popen(m_script_path.c_str(), "r"); + if (!m_pipe) { + perror("cfws: popen"); + return false; + } + + m_is_open = true; + return true; +} + std::string CGIScript::read_output() { std::stringstream sstream; diff --git a/src/CGIScript.h b/src/CGIScript.h index a64c55a..622206d 100644 --- a/src/CGIScript.h +++ b/src/CGIScript.h @@ -4,19 +4,18 @@ #include #include -#include "HttpRequest.h" - class CGIScript { public: - CGIScript(const std::string& path, const HttpRequest&); + CGIScript(const std::string& script_path); ~CGIScript(); void set_environment(const char* key, const char* value); - bool is_open() const { return m_is_open; } + bool open(); std::string read_output(); private: FILE* m_pipe; + const std::string& m_script_path; bool m_is_open{false}; std::vector m_environment_variables; diff --git a/src/main.cpp b/src/main.cpp index c24747e..cd338b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,8 +55,13 @@ static HttpResponse serve_from_cgi(const std::string& script_path, HttpRequest r HttpResponse response; response.add_header("Server", "cfws"); - CGIScript script(script_path, request); - if (!script.is_open()) { + CGIScript script(script_path); + script.set_environment("REQUEST_METHOD", "GET"); + script.set_environment("REQUEST_URI", request.uri().c_str()); + script.set_environment("PATH_INFO", request.uri().c_str()); + script.set_environment("CONTENT_LENGTH", "0"); + + if (!script.open()) { response.set_status_code(HttpStatusCode::InternalServerError); response.add_header("Content-Type", "text/plain"); response.set_content("Failed to open CGI script!"); -- cgit v1.2.3