//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;
}