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