CGI program to show parameters


/ Published in: C
Save to your folder(s)

Compile, put exe in cgi-bin folder, and post form data to it.


Copy this code and paste it in your HTML
  1. /*
  2. This CGI program shows all parameters sent to it
  3. Zufolek 2009
  4. */
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12.  
  13.  
  14. /* structure to hold one cgi arg (or parameter)*/
  15. typedef struct{
  16. char*name;
  17. char*value;
  18. } Arg;
  19.  
  20.  
  21. /* prints error message to stdout and terminates program */
  22. void err(char*s){
  23. puts("<p>Program has encountered an error:<br>");
  24. puts(s);
  25. exit(1);
  26. }
  27.  
  28.  
  29. /* returns value of one hex digit */
  30. int getHex(char c){
  31. if(isdigit(c))return c-'0';
  32. if(isalpha(c))return tolower(c)-'a'+10;
  33. return 0;
  34. }
  35.  
  36.  
  37. /* decodes a hex value */
  38. char decode(char*s){
  39. return getHex(s[0])*16+getHex(s[1]);
  40. }
  41.  
  42.  
  43. /* prevents making tags in html */
  44. int safeChar(char c){
  45. switch(c){
  46. case '<':
  47. case '>':
  48. return 0;
  49. }
  50. return 1;
  51. }
  52.  
  53.  
  54. /* checks if there are valid hex values at specified pointer */
  55. int isSafe(char*s){
  56. if(!isxdigit(s[0]) || !isxdigit(s[1])) return 0;
  57. return 1;
  58. }
  59.  
  60.  
  61. /* decodes text in parameters */
  62. char*fixData(char*i){
  63. int l=strlen(i);
  64. char*m=malloc(l*4+8);
  65. char*o=m;
  66. if(!m)err("Memory allocation failure for fixData");
  67. while(*i){
  68. if(*i=='+')*o=' ';
  69. else if(*i=='\n'){
  70. }else if(*i=='%'&&isSafe(i+1)){
  71. char d=decode(i+1);
  72. if(safeChar(d)){
  73. if(d=='\n')o+=sprintf(o,"<br>")-1;
  74. else if(d=='\r')--o;
  75. else *o=d;
  76. }else o+=sprintf(o,"&#%d;",d)-1;
  77. i+=2;
  78. }else *o=*i;
  79. ++i;
  80. ++o;
  81. }
  82. *o=0;
  83. m=realloc(m,o-m+1);
  84. if(!m)err("Memory reallocation failure for fixData");
  85. return m;
  86. }
  87.  
  88.  
  89. /* prints a basic http header to stdout */
  90. void init(){
  91. printf("Content-Type: text/html\n\n");
  92. }
  93.  
  94.  
  95. /* reads input string from stdin */
  96. char*getInput(){
  97. char*v=getenv("CONTENT_LENGTH");
  98. char*s=0;
  99. if(v){
  100. int l=atoi(v);
  101. if(l){
  102. s=malloc(l+1);
  103. FILE*f=stdin;
  104. if(!s)err("Memory allocation failure for input");
  105. fread(s,1,l,f);
  106. s[l]=0;
  107. }
  108. }
  109. return s;
  110. }
  111.  
  112.  
  113. /* Counts the number of parameters in input string */
  114. int countVars(char*s){
  115. int n=1;
  116. while(*s){
  117. if(*s=='&')++n;
  118. ++s;
  119. }
  120. return n;
  121. }
  122.  
  123.  
  124. /* Moves pointer-pointer s just past next occurrence of character c,
  125.  which is then set to 0, or returns 1 if end of string is reached */
  126. int skipTo(char**s,char c){
  127. while(**s != c){
  128. if(!**s)return 1;
  129. ++*s;
  130. }
  131. **s=0;
  132. ++*s;
  133. return 0;
  134. }
  135.  
  136.  
  137. /* Parses input string for args */
  138. Arg*parseInput(char*s){
  139. int l=countVars(s);
  140. Arg*arg=malloc((l+1)*sizeof(Arg));
  141. Arg*a=arg;
  142. if(!a)err("Memory allocation failure for args list");
  143. while(*s){
  144. a->name=s;
  145. if(skipTo(&s,'='))goto Done;
  146. a->value=s;
  147. if(skipTo(&s,'&'))goto Done;
  148. ++a;
  149. }
  150. Done:
  151. arg[l].name=0;/* marks the end of arg list */
  152. return arg;
  153. }
  154.  
  155.  
  156. /* prints all the args to stdout */
  157. void showArgs(Arg*a){
  158. while(a->name){
  159. printf("Arg name=%s value=%s<br>\n",a->name,a->value);
  160. ++a;
  161. }
  162. }
  163.  
  164.  
  165.  
  166. int main(){
  167. char*i;
  168. Arg*a;
  169. init();
  170. i=getInput();
  171. if(i){
  172. a=parseInput(i);
  173. showArgs(a);
  174. free(i);
  175. free(a);
  176. }else puts("No input parameters.");
  177. return 0;
  178. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.