荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: icefire (懒懒的心懒懒的我), 信区: Program
标  题: [转寄] [转载] 范文cgi:得到CGI的POST输入参数: cgiparse.c
发信站: BBS 荔园晨风站 (Sun Nov 22 19:16:09 1998), 站内信件

【 以下文字转载自 icefire 的信箱 】
【 原文由 icefire.bbs@melon.gznet.edu.cn 所发表 】
发信人: italina (欣儿 — 忙于论文), 信区: CGI
标  题: [转载] 范文cgi:得到CGI的POST输入参数: cgiparse.c
发信站: 华南网木棉站 (Mon Nov 16 22:50:16 1998), 站内信件

/****************************************/
/* cgiparse.c, arranged by digger       */
/****************************************/
/* this module is for POST method only  */
/* CGI_get_inputs() parse the string    */
/* read from stdin and store the name   */
/* and value in globle structure array  */
/* "inputs", the stucture named "input" */
/* is defined in cgiparse.h             */
/* CGI_end_inputs() free the space that */
/* allocate for "inputs"                */
/****************************************/
/* you need following statements in ur  */
/* calling module:                      */
/*      #include "cgiparse.h"           */
/*      extern input inputs[];          */
/****************************************/

#include "cgiparse.h"
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 64
#define MAXPARM 10
#define FALSE   -1
#define TRUE    0

input   inputs[MAXPARM];
int     parmnum = 0;

char * ReadStdin(FILE * f, char stop, int * len) /* stop always be '&' */
{
        int     wsize;
        char *  word;
        int     x;
        wsize = MAXSIZE;
        x = 0;
        word = (char *)malloc(MAXSIZE +1);
        while (1) {
                word[x] = (char)fgetc(f);
                if (x == wsize) {
                        wsize += 64;
                        word = (char *)realloc(word, wsize+1);
                        word[x + 1] = '\0';
                }
                --(*len);
                if ((word[x] == stop) || feof(f) || (!(*len))) {
                        if (word[x] != stop) x++;
                        word[x]= '\0';
                        return word;
                }
                ++x;
        }
}

void AddToSpace(char * str)
{
        int i;
        for (i = 0;i < str[i];i++) {
                if (str[i] == '+')
                        str[i] = ' ';
        }
}

char ConvertToHex(char * change)
{
        char hexdigit;
        hexdigit = (change[0] >= 'A' ? ((change[0] & 0xdf) - 'A') + 10:
                (change[0] - '0'));
        hexdigit *= 16;
        hexdigit += (change[1] >= 'A' ? ((change[1] & 0xdf) - 'A') + 10:
                (change[1] - '0'));
        return(hexdigit);
}

void Convert(char * data)
{
        int i, j;
        for (i = 0,j = 0;data[j];++i,++j) {
                if ((data[i] = data[j]) == '%') {
                        data[i] = ConvertToHex(&data[j + 1]);
                        j += 2;
                }
        }
}

char * ReadData(char * line, char stop) /* stop always be '=' */
{
        int i, j;
        char * word = (char *)malloc(strlen(line) + 1);
        for (i = 0;line[i] && line[i] != stop;i++)
                word[i] = line[i];
        word[i] = '\0';
        if (line[i]) ++i;
        j = 0;
        while(line[j++] = line[i++]);
        return word;
}

int CGI_get_inputs( )
{
        int len;
        int i;
        parmnum = 0;
        len = atoi(getenv("CONTENT_LENGTH"));
        for (i = 0;len && !feof(stdin) && i < MAXPARM;i ++) {
                inputs[i].val = ReadStdin(stdin, '&', &len);
                AddToSpace(inputs[i].val);
                Convert(inputs[i].val);
                inputs[i].name = ReadData(inputs[i].val, '=');
                parmnum ++;
        }
        return (parmnum);
}

void CGI_end_inputs( )
{
        int i;
        for (i = 0;i < parmnum;i ++) {
                free(inputs[i].name);
                inputs[i].name = NULL;
                free(inputs[i].val);
                inputs[i].val = NULL;
        }
}
--
※ 转载:.华南网木棉站 bbs.gznet.edu.cn.[FROM: 202.38.214.40]
--
※ 转寄:.华南网木棉站 bbs.gznet.edu.cn.[FROM: linux.szu.edu.cn]
--
※ 转载:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.0.22]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店