forked from benibela/rcmdline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcmdlinecgi.pas
199 lines (156 loc) · 4.93 KB
/
rcmdlinecgi.pas
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
(*** @abstract(
Request reader for CGI applications (fpc only)
)*)
unit rcmdlinecgi;
{$ifdef fpc}{$mode delphi}{$endif}
interface
uses
Classes, SysUtils, rcmdline, custcgi, custweb, HTTPDefs;
type
{ TCommandLineReaderGUI }
(*** @abstract(
Request reader for CGI applications, like TCommandLineReader (fpc only)
)
*)
{ TCommandLineReaderCGI }
TCgiHandlerForCommandLineReader = class;
TCommandLineReaderCGI=class(TCommandLineReader)
protected
handlerOwned: TCgiHandlerForCommandLineReader;
req: TCGIRequest;
response: TCGIResponse;
procedure showErrorCGI(error: string);
public
//**Creates a reader using the standard CGI interface
constructor create();overload;
//**Creates a reader reading the parameters from the given request, and writing errors to the given response
constructor create(request: TCGIRequest; response: TCGIResponse = nil);overload;
destructor destroy; override;
//**Actually reads the CGI data (called automatically, when a read.. function is called)
procedure parse(autoReset: boolean = true);override;
//** Return
function urlEncodeParams: string;
public
//**If this is false (=default), parameters declared with declareFile will be disabled
AllowFiles: boolean;
end;
{ TCommandLineReaderCGIApplication }
{ TCgiHandlerForCommandLineReader }
TCgiHandlerForCommandLineReader = class(TCgiHandler)
public
procedure init;
end;
implementation
uses strutils, bbutils;
{ TCgiHandlerForCommandLineReader }
procedure TCgiHandlerForCommandLineReader.init;
var
temp: TRequest;
temp2: TResponse;
begin
WaitForRequest(temp, temp2);
end;
{ TCommandLineReaderCGI }
procedure TCommandLineReaderCGI.showErrorCGI(error: string);
procedure answer(s: string);
begin
if response = nil then writeln(s)
else response.Contents.Add(s);
end;
begin
if response = nil then begin //response seems to handle the content-type
answer('Content-Type: text/html');
answer('');
end;
answer('<html><head>');
answer('<title>Error</title>');
answer('</head><body>');
answer('<h1>Error</h1>');
answer('<pre>');
answer(error);
answer('</pre>');
answer('</body></html>');
if handlerOwned <> nil then begin
response.SendResponse;
halt;
end;
end;
constructor TCommandLineReaderCGI.create;
begin
handlerOwned := TCgiHandlerForCommandLineReader.Create(nil);
handlerOwned.init;
create(handlerOwned.Request, handlerOwned.Response);
end;
constructor TCommandLineReaderCGI.create(request: TCGIRequest; response: TCGIResponse = nil);
begin
inherited create;
onShowError := showErrorCGI;
AllowFiles:=false;
req := request;
self.response := response;
end;
destructor TCommandLineReaderCGI.destroy;
begin
handlerOwned.free;
inherited destroy;
end;
procedure TCommandLineReaderCGI.parse(autoReset: boolean = true);
procedure parseVariables(list: TStrings);
var
name: String;
value: String;
j, i: Integer;
found: Boolean;
begin
for j:=0 to list.Count-1 do begin
list.GetNameValue(j, name, value);
if (name = '') then continue; //workaround for empty input created by codemirror
found := false;
for i := 0 to high(propertyArray) do begin
if SameText(propertyArray[i].name, name) then begin
propertyArray[i].strvalue := value;
propertyArray[i].found := true;
parseSingleValue(propertyArray[i]);
if assigned(onOptionRead) then onOptionRead(self, name, value);
found := true;
break;
end;
end;
if not found then raiseErrorWithHelp('Unknown parameter: '+name);
end;
end;
var
i: Integer;
temp: TStringArray;
begin
inherited parse(autoReset);
parsed := true;
req.QueryFields.Clear; //recreate req.QueryFields because fpc is completely broken
temp := strSplit(GetEnvironmentVariable('QUERY_STRING'), '&');
for i := 0 to high(temp) do
req.QueryFields.add(HTTPDecode(temp[i]));
parseVariables(req.QueryFields);
parseVariables(req.ContentFields);
if not AllowFiles then
for i:=0 to high(propertyArray) do
if (propertyArray[i].found) and (propertyArray[i].kind = kpFile) then
raiseErrorWithHelp('File parameters (like '+propertyArray[i].name+') have been forbidden due to security reasons');
end;
function TCommandLineReaderCGI.urlEncodeParams: string;
function encode(s: string): string;
begin
result := StringsReplace(s, ['%', #9, #10, #13, '"', '<', '>', '#', '$', '&', '+', ' ', ',', '/', ':', ';', '=', '?', '@'],
['%25', '%09', '%0A', '%0D', '%22', '%3C', '%3E', '%23', '%24', '%26', '%2B', '+', '%2C', '%2F', '%3A','%3B', '%3D', '%3F', '%40'],
[rfReplaceAll]);
end;
var
i: Integer;
begin
result := '';
for i := 0 to high(propertyArray) do begin
if not propertyArray[i].found then continue;
if result <> '' then result += '&';
result += encode(propertyArray[i].name) + '=' + encode(propertyArray[i].strvalue);
end;
end;
end.