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 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'src/CGIScript.cpp') 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; -- cgit v1.2.3