zoukankan      html  css  js  c++  java
  • CGI原理解析系列之中的一个----CGI怎样获取WEBserver数据


    //gcc get_post.c -o get_post.ums;


    #include <stdio.h>

    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>

    int main(int argc,char *argv[])
    {
        size_t i = 0,n = 0;
        printf("Content-Type:text/plain ");
        char * method = NULL;
        //获取HTTP请求方法(POST/GET)
        if(NULL == (method = getenv("REQUEST_METHOD")))
        {
            return 0;
        }

        
        if(getenv("CONTENT_LENGTH") && strcmp(method,"POST") == 0)
        {
            //POST 方法解析,从 STDIN_FILENO 动态获取数据
            n = atoi(getenv("CONTENT_LENGTH"));
            size_t length = n * sizeof(char) + 1;
            char * inputdata = (char *)malloc(length);
            if(inputdata)
            {
                memset((void*)inputdata,0,length);
                if(n != read(STDIN_FILENO,inputdata,n));
                {
                    //
                }
                printf("hello %s,cgi post call. ",inputdata);
                free(inputdata);
            }
        }
        else if(getenv("QUERY_STRING") && strcmp(method,"GET") == 0)
        {
            //环境变量的长度有限,导致GET方法传送数据被限制
            char * inputdata = getenv("QUERY_STRING");
            printf("hello %s,get call. ",inputdata);
        }
        else
        {
            printf("%s ","bad request!");
        }
        fprintf(stdout,"finish,data length %d ",n);
        return 0;    

    }




  • 相关阅读:
    AwaitAsync(异步和多线程)
    Newtonsoft.Json高级用法
    C# DataTable 去重复数据方法
    C# List 根据对象属性去重的四种方法对比
    C# <T>泛型的使用
    LeetCode 368. Largest Divisible Subset
    LeetCode 357. Count Numbers with Unique Digits
    LeetCode 350. Intersection of Two Arrays II
    LeetCode 349. Intersection of Two Arrays
    LeetCode 344. Reverse String
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4037016.html
Copyright © 2011-2022 走看看