当前位置: 技术问答>linux和unix
请教CGI程序传递参数问题
来源: 互联网 发布时间:2016-07-10
本文导语: 我在开发板上移植好boa后,想测试CGI程序. 我的CGI程序: #include #include #include #include #include #define IOCTL_LED_ON 0 #define IOCTL_LED_OFF 1 #define MAX_ARG 150 int main(void) { unsigned int led_no;...
我在开发板上移植好boa后,想测试CGI程序.
我的CGI程序:
#include
#include
#include
#include
#include
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
#define MAX_ARG 150
int main(void)
{
unsigned int led_no;
int fd = -1;
char *buf = getenv("QUERY_STRING");
if (buf == NULL)
return -1;
char para1[MAX_ARG], para2[MAX_ARG];
char value1[MAX_ARG], value2[MAX_ARG];
char *p = strchr(buf, '&');
*p = '';
strcpy(para1, buf);
strcpy(para2, p + 1);
p = strchr(para1, '=');
*p = '';
strcpy(value1, p + 1);
p = strchr(para2, '=');
*p = '';
strcpy(value2, p + 1);
int num, status;
num = atoi(value1);
status = atoi(value2);
fd = open("/dev/mini2440_led", 0);
if (fd 3)
{
goto err;
}
ioctl(fd, status, num);
close(fd);
return 0;
err:
if (fd > 0)
close(fd);
return -1;
}
我网页中的 form
LED Control
This is my first web program
Input led number:
[color=#FF0000]NO.:
(Notice!! the number is: 1-6)
Status:[/color]
Notice!!(1: on, 0: off)
提交数据后, 参数应该是 "no=xx&status=xxx"
我在CGI程序中,用getenv("QUERY_STRING");获取参数,不知这样做对不对?
结果网页提交数据后出现以下错误:
502 Bad Gateway
The CGI was not CGI/1.1 compliant.
我CGI的makefile
CC = /usr/local/arm/3.4.1/bin/arm-linux-gcc
test_led : test_led.c
$(CC) test_led.c -o test_led
请问我这样做对不对,不对该怎么样改,才能通过网页传递参数给CGI程序?
我的CGI程序:
#include
#include
#include
#include
#include
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
#define MAX_ARG 150
int main(void)
{
unsigned int led_no;
int fd = -1;
char *buf = getenv("QUERY_STRING");
if (buf == NULL)
return -1;
char para1[MAX_ARG], para2[MAX_ARG];
char value1[MAX_ARG], value2[MAX_ARG];
char *p = strchr(buf, '&');
*p = '';
strcpy(para1, buf);
strcpy(para2, p + 1);
p = strchr(para1, '=');
*p = '';
strcpy(value1, p + 1);
p = strchr(para2, '=');
*p = '';
strcpy(value2, p + 1);
int num, status;
num = atoi(value1);
status = atoi(value2);
fd = open("/dev/mini2440_led", 0);
if (fd 3)
{
goto err;
}
ioctl(fd, status, num);
close(fd);
return 0;
err:
if (fd > 0)
close(fd);
return -1;
}
我网页中的 form
LED Control
This is my first web program
Input led number:
[color=#FF0000]NO.:
(Notice!! the number is: 1-6)
Status:[/color]
Notice!!(1: on, 0: off)
提交数据后, 参数应该是 "no=xx&status=xxx"
我在CGI程序中,用getenv("QUERY_STRING");获取参数,不知这样做对不对?
结果网页提交数据后出现以下错误:
502 Bad Gateway
The CGI was not CGI/1.1 compliant.
我CGI的makefile
CC = /usr/local/arm/3.4.1/bin/arm-linux-gcc
test_led : test_led.c
$(CC) test_led.c -o test_led
请问我这样做对不对,不对该怎么样改,才能通过网页传递参数给CGI程序?
|
传递参数简单,就是GET 或POST方法,参数以 name=value&的串传到你的web server这边。对于参数的处理看你web server的实现了,既然你是从环境变量或得,server这边就该吧http包解析出来的参数先保存到环境变量中,以待CGI程序处理。
|
目录是否不正确哦。。
|
可以用gdb跟一下,如果对协议不是很熟悉,光用眼看很难的