blob: 87310238e4c1b7c0028d4d12446780b4431175b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include "CGIScript.h"
#include <cstdlib>
#include <sstream>
#include <string>
CGIScript::CGIScript(const std::string& script_path)
: m_script_path(script_path)
{
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");
}
CGIScript::~CGIScript()
{
pclose(m_pipe);
for (const auto* key : m_environment_variables)
unsetenv(key);
}
void CGIScript::set_environment(const char* key, const char* value)
{
m_environment_variables.push_back(key);
setenv(key, value, 1);
}
bool CGIScript::open()
{
m_pipe = popen(m_script_path.c_str(), "r");
if (m_pipe == nullptr) {
perror("cfws: popen");
return false;
}
m_is_open = true;
return true;
}
std::string CGIScript::read_output()
{
std::stringstream sstream;
char ch = 0;
while ((ch = fgetc(m_pipe)) != EOF)
sstream << ch;
return sstream.str();
}
|