CGI原理解析系列之一----CGI如何擷取WEB伺服器資料

來源:互聯網
上載者:User

標籤:linux   c   web伺服器   cgi   gcc   


//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\n\n");
    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.\n",inputdata);
            free(inputdata);
        }
    }
    else if(getenv("QUERY_STRING") && strcmp(method,"GET") == 0)
    {
        //環境變數的長度有限,導致GET方法傳送資料被限制
        char * inputdata = getenv("QUERY_STRING");
        printf("hello %s,get call.\n",inputdata);
    }
    else
    {
        printf("%s\n","bad request!");
    }
    fprintf(stdout,"finish,data length %d\n",n);
    return 0;    

}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.