#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "json.h"
#define MAXLEN 1024
char* getcgidata(FILE* fp, char* requestmethod);
FILE *fp_t = NULL;
int main(void)
{
char * cgistr = NULL;
char * req_method = NULL;
fp_t = fopen("../data.txt", "w+");
printf( "Content-type: application/text;charset=utf-8\n\n" );
req_method = getenv("REQUEST_METHOD");
cgistr = getcgidata(stdin, req_method);
fprintf(fp_t, "%s\n", req_method);
fprintf(fp_t, "%s\n", cgistr);
if (!strcmp(req_method, "GET"))
fprintf(stdout,"you get param is %s","name:zhangzhen age:17 school:guangzhou sex:nan");
if (!strcmp(req_method, "POST"))
fprintf(stdout,"you post param is %s",cgistr);
}
char* getcgidata(FILE* fp, char* requestmethod)
{
char* input;
int len;
int size = MAXLEN;
int i = 0;
if (!strcmp(requestmethod, "GET"))
{
input = getenv("QUERY_STRING");
fprintf(fp_t, "%s\n", input);
return input;
}
else if (!strcmp(requestmethod, "POST"))
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char)*(size + 1));
fprintf(fp_t, "%d\n", len);
if (len == 0)
{
input[0] = '\0';
return input;
}
while(1)
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+1] = '\0';
return input;
}
--len;
if (feof(fp) || (!(len)))
{
i++;
input[i] = '\0';
return input;
}
i++;
}
}
return NULL;
}
c语言解析ajax的post、get的cgj程序
最新推荐文章于 2023-05-29 16:58:03 发布