diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CGIScript.cpp | 33 | ||||
-rw-r--r-- | src/CGIScript.h | 7 | ||||
-rw-r--r-- | 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 <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; 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 <string> #include <vector> -#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<const char*> 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!"); |