¸®´ª½º¿¡¼ GoAheadÀ¥¼¹ö 2.1¹öÁ¯À¸·Î À¥¼¹ö¸¦ ¿Ã·Á c¾ð¾î·Î cgi¸¦ Â¥¼ ·Î±×ÀÎÀ» ÀÎÁõÇÒ·Á°í ±¸ÇسõÀº°Ì´Ï´Ù.
±Ùµ¥ Á¤ÀÛ cgi¿¡¼ À¥À̵¿ ¸í·ÉÀ» ÇØÁá´Âµ¥µµ ºÒ±¸ÇÏ°í ¾ÈµÇ³×¿©..
2Ʋµ¿¾È °è¼Ó Çغôµ¥ ¸Ó°¡ ¹®Á¦ÀÎÁö ¸ô¶óµµ ¾ÈµË´Ï´Ù
¹Ø¿¡ ÆĶõ»ö ºÎºÐÀÔ´Ï´Ù...
--------Check.c----
#include <stdio.h> /* Ç¥ÁØ Çì´õÆÄÀÏ */
#include <stdlib.h> /* getenv() ÇÔ¼ö µîÀÇ Çì´õÆÄÀÏ */
#include <string.h> /* ¹®ÀÚ¿ ÇÔ¼ö Çì´õÆÄÀÏ */
#include <time.h> /* ½Ã°£ÇÔ¼ö Çì´õÆÄÀÏ */
#define MAX 1000 /* ÀԷº¯¼ö°¡ ÃÖ´ë 1000°³·Î °¡Á¤ */
typedef struct { /* entry ¶ó´Â ±¸Á¶Ã¼ÇüÀ» ¼±¾ð */
char *name; /* º¯¼ö¸í; INPUT¹®¿¡¼ÀÇ name */
char *val; /* °ª; INPUT¹®¿¡¼ÀÇ value */
} entry;
int nEntries; /* ½ÇÁ¦ ÀԷ°ªÀÇ °³¼ö¸¦ ´ãÀ» º¯¼ö */
entry entries[MAX]; /* entryÇüÀÇ entries ¹è¿º¯¼ö ¼±¾ð */
void getEntries(); /* º¯¼ö ¹× °ª ÀÔ·Â ÇÔ¼ö */
void printout(); /* º¯¼ö ¹× °ª Ãâ·Â ÇÔ¼ö */
char *makeword (char *, char); /* º¯¼ö¿Í °ª ºÐ¸®ÇÔ¼ö */
char *fmakeword(FILE *, char, int *); /* º¯¼ö¿Í °ªÀÇ ½ÖÀ» ¸¸µê */
char *qURLdecode(char *); /* ÇÑ±Û DECODE ÇÔ¼ö */
char _x2c(char, char); /* 16Áø¼ö Á¶ÇÕ ÇÔ¼ö */
main()
{
getEntries(); /* ¸Å°³º¯¼ö¸¦ Àü´Þ¹ÞÀ½ */
printout(); /* ȸéÃâ·Â ÇÔ¼ö¸¦ È£Ãâ */
return 0;
}
void getEntries()
{
register int x, m=0;
int cl;
/* FORM¿¡¼ METHOD=POST·Î ÁöÁ¤µÇ¾ú´ÂÁö È®ÀÎ */
if(strcmp(getenv("REQUEST_METHOD"), "POST")) {
printf("ÀÌ ÇÁ·Î±×·¥À» ½ÇÇà½ÃÅ°·Á¸é FORM¹®¿¡¼ ");
printf("POST ¹æ½ÄÀ» ÁöÁ¤Çؾ߸¸ ÇÕ´Ï´Ù.\n");
exit(1);
}
/* TYPEÀº application/x-www-form-urlencoded À̾î¾ß ÇÔ */
if (strcmp(getenv("CONTENT_TYPE"),
"application/x-www-form-urlencoded")) {
printf("ÀÌ ÇÁ·Î±×·¥Àº FORMÀÇ °á°ú¸¦ ");
printf("Çؼ®Çϱâ À§Çؼ¸¸ »ç¿ëµË´Ï´Ù.\n");
exit(1);
}
/* ÀԷµǴ º¯¼ö¿Í °ªÀÇ ±æÀ̸¦ ¹Þ¾ÆµéÀÓ */
cl=atoi(getenv("CONTENT_LENGTH"));
/* stdinÀ¸·Î ÀԷµǴ ¹®ÀÚ¿À» º¯¼ö¿Í °ªÀ¸·Î À߶󳻾î
entries ±¸Á¶Ã¼¿¡ ÀúÀå */
for(x=0; cl && (!feof(stdin)); x++) {
m=x;
entries[x].val=fmakeword(stdin, '&', &cl);
entries[x].name=makeword(entries[x].val, '=');
}
nEntries=m+1;
/* 16Áø¼ö·Î ÄÚµåÈµÈ ÇѱÛÀ» ¿ø»óÅ·Πº¹¿ø */
for (x=0; x<nEntries; x++) {
qURLdecode(entries[x].name);
qURLdecode(entries[x].val);
}
}
char *fmakeword(FILE *f, char stop, int *cl)
{
int wsize=102400, ll=0;
char *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;
}
}
char *makeword(char *line, char stop)
{
int x=0, y=0;
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;
while(line[y++]=line[x++]);
return word;
}
char *qURLdecode(char *str)
{
int i, j;
if(!str) return;
for(i=j=0; str[j]; i++, j++)
switch(str[j]){
/* ´õÇϱâ(+) ±âÈ£¸¦ °ø¹é(' ')À¸·Î º¯È¯ */
case '+': str[i]=' ';
break;
/* '%'·Î ½ÃÀ۵Ǵ µÎ ¹®ÀÚ¸¦ ÇÑ±Û ÇѱÛÀÚ·Î º¯È¯ */
case '%': str[i]=_x2c(str[j+1], str[j+2]);
j+=2;
break;
}
str[i]='\0';
return str;
}
char _x2c(char hex_up, char hex_low)
{
char digit;
/* µÎ °³ÀÇ 16Áø¼ö °ªÀ» ÇϳªÀÇ 16Áø¼ö °ªÀ¸·Î º¯È¯ÇÑ´Ù */
digit = 16 * (hex_up >= 'A' ?
((hex_up & 0xdf) - 'A') + 10 : (hex_up - '0'));
digit += (hex_low >= 'A' ?
((hex_low & 0xdf) - 'A') + 10 : (hex_low - '0'));
return (digit);
}
void printout()
{
FILE *fp;
int i, j;
char *remote_host; /* ¿ø°Ý È£½ºÆ®ÀÇ À̸§À» ±â¾ïÇÏ´Â º¯¼ö */
time_t t=time(NULL); /* ÇöÀç ½Ã°£À» ±â¾ïÇÏ´Â º¯¼ö */
/* Ãâ·ÂµÇ´Â ³»¿ëÀÌ ÅؽºÆ® ÇüÅÂÀÇ HTML ¹®ÀåÀÓÀ» ¾Ë·ÁÁÖ¸ç,
ÀÌÈķδ printf()¹® ³»¿¡ HTML ¸í·ÉÀ» ±â¼úÇÑ´Ù */
printf("Content-type: text/html%c%c", 10, 10);
printf("<HTML>\n");
printf("<HEAD>\n");
printf(" <TITLE> ÆĶó¸ÞÅ͵éÀÇ °ªÀ» Ãâ·ÂÇÔ </TITLE>\n");
printf("</HEAD>\n");
printf("<BODY>\n");
printf("<BR><UL>\n");
for (i=0; i<nEntries; i++) {
printf("<LI> %s : ", entries[i].name);
printf("%s \n <BR>", entries[i].val);
}
printf("<LI> ³¯Â¥: %s \n <BR>", ctime(&t));
if ((remote_host=getenv("REMOTE_HOST")) == NULL)
remote_host="null";
printf("<LI> ¼Û½Å: %s \n <BR>", remote_host);
printf("Location: 192.168.10.88/1.html \n"); <-- ¿äºÎºÐÀ» ³Ö¾î¼ Çߴµ¥ ¾ÈµË´Ï´Ù. ¹°·Ð ÀÚ¹Ù·ÎµÈ 3°¡Áö ¹æ¹ý ´Ù½á봣±¸¿°..
printf("</UL></BODY>\n");
printf("</HTML>\n");
}