[1] | 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 | |
---|
[217] | 127 | extern |
---|
| 128 | #ifdef __cplusplus |
---|
| 129 | "C" |
---|
| 130 | #endif |
---|
| 131 | char *cgiSid; |
---|
| 132 | |
---|
[1] | 133 | /* A macro providing the same incorrect spelling that is |
---|
| 134 | found in the HTTP/CGI specifications */ |
---|
| 135 | #define cgiReferer cgiReferrer |
---|
| 136 | |
---|
| 137 | /* The number of bytes of data received. |
---|
| 138 | Note that if the submission is a form submission |
---|
| 139 | the library will read and parse all the information |
---|
| 140 | directly from cgiIn; the programmer need not do so. */ |
---|
| 141 | |
---|
| 142 | extern |
---|
| 143 | #ifdef __cplusplus |
---|
| 144 | "C" |
---|
| 145 | #endif |
---|
| 146 | int cgiContentLength; |
---|
| 147 | |
---|
| 148 | /* Pointer to CGI output. The cgiHeader functions should be used |
---|
| 149 | first to output the mime headers; the output HTML |
---|
| 150 | page, GIF image or other web document should then be written |
---|
| 151 | to cgiOut by the programmer. In the standard CGIC library, |
---|
| 152 | cgiOut is always equivalent to stdout. */ |
---|
| 153 | |
---|
| 154 | extern |
---|
| 155 | #ifdef __cplusplus |
---|
| 156 | "C" |
---|
| 157 | #endif |
---|
| 158 | FILE *cgiOut; |
---|
| 159 | |
---|
| 160 | /* Pointer to CGI input. The programmer does not read from this. |
---|
| 161 | We have continued to export it for backwards compatibility |
---|
| 162 | so that cgic 1.x applications link properly. */ |
---|
| 163 | |
---|
| 164 | extern |
---|
| 165 | #ifdef __cplusplus |
---|
| 166 | "C" |
---|
| 167 | #endif |
---|
| 168 | FILE *cgiIn; |
---|
| 169 | |
---|
| 170 | /* Possible return codes from the cgiForm family of functions (see below). */ |
---|
| 171 | |
---|
| 172 | typedef enum { |
---|
| 173 | cgiFormSuccess, |
---|
| 174 | cgiFormTruncated, |
---|
| 175 | cgiFormBadType, |
---|
| 176 | cgiFormEmpty, |
---|
| 177 | cgiFormNotFound, |
---|
| 178 | cgiFormConstrained, |
---|
| 179 | cgiFormNoSuchChoice, |
---|
| 180 | cgiFormMemory, |
---|
| 181 | cgiFormNoFileName, |
---|
| 182 | cgiFormNoContentType, |
---|
| 183 | cgiFormNotAFile, |
---|
| 184 | cgiFormOpenFailed, |
---|
| 185 | cgiFormIO, |
---|
| 186 | cgiFormEOF |
---|
| 187 | } cgiFormResultType; |
---|
| 188 | |
---|
| 189 | /* These functions are used to retrieve form data. See |
---|
| 190 | cgic.html for documentation. */ |
---|
| 191 | |
---|
| 192 | extern |
---|
| 193 | #ifdef __cplusplus |
---|
| 194 | "C" |
---|
| 195 | #endif |
---|
| 196 | cgiFormResultType cgiFormString( |
---|
| 197 | char *name, char *result, int max); |
---|
| 198 | |
---|
| 199 | extern |
---|
| 200 | #ifdef __cplusplus |
---|
| 201 | "C" |
---|
| 202 | #endif |
---|
| 203 | cgiFormResultType cgiFormStringNoNewlines( |
---|
| 204 | char *name, char *result, int max); |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | extern |
---|
| 208 | #ifdef __cplusplus |
---|
| 209 | "C" |
---|
| 210 | #endif |
---|
| 211 | cgiFormResultType cgiFormStringSpaceNeeded( |
---|
| 212 | char *name, int *length); |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | extern |
---|
| 216 | #ifdef __cplusplus |
---|
| 217 | "C" |
---|
| 218 | #endif |
---|
| 219 | cgiFormResultType cgiFormStringMultiple( |
---|
| 220 | char *name, char ***ptrToStringArray); |
---|
| 221 | |
---|
| 222 | extern |
---|
| 223 | #ifdef __cplusplus |
---|
| 224 | "C" |
---|
| 225 | #endif |
---|
| 226 | void cgiStringArrayFree(char **stringArray); |
---|
| 227 | |
---|
| 228 | extern |
---|
| 229 | #ifdef __cplusplus |
---|
| 230 | "C" |
---|
| 231 | #endif |
---|
| 232 | cgiFormResultType cgiFormInteger( |
---|
| 233 | char *name, int *result, int defaultV); |
---|
| 234 | |
---|
| 235 | extern |
---|
| 236 | #ifdef __cplusplus |
---|
| 237 | "C" |
---|
| 238 | #endif |
---|
| 239 | cgiFormResultType cgiFormIntegerBounded( |
---|
| 240 | char *name, int *result, int min, int max, int defaultV); |
---|
| 241 | |
---|
| 242 | extern |
---|
| 243 | #ifdef __cplusplus |
---|
| 244 | "C" |
---|
| 245 | #endif |
---|
| 246 | cgiFormResultType cgiFormDouble( |
---|
| 247 | char *name, double *result, double defaultV); |
---|
| 248 | |
---|
| 249 | extern |
---|
| 250 | #ifdef __cplusplus |
---|
| 251 | "C" |
---|
| 252 | #endif |
---|
| 253 | cgiFormResultType cgiFormDoubleBounded( |
---|
| 254 | char *name, double *result, double min, double max, double defaultV); |
---|
| 255 | |
---|
| 256 | extern |
---|
| 257 | #ifdef __cplusplus |
---|
| 258 | "C" |
---|
| 259 | #endif |
---|
| 260 | cgiFormResultType cgiFormSelectSingle( |
---|
| 261 | char *name, char **choicesText, int choicesTotal, |
---|
| 262 | int *result, int defaultV); |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | extern |
---|
| 266 | #ifdef __cplusplus |
---|
| 267 | "C" |
---|
| 268 | #endif |
---|
| 269 | cgiFormResultType cgiFormSelectMultiple( |
---|
| 270 | char *name, char **choicesText, int choicesTotal, |
---|
| 271 | int *result, int *invalid); |
---|
| 272 | |
---|
| 273 | /* Just an alias; users have asked for this */ |
---|
| 274 | #define cgiFormSubmitClicked cgiFormCheckboxSingle |
---|
| 275 | |
---|
| 276 | extern |
---|
| 277 | #ifdef __cplusplus |
---|
| 278 | "C" |
---|
| 279 | #endif |
---|
| 280 | cgiFormResultType cgiFormCheckboxSingle( |
---|
| 281 | char *name); |
---|
| 282 | |
---|
| 283 | extern |
---|
| 284 | #ifdef __cplusplus |
---|
| 285 | "C" |
---|
| 286 | #endif |
---|
| 287 | cgiFormResultType cgiFormCheckboxMultiple( |
---|
| 288 | char *name, char **valuesText, int valuesTotal, |
---|
| 289 | int *result, int *invalid); |
---|
| 290 | |
---|
| 291 | extern |
---|
| 292 | #ifdef __cplusplus |
---|
| 293 | "C" |
---|
| 294 | #endif |
---|
| 295 | cgiFormResultType cgiFormRadio( |
---|
| 296 | char *name, char **valuesText, int valuesTotal, |
---|
| 297 | int *result, int defaultV); |
---|
| 298 | |
---|
| 299 | /* The paths returned by this function are the original names of files |
---|
| 300 | as reported by the uploading web browser and shoult NOT be |
---|
| 301 | blindly assumed to be "safe" names for server-side use! */ |
---|
| 302 | extern |
---|
| 303 | #ifdef __cplusplus |
---|
| 304 | "C" |
---|
| 305 | #endif |
---|
| 306 | cgiFormResultType cgiFormFileName( |
---|
| 307 | char *name, char *result, int max); |
---|
| 308 | |
---|
| 309 | /* The content type of the uploaded file, as reported by the browser. |
---|
| 310 | It should NOT be assumed that browsers will never falsify |
---|
| 311 | such information. */ |
---|
| 312 | extern |
---|
| 313 | #ifdef __cplusplus |
---|
| 314 | "C" |
---|
| 315 | #endif |
---|
| 316 | cgiFormResultType cgiFormFileContentType( |
---|
| 317 | char *name, char *result, int max); |
---|
| 318 | |
---|
| 319 | extern |
---|
| 320 | #ifdef __cplusplus |
---|
| 321 | "C" |
---|
| 322 | #endif |
---|
| 323 | cgiFormResultType cgiFormFileSize( |
---|
| 324 | char *name, int *sizeP); |
---|
| 325 | |
---|
| 326 | typedef struct cgiFileStruct *cgiFilePtr; |
---|
| 327 | |
---|
| 328 | extern |
---|
| 329 | #ifdef __cplusplus |
---|
| 330 | "C" |
---|
| 331 | #endif |
---|
| 332 | cgiFormResultType cgiFormFileOpen( |
---|
| 333 | char *name, cgiFilePtr *cfpp); |
---|
| 334 | |
---|
| 335 | extern |
---|
| 336 | #ifdef __cplusplus |
---|
| 337 | "C" |
---|
| 338 | #endif |
---|
| 339 | cgiFormResultType cgiFormFileRead( |
---|
| 340 | cgiFilePtr cfp, char *buffer, int bufferSize, int *gotP); |
---|
| 341 | |
---|
| 342 | extern |
---|
| 343 | #ifdef __cplusplus |
---|
| 344 | "C" |
---|
| 345 | #endif |
---|
| 346 | cgiFormResultType cgiFormFileClose( |
---|
| 347 | cgiFilePtr cfp); |
---|
| 348 | |
---|
| 349 | extern |
---|
| 350 | #ifdef __cplusplus |
---|
| 351 | "C" |
---|
| 352 | #endif |
---|
| 353 | cgiFormResultType cgiCookieString( |
---|
| 354 | char *name, char *result, int max); |
---|
| 355 | |
---|
| 356 | extern |
---|
| 357 | #ifdef __cplusplus |
---|
| 358 | "C" |
---|
| 359 | #endif |
---|
| 360 | cgiFormResultType cgiCookieInteger( |
---|
| 361 | char *name, int *result, int defaultV); |
---|
| 362 | |
---|
| 363 | cgiFormResultType cgiCookies( |
---|
| 364 | char ***ptrToStringArray); |
---|
| 365 | |
---|
| 366 | /* path can be null or empty in which case a path of / (entire site) is set. |
---|
| 367 | domain can be a single web site; if it is an entire domain, such as |
---|
| 368 | 'boutell.com', it should begin with a dot: '.boutell.com' */ |
---|
| 369 | extern |
---|
| 370 | #ifdef __cplusplus |
---|
| 371 | "C" |
---|
| 372 | #endif |
---|
| 373 | void cgiHeaderCookieSetString(char *name, char *value, |
---|
| 374 | int secondsToLive, char *path, char *domain); |
---|
| 375 | extern |
---|
| 376 | #ifdef __cplusplus |
---|
| 377 | "C" |
---|
| 378 | #endif |
---|
| 379 | void cgiHeaderCookieSetInteger(char *name, int value, |
---|
| 380 | int secondsToLive, char *path, char *domain); |
---|
| 381 | extern |
---|
| 382 | #ifdef __cplusplus |
---|
| 383 | "C" |
---|
| 384 | #endif |
---|
| 385 | void cgiHeaderLocation(char *redirectUrl); |
---|
| 386 | extern |
---|
| 387 | #ifdef __cplusplus |
---|
| 388 | "C" |
---|
| 389 | #endif |
---|
| 390 | void cgiHeaderStatus(int status, char *statusMessage); |
---|
| 391 | extern |
---|
| 392 | #ifdef __cplusplus |
---|
| 393 | "C" |
---|
| 394 | #endif |
---|
| 395 | void cgiHeaderContentType(char *mimeType); |
---|
| 396 | |
---|
| 397 | typedef enum { |
---|
| 398 | cgiEnvironmentIO, |
---|
| 399 | cgiEnvironmentMemory, |
---|
| 400 | cgiEnvironmentSuccess, |
---|
| 401 | cgiEnvironmentWrongVersion |
---|
| 402 | } cgiEnvironmentResultType; |
---|
| 403 | |
---|
| 404 | extern |
---|
| 405 | #ifdef __cplusplus |
---|
| 406 | "C" |
---|
| 407 | #endif |
---|
| 408 | cgiEnvironmentResultType cgiWriteEnvironment(char *filename); |
---|
| 409 | extern cgiEnvironmentResultType cgiReadEnvironment(char *filename); |
---|
| 410 | |
---|
| 411 | extern |
---|
| 412 | #ifdef __cplusplus |
---|
| 413 | "C" |
---|
| 414 | #endif |
---|
| 415 | int cgiMain(); |
---|
| 416 | extern |
---|
| 417 | #ifdef __cplusplus |
---|
| 418 | "C" |
---|
| 419 | #endif |
---|
| 420 | int cgiMain_init(); |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | extern |
---|
| 424 | #ifdef __cplusplus |
---|
| 425 | "C" |
---|
| 426 | #endif |
---|
| 427 | cgiFormResultType cgiFormEntries( |
---|
| 428 | char ***ptrToStringArray); |
---|
| 429 | |
---|
| 430 | /* Output string with the <, &, and > characters HTML-escaped. |
---|
| 431 | 's' is null-terminated. Returns cgiFormIO in the event |
---|
| 432 | of error, cgiFormSuccess otherwise. */ |
---|
| 433 | cgiFormResultType cgiHtmlEscape(char *s); |
---|
| 434 | |
---|
| 435 | /* Output data with the <, &, and > characters HTML-escaped. |
---|
| 436 | 'data' is not null-terminated; 'len' is the number of |
---|
| 437 | bytes in 'data'. Returns cgiFormIO in the event |
---|
| 438 | of error, cgiFormSuccess otherwise. */ |
---|
| 439 | cgiFormResultType cgiHtmlEscapeData(char *data, int len); |
---|
| 440 | |
---|
| 441 | /* Output string with the " character HTML-escaped, and no |
---|
| 442 | other characters escaped. This is useful when outputting |
---|
| 443 | the contents of a tag attribute such as 'href' or 'src'. |
---|
| 444 | 's' is null-terminated. Returns cgiFormIO in the event |
---|
| 445 | of error, cgiFormSuccess otherwise. */ |
---|
| 446 | cgiFormResultType cgiValueEscape(char *s); |
---|
| 447 | |
---|
| 448 | /* Output data with the " character HTML-escaped, and no |
---|
| 449 | other characters escaped. This is useful when outputting |
---|
| 450 | the contents of a tag attribute such as 'href' or 'src'. |
---|
| 451 | 'data' is not null-terminated; 'len' is the number of |
---|
| 452 | bytes in 'data'. Returns cgiFormIO in the event |
---|
| 453 | of error, cgiFormSuccess otherwise. */ |
---|
| 454 | cgiFormResultType cgiValueEscapeData(char *data, int len); |
---|
| 455 | |
---|
| 456 | #endif /* CGI_C */ |
---|
| 457 | |
---|