blob: 6bc62ce15c7da012760944093416980c91bcbc47 (
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
|
#include "CGIScript.h"
#include <cstdlib>
#include <string>
#include <sstream>
CGIScript::CGIScript(const std::string& path, const HttpRequest& request)
{
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("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;
}
CGIScript::~CGIScript()
{
pclose(m_pipe);
for (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, true);
}
std::string CGIScript::read_output()
{
std::stringstream sstream;
char ch;
while ((ch = fgetc(m_pipe)) != EOF)
sstream << ch;
return sstream.str();
}
|