1 | /* The CGI_C library, by Thomas Boutell, version 2.01. CGI_C is intended |
---|
2 | to be a high-quality API to simplify CGI programming tasks. */ |
---|
3 | |
---|
4 | /* Make sure this is only included once. */ |
---|
5 | |
---|
6 | #ifndef CGI_C |
---|
7 | #define CGI_C 1 |
---|
8 | |
---|
9 | /* Bring in standard I/O since some of the functions refer to |
---|
10 | types defined by it, such as FILE *. */ |
---|
11 | |
---|
12 | //#include "fcgi_stdio.h" |
---|
13 | #include <fcgiapp.h> |
---|
14 | #include <stdio.h> |
---|
15 | |
---|
16 | /* The various CGI environment variables. Instead of using getenv(), |
---|
17 | the programmer should refer to these, which are always |
---|
18 | valid null-terminated strings (they may be empty, but they |
---|
19 | will never be null). If these variables are used instead |
---|
20 | of calling getenv(), then it will be possible to save |
---|
21 | and restore CGI environments, which is highly convenient |
---|
22 | for debugging. */ |
---|
23 | |
---|
24 | |
---|
25 | typedef struct cgiFormEntryStruct { |
---|
26 | char *attr; |
---|
27 | /* value is populated for regular form fields only. |
---|
28 | For file uploads, it points to an empty string, and file |
---|
29 | upload data should be read from the file tfileName. */ |
---|
30 | char *value; |
---|
31 | /* When fileName is not an empty string, tfileName is not null, |
---|
32 | and 'value' points to an empty string. */ |
---|
33 | /* Valid for both files and regular fields; does not include |
---|
34 | terminating null of regular fields. */ |
---|
35 | int valueLength; |
---|
36 | char *fileName; |
---|
37 | char *contentType; |
---|
38 | /* Temporary file name for working storage of file uploads. */ |
---|
39 | char *tfileName; |
---|
40 | struct cgiFormEntryStruct *next; |
---|
41 | } cgiFormEntry; |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | struct cgi_env { |
---|
47 | int cgiRestored;// = 0; |
---|
48 | char *cgiServerSoftware; |
---|
49 | char *cgiServerName; |
---|
50 | char *cgiGatewayInterface; |
---|
51 | char *cgiServerProtocol; |
---|
52 | char *cgiServerPort; |
---|
53 | char *cgiRequestMethod; |
---|
54 | char *cgiPathInfo; |
---|
55 | char *cgiPathTranslated; |
---|
56 | char *cgiScriptName; |
---|
57 | char *cgiQueryString; |
---|
58 | char *cgiRemoteHost; |
---|
59 | char *cgiRemoteAddr; |
---|
60 | char *cgiAuthType; |
---|
61 | char *cgiRemoteUser; |
---|
62 | char *cgiRemoteIdent; |
---|
63 | char cgiContentTypeData[1024]; |
---|
64 | char *cgiContentType;// = cgiContentTypeData; |
---|
65 | |
---|
66 | char * cgiMultipartBoundary; |
---|
67 | cgiFormEntry *cgiFormEntryFirst; |
---|
68 | int cgiTreatUrlEncoding; |
---|
69 | |
---|
70 | char *cgiAccept; |
---|
71 | char *cgiUserAgent; |
---|
72 | char *cgiReferrer; |
---|
73 | |
---|
74 | /* Cookies as sent to the server. You can also get them |
---|
75 | individually, or as a string array; see the documentation. */ |
---|
76 | char *cgiCookie; |
---|
77 | char *cgiSid; |
---|
78 | |
---|
79 | /* A macro providing the same incorrect spelling that is |
---|
80 | found in the HTTP/CGI specifications */ |
---|
81 | #define cgiReferer cgiReferrer |
---|
82 | |
---|
83 | /* The number of bytes of data received. |
---|
84 | Note that if the submission is a form submission |
---|
85 | the library will read and parse all the information |
---|
86 | directly from cgiIn; the programmer need not do so. */ |
---|
87 | |
---|
88 | int cgiContentLength; |
---|
89 | |
---|
90 | /* Pointer to CGI output. The cgiHeader functions should be used |
---|
91 | first to output the mime headers; the output HTML |
---|
92 | page, GIF image or other web document should then be written |
---|
93 | to cgiOut by the programmer. In the standard CGIC library, |
---|
94 | cgiOut is always equivalent to stdout. */ |
---|
95 | |
---|
96 | char *cgiFindTarget; |
---|
97 | cgiFormEntry *cgiFindPos; |
---|
98 | |
---|
99 | }; |
---|
100 | /* Possible return codes from the cgiForm family of functions (see below). */ |
---|
101 | |
---|
102 | typedef enum { |
---|
103 | cgiFormSuccess, |
---|
104 | cgiFormTruncated, |
---|
105 | cgiFormBadType, |
---|
106 | cgiFormEmpty, |
---|
107 | cgiFormNotFound, |
---|
108 | cgiFormConstrained, |
---|
109 | cgiFormNoSuchChoice, |
---|
110 | cgiFormMemory, |
---|
111 | cgiFormNoFileName, |
---|
112 | cgiFormNoContentType, |
---|
113 | cgiFormNotAFile, |
---|
114 | cgiFormOpenFailed, |
---|
115 | cgiFormIO, |
---|
116 | cgiFormEOF |
---|
117 | } cgiFormResultType; |
---|
118 | |
---|
119 | /* These functions are used to retrieve form data. See |
---|
120 | cgic.html for documentation. */ |
---|
121 | |
---|
122 | extern |
---|
123 | #ifdef __cplusplus |
---|
124 | "C" |
---|
125 | #endif |
---|
126 | cgiFormResultType cgiFormString( |
---|
127 | char *name, char *result, int max,struct cgi_env **); |
---|
128 | |
---|
129 | extern |
---|
130 | #ifdef __cplusplus |
---|
131 | "C" |
---|
132 | #endif |
---|
133 | cgiFormResultType cgiFormStringNoNewlines( |
---|
134 | char *name, char *result, int max,struct cgi_env ** ce); |
---|
135 | |
---|
136 | |
---|
137 | extern |
---|
138 | #ifdef __cplusplus |
---|
139 | "C" |
---|
140 | #endif |
---|
141 | cgiFormResultType cgiFormStringSpaceNeeded( |
---|
142 | char *name, int *length,struct cgi_env ** ce); |
---|
143 | |
---|
144 | |
---|
145 | extern |
---|
146 | #ifdef __cplusplus |
---|
147 | "C" |
---|
148 | #endif |
---|
149 | cgiFormResultType cgiFormStringMultiple( |
---|
150 | char *name, char ***ptrToStringArray,struct cgi_env ** ce); |
---|
151 | |
---|
152 | extern |
---|
153 | #ifdef __cplusplus |
---|
154 | "C" |
---|
155 | #endif |
---|
156 | void cgiStringArrayFree(char **stringArray); |
---|
157 | |
---|
158 | extern |
---|
159 | #ifdef __cplusplus |
---|
160 | "C" |
---|
161 | #endif |
---|
162 | cgiFormResultType cgiFormInteger( |
---|
163 | char *name, int *result, int defaultV,struct cgi_env ** ce); |
---|
164 | |
---|
165 | extern |
---|
166 | #ifdef __cplusplus |
---|
167 | "C" |
---|
168 | #endif |
---|
169 | cgiFormResultType cgiFormIntegerBounded( |
---|
170 | char *name, int *result, int min, int max, int defaultV,struct cgi_env **ce); |
---|
171 | |
---|
172 | extern |
---|
173 | #ifdef __cplusplus |
---|
174 | "C" |
---|
175 | #endif |
---|
176 | cgiFormResultType cgiFormDouble( |
---|
177 | char *name, double *result, double defaultV,struct cgi_env **); |
---|
178 | |
---|
179 | extern |
---|
180 | #ifdef __cplusplus |
---|
181 | "C" |
---|
182 | #endif |
---|
183 | cgiFormResultType cgiFormDoubleBounded( |
---|
184 | char *name, double *result, double min, double max, double defaultV, struct cgi_env ** ce); |
---|
185 | |
---|
186 | extern |
---|
187 | #ifdef __cplusplus |
---|
188 | "C" |
---|
189 | #endif |
---|
190 | cgiFormResultType cgiFormSelectSingle( |
---|
191 | char *name, char **choicesText, int choicesTotal, |
---|
192 | int *result, int defaultV,struct cgi_env **ce); |
---|
193 | |
---|
194 | |
---|
195 | extern |
---|
196 | #ifdef __cplusplus |
---|
197 | "C" |
---|
198 | #endif |
---|
199 | cgiFormResultType cgiFormSelectMultiple( |
---|
200 | char *name, char **choicesText, int choicesTotal, |
---|
201 | int *result, int *invalid,struct cgi_env **); |
---|
202 | |
---|
203 | /* Just an alias; users have asked for this */ |
---|
204 | #define cgiFormSubmitClicked cgiFormCheckboxSingle |
---|
205 | |
---|
206 | extern |
---|
207 | #ifdef __cplusplus |
---|
208 | "C" |
---|
209 | #endif |
---|
210 | cgiFormResultType cgiFormCheckboxSingle( |
---|
211 | char *name,struct cgi_env ** ce); |
---|
212 | |
---|
213 | extern |
---|
214 | #ifdef __cplusplus |
---|
215 | "C" |
---|
216 | #endif |
---|
217 | cgiFormResultType cgiFormCheckboxMultiple( |
---|
218 | char *name, char **valuesText, int valuesTotal, |
---|
219 | int *result, int *invalid,struct cgi_env ** ce); |
---|
220 | |
---|
221 | extern |
---|
222 | #ifdef __cplusplus |
---|
223 | "C" |
---|
224 | #endif |
---|
225 | cgiFormResultType cgiFormRadio( |
---|
226 | char *name, char **valuesText, int valuesTotal, |
---|
227 | int *result, int defaultV,struct cgi_env **ce); |
---|
228 | |
---|
229 | /* The paths returned by this function are the original names of files |
---|
230 | as reported by the uploading web browser and shoult NOT be |
---|
231 | blindly assumed to be "safe" names for server-side use! */ |
---|
232 | extern |
---|
233 | #ifdef __cplusplus |
---|
234 | "C" |
---|
235 | #endif |
---|
236 | cgiFormResultType cgiFormFileName( |
---|
237 | char *name, char *result, int max,struct cgi_env **); |
---|
238 | |
---|
239 | /* The content type of the uploaded file, as reported by the browser. |
---|
240 | It should NOT be assumed that browsers will never falsify |
---|
241 | such information. */ |
---|
242 | extern |
---|
243 | #ifdef __cplusplus |
---|
244 | "C" |
---|
245 | #endif |
---|
246 | cgiFormResultType cgiFormFileContentType( |
---|
247 | char *name, char *result, int max,struct cgi_env ** ce); |
---|
248 | |
---|
249 | extern |
---|
250 | #ifdef __cplusplus |
---|
251 | "C" |
---|
252 | #endif |
---|
253 | cgiFormResultType cgiFormFileSize( |
---|
254 | char *name, int *sizeP,struct cgi_env ** ce); |
---|
255 | |
---|
256 | typedef struct cgiFileStruct *cgiFilePtr; |
---|
257 | |
---|
258 | extern |
---|
259 | #ifdef __cplusplus |
---|
260 | "C" |
---|
261 | #endif |
---|
262 | cgiFormResultType cgiFormFileOpen( |
---|
263 | char *name, cgiFilePtr *cfpp,struct cgi_env ** ce); |
---|
264 | |
---|
265 | extern |
---|
266 | #ifdef __cplusplus |
---|
267 | "C" |
---|
268 | #endif |
---|
269 | cgiFormResultType cgiFormFileRead( |
---|
270 | cgiFilePtr cfp, char *buffer, int bufferSize, int *gotP); |
---|
271 | |
---|
272 | extern |
---|
273 | #ifdef __cplusplus |
---|
274 | "C" |
---|
275 | #endif |
---|
276 | cgiFormResultType cgiFormFileClose( |
---|
277 | cgiFilePtr cfp); |
---|
278 | |
---|
279 | extern |
---|
280 | #ifdef __cplusplus |
---|
281 | "C" |
---|
282 | #endif |
---|
283 | cgiFormResultType cgiCookieString( |
---|
284 | char *name, char *result, int max,char * cgiCookie); |
---|
285 | |
---|
286 | extern |
---|
287 | #ifdef __cplusplus |
---|
288 | "C" |
---|
289 | #endif |
---|
290 | cgiFormResultType cgiCookieInteger( |
---|
291 | char *name, int *result, int defaultV,char * cgiCookie); |
---|
292 | |
---|
293 | cgiFormResultType cgiCookies( |
---|
294 | char ***ptrToStringArray,char* cgiCookie); |
---|
295 | |
---|
296 | /* path can be null or empty in which case a path of / (entire site) is set. |
---|
297 | domain can be a single web site; if it is an entire domain, such as |
---|
298 | 'boutell.com', it should begin with a dot: '.boutell.com' */ |
---|
299 | extern |
---|
300 | #ifdef __cplusplus |
---|
301 | "C" |
---|
302 | #endif |
---|
303 | void cgiHeaderCookieSetString(char *name, char *value, |
---|
304 | int secondsToLive, char *path, char *domain,FCGX_Stream *out); |
---|
305 | extern |
---|
306 | #ifdef __cplusplus |
---|
307 | "C" |
---|
308 | #endif |
---|
309 | void cgiHeaderCookieSetInteger(char *name, int value, |
---|
310 | int secondsToLive, char *path, char *domain,FCGX_Stream *out); |
---|
311 | extern |
---|
312 | #ifdef __cplusplus |
---|
313 | "C" |
---|
314 | #endif |
---|
315 | void cgiHeaderLocation(char *redirectUrl,FCGX_Stream * out); |
---|
316 | extern |
---|
317 | #ifdef __cplusplus |
---|
318 | "C" |
---|
319 | #endif |
---|
320 | void cgiHeaderStatus(int status, char *statusMessage,FCGX_Stream * out); |
---|
321 | extern |
---|
322 | #ifdef __cplusplus |
---|
323 | "C" |
---|
324 | #endif |
---|
325 | void cgiHeaderContentType(char *mimeType,FCGX_Stream * out); |
---|
326 | |
---|
327 | typedef enum { |
---|
328 | cgiEnvironmentIO, |
---|
329 | cgiEnvironmentMemory, |
---|
330 | cgiEnvironmentSuccess, |
---|
331 | cgiEnvironmentWrongVersion |
---|
332 | } cgiEnvironmentResultType; |
---|
333 | |
---|
334 | extern |
---|
335 | #ifdef __cplusplus |
---|
336 | "C" |
---|
337 | #endif |
---|
338 | cgiEnvironmentResultType cgiWriteEnvironment(char *filename); |
---|
339 | extern cgiEnvironmentResultType cgiReadEnvironment(char *filename); |
---|
340 | |
---|
341 | extern |
---|
342 | #ifdef __cplusplus |
---|
343 | "C" |
---|
344 | #endif |
---|
345 | int cgiMain(); |
---|
346 | extern |
---|
347 | |
---|
348 | |
---|
349 | #ifdef __cplusplus |
---|
350 | "C" |
---|
351 | #endif |
---|
352 | cgiFormResultType cgiFormEntries( |
---|
353 | char ***ptrToStringArray,struct cgi_env **ce); |
---|
354 | |
---|
355 | /* Output string with the <, &, and > characters HTML-escaped. |
---|
356 | 's' is null-terminated. Returns cgiFormIO in the event |
---|
357 | of error, cgiFormSuccess otherwise. */ |
---|
358 | cgiFormResultType cgiHtmlEscape(char *s,FCGX_Stream *out); |
---|
359 | |
---|
360 | /* Output data with the <, &, and > characters HTML-escaped. |
---|
361 | 'data' is not null-terminated; 'len' is the number of |
---|
362 | bytes in 'data'. Returns cgiFormIO in the event |
---|
363 | of error, cgiFormSuccess otherwise. */ |
---|
364 | cgiFormResultType cgiHtmlEscapeData(char *data, int len,FCGX_Stream *out); |
---|
365 | |
---|
366 | /* Output string with the " character HTML-escaped, and no |
---|
367 | other characters escaped. This is useful when outputting |
---|
368 | the contents of a tag attribute such as 'href' or 'src'. |
---|
369 | 's' is null-terminated. Returns cgiFormIO in the event |
---|
370 | of error, cgiFormSuccess otherwise. */ |
---|
371 | cgiFormResultType cgiValueEscape(char *s,FCGX_Stream *out); |
---|
372 | |
---|
373 | /* Output data with the " character HTML-escaped, and no |
---|
374 | other characters escaped. This is useful when outputting |
---|
375 | the contents of a tag attribute such as 'href' or 'src'. |
---|
376 | 'data' is not null-terminated; 'len' is the number of |
---|
377 | bytes in 'data'. Returns cgiFormIO in the event |
---|
378 | of error, cgiFormSuccess otherwise. */ |
---|
379 | cgiFormResultType cgiValueEscapeData(char *data, int len,FCGX_Stream *out); |
---|
380 | |
---|
381 | int cgiMain_init(int argc, char *argv[],struct cgi_env ** c,FCGX_Request *); |
---|
382 | |
---|
383 | void cgiFreeResources(struct cgi_env ** c); |
---|
384 | |
---|
385 | #endif /* CGI_C */ |
---|
386 | |
---|