-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcgi.cpp
161 lines (142 loc) · 5.13 KB
/
fcgi.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// FROM: http://chriswu.me/blog/getting-request-uri-and-content-in-c-plus-plus-fcgi/
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fcgi_stdio.h>
#include "fcgio.h"
#include "ConfigFile.h"
#include <fstream>
#include <string.h>
using namespace std;
// Maximum bytes
const unsigned long STDIN_MAX = 10000000;
string get_request_content(const FCGX_Request &request) {
char *content_length_str = FCGX_GetParam("CONTENT_LENGTH", request.envp);
unsigned long content_length = STDIN_MAX;
if (content_length_str) {
content_length = (unsigned long) strtol(content_length_str, &content_length_str, 10);
if (*content_length_str) {
cerr << "Can't Parse 'CONTENT_LENGTH='"
<< FCGX_GetParam("CONTENT_LENGTH", request.envp)
<< "'. Consuming stdin up to " << STDIN_MAX << endl;
}
if (content_length > STDIN_MAX) {
content_length = STDIN_MAX;
}
} else {
// Do not read from stdin if CONTENT_LENGTH is missing
content_length = 0;
}
char *content_buffer = new char[content_length];
cin.read(content_buffer, content_length);
content_length = (unsigned long) cin.gcount();
do cin.ignore(1024); while (cin.gcount() == 1024);
string content(content_buffer, content_length);
delete[] content_buffer;
return content;
}
string getFilename(const FCGX_Request &request) {
const char *contentDisposition = FCGX_GetParam("HTTP_CONTENT_DISPOSITION", request.envp);
if (contentDisposition) {
string content(contentDisposition);
int index = (int) content.find("filename=");
string filename;
if (index != -1) {
index += 10;
try {
while (contentDisposition[index] != '\"') {
filename.push_back(contentDisposition[index]);
index++;
}
}
catch (exception &e) {
perror("Error reading filename.");
return "";
}
return filename;
}
}
return "";
}
int main(void) {
// Backup the stdio streambufs
streambuf *cin_streambuf = cin.rdbuf();
streambuf *cout_streambuf = cout.rdbuf();
streambuf *cerr_streambuf = cerr.rdbuf();
FCGX_Request request;
FCGX_Init();
int port;
string ip = "";
if (ConfigFile::getConfigFile().readFCGI(&ip, &port) == -1) return -1;
string s = ":";
s.append(to_string(port));
char const *port_char = s.c_str();
int sock = FCGX_OpenSocket(port_char, 1024);
FCGX_InitRequest(&request, sock, 0);
while (FCGX_Accept_r(&request) >= 0) {
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
// get the request URI
const char *uri = FCGX_GetParam("REQUEST_URI", request.envp);
string content = get_request_content(request);
if (content.length() == 0) {
content = "";
}
string filename = getFilename(request);
if (filename.length() > 0) {
if (filename.substr(filename.length() - 4, 4) == ".txt") {
ofstream myfile;
myfile.open(filename.c_str());
myfile << content << "\n";
myfile.close();
cout << "HTTP/1.1 200 OK\r\n"
<< "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>POST request!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>FILE SAVED to " << filename.c_str() << "/h1>\n"
<< " </body>\n"
<< "</html>\n"
<< "\r\n";
} else {
cout << "HTTP/1.1 200 OK\r\n"
<< "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>POST request!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Wrong file extension./h1>\n"
<< " </body>\n"
<< "</html>\r\n"
<< "\r\n";
}
} else {
cout << "HTTP/1.1 200 OK\r\n"
<< "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>Hello, World!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Hello " << " from " << uri << " !</h1>\n"
<< " </body>\n"
<< "</html>\n"
<< "\r\n";
}
}
// restore stdio streambufs
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
return 0;
}