#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[] )
{
char *UserInput = NULL; // index the user input data.
char *request_method = NULL; //index the html transfer type.
int data_len = 0; // index the input data's length.
printf("Content-type:text/html;charset=utf-8%d%d", 10, 10); //response header.
printf("\n");
// HTML page header.
printf("<html><head>");
printf("<title>%s</title></head>", "CGI Test");
printf("<body>");
if( ! getenv("REQUEST_METHOD") )
{
printf("No client request from user !\n");
return -1;
}
request_method = getenv("REQUEST_METHOD"); // trans-type.
// trans-type : GET
if( ! strcmp( request_method, "GET" ) )
{
if( getenv( "QUERY_STRING" ) )
{
UserInput = getenv( "QUERY_STRING" );
}
data_len = strlen( UserInput );
if( NULL == UserInput || 0 == data_len )
{
printf( "There's no input data !\n" );
return -1;
}
UserInput[data_len] = '\0';
printf("method = %s, data_len = %d<br>", request_method, data_len);
printf("Form data: %s", UserInput);
}
// trans-type : POST
else if( ! strcmp( request_method, "POST" ) )
{
if( getenv( "CONTENT_LENGTH" ) ) // the real length of input data.
data_len = atoi( getenv( "CONTENT_LENGTH" ) );
if( 0 == data_len )
{
printf( "There's no input data !\n" );
return -1;
}
UserInput = (char *)malloc( sizeof(char) * (data_len + 1) );
if( NULL == UserInput )
{
perror( "malloc for 'UserInput'" );
UserInput = NULL;
return -1;
}
if( fread( UserInput, sizeof(char), data_len, stdin ) <= 0 )
{
perror("fread input data");
return -1;
}
UserInput[data_len] = '\0';
printf("method = %s, data_len = %d<br>", request_method, data_len);
printf("<br>Form data:<br> %s", UserInput);
free( UserInput );
UserInput = NULL;
}
printf("</body></html>");
fflush( stdout );
return 0;
}
一个简单的浏览器端CGI调用
4星 · 超过85%的资源 需积分: 46 63 浏览量
2013-06-15
12:42:54
上传
评论 3
收藏 2KB ZIP 举报
浪子--赟
- 粉丝: 115
- 资源: 14
最新资源
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
- 修复Windows 10 LTSC 2021资源占用率高
- Hash工具,小巧绿色hash校验工具,免费hash工具
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 鸭子开车记(儿童绘本)
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈