-
Notifications
You must be signed in to change notification settings - Fork 6
/
CGIProcessor.cpp
113 lines (98 loc) · 3.07 KB
/
CGIProcessor.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
#include "CGIProcessor.h"
#include "DijkstraShortPath.h"
#include "GraphMLReporter.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
std::vector<String> CGIProcessor::GetRequestParams()
{
std::vector<String> res = m_parameters;
if (res.empty())
{
char * requestMethod = getenv("REQUEST_METHOD");
if (strstr(requestMethod, "POST"))
{
// "REQUEST_STRING" should have start=n0&finish=n2&report=xml
char* requestBuffer = getenv("QUERY_STRING");
if (requestBuffer)
{
String requestString (requestBuffer);
std::vector<String> delemiters;
delemiters.push_back("=");
delemiters.push_back("&");
res = SplitString(requestString, delemiters);
}
}
m_parameters = res;
}
return res;
}
// Return buffer of graph.
String CGIProcessor::GetGraphBuffer()
{
String res = m_graphBuffer;
if (res.IsEmpty())
{
char * requestMethod = getenv("REQUEST_METHOD");
if (strstr(requestMethod, "POST"))
{
// CGI mode.
const char * strContentLength = getenv("CONTENT_LENGTH");
if (strContentLength)
{
long contentLength = strtol(strContentLength, NULL, 10);
if (contentLength > 0)
{
char* postdata = new char[contentLength + 1];
size_t total_readed_size = 0;
while (total_readed_size < contentLength)
{
size_t readed_size = fread(postdata + total_readed_size, 1, contentLength - total_readed_size, stdin);
total_readed_size += readed_size;
if (readed_size == 0)
break;
}
postdata[contentLength] = 0;
res.FromLocale(postdata);
delete[] postdata;
}
}
}
m_graphBuffer = res;
}
return res;
}
std::vector<String> CGIProcessor::SplitString(const String& inputString, const std::vector<String>& delemiters)
{
std::vector<String> res;
String input = inputString;
while (!input.IsEmpty())
{
int nMinPosition = int(input.Count());
int delimiterLength = 0;
for (const String& delemiter : delemiters)
{
int pos = input.Find(delemiter);
if (pos >= 0)
{
nMinPosition = std::min(pos, nMinPosition);
delimiterLength = delemiter.Count();
}
}
if (nMinPosition < input.Count())
{
String param = String(input).SubStr(0, nMinPosition);
res.push_back(param);
input = input.SubStr(nMinPosition + delimiterLength);
}
else
{
break;
}
}
if (!input.IsEmpty())
{
res.push_back(input);
}
return res;
}