总结CGIC

参考资料:

非常不错的总结,推荐
https://blog.51cto.com/9291927/1828278

这里也有一篇不错的总结,推荐
https://www.cnblogs.com/programmer-wfq/p/5582114.html

https://blog.csdn.net/a642960662/article/details/66473794?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-6&spm=1001.2101.3001.4242

https://blog.csdn.net/yand789/article/details/38111667

https://www.iqiyi.com/v_19rr7ydm10.html#curid=719231400_0b8a33f235040f021908837f6c521bf0

https://www.xuebuyuan.com/1864640.html

关于cgi中如何获取get与post的参数信息:

#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>
char InputBuffer[4096];
 
int DecodeAndProcessData(char *input);    /*具体译码和处理数据,该函数代码略*/
 
int main(int argc, char*argv[])
{
    
    
        int   ContentLength;   /*数据长度*/
        int   x;
        int   i;
        char   *p;
        char   *pRequestMethod;     /*   METHOD属性值   */
        setvbuf(stdin,NULL,_IONBF,0);     /*关闭stdin的缓冲*/
        printf("Content-type:text/html; charset=UTF-8\n\n");     /*从stdout中输出,告诉Web服务器返回的信息类型*/
        printf("\n");                                           /*插入一个空行,结束头部信息*/
        printf("<p>hello test</p>");
        /*   从环境变量REQUEST_METHOD中得到METHOD属性值   */
 
        pRequestMethod = getenv("REQUEST_METHOD");
        if(pRequestMethod==NULL)
        {
    
    
                printf("<p>request = null</p>");
                return   0;
        }
        if (strcasecmp(pRequestMethod,"POST")==0)
        {
    
    
                printf("<p>OK the method is POST!\n</p>");
                p = getenv("CONTENT_LENGTH");     //从环境变量CONTENT_LENGTH中得到数据长度    
                if (p!=NULL)
                {
    
    
                        ContentLength = atoi(p);
                }
                else
                {
    
    
                        ContentLength = 0;
                }
                if (ContentLength > sizeof(InputBuffer)-1)   {
    
    
                        ContentLength = sizeof (InputBuffer) - 1;
                }
 
                i   =   0;
                while (i < ContentLength)
                {
    
                             //从stdin中得到Form数据    
                        x  = fgetc(stdin);
                        if (x==EOF)
                                break;
                        InputBuffer[i++] = x;
                }
                InputBuffer[i] = '\0';
                ContentLength   =   i;
                DecodeAndProcessData(InputBuffer);                 //具体译码和处理数据,该函数代码略    
        }
        else if (strcasecmp(pRequestMethod,"GET")==0)
        {
    
    
                printf("<p>OK the method is GET!\n</p>");
                p = getenv("QUERY_STRING");     //从环境变量QUERY_STRING中得到Form数据    
                if   (p!=NULL)
                {
    
    
                        strncpy(InputBuffer,p,sizeof(InputBuffer));
                        DecodeAndProcessData(InputBuffer);    //具体译码和处理数据,该函数代码略    
                }
        }
        printf("<HEAD><TITLE>Submitted OK</TITLE></HEAD>\n");//从stdout中输出返回信息    
        printf("<BODY>The information you supplied has been accepted.</BODY>\n");
 
        return   0;
}
 
 
int DecodeAndProcessData(char *input)    //具体译码和处理数据   
{
    
    
        printf("POST内容:%s",input);
        // 补充具体操作
        return 0;
}
 

其中GET请求可以通过
cgic库中
char *content;
content=getenv(“QUERY_STRING”);
printf("%s",cgiQueryString);
的方式直接获取
POST的参数须解析 stdin


传递参数校验

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "cgic.h"
#include "cJSON.h"
#define LABEL_LEN 32

uint32_t rwg_get_request_params(char *key,char *value,uint32_t len){
    
    
    cgiFormResultType cgi_form_result;
    cgi_form_result=cgiFormString(key,value,len);
    return cgi_form_result;
}
int cgiMain(){
    
    
    cgiHeaderContentType("text/html");
    cgiFormResultType cgi_form_result;
    // char label[LABEL_LEN];
    // cgi_form_result=cgiFormString("label",label,LABEL_LEN);
    // if (cgi_form_result == cgiFormSuccess)
    // {
    
    
    //     printf("Parameters received successfully:%s",label);
    // }
    // else
    // {
    
    
    //     printf("Parameter reception failed,identification code:%d",cgi_form_result);
    // }
    char *label_key_test="label";
    char label_value_test[LABEL_LEN];
    cgi_form_result=rwg_get_request_params(label_key_test,label_value_test,LABEL_LEN);
    printf("rwg_get_request_params  result :%d\n",cgi_form_result);
    if (cgi_form_result == cgiFormSuccess)
    {
    
    
        printf("Parameters received successfully:%s\n",label_value_test);
    }
    else
    {
    
    
        printf("Parameter reception failed,identification code:%d\n",cgi_form_result);
    }
}

Guess you like

Origin blog.csdn.net/lzl980111/article/details/111469548