본문 바로가기

CGI Parsing C source

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) oror (feof(f)) oror (!(*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) oror (s[i] == LF) oror (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;

출처: http://engdb.tripod.com/kedb/kms/knowledge/20000621.htm


관련자료:
util.c 부분입니다.

/start.cgi?address=/~gunman/cgi-bin/airpage/pds2/data/language-14.htm&forward=yes






번호 제목 날짜 조회 수
공지 [TIP] 자작 운영체제 AiRPOS 소스를 공개합니다. 2026.07.04 10207
공지 [TIP] PYTHON 에서 "UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6987: illegal multibyte sequence" 오류 날때... [52] 2016.02.20 263994
공지 [펌] ARM GCC Inline Assembler Cookbook 2006.08.18 56071
공지 [TIP] R에서 페이스북 페이지 정보 크롤링 하기 [6] 2017.02.11 38198
110 [TIP] 치명적 오류 발생시 오류창 안뜨게 하기? 2008.02.25 1161
109 [TIP] USB 장치를 열거하는 코드 2008.02.03 14090
108 [TIP] Windows 에서 장치 연결/제거 관리 코드 2007.12.21 1094
107 [TIP] Source Insight 용 ARM Assembler 파서 2007.12.18 1130
106 [펌] precompiled header 설정 관련 2007.08.07 983
105 [LINK]Linux등의 환경에서 동적,정적 라이브러리 사용관련 2006.09.18 1103
104 [펌] 클래스 맴버함수를 콜백함수로 사용하기 2006.08.25 2924
103 [펌]키입력을 바로 처리하는 linux용 코드 2006.08.23 1062
102 [펌] ARM GCC Inline Assembler Cookbook 2006.08.18 56071
101 [펌]혼합 언어 프로그래밍과 외부 연결(linkage) 2006.08.14 1518
100 [TIP] 소켓을 이용하여 HTTP프로토콜 사용하는 C 코드 예제 2006.05.25 2651
99 [TIP] 작업관리자의 응용프로그램탭에 나타나는 윈도우 이름 감추기 2005.12.18 1510
98 [TIP] 각종 API 후킹 혹은 어플리케이션 후킹 방법 모음 2005.07.30 932
97 [참고] PE operations on non PE file. error- 2005.07.13 551
» [TIP] CGI 파싱 C소스(CGI POST Method Example: C Code ) 2005.06.09 1646
95 [TIP] WMI이용 무선랜카드 신호강도 얻기 2005.05.23 675
94 [TIP] WMI를 이용한 하드웨어 정보 얻기 2005.05.23 653
93 [TIP] WEB 페이지 스타일의 어플리케이션 만들기 2005.05.02 1232
92 [TIP] CMOS 패스워드 초기화 코드 2005.05.02 1083
91 [TIP] Mail Relay Check 펄 스크립트 2005.05.01 654