1996. 3. 18. 04:46
내발자국[동호회]
------------------------------------
제 목 : [9510]www가 기가막혀(13)
------------------------------------
3 간단한 CGI 프로그램 만들기
다음은 간단한 CGI 프로그램으로 사용자가 브라우저에서 finge
r, date, archie, calendar와 같은 UNIX 명령어를 고르면 서버에서
실제 수행하고 결과를 보여주는 CGI 프로그램을 만들려고 한다.
일단 그러한 CGI 프로그램을 부르는 HTML 문서는 다음과 같이
구성한다.
-----CGI1.HTML-------------------------------------
Form Example #1
This is a very simple fill-out form example for finger and dat
e.
----------------------------------------------------
보면 알 수 있듯이 이 HTML문서는 casaturn.kaist.ac.kr의 /cgi
-bin/finger(물론 /는 시스템 루트 디렉토리가 아니라 http 데몬의
설정 파일에 기술된 http의 설치 디렉토리를 의미하게 된다)라는
프로그램을 실행시키게 되는데 finger와 date는 다음과 같다.
-------FINGER--------------------------------------#
!/bin/sh
FINGER=/usr/ucb/finger
echo Content-type: text/html
echo
# /usr/ucb/finger가 있으면 이 프로그램을 실행시킨다.
if [ -x $FINGER ]; then
# 파라미터 없이 이 프로그램이 실행된 것이라면 뒤에
# EOM이 나오기 전까지의 문장들을 첨가한다.
if [ $# = 0 ]; then
cat << EOM
Finger Gateway
This is a gateway to "finger". Type a
user@host combination in your browser's search dialog.
제 목 : [9510]www가 기가막혀(13)
------------------------------------
3 간단한 CGI 프로그램 만들기
다음은 간단한 CGI 프로그램으로 사용자가 브라우저에서 finge
r, date, archie, calendar와 같은 UNIX 명령어를 고르면 서버에서
실제 수행하고 결과를 보여주는 CGI 프로그램을 만들려고 한다.
일단 그러한 CGI 프로그램을 부르는 HTML 문서는 다음과 같이
구성한다.
-----CGI1.HTML-------------------------------------
FInger and Date Example #1
This is a very simple fill-out form example for finger and dat
e.
----------------------------------------------------
보면 알 수 있듯이 이 HTML문서는 casaturn.kaist.ac.kr의 /cgi
-bin/finger(물론 /는 시스템 루트 디렉토리가 아니라 http 데몬의
설정 파일에 기술된 http의 설치 디렉토리를 의미하게 된다)라는
프로그램을 실행시키게 되는데 finger와 date는 다음과 같다.
-------FINGER--------------------------------------#
!/bin/sh
FINGER=/usr/ucb/finger
echo Content-type: text/html
echo
# /usr/ucb/finger가 있으면 이 프로그램을 실행시킨다.
if [ -x $FINGER ]; then
# 파라미터 없이 이 프로그램이 실행된 것이라면 뒤에
# EOM이 나오기 전까지의 문장들을 첨가한다.
if [ $# = 0 ]; then
cat << EOM
Finger Gateway
user@host combination in your browser's search dialog.
EOM
# 파라미터가 있었다면 그 사람의 이름을 finger하게 된다.
else
echo \
$FINGER "$*"
fi
# /usr/ucb/finger가 없으면 에러 메시지를 남기고 끝낸다.
else
echo Cannot find finger on this system.
fi
----------------------------------------------------
#!/bin/sh는 이 프로그램이 불리면 /bin/sh을 불러서 이 프로그
램을 실행하라는 의미이다. 다른 것은 위에서 설명한 finger와 거
의 내용이 같기 때문에 생략한다.
--------- DATE ------------------------------------
#!/bin/sh
DATE=/bin/date
echo Content-type: text/plain
echo
if [ -x $DATE ]; then
$DATE
else
echo Cannot find date command on this system.
fi
----------------------------------------------------
다음은 아치 서비스를 연결시켜 놓은 예이다. 다른 것과 동일하
기 때문에 설명을 생략한다. 역시 Calendar역시 설명을 생략한다.
----- ARCHIE ---------------------------------------
#!/bin/sh
ARCHIE=/usr/local/bin/archie
echo Content-type: text/html
echo
if [ -x $ARCHIE ]; then
if [ $# = 0 ]; then
cat << EOM
Archie Gateway
Archie Gateway
This is a gateway to archie. Type search query in your brows
er's search
dialog.
EOM
else
echo \
$ARCHIE "$*"
fi
else
echo Cannot find archie on this system.
fi
----------------------------------------------------
----- CALENDAR -----------------------------------
#!/bin/sh
CAL=/bin/cal
echo Content-type: text/html
echo
if [ -x $CAL ]; then
if [ $# = 0 ]; then
cat << EOM
Calendar
Calendar
To look up a calendar month, type the month
followed by a space then the year.
Example:3 1993
would give
the calendar for March 1993. EOM
else
echo \
$CAL $*
fi
else
echo Cannot find cal on this system.
fi
-------------------------------------------------
위의 cgi1.html을 수행시켜 보면 생각한 바와 같이 다음 <그림
16>과 같이 수행된다.
----------------------------------------------------
그림 파일명 : 14cgi1.gif 그림 파일명 : 14cgi1r.gif
----- <그림 16> cgi1.html 수행한 예 --------------------
그렇다면 /cgi-bin/post-query라는 프로그램은 어떤 식으로 생
겼을까 분석해 보자. 아래에서 보는 바와 같이 사용자가 입력한
문자열을 입력으로 받아서 <그림 2>에서 보는 바와 같이 결과를
만들어 주는 HTML파일을 출력하는 것을 볼 수 있다. 그러면 그
것을 받아서 브라우저는 다시 그려 주면 되는 것이다. post-quer
y는 두개의 파일post-query.c와 util.c로 구성이 되는데 컴파일하
기 위해서는
gcc -o post-query post-query.c util.c
와 같이하면 post-query가 만들어지고 cgi-bin 디렉토리 밑에 복
사해 두면 외부에서 접속할 수 있게 된다.
---- POST_QUERY.C ---------------------------------
#include
#include
#define MAX_ENTRIES 10000
typedef struct {
char *name;
char *val;
} entry;
char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url); void plustospace(char *str);
main(int argc, char *argv[]) {
entry entries[MAX_ENTRIES];
register int x,m=0;
int cl;
/* 우선 서버가 브라우저에게 보낼 때, 처음 시작은 다음과
같이,
Content-type이 text/html임을 알려준다.
*/
printf("Content-type: text/html%c%c",10,10);
/* 또 다른 입력 방법으로 GET이 있는데, 권장하지 않는다
*/
if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
printf("This script should be referenced with a METH
OD of POST.\n");
printf("If you don't understand this, see this ");
printf(" ware/Mosaic/Docs/fill
-out-forms/overview.html\">forms overview.%c",10);
exit(1);
}
if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-
form-urlencoded")) {
printf("This script can only be used to decode form re
sults. \n");
exit(1);
}
/* 브라우저가 사용자가 입력한 것을 보낸 것을 하나하나 체
크해서 entries[x].name에 저장해 둔다.
*/
cl = atoi(getenv("CONTENT_LENGTH"));
for(x=0;cl && (!feof(stdin));x++) {
m=x;
entries[x].val = fmakeword(stdin,'&',&cl);
plustospace(entries[x].val);
unescape_url(entries[x].val);
entries[x].name = makeword(entries[x].val,'=');
}
/* HTML로 사용자가 입력한 것들이 저장된 entries[x].name
을 브라우저에게 다시 보내 준다. */
printf("Query Results
");
printf("You submitted the following name/value pairs:
%c",10); printf("%c",10);
%c",10);
for(x=0; x <= m; x++)
printf("%s = %s
%c",entries[x].na
me, entries[x].val,10);
printf("
}
----------------------------------------------------
------- UTIL.C -------------------------------------
#include
#define LF 10
#define CR 13
void getword(char *word, char *line, char stop) {
int x = 0,y;
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++]);
}
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;
}
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;
}
}
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';
}
void plustospace(char *str) {
register int x;
for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}
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;
} 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;
}
}
void send_fd(FILE *f, FILE *fd) {
int num_chars=0;
char c;
while (1) {
c = fgetc(f);
if(feof(f))
return;
fputc(c,fd);
}
}
int ind(char *s, char c) {
register int x;
for(x=0;s[x];x++)
if(s[x] == c) return x; return -1;
}
void escape_shell_cmd(char *cmd) {
register int x,y,l;
l=strlen(cmd);
for(x=0;cmd[x];x++) {
if(ind("&;`'\"|*?~<>^()[]{}$\\",cmd[x]) != -1){
for(y=l+1;y>x;y--)
cmd[y] = cmd[y-1];
l++; /* length has been increased */
cmd[x] = '\\';
x++; /* skip the character */
}
} }
----------------------------------------------------
'내발자국[동호회]' 카테고리의 다른 글
[HTML] WWW이 기가막혀! (1)(2)(3)(4)(8) (0) | 1996.03.18 |
---|---|
[HTML] WWW이 기가 막혀! (13)-HTML문서(8) (0) | 1996.03.18 |
[HTML] WWW이 기가 막혀! (11)-HTML문서(6) (0) | 1996.03.18 |
[HTML] WWW이 기가 막혀! (10)-HTML문서(5) (0) | 1996.03.18 |
[HTML] WWW 이 기가 막혀! (9)-HTML문서(4) (0) | 1996.03.18 |