加法的CGI程序

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章