PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cgihtml-1.69/cgi-lib.c

http://jacl.googlecode.com/
C | 700 lines | 590 code | 52 blank | 58 comment | 196 complexity | f45646cecb8c9fdc435f002b390e5e05 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, GPL-2.0
  1. /* cgi-lib.c - C routines that make writing CGI scripts in C a breeze
  2. Eugene Kim, <eekim@eekim.com>
  3. $Id: cgi-lib.c 1619 2010-02-04 04:12:44Z jacl $
  4. Motivation: Perl is a much more convenient language to use when
  5. writing CGI scripts. Unfortunately, it is also a larger drain on
  6. the system. Hopefully, these routines will make writing CGI
  7. scripts just as easy in C.
  8. Copyright (C) 1996,1997 Eugene Eric Kim
  9. All Rights Reserved
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #ifdef WINDOWS
  16. #include <io.h>
  17. #endif
  18. #include "cgi-lib.h"
  19. #include "html-lib.h"
  20. #include "string-lib.h"
  21. /* symbol table for CGI encoding */
  22. #define _NAME 0
  23. #define _VALUE 1
  24. short accept_image()
  25. {
  26. char *httpaccept = getenv("HTTP_ACCEPT");
  27. if (strstr(httpaccept,"image") == NULL)
  28. return 0;
  29. else
  30. return 1;
  31. }
  32. /* x2c() and unescape_url() stolen from NCSA code */
  33. char x2c(char *what)
  34. {
  35. register char digit;
  36. digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
  37. digit *= 16;
  38. digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
  39. return(digit);
  40. }
  41. void unescape_url(char *url)
  42. {
  43. register int x,y;
  44. for (x=0,y=0; url[y]; ++x,++y) {
  45. if((url[x] = url[y]) == '%') {
  46. url[x] = x2c(&url[y+1]);
  47. y+=2;
  48. }
  49. }
  50. url[x] = '\0';
  51. }
  52. char *get_DEBUG()
  53. {
  54. int bufsize = 1024;
  55. char *buffer = (char *)malloc(sizeof(char) * bufsize + 1);
  56. int i = 0;
  57. char ch;
  58. fprintf(stderr,"\n--- cgihtml Interactive Mode ---\n");
  59. fprintf(stderr,"Enter CGI input string. Remember to encode appropriate ");
  60. fprintf(stderr,"characters.\nPress ENTER when done:\n\n");
  61. while ( (i<=bufsize) && ((ch = getc(stdin)) != '\n') ) {
  62. buffer[i] = ch;
  63. i++;
  64. if (i>bufsize) {
  65. bufsize *= 2;
  66. buffer = (char *)realloc(buffer,bufsize);
  67. }
  68. }
  69. buffer[i] = '\0';
  70. fprintf(stderr,"\n Input string: %s\nString length: %d\n",buffer,i);
  71. fprintf(stderr,"--- end cgihtml Interactive Mode ---\n\n");
  72. return buffer;
  73. }
  74. char *get_POST()
  75. {
  76. unsigned int content_length;
  77. char *buffer = NULL;
  78. if (CONTENT_LENGTH != NULL) {
  79. content_length = atoi(CONTENT_LENGTH);
  80. buffer = (char *)malloc(sizeof(char) * content_length + 1);
  81. if (fread(buffer,sizeof(char),content_length,stdin) != content_length) {
  82. /* consistency error. */
  83. fprintf(stderr,"caught by cgihtml: input length < CONTENT_LENGTH\n");
  84. exit(1);
  85. }
  86. buffer[content_length] = '\0';
  87. }
  88. return buffer;
  89. }
  90. char *get_GET()
  91. {
  92. char *buffer;
  93. if (QUERY_STRING == NULL)
  94. return NULL;
  95. buffer = newstr(QUERY_STRING);
  96. return buffer;
  97. }
  98. int parse_CGI_encoded(llist *entries, char *buffer)
  99. {
  100. int i, j, num, token;
  101. int len = strlen(buffer);
  102. char *lexeme = (char *)malloc(sizeof(char) * len + 1);
  103. entrytype entry;
  104. node *window;
  105. list_create(entries);
  106. window = entries->head;
  107. entry.name = NULL;
  108. entry.value = NULL;
  109. i = 0;
  110. num = 0;
  111. token = _NAME;
  112. while (i < len) {
  113. j = 0;
  114. while ( (buffer[i] != '=') && (buffer[i] != '&') && (i < len) ) {
  115. lexeme[j] = (buffer[i] == '+') ? ' ' : buffer[i];
  116. i++;
  117. j++;
  118. }
  119. lexeme[j] = '\0';
  120. if (token == _NAME) {
  121. entry.name = newstr(lexeme);
  122. unescape_url(entry.name);
  123. if ( (buffer[i] != '=') || (i == len - 1) ) {
  124. entry.value = (char *)malloc(sizeof(char));
  125. strcpy(entry.value,"");
  126. window = list_insafter(entries, window, entry);
  127. free(entry.name);
  128. entry.name = NULL;
  129. free(entry.value);
  130. entry.value = NULL;
  131. if (i == len - 1) /* null value at end of expression */
  132. num++;
  133. else { /* error in expression */
  134. free(lexeme);
  135. return -1;
  136. }
  137. }
  138. else
  139. token = _VALUE;
  140. }
  141. else {
  142. entry.value = newstr(lexeme);
  143. unescape_url(entry.value);
  144. window = list_insafter(entries, window, entry);
  145. free(entry.name);
  146. entry.name = NULL;
  147. free(entry.value);
  148. entry.value = NULL;
  149. token = _NAME;
  150. num++;
  151. }
  152. i++;
  153. j = 0;
  154. }
  155. free(lexeme);
  156. if (entry.name != NULL)
  157. free(entry.name);
  158. if (entry.value != NULL)
  159. free(entry.value);
  160. return num;
  161. }
  162. /* stolen from k&r and seriously modified to do what I want */
  163. int cgetline(char s[], int lim)
  164. {
  165. int c=0, i=0, num;
  166. for (i=0; (i<lim) && ((c=getchar())!=EOF) && (c!='\n'); i++) {
  167. s[i] = c;
  168. }
  169. if (c == '\n') {
  170. s[i] = c;
  171. }
  172. if ((i==0) && (c!='\n'))
  173. num = 0;
  174. else if (i == lim)
  175. num = i;
  176. else
  177. num = i+1;
  178. return num;
  179. }
  180. int parse_form_encoded(llist* entries)
  181. {
  182. long content_length;
  183. entrytype entry;
  184. node* window;
  185. FILE *uploadfile = NULL;
  186. char *uploadfname, *tempstr, *boundary;
  187. char *buffer = (char *)malloc(sizeof(char) * BUFSIZ + 1);
  188. char *prevbuf = (char *)malloc(sizeof(char) + BUFSIZ + 1);
  189. short isfile,done,start;
  190. int i,j;
  191. int bytesread, prevbytesread = 0;
  192. int buffersize;
  193. int numentries = 0;
  194. #ifdef WINDOWS
  195. setmode(fileno(stdin), O_BINARY); /* define stdin as binary */
  196. _fmode = BINARY; /* default all file I/O as binary */
  197. #endif
  198. if (CONTENT_LENGTH != NULL)
  199. content_length = atol(CONTENT_LENGTH);
  200. else
  201. return 0;
  202. /* get boundary */
  203. tempstr = newstr(CONTENT_TYPE);
  204. boundary = strstr(tempstr,"boundary=");
  205. boundary += (sizeof(char) * 9);
  206. /* create list */
  207. list_create(entries);
  208. window = entries->head;
  209. /* ignore first boundary; this isn't so robust; improve it later */
  210. cgetline(buffer,BUFSIZ);
  211. /* now start parsing */
  212. while ((bytesread=cgetline(buffer,BUFSIZ)) != 0) {
  213. start = 1;
  214. /* this assumes that buffer contains no binary characters.
  215. if the buffer contains the first valid header, then this
  216. is a safe assumption. however, it should be improved for
  217. robustness sake. */
  218. buffer[bytesread] = '\0';
  219. tempstr = newstr(buffer);
  220. tempstr += (sizeof(char) * 38); /* 38 is header up to name */
  221. entry.name = tempstr;
  222. entry.value = (char *)malloc(sizeof(char) * BUFSIZ + 1);
  223. buffersize = BUFSIZ;
  224. strcpy(entry.value,"");
  225. while (*tempstr != '"')
  226. tempstr++;
  227. *tempstr = '\0';
  228. if (strstr(buffer,"filename=\"") != NULL) {
  229. isfile = 1;
  230. tempstr = newstr(buffer);
  231. tempstr = strstr(tempstr,"filename=\"");
  232. tempstr += (sizeof(char) * 10);
  233. if (strlen(tempstr) >= BUFSIZ)
  234. entry.value = (char *) realloc(entry.value, sizeof(char) *
  235. strlen(tempstr)+1);
  236. entry.value = tempstr;
  237. while (*tempstr != '"')
  238. tempstr++;
  239. *tempstr = '\0';
  240. /* Netscape's Windows browsers handle paths differently from its
  241. UNIX and Mac browsers. It delivers a full path for the uploaded
  242. file (which it shouldn't do), and it uses backslashes rather than
  243. forward slashes. No need to worry about Internet Explorer, since
  244. it doesn't support HTTP File Upload at all. */
  245. if (strstr(lower_case(HTTP_USER_AGENT),"win") != 0) {
  246. tempstr = strrchr(entry.value, '\\');
  247. if (tempstr) {
  248. tempstr++;
  249. entry.value = tempstr;
  250. }
  251. }
  252. window = list_insafter(entries,window,entry);
  253. numentries++;
  254. uploadfname = (char *)malloc(strlen(UPLOADDIR)+strlen(entry.value)+2);
  255. #ifdef WINDOWS
  256. sprintf(uploadfname,"%s\\%s",UPLOADDIR,entry.value);
  257. #else
  258. sprintf(uploadfname,"%s/%s",UPLOADDIR,entry.value);
  259. #endif
  260. if ( (uploadfile = fopen(uploadfname,"w")) == NULL) {
  261. /* null filename; for now, just don't save info. later, save
  262. to default file */
  263. isfile = 0;
  264. }
  265. }
  266. else
  267. isfile = 0;
  268. /* ignore rest of headers and first blank line */
  269. while (cgetline(buffer, BUFSIZ) > 1) {
  270. /* DOS style blank line? */
  271. if ((buffer[0] == '\r') && (buffer[1] == '\n'))
  272. break;
  273. }
  274. done = 0;
  275. j = 0;
  276. while (!done) {
  277. bytesread = cgetline(buffer,BUFSIZ);
  278. buffer[bytesread] = '\0';
  279. if (bytesread && strstr(buffer,boundary) == NULL) {
  280. if (start) {
  281. i = 0;
  282. while (i < bytesread) {
  283. prevbuf[i] = buffer[i];
  284. i++;
  285. }
  286. prevbytesread = bytesread;
  287. start = 0;
  288. }
  289. else {
  290. /* flush buffer */
  291. i = 0;
  292. while (i < prevbytesread) {
  293. if (isfile)
  294. fputc(prevbuf[i],uploadfile);
  295. else {
  296. if (j > buffersize) {
  297. buffersize += BUFSIZ;
  298. entry.value = (char *) realloc(entry.value, sizeof(char) *
  299. buffersize+1);
  300. }
  301. entry.value[j] = prevbuf[i];
  302. j++;
  303. }
  304. i++;
  305. }
  306. /* buffer new input */
  307. i = 0;
  308. while (i < bytesread) {
  309. prevbuf[i] = buffer[i];
  310. i++;
  311. }
  312. prevbytesread = bytesread;
  313. }
  314. }
  315. else {
  316. done = 1;
  317. /* flush buffer except last two characters */
  318. i = 0;
  319. while (i < prevbytesread - 2) {
  320. if (isfile)
  321. fputc(prevbuf[i],uploadfile);
  322. else {
  323. if (j > buffersize) {
  324. buffersize += BUFSIZ;
  325. entry.value = (char *) realloc(entry.value, sizeof(char) *
  326. buffersize+1);
  327. }
  328. entry.value[j] = prevbuf[i];
  329. j++;
  330. }
  331. i++;
  332. }
  333. }
  334. }
  335. if (isfile)
  336. fclose(uploadfile);
  337. else {
  338. entry.value[j] = '\0';
  339. window = list_insafter(entries,window,entry);
  340. numentries++;
  341. j = 0;
  342. }
  343. }
  344. return numentries;
  345. }
  346. int read_cgi_input(llist* entries)
  347. {
  348. char *input;
  349. int status;
  350. /* check for form upload. this needs to be first, because the
  351. standard way of checking for POST data is inadequate. If you
  352. are uploading a 100 MB file, you are unlikely to have a buffer
  353. in memory large enough to store the raw data for parsing.
  354. Instead, parse_form_encoded parses stdin directly.
  355. In the future, I may modify parse_CGI_encoded so that it also
  356. parses POST directly from stdin. I'm undecided on this issue,
  357. because doing so will make parse_CGI_encoded less general. */
  358. if ((CONTENT_TYPE != NULL) &&
  359. (strstr(CONTENT_TYPE,"multipart/form-data") != NULL) )
  360. return parse_form_encoded(entries);
  361. /* get the input */
  362. if (REQUEST_METHOD == NULL)
  363. input = get_DEBUG();
  364. else if (!strcmp(REQUEST_METHOD,"POST"))
  365. input = get_POST();
  366. else if (!strcmp(REQUEST_METHOD,"GET"))
  367. input = get_GET();
  368. else { /* error: invalid request method */
  369. fprintf(stderr,"caught by cgihtml: REQUEST_METHOD invalid\n");
  370. exit(1);
  371. }
  372. /* parse the input */
  373. if (input == NULL)
  374. return 0;
  375. status = parse_CGI_encoded(entries,input);
  376. free(input);
  377. return status;
  378. }
  379. int read_file_upload(llist *entries, int maxfilesize)
  380. {
  381. return parse_form_encoded(entries);
  382. }
  383. char *cgi_val(llist l, char *name)
  384. {
  385. short FOUND = 0;
  386. node* window;
  387. window = l.head;
  388. while ( (window != 0) && (!FOUND) )
  389. if (!strcmp(window->entry.name,name))
  390. FOUND = 1;
  391. else
  392. window = window->next;
  393. if (FOUND)
  394. return window->entry.value;
  395. else
  396. return NULL;
  397. }
  398. /* cgi_val_multi - contributed by Mitch Garnaat <garnaat@wrc.xerox.com>;
  399. modified by me */
  400. char **cgi_val_multi(llist l, char *name)
  401. {
  402. short FOUND = 0;
  403. node* window;
  404. char **ret_val = 0;
  405. int num_vals = 0, i;
  406. window = l.head;
  407. while (window != 0) {
  408. if (!strcmp(window->entry.name,name)) {
  409. FOUND = 1;
  410. num_vals++;
  411. }
  412. window = window->next;
  413. }
  414. if (FOUND) {
  415. /* copy the value pointers into the returned array */
  416. ret_val = (char**) malloc(sizeof(char*) * (num_vals + 1));
  417. window = l.head;
  418. i = 0;
  419. while (window != NULL) {
  420. if (!strcmp(window->entry.name,name)) {
  421. ret_val[i] = window->entry.value;
  422. i++;
  423. }
  424. window = window->next;
  425. }
  426. /* NULL terminate the array */
  427. ret_val[i] = 0;
  428. return ret_val;
  429. }
  430. else
  431. return NULL;
  432. }
  433. char *cgi_name(llist l, char *value)
  434. {
  435. short FOUND = 0;
  436. node* window;
  437. window = l.head;
  438. while ( (window != 0) && (!FOUND) )
  439. if (!strcmp(window->entry.value,value))
  440. FOUND = 1;
  441. else
  442. window = window->next;
  443. if (FOUND)
  444. return window->entry.name;
  445. else
  446. return NULL;
  447. }
  448. char **cgi_name_multi(llist l, char *value)
  449. {
  450. short FOUND = 0;
  451. node* window;
  452. char **ret_val = 0;
  453. int num_vals = 0, i;
  454. window = l.head;
  455. while (window != 0) {
  456. if (!strcmp(window->entry.value,value)) {
  457. FOUND = 1;
  458. num_vals++;
  459. }
  460. window = window->next;
  461. }
  462. if (FOUND) {
  463. /* copy the value pointers into the returned array */
  464. ret_val = (char**) malloc(sizeof(char*) * (num_vals + 1));
  465. window = l.head;
  466. i = 0;
  467. while (window != NULL) {
  468. if (!strcmp(window->entry.value,value)) {
  469. ret_val[i] = window->entry.name;
  470. i++;
  471. }
  472. window = window->next;
  473. }
  474. /* NULL terminate the array */
  475. ret_val[i] = 0;
  476. return ret_val;
  477. }
  478. else
  479. return NULL;
  480. }
  481. /* miscellaneous useful CGI routines */
  482. int parse_cookies(llist *entries)
  483. {
  484. char *cookies = getenv("HTTP_COOKIE");
  485. node* window;
  486. entrytype entry;
  487. int i,len;
  488. int j = 0;
  489. int numcookies = 0;
  490. short NM = 1;
  491. if (cookies == NULL)
  492. return 0;
  493. list_create(entries);
  494. window = entries->head;
  495. len = strlen(cookies);
  496. entry.name = (char *)malloc(sizeof(char) * len + 1);
  497. entry.value = (char *)malloc(sizeof(char) * len + 1);
  498. for (i = 0; i < len; i++) {
  499. if (cookies[i] == '=') {
  500. entry.name[j] = '\0';
  501. if (i == len - 1) {
  502. strcpy(entry.value,"");
  503. window = list_insafter(entries,window,entry);
  504. numcookies++;
  505. }
  506. j = 0;
  507. NM = 0;
  508. }
  509. else if ( (cookies[i] == '&') || (i == len - 1) ) {
  510. if (!NM) {
  511. if (i == len - 1) {
  512. entry.value[j] = cookies[i];
  513. j++;
  514. }
  515. entry.value[j] = '\0';
  516. window = list_insafter(entries,window,entry);
  517. numcookies++;
  518. j = 0;
  519. NM = 1;
  520. }
  521. }
  522. else if ( (cookies[i] == ';') || (i == len - 1) ) {
  523. if (!NM) {
  524. if (i == len - 1) {
  525. entry.value[j] = cookies[i];
  526. j++;
  527. }
  528. entry.value[j] = '\0';
  529. window = list_insafter(entries,window,entry);
  530. numcookies++;
  531. i++; /* erases trailing space */
  532. j = 0;
  533. NM = 1;
  534. }
  535. }
  536. else if (NM) {
  537. entry.name[j] = cookies[i];
  538. j++;
  539. }
  540. else if (!NM) {
  541. entry.value[j] = cookies[i];
  542. j++;
  543. }
  544. }
  545. return numcookies;
  546. }
  547. void print_cgi_env()
  548. {
  549. if (SERVER_SOFTWARE != NULL)
  550. printf("<p>SERVER_SOFTWARE = %s<br>\n",SERVER_SOFTWARE);
  551. if (SERVER_NAME != NULL)
  552. printf("SERVER_NAME = %s<br>\n",SERVER_NAME);
  553. if (GATEWAY_INTERFACE !=NULL)
  554. printf("GATEWAY_INTERFACE = %s<br>\n",GATEWAY_INTERFACE);
  555. if (SERVER_PROTOCOL != NULL)
  556. printf("SERVER_PROTOCOL = %s<br>\n",SERVER_PROTOCOL);
  557. if (SERVER_PORT != NULL)
  558. printf("SERVER_PORT = %s<br>\n",SERVER_PORT);
  559. if (REQUEST_METHOD != NULL)
  560. printf("REQUEST_METHOD = %s<br>\n",REQUEST_METHOD);
  561. if (PATH_INFO != NULL)
  562. printf("PATH_INFO = %s<br>\n",PATH_INFO);
  563. if (PATH_TRANSLATED != NULL)
  564. printf("PATH_TRANSLATED = %s<br>\n",PATH_TRANSLATED);
  565. if (SCRIPT_NAME != NULL)
  566. printf("SCRIPT_NAME = %s<br>\n",SCRIPT_NAME);
  567. if (QUERY_STRING != NULL)
  568. printf("QUERY_STRING = %s<br>\n",QUERY_STRING);
  569. if (REMOTE_HOST != NULL)
  570. printf("REMOTE_HOST = %s<br>\n",REMOTE_HOST);
  571. if (REMOTE_ADDR != NULL)
  572. printf("REMOTE_ADDR = %s<br>\n",REMOTE_ADDR);
  573. if (AUTH_TYPE != NULL)
  574. printf("AUTH_TYPE = %s<br>\n",AUTH_TYPE);
  575. if (REMOTE_USER != NULL)
  576. printf("REMOTE_USER = %s<br>\n",REMOTE_USER);
  577. if (REMOTE_IDENT != NULL)
  578. printf("REMOTE_IDENT = %s<br>\n",REMOTE_IDENT);
  579. if (CONTENT_TYPE != NULL)
  580. printf("CONTENT_TYPE = %s<br>\n",CONTENT_TYPE);
  581. if (CONTENT_LENGTH != NULL)
  582. printf("CONTENT_LENGTH = %s<br></p>\n",CONTENT_LENGTH);
  583. if (HTTP_USER_AGENT != NULL)
  584. printf("HTTP_USER_AGENT = %s<br></p>\n",HTTP_USER_AGENT);
  585. }
  586. void print_entries(llist l)
  587. {
  588. node* window;
  589. window = l.head;
  590. printf("<dl>\n");
  591. while (window != NULL) {
  592. printf(" <dt> <b>%s</b>\n",window->entry.name);
  593. printf(" <dd> %s\n",replace_ltgt(window->entry.value));
  594. window = window->next;
  595. }
  596. printf("</dl>\n");
  597. }
  598. char *escape_input(char *str)
  599. /* takes string and escapes all metacharacters. should be used before
  600. including string in system() or similar call. */
  601. {
  602. unsigned int i,j = 0;
  603. char *newstring = (char *)malloc(sizeof(char) * (strlen(str) * 2 + 1));
  604. for (i = 0; i < strlen(str); i++) {
  605. if (!( ((str[i] >= 'A') && (str[i] <= 'Z')) ||
  606. ((str[i] >= 'a') && (str[i] <= 'z')) ||
  607. ((str[i] >= '0') && (str[i] <= '9')) )) {
  608. newstring[j] = '\\';
  609. j++;
  610. }
  611. newstring[j] = str[i];
  612. j++;
  613. }
  614. newstring[j] = '\0';
  615. return newstring;
  616. }
  617. /* boolean functions */
  618. short is_form_empty(llist l)
  619. {
  620. node* window;
  621. short EMPTY = 1;
  622. window = l.head;
  623. while ( (window != NULL) && (EMPTY == 1) ) {
  624. if (strcmp(window->entry.value,""))
  625. EMPTY = 0;
  626. window = window->next;
  627. }
  628. return EMPTY;
  629. }
  630. short is_field_exists(llist l, char *str)
  631. {
  632. if (cgi_val(l,str) == NULL)
  633. return 0;
  634. else
  635. return 1;
  636. }
  637. /* is_field_empty returns true either if the field exists but is empty
  638. or if the field does not exist. */
  639. short is_field_empty(llist l, char *str)
  640. {
  641. char *temp = cgi_val(l,str);
  642. if ( (temp == NULL) || (!strcmp(temp,"")) )
  643. return 1;
  644. else
  645. return 0;
  646. }