CGI POST Method
in place June,21 2000, last modified June 21, 2000


CGI POST Method Example: HTML

<HTML>
<HEAD>
<TITLE> POST METHOD CGI </TITLE>
<BODY>
<H1>POST METHOD</H1>
<FORM ACTION="/cgi-bin/cgi.cgi" METHOD="POST">
PIZ <INPUT TYPE="TEXT" NAME=PIZZA ><BR>
TOP <INPUT TYPE=TEXT NAME=TOPPING><BR>
ETC <INPUT TYPE=TEXT NAME=ETC><BR>
<INPUT TYPE=SUBMIT VALUE=GO>
</FORM>
</BODY>
</HTML>

CGI POST Method Example: C Code

cgi.c file

#include 
#include "typedef.h"

main()
{
int count;
entry entries[5];

printf("Content-type : text/html\n\n");
printf("<HTML><BODY>");
post_method(entries,&count);

printf("<H1>VICS INTERFACE</H1>");
printf(" %s : %s <BR>", entries[0].name,entries[0].val);
printf(" %s : %s <BR>", entries[1].name,entries[1].val);
printf("</BODY></HTML>");

}

getp.c file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "typedef.h"
char *fmakeword(FILE *f, char stop, int *len);
void plustospace(char *str);
void unescape_url(char *url);
char *makeword(char *line, char stop);

void post_method(entry entries[], int *post_cell_count)
{
int x;
int cl;

if(strcmp(getenv("REQUEST_METHOD"),"POST"))

{
printf("You request the page with %s method <BR>",getenv("REQUEST_METHOD"));
printf("This script should be referenced with a METHOD of POST.\n");
exit(1);
}

if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded"))
{
printf("This script can only be used to decode from results.\n");
exit(1);
}

cl = atoi(getenv("CONTENT_LENGTH"));
printf(" length = %d <BR>",cl);

for (x=0; cl && (!feof(stdin)); x++) {
*post_cell_count = x;
entries[x].val = fmakeword(stdin,'&',&cl);
plustospace(entries[x].val);
unescape_url(entries[x].val);
entries[x].name=makeword(entries[x].val,'=');
}
}

util.c file

#include <stdio.h>
#define LF 10
#define CR 13

/*-----------------------------
  makeword
------------------------------*/
char *makeword(char *line, char stop)
{
int x = 0, y;
char *word = (char *) malloc(sizeof(char) * (strlen(line)+1));
for(x=0;((line[x]) && (line[x]!=stop));x++)
	word[x] = line[x];

word[x] = '\0';
if(line[x]) ++x;
y = 0;

while(line[y++] = line[x++]);
	return word;
}
/*-----------------------------
  fmakeword 
------------------------------*/
char *fmakeword(FILE *f, char stop, int *cl)
{
int wsize;
char *word;
int ll;

wsize = 102400;
ll = 0;
word = (char *) malloc(sizeof(char) * (wsize+1));
while(1)
	{
          word[ll] = (char) fgetc(f);
          if(ll==wsize) {
                     word[ll+1] = '\0';
                     wsize+=102400;
                     word = (char *)realloc(word,sizeof(char)*(wsize+1));
            }
            --(*cl);
            if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
                if(word[ll] != stop) ll++;
                word[ll] = '\0';
                return word;
                }
            ++ll;
        }
}

/*-----------------------------
  x2c
------------------------------*/
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);
}


 /*-----------------------------
  unescape_url
------------------------------*/	
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';
}

/*-----------------------------
  plustospace 
------------------------------*/
void plustospace(char *str)
{
  register int x;

  for (x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}

/*-----------------------------
  rind
------------------------------*/
int rind(char *s, char c)
{
  register int x;
  for (x=strlen(s) - 1;x != -1;x--)
      if(s[x] == c) return x;
  return -1;
}

/*-----------------------------
  getline
------------------------------*/
int getline(char *s, int n, FILE *f)
{
  register int i=0;

  while(1)
  {
    s[i] = (char)fgetc(f);

    if(s[i] == CR)
       s[i] = fgetc(f);

    if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1)))
    {
        s[i] = '\0';
        return (feof(f) ? 1 :0);
    }
    ++i;
  }
}

typedef.h

typedef struct {
	char *name;
	char *val;
} entry;

typedef struct {
	char name[21];
	char val[50];
} gentry;

Makefile

OBJS= getp.o util.o

cgi : $(OBJS) cgi.o
        $(CC) $(CFLAGS) -L$(LIBHOME) $(PROLDLIBS) $(OBJS) cgi.o -o $*
        cp cgi /httpd/cgi-bin/cgi.cgi

cgi.o : cgi.c
        $(CC) $(CFLAGS) -L$(LIBHOME) $(PROLDLIBS) -c $*.c

util.o : util.c
        $(CC) $(CFLAGS) -L$(LIBHOME) $(PROLDLIBS) -c $*.c

getp.o : getp.c
        $(CC) $(CFLAGS) -L$(LIBHOME) $(PROLDLIBS) -c $*.c

cleanup :
        -rm *.o


Knowledge & Engineering Databases (c) copyright Namchul Do, 2000