Linux下嵌入式Web服务器BOA和CGI编程开发
一、环境搭建
二、相关配置(部分)
三、调试运行
四、测试源码参考
五、常见错误
六、扩展(CCGI,SQLite)
# 一、环境搭建
操作系统:Ubuntu12.04 LTS
boa下载地址(但是我找不到...): http://www.boa.org/
我是其他网站找到的资源,但是忘了网址了,所以我直接上云盘资源
链接: https://pan.baidu.com/s/1_SpR9MDcmSB8jpDm76fw6A 提取码: hb1e
可以参考:Ubuntu下boa服务器的配置与搭建
cgi:直接终端安装 sudo apt-get install apache2
可以参考: ubuntu 下搭建cgi环境
# 二、相关配置(部分)
boa我的配置:/etc/boa$ sudo vi boa.conf
View Code
cgi我的配置:sudo vi /etc/apache2/sites-enabled/000-default
View Code
# 三、调试运行
都配好后运行 sudo ./boa
访问 http://127.0.0.1:端口号
我的就是 http://127.0.0.1:88
gcc -o test.cgi test.c 编译生成 test.cgi
把cgi文件拷贝到 cgi-bin 下
网址就是 http://localhost:88/cgi-bin/test.cgi
# 四、测试源码参考
下面提供几个测试代码,转自其他大佬,不过我找不到网址了。
1、
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
long m,n;
printf("Content-type:text/html\n\n");
printf("<HTML>");
printf("<HEAD>");
printf("<TITLE>multi</TITLE>");
printf("</HEAD>");
printf("<BODY>");
printf("<H2 ALIGN=\"center\">multi control</H2>");
printf("<FORM METHOD=\"GET\" ACTION=\"test1.cgi\">");
printf("<P>Direction:<INPUT TYPE=\"text\" NAME=\"m\" VALUE=\"\" size=\"18\">");
printf("<P>Step Number:<INPUT TYPE=\"text\" NAME=\"n\" VALUE=\"\" size=\"17\">");
printf("<P ALIGN=\"left\">");
printf("<INPUT TYPE=\"SUBMIT\" VALUE=\"Submit\">");
printf("<INPUT TYPE=\"RESET\" VALUE=\"Reset\">");
printf("</P>");
printf("</FORM>");
printf("</BODY>");
printf("</HTML>");
data=getenv("QUERY_STRING");
if(!data)
printf("<P>get no datas and it's wrong.");
else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
printf("<P>the input must be numbers");
else
printf("<P>%ld and %ld multi= %ld",m,n,m*n);
return 0;
}
2、
/var/www 下的pass.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>用户登陆验证</title> </head> <body> <form name="form1" action="/cgi-bin/pass.cgi" method="POST"> <table align="center"> <tr><td align="center" colspan="2"></td></tr> <tr> <td align="right">用户名</td> <td><input type="text" name="Username"></td> </tr> <tr> <td align="right">密 码</td> <td><input type="password" name="Password"></td> </tr> <tr> <td><input type="submit" value="登 录"></td> <td><input type="reset" value="取 消"></td> </tr> </table> </form> </body> </html>
/var/www/cgi-bin 下的pass.c
/*=====================================================================
cgi例子
=====================================================================*/
//pass.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getcgidata(FILE* fp, char* requestmethod);
int main()
{
char *input;
char *req_method;
char name[64];
char pass[64];
int i = 0;
int j = 0;
// printf("Content-type: text/plain; charset=iso-8859-1\n\n");
printf("Content-type: text/html\n\n");
printf("The following is query reuslt:<br><br>");
req_method = getenv("REQUEST_METHOD");
input = getcgidata(stdin, req_method);
// 我们获取的input字符串可能像如下的形式
// Username="admin"&Password="aaaaa"
// 其中"Username="和"&Password="都是固定的
// 而"admin"和"aaaaa"都是变化的,也是我们要获取的
// 前面9个字符是UserName=
// 在"UserName="和"&"之间的是我们要取出来的用户名
for ( i = 9; i < (int)strlen(input); i++ )
{
if ( input[i] == '&' )
{
name[j] = '\0';
break;
}
name[j++] = input[i];
}
// 前面9个字符 + "&Password="10个字符 + Username的字符数
// 是我们不要的,故省略掉,不拷贝
for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )
{
pass[j++] = input[i];
}
pass[j] = '\0';
printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass);
return 0;
}
char* getcgidata(FILE* fp, char* requestmethod)
{
char* input;
int len;
int size = 1024;
int i = 0;
if (!strcmp(requestmethod, "GET"))
{
input = getenv("QUERY_STRING");
return input;
}
else if (!strcmp(requestmethod, "POST"))
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char)*(size + 1));
if (len == 0)
{
input[0] = '\0';
return input;
}
while(1)
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+1] = '\0';
return input;
}
--len;
if (feof(fp) || (!(len)))
{
i++;
input[i] = '\0';
return input;
}
i++;
}
}
return NULL;
}
输入数据点击“登录”
自动跳转到 /cgi-bin/pass.cgi ,获取到数据并打印
# 五、常见错误
你的配置会影响网址,502什么错误也是配置或权限有问题导致的。
如:
502 Bad Gateway
The CGI was not CGI/1.1 compliant.
cgi_header: unable to find LFLF
1.可能是网址打错了(路径是否和配置文件对应)
2.配置有问题
3.权限没给足 chmod 777 test.cgi
# 六、扩展(CCGI,SQLite)
CGIC的主站点: http://www.boutell.com/cgic/
SQLite官网:http://www.sqlite.org/
配置参考大佬博客:项目实战
我在调试中遇到的问题也在大佬的博客下面做了 评论 ,如果大家碰到问题可以参考一下。
补充:编译时会出错,使用
gcc -o config.cgi config.c sqlite3.c cgic.c -lsqlite3 -lpthread -ldl
![涉及文件]
![html效果]
![cgi页面]
数据已写入数据库,可使用以下命令
sqlite3 person.db .table select * from person;







浙公网安备 33010602011771号