Boa是一种非常小巧的Web服务器,其可执行代码只有大约60KB左右。作为一种单任务Web服务器,Boa只能依次完成用户的请求,而不会fork出新的进程来处理并发连接请求。但Boa支持CGI,能够为CGI程序fork出一个进程来执行。Boa的设计目标是速度和安全。
一、Boa在嵌入式Linux的开发中非常广泛,网上也有许多关于Boa移植的文章,这里不再详细说明,只介绍boa.conf中配置时需要注意的点,具体参照http://blog.youkuaiyun.com/manchestermi/article/details/50826129
我用的飞凌OKMX6UL-C开发板,boa.conf位于/etc/boa/,同时在、usr/local/boa/下也存在,boa的执行文件在 /sbin , cgi-bin目录定向在 /var/www/cgi-bin
1.CGIPath /bin:/usr/bin:/usr/local/bin:/sbin 这条语句定义CGI程序的环境变量,一定要把boa执行文件所在的目录(我的在/sbin)添加进来,不然涉及 post、get数据交互没法运行。
2.ScriptAlias /cgi-bin/ /var/www/cgi-bin/ CGI程序的存放目录,自己编写的CGI程序要放在这里面。
二、嵌入式Linux的CGI程序的一般用C语言实现,这个用到了 cgic 库。cgic库下有专门的测试程序 cgictest.c,在arm-linux-gcc下编译后,放到前面的/var/www/cgi-bin/ 下,通过浏览器就可以访问了,结合cgic.html的说明,可以比较容易的了解cgic库的各种用法。自己编写cgi程序,可以直接以cgictest.c为模板,在int cgiMain() {}中编写自己的实现程序即可。
三、通过网页上传文件到 开发板
1.在网页中添加 file表单,java script
var vv='<form action="" method="post" enctype="multipart/form-data" name="fileupForm" id="fileupForm" onSubmit="return CheckFile()">'+
'<input name="upfile" type="file" id="upfile" size="250" value=""/>'+
'<input name="subutton" type="submit" id="subutton" value="上传" /></form>';
$("#contents").html(vv);
2.编写cgi程序,以cgictest.c为模板,修改 file()函数,从cgic存的临时文件(cgic将收到的上传文件存为名称cgicxxxxxx的临时文件,放在/tmp下)读出数据,并写入新的文件,
void File()
{
cgiFilePtr file;
int fd;
mode_t mode;
char name[1024];
char contentType[1024];
char buffer[1024];
int size;
int got;
if (cgiFormFileName("upfile", name, sizeof(name)) != cgiFormSuccess) {
printf("<p>No file was uploaded.<p>\n");
return;
}
fprintf(cgiOut, "The filename submitted was: ");
cgiHtmlEscape(name);
fprintf(cgiOut, "<p>\n");
cgiFormFileSize("upfile", &size);
fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
cgiFormFileContentType("upfile", contentType, sizeof(contentType));
fprintf(cgiOut, "The alleged content type of the file was: ");
cgiHtmlEscape(contentType);
fprintf(cgiOut, "<p>\n");
fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.<p>\n");
fprintf(cgiOut, "The file's contents are shown here:<p>\n");
if (cgiFormFileOpen("upfile", &file) != cgiFormSuccess) {
fprintf(cgiOut, "Could not open the file.<p>\n");
return;
}
//打开或创建文件在当前目录,即/var/www/cgi-bin
//注意文件存取权限,否如会出现文件无法访问
mode=S_IRWXU|S_IRGRP|S_IROTH; //
fd = open(name,O_RDWR|O_CREAT|O_TRUNC,mode);
if(fd < 0){
fprintf(cgiOut, "<p>Could not create the file,error:%d<p>\n",fd);
}
fprintf(cgiOut, "<pre>\n");
while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
cgiFormSuccess)
{
//写入文件
if(got>0){
write(fd,buffer,got);
}
cgiHtmlEscapeData(buffer, got);
}
fprintf(cgiOut, "</pre>\n");
cgiFormFileClose(file);
close(fd);
}
3.编译cgi程序,得到cgiproc.cgi,放入 cgi-bin文件夹下,进行文件上传操作,中间出现如下几个问题
3.1 cgic库会将上传的文件在/tmp下存储临时文件 cgicxxxxxx,如图
我为了清除临时文件,直接 rm -r tmp,然后重新 mkdir tmp,但是出现post错误,http error 502,不得已重新烧了整个文件系统,才恢复。不知道 里面的 ltib 文件夹是否有影响,待考证?
3.2 通过网页上传文件,可以看到临时文件成功,但是网页上还是报错 http error 502,后来发现是cgi程序没有在当前目录创建文件的权限,将/var/www/cgi-bin改为 777 权限,cgiproc.cgi也为 777 权限,这时上传文件成功,并且在当前文件夹下出现了上传的同名文件。所以 这个一定要注意 权限问题。