#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (1024 * 10)
int get_http_request(char* query)
{
//1.获取到方法
char* method = getenv("REQUEST_METHOD");
if (method == NULL){
fprintf(stderr, "REQUEST_METHOD failed\n");
return -1;
}
//2.GET方法获得query_string
if (strcmp(method, "GET") == 0){
char* query_string = getenv("QUERY_STRING");
if (query == NULL){
fprintf(stderr, "QUERY_STRING failed\n");
return -1;
}
strcpy(query, query_string);
}
//3.POST方法获得centent_length和body
return 0;
}
int main()
{
//1.基于CGI协议获取到需要的参数
char query[SIZE] = {0};
int ret = get_http_request(query);
if (ret < 0){
return -1;
}
//2.根据业务逻辑,得到需要的结果
float a, b;
sscanf(query, "a=%f&b=%f\n", &a, &b);
float result = a + b;
//3.将结果构造为HTML,写回到标准输入中
printf("<html><head><meta charset = \"utf-8\"></head><body><h1>这两个数的和为:%f</h1><a href=\"http://39.106.164.95:9000/cal/index.html\">返回上一页</a></body></html>", result);
return 0;
}