diff options
author | cflip <cflip@cflip.net> | 2022-09-19 13:47:51 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-09-19 13:48:33 -0600 |
commit | 2ef8197a75c3d7605279b6c41c2eb3e1643d5374 (patch) | |
tree | aebda710c6ac59132d47554fc5420e2ac6cc16d3 /src/CGIScript.cpp | |
parent | 91c237df6a65f6ec446eea4b89216ad53c9f7e0e (diff) |
Refactor the CGIScript class a bit
This allows the user of the class to set environment variables before
opening the pipe to the script.
Diffstat (limited to 'src/CGIScript.cpp')
-rw-r--r-- | src/CGIScript.cpp | 33 |
1 files changed, 17 insertions, 16 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 <string> #include <sstream> -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; |