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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
|
/* cgi-lib.c - C routines that make writing CGI scripts in C a breeze
Eugene Kim, <eekim@eekim.com>
$Id: cgi-lib.c,v 1.1 2001-10-23 19:08:21 smm Exp $
Motivation: Perl is a much more convenient language to use when
writing CGI scripts. Unfortunately, it is also a larger drain on
the system. Hopefully, these routines will make writing CGI
scripts just as easy in C.
Copyright (C) 1996,1997 Eugene Eric Kim
All Rights Reserved
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef WINDOWS
#include <io.h>
#endif
#include "cgi-lib.h"
#include "html-lib.h"
#include "string-lib.h"
/* symbol table for CGI encoding */
#define _NAME 0
#define _VALUE 1
short accept_image()
{
char *httpaccept = getenv("HTTP_ACCEPT");
if (strstr(httpaccept,"image") == NULL)
return 0;
else
return 1;
}
/* x2c() and unescape_url() stolen from NCSA code */
char x2c(char *what)
{
register char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
void unescape_url(char *url)
{
register int x,y;
for (x=0,y=0; url[y]; ++x,++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
char *get_DEBUG()
{
int bufsize = 1024;
char *buffer = (char *)malloc(sizeof(char) * bufsize + 1);
int i = 0;
char ch;
fprintf(stderr,"\n--- cgihtml Interactive Mode ---\n");
fprintf(stderr,"Enter CGI input string. Remember to encode appropriate ");
fprintf(stderr,"characters.\nPress ENTER when done:\n\n");
while ( (i<=bufsize) && ((ch = getc(stdin)) != '\n') ) {
buffer[i] = ch;
i++;
if (i>bufsize) {
bufsize *= 2;
buffer = (char *)realloc(buffer,bufsize);
}
}
buffer[i] = '\0';
fprintf(stderr,"\n Input string: %s\nString length: %d\n",buffer,i);
fprintf(stderr,"--- end cgihtml Interactive Mode ---\n\n");
return buffer;
}
char *get_POST()
{
unsigned int content_length;
char *buffer;
if (CONTENT_LENGTH != NULL) {
content_length = atoi(CONTENT_LENGTH);
buffer = (char *)malloc(sizeof(char) * content_length + 1);
if (fread(buffer,sizeof(char),content_length,stdin) != content_length) {
/* consistency error. */
fprintf(stderr,"caught by cgihtml: input length < CONTENT_LENGTH\n");
exit(1);
}
buffer[content_length] = '\0';
}
return buffer;
}
char *get_GET()
{
char *buffer;
if (QUERY_STRING == NULL)
return NULL;
buffer = newstr(QUERY_STRING);
return buffer;
}
int parse_CGI_encoded(llist *entries, char *buffer)
{
int i, j, num, token;
int len = strlen(buffer);
char *lexeme = (char *)malloc(sizeof(char) * len + 1);
entrytype entry;
node *window;
list_create(entries);
window = entries->head;
entry.name = NULL;
entry.value = NULL;
i = 0;
num = 0;
token = _NAME;
while (i < len) {
j = 0;
while ( (buffer[i] != '=') && (buffer[i] != '&') && (i < len) ) {
lexeme[j] = (buffer[i] == '+') ? ' ' : buffer[i];
i++;
j++;
}
lexeme[j] = '\0';
if (token == _NAME) {
entry.name = newstr(lexeme);
unescape_url(entry.name);
if ( (buffer[i] != '=') || (i == len - 1) ) {
entry.value = (char *)malloc(sizeof(char));
strcpy(entry.value,"");
window = list_insafter(entries, window, entry);
free(entry.name);
entry.name = NULL;
free(entry.value);
entry.value = NULL;
if (i == len - 1) /* null value at end of expression */
num++;
else { /* error in expression */
free(lexeme);
return -1;
}
}
else
token = _VALUE;
}
else {
entry.value = newstr(lexeme);
unescape_url(entry.value);
window = list_insafter(entries, window, entry);
free(entry.name);
entry.name = NULL;
free(entry.value);
entry.value = NULL;
token = _NAME;
num++;
}
i++;
j = 0;
}
free(lexeme);
if (entry.name != NULL)
free(entry.name);
if (entry.value != NULL)
free(entry.value);
return num;
}
/* stolen from k&r and seriously modified to do what I want */
int getline(char s[], int lim)
{
int c, i=0, num;
for (i=0; (i<lim) && ((c=getchar())!=EOF) && (c!='\n'); i++) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
}
if ((i==0) && (c!='\n'))
num = 0;
else if (i == lim)
num = i;
else
num = i+1;
return num;
}
int parse_form_encoded(llist* entries)
{
long content_length;
entrytype entry;
node* window;
FILE *uploadfile;
char *uploadfname, *tempstr, *boundary;
char *buffer = (char *)malloc(sizeof(char) * BUFSIZ + 1);
char *prevbuf = (char *)malloc(sizeof(char) + BUFSIZ + 1);
short isfile,done,start;
int i,j;
int bytesread,prevbytesread;
int buffersize;
int numentries = 0;
#ifdef WINDOWS
setmode(fileno(stdin), O_BINARY); /* define stdin as binary */
_fmode = BINARY; /* default all file I/O as binary */
#endif
if (CONTENT_LENGTH != NULL)
content_length = atol(CONTENT_LENGTH);
else
return 0;
/* get boundary */
tempstr = newstr(CONTENT_TYPE);
boundary = strstr(tempstr,"boundary=");
boundary += (sizeof(char) * 9);
/* create list */
list_create(entries);
window = entries->head;
/* ignore first boundary; this isn't so robust; improve it later */
getline(buffer,BUFSIZ);
/* now start parsing */
while ((bytesread=getline(buffer,BUFSIZ)) != 0) {
start = 1;
/* this assumes that buffer contains no binary characters.
if the buffer contains the first valid header, then this
is a safe assumption. however, it should be improved for
robustness sake. */
buffer[bytesread] = '\0';
tempstr = newstr(buffer);
tempstr += (sizeof(char) * 38); /* 38 is header up to name */
entry.name = tempstr;
entry.value = (char *)malloc(sizeof(char) * BUFSIZ + 1);
buffersize = BUFSIZ;
strcpy(entry.value,"");
while (*tempstr != '"')
tempstr++;
*tempstr = '\0';
if (strstr(buffer,"filename=\"") != NULL) {
isfile = 1;
tempstr = newstr(buffer);
tempstr = strstr(tempstr,"filename=\"");
tempstr += (sizeof(char) * 10);
if (strlen(tempstr) >= BUFSIZ)
entry.value = (char *) realloc(entry.value, sizeof(char) *
strlen(tempstr)+1);
entry.value = tempstr;
while (*tempstr != '"')
tempstr++;
*tempstr = '\0';
/* Netscape's Windows browsers handle paths differently from its
UNIX and Mac browsers. It delivers a full path for the uploaded
file (which it shouldn't do), and it uses backslashes rather than
forward slashes. No need to worry about Internet Explorer, since
it doesn't support HTTP File Upload at all. */
if (strstr(lower_case(HTTP_USER_AGENT),"win") != 0) {
tempstr = strrchr(entry.value, '\\');
if (tempstr) {
tempstr++;
entry.value = tempstr;
}
}
window = list_insafter(entries,window,entry);
numentries++;
uploadfname = (char *)malloc(strlen(UPLOADDIR)+strlen(entry.value)+2);
#ifdef WINDOWS
sprintf(uploadfname,"%s\\%s",UPLOADDIR,entry.value);
#else
sprintf(uploadfname,"%s/%s",UPLOADDIR,entry.value);
#endif
if ( (uploadfile = fopen(uploadfname,"w")) == NULL) {
/* null filename; for now, just don't save info. later, save
to default file */
isfile = 0;
}
}
else
isfile = 0;
/* ignore rest of headers and first blank line */
while (getline(buffer, BUFSIZ) > 1) {
/* DOS style blank line? */
if ((buffer[0] == '\r') && (buffer[1] == '\n'))
break;
}
done = 0;
j = 0;
while (!done) {
bytesread = getline(buffer,BUFSIZ);
buffer[bytesread] = '\0';
if (bytesread && strstr(buffer,boundary) == NULL) {
if (start) {
i = 0;
while (i < bytesread) {
prevbuf[i] = buffer[i];
i++;
}
prevbytesread = bytesread;
start = 0;
}
else {
/* flush buffer */
i = 0;
while (i < prevbytesread) {
if (isfile)
fputc(prevbuf[i],uploadfile);
else {
if (j > buffersize) {
buffersize += BUFSIZ;
entry.value = (char *) realloc(entry.value, sizeof(char) *
buffersize+1);
}
entry.value[j] = prevbuf[i];
j++;
}
i++;
}
/* buffer new input */
i = 0;
while (i < bytesread) {
prevbuf[i] = buffer[i];
i++;
}
prevbytesread = bytesread;
}
}
else {
done = 1;
/* flush buffer except last two characters */
i = 0;
while (i < prevbytesread - 2) {
if (isfile)
fputc(prevbuf[i],uploadfile);
else {
if (j > buffersize) {
buffersize += BUFSIZ;
entry.value = (char *) realloc(entry.value, sizeof(char) *
buffersize+1);
}
entry.value[j] = prevbuf[i];
j++;
}
i++;
}
}
}
if (isfile)
fclose(uploadfile);
else {
entry.value[j] = '\0';
window = list_insafter(entries,window,entry);
numentries++;
j = 0;
}
}
return numentries;
}
int read_cgi_input(llist* entries)
{
char *input;
int status;
/* check for form upload. this needs to be first, because the
standard way of checking for POST data is inadequate. If you
are uploading a 100 MB file, you are unlikely to have a buffer
in memory large enough to store the raw data for parsing.
Instead, parse_form_encoded parses stdin directly.
In the future, I may modify parse_CGI_encoded so that it also
parses POST directly from stdin. I'm undecided on this issue,
because doing so will make parse_CGI_encoded less general. */
if ((CONTENT_TYPE != NULL) &&
(strstr(CONTENT_TYPE,"multipart/form-data") != NULL) )
return parse_form_encoded(entries);
/* get the input */
if (REQUEST_METHOD == NULL)
input = get_DEBUG();
else if (!strcmp(REQUEST_METHOD,"POST"))
input = get_POST();
else if (!strcmp(REQUEST_METHOD,"GET"))
input = get_GET();
else { /* error: invalid request method */
fprintf(stderr,"caught by cgihtml: REQUEST_METHOD invalid\n");
exit(1);
}
/* parse the input */
if (input == NULL)
return 0;
status = parse_CGI_encoded(entries,input);
free(input);
return status;
}
int read_file_upload(llist *entries, int maxfilesize)
{
return parse_form_encoded(entries);
}
char *cgi_val(llist l, char *name)
{
short FOUND = 0;
node* window;
window = l.head;
while ( (window != 0) && (!FOUND) )
if (!strcmp(window->entry.name,name))
FOUND = 1;
else
window = window->next;
if (FOUND)
return window->entry.value;
else
return NULL;
}
/* cgi_val_multi - contributed by Mitch Garnaat <garnaat@wrc.xerox.com>;
modified by me */
char **cgi_val_multi(llist l, char *name)
{
short FOUND = 0;
node* window;
char **ret_val = 0;
int num_vals = 0, i;
window = l.head;
while (window != 0) {
if (!strcmp(window->entry.name,name)) {
FOUND = 1;
num_vals++;
}
window = window->next;
}
if (FOUND) {
/* copy the value pointers into the returned array */
ret_val = (char**) malloc(sizeof(char*) * (num_vals + 1));
window = l.head;
i = 0;
while (window != NULL) {
if (!strcmp(window->entry.name,name)) {
ret_val[i] = window->entry.value;
i++;
}
window = window->next;
}
/* NULL terminate the array */
ret_val[i] = 0;
return ret_val;
}
else
return NULL;
}
char *cgi_name(llist l, char *value)
{
short FOUND = 0;
node* window;
window = l.head;
while ( (window != 0) && (!FOUND) )
if (!strcmp(window->entry.value,value))
FOUND = 1;
else
window = window->next;
if (FOUND)
return window->entry.name;
else
return NULL;
}
char **cgi_name_multi(llist l, char *value)
{
short FOUND = 0;
node* window;
char **ret_val = 0;
int num_vals = 0, i;
window = l.head;
while (window != 0) {
if (!strcmp(window->entry.value,value)) {
FOUND = 1;
num_vals++;
}
window = window->next;
}
if (FOUND) {
/* copy the value pointers into the returned array */
ret_val = (char**) malloc(sizeof(char*) * (num_vals + 1));
window = l.head;
i = 0;
while (window != NULL) {
if (!strcmp(window->entry.value,value)) {
ret_val[i] = window->entry.name;
i++;
}
window = window->next;
}
/* NULL terminate the array */
ret_val[i] = 0;
return ret_val;
}
else
return NULL;
}
/* miscellaneous useful CGI routines */
int parse_cookies(llist *entries)
{
char *cookies = getenv("HTTP_COOKIE");
node* window;
entrytype entry;
int i,len;
int j = 0;
int numcookies = 0;
short NM = 1;
if (cookies == NULL)
return 0;
list_create(entries);
window = entries->head;
len = strlen(cookies);
entry.name = (char *)malloc(sizeof(char) * len + 1);
entry.value = (char *)malloc(sizeof(char) * len + 1);
for (i = 0; i < len; i++) {
if (cookies[i] == '=') {
entry.name[j] = '\0';
if (i == len - 1) {
strcpy(entry.value,"");
window = list_insafter(entries,window,entry);
numcookies++;
}
j = 0;
NM = 0;
}
else if ( (cookies[i] == '&') || (i == len - 1) ) {
if (!NM) {
if (i == len - 1) {
entry.value[j] = cookies[i];
j++;
}
entry.value[j] = '\0';
window = list_insafter(entries,window,entry);
numcookies++;
j = 0;
NM = 1;
}
}
else if ( (cookies[i] == ';') || (i == len - 1) ) {
if (!NM) {
if (i == len - 1) {
entry.value[j] = cookies[i];
j++;
}
entry.value[j] = '\0';
window = list_insafter(entries,window,entry);
numcookies++;
i++; /* erases trailing space */
j = 0;
NM = 1;
}
}
else if (NM) {
entry.name[j] = cookies[i];
j++;
}
else if (!NM) {
entry.value[j] = cookies[i];
j++;
}
}
return numcookies;
}
void print_cgi_env()
{
if (SERVER_SOFTWARE != NULL)
printf("<p>SERVER_SOFTWARE = %s<br>\n",SERVER_SOFTWARE);
if (SERVER_NAME != NULL)
printf("SERVER_NAME = %s<br>\n",SERVER_NAME);
if (GATEWAY_INTERFACE !=NULL)
printf("GATEWAY_INTERFACE = %s<br>\n",GATEWAY_INTERFACE);
if (SERVER_PROTOCOL != NULL)
printf("SERVER_PROTOCOL = %s<br>\n",SERVER_PROTOCOL);
if (SERVER_PORT != NULL)
printf("SERVER_PORT = %s<br>\n",SERVER_PORT);
if (REQUEST_METHOD != NULL)
printf("REQUEST_METHOD = %s<br>\n",REQUEST_METHOD);
if (PATH_INFO != NULL)
printf("PATH_INFO = %s<br>\n",PATH_INFO);
if (PATH_TRANSLATED != NULL)
printf("PATH_TRANSLATED = %s<br>\n",PATH_TRANSLATED);
if (SCRIPT_NAME != NULL)
printf("SCRIPT_NAME = %s<br>\n",SCRIPT_NAME);
if (QUERY_STRING != NULL)
printf("QUERY_STRING = %s<br>\n",QUERY_STRING);
if (REMOTE_HOST != NULL)
printf("REMOTE_HOST = %s<br>\n",REMOTE_HOST);
if (REMOTE_ADDR != NULL)
printf("REMOTE_ADDR = %s<br>\n",REMOTE_ADDR);
if (AUTH_TYPE != NULL)
printf("AUTH_TYPE = %s<br>\n",AUTH_TYPE);
if (REMOTE_USER != NULL)
printf("REMOTE_USER = %s<br>\n",REMOTE_USER);
if (REMOTE_IDENT != NULL)
printf("REMOTE_IDENT = %s<br>\n",REMOTE_IDENT);
if (CONTENT_TYPE != NULL)
printf("CONTENT_TYPE = %s<br>\n",CONTENT_TYPE);
if (CONTENT_LENGTH != NULL)
printf("CONTENT_LENGTH = %s<br></p>\n",CONTENT_LENGTH);
if (HTTP_USER_AGENT != NULL)
printf("HTTP_USER_AGENT = %s<br></p>\n",HTTP_USER_AGENT);
}
void print_entries(llist l)
{
node* window;
window = l.head;
printf("<dl>\n");
while (window != NULL) {
printf(" <dt> <b>%s</b>\n",window->entry.name);
printf(" <dd> %s\n",replace_ltgt(window->entry.value));
window = window->next;
}
printf("</dl>\n");
}
char *escape_input(char *str)
/* takes string and escapes all metacharacters. should be used before
including string in system() or similar call. */
{
unsigned int i,j = 0;
char *newstring = (char *)malloc(sizeof(char) * (strlen(str) * 2 + 1));
for (i = 0; i < strlen(str); i++) {
if (!( ((str[i] >= 'A') && (str[i] <= 'Z')) ||
((str[i] >= 'a') && (str[i] <= 'z')) ||
((str[i] >= '0') && (str[i] <= '9')) )) {
newstring[j] = '\\';
j++;
}
newstring[j] = str[i];
j++;
}
newstring[j] = '\0';
return newstring;
}
/* boolean functions */
short is_form_empty(llist l)
{
node* window;
short EMPTY = 1;
window = l.head;
while ( (window != NULL) && (EMPTY == 1) ) {
if (strcmp(window->entry.value,""))
EMPTY = 0;
window = window->next;
}
return EMPTY;
}
short is_field_exists(llist l, char *str)
{
if (cgi_val(l,str) == NULL)
return 0;
else
return 1;
}
/* is_field_empty returns true either if the field exists but is empty
or if the field does not exist. */
short is_field_empty(llist l, char *str)
{
char *temp = cgi_val(l,str);
if ( (temp == NULL) || (!strcmp(temp,"")) )
return 1;
else
return 0;
}
|