blob: 3ef1f3f3e39665bed907e868944fc33b34036064 (
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include "CGIScript.h"
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
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();
}
void CGIScript::validate_path(const std::string& script_path)
{
namespace fs = std::filesystem;
if (!fs::exists(script_path)) {
std::cerr << "cfws: Script not found: " << script_path << std::endl;
exit(1);
}
if (access(script_path.c_str(), X_OK)) {
std::cerr << "cfws: Script does not have execute permissions: " << script_path << std::endl;
exit(1);
}
}
|