HTTP协议实用-加法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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值