SylixOS移植Boa服务器

来源:互联网 发布:pc端微信多开软件 编辑:程序博客网 时间:2024/06/05 20:54

1. Boa简介

Boa是一种非常小巧的Web服务器,其可执行代码只有大约60KB左右。作为一种单任务Web服务器,Boa只能依次完成用户的请求,而不会fork出新的进程来处理并发连接请求。但Boa支持CGI,能够为CGI程序fork出一个进程来执行。Boa的设计目标是速度和安全,适合于嵌入式系统的单任务的http服务器,源代码开放、性能高。

下面给大家介绍一下Boa服务器在SylixOS上移植的具体操作步骤,希望能够有帮助。

2. SylixOS Boa移植:

Boa采用服务器模型,因此需要编译出服务器的可执行程序。

2.1 下载Boa源码

在官网下载第三方中间件的资源,Boa的官方网站为:http://www.boa.org/。本文中使用的版本是boa-0.94.13,下载后解压文件,文件目录如图 2-1所示。

172612_8gzq_2859455.png

图 2-1 Boa解压后的文件

2.2 服务器工程创建

在Real-Evo IDE中创建boa_sylixos应用工程,删除工程中src目录下的boa_sylixos.c文件,导入源码中的src目录下的一系列相关.c和.h文件。导入完成后的工程文件如图 2-2所示。


图 2-2 工程文件

2.3 修改源码文件

具体源码文件修改,详见《移植boa服务器过程文档》。

2.4 修改boa.conf配置文件

在boa-0.94.13目录下已有一个示例boa.conf,可以在其基础上进行修改。如下:

(1)Group的修改

修改:Group nogroup

为 :Group 0

(2)user的修改

修改:User nobody

为 :User 0

(3)ScriptAlias的修改

修改:ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

为 :ScriptAlias /cgi-bin/ /var/www/cgi-bin/

(5)DoucmentRoot的修改

DoucmentRoot /var/www

(6)ServerName的设置

修改:#ServerName www.your.org.here

为 :ServerName www.your.org.here

否则会出现错误“gethostbyname::No such file or directory”

(7)AccessLog修改

修改:AccessLog /var/log/boa/access_log

为:#AccessLog /var/log/boa/access_log

否则会出现错误提示:“unable to dup2 the error log: Bad file descriptor”

2.5 编译生成boa_sylixos可执行文件

编译生成可执行文件boa_sylixos,并将之上传至目标机/etc/boa目录下;

2.6 拷贝mime.types文件

将linux /etc目录下的mime.types文件拷贝到目标机/etc目录下;(mime.types文件用来指明不同文件扩展名对应的MIME类型,一般可以直接从Linux主机上拷贝一个,大部分也都是在主机的/etc目录下。)

2.7 新建工程boa_cgi_child_func

目的:实现Boa源码中的fork函数功能

1) 创建app工程,修改boa_cgi_child_func.c文件,加入如下代码:

#include#include#define NI_MAXHOST 20#define CGI_ENV_MAX 50#define SOCKETBUF_SIZE 8192#define MAX_HEADER_LENGTH 1024#define CLIENT_STREAM_SIZE SOCKETBUF_SIZE#define BUFFER_SIZE CLIENT_STREAM_SIZEstruct mmap_entry {dev_t dev;ino_t ino;char *mmap;int use_count;size_t len;};struct request { /* pending requests */int fd; /* client's socket fd */int status; /* see #defines.h */time_t time_last; /* time of last succ. op. */char *pathname; /* pathname of requested file */int simple; /* simple request? */int keepalive; /* keepalive status */int kacount; /* keepalive count */int data_fd; /* fd of data */unsigned long filesize; /* filesize */unsigned long filepos; /* position in file */char *data_mem; /* mmapped/malloced char array */int method; /* M_GET, M_POST, etc. */char *logline; /* line to log file */char *header_line; /* beginning of un or incompletely processed header line */char *header_end; /* last known end of header, or end of processed data */int parse_pos; /* how much have we parsed */int client_stream_pos; /* how much have we read... */int buffer_start; /* where the buffer starts */int buffer_end; /* where the buffer ends */char *http_version; /* HTTP/?.? of req */int response_status; /* R_NOT_FOUND etc. */char *if_modified_since; /* If-Modified-Since */time_t last_modified; /* Last-modified: */char local_ip_addr[NI_MAXHOST]; /* for virtualhost *//* CGI vars */int remote_port; /* could be used for ident */char remote_ip_addr[NI_MAXHOST]; /* after inet_ntoa */int is_cgi; /* true if CGI/NPH */int cgi_status;int cgi_env_index; /* index into array *//* Agent and referer for logfiles */char *header_user_agent;char *header_referer;int post_data_fd; /* fd for post data tmpfile */char *path_info; /* env variable */char *path_translated; /* env variable */char *script_name; /* env variable */char *query_string; /* env variable */char *content_type; /* env variable */char *content_length; /* env variable */struct mmap_entry *mmap_entry_var;struct request *next; /* next */struct request *prev; /* previous *//* everything below this line is kept regardless */char buffer[BUFFER_SIZE + 1]; /* generic I/O buffer */char request_uri[MAX_HEADER_LENGTH + 1]; /* uri */char client_stream[CLIENT_STREAM_SIZE]; /* data from client - fit or be hosed */char *cgi_env[CGI_ENV_MAX + 4]; /* CGI environment */#ifdef ACCEPT_ONchar accept[MAX_ACCEPT_LENGTH]; /* Accept: fields */#endif};typedef struct request request;int main (int argc, char **argv){void (*child_func)(request *req,int *pipes,int use_pipes);request *req;int *pipes;int use_pipes;child_func = (void *) atoi(argv[0]);req = (request *)atoi(argv[1]);pipes = (int *) atoi(argv[2]);use_pipes = *((int *) atoi(argv[3]));child_func(req,pipes,use_pipes);return (0);}


2.8 测试Boa服务器功能2) 编译生成boa_cgi_child_func可执行文件,利用FTP工具上传到目标机/etc/boa目录下。

2.8.1 不包含cgic库的CGI程序

1) index.html 网页

<html><head><title>CGI登陆</title></head><body><form name="login" action="./cgi-bin/login.cgi">姓名:<input type="text" name="name" /><br/>密码:<input type="password" name="pwd" /><br/>确定:<input type="submit" value="登陆" /></form></body></html>

2) test.c CGI测试程序代码

#include<stdio.h>#include<stdlib.h>int main(){char *date;char name[50],pwd[20];printf("content-type:text/html;charset=gb2312\n\n");printf("<TITLE>登陆结果</TITLE>");printf("<H3>登陆结果</h3>");date=getenv("QUERY_STRING");if(date==NULL)printf("<p>错误:数据没有被输入或数据传输发生错误</p>");else{sscanf(date,"name=%[^&]&pwd=%s",name,pwd);printf("<p>name=%s</p>",name);printf("<p>pwd=%s</p>",pwd);printf("%s",date);}return 0;}

3) 操作流程

a) 将index.html拷贝到先前我们创建的/var/www目录下;

b) 在IDE中新建boa_test应用工程,生成测试CGI程序:boa_test;

c) 将boa_test拷贝到/var/www/cgi-bin目录下

d) 执行./boa_sylixos

e) 在浏览器地址栏输入http://192.168.7.32/index.html,产生如图 2-3所示。

172733_Lz9s_2859455.png

图 2-3 index.html登陆界面

f) 在姓名和密码栏分别输入相应值,点击登陆按钮,进入如图 24所示界面。


图 2-4 CGI响应程序执行

2.8.2 包含cgic库的CGI程序

1) 操作流程

g) 从CGI官网(http://www.boutell.com/cgic/)上下载cgic库文件;

h) 在RealEvo IDE中创建boa_cgictest应用工程;

i) 把cgic.h、cgic.c、cgictest.c 拷贝到工程boa_cgictest源码文件所在的目录src下,然后编译生成boa_cgictest文件,并将之上传到目标机/var/www/cgi-bin目录下;

j) 执行./boa_sylixos

k) 在浏览器地址栏输入http://192.168.7.32/cgi-bin/boa_cgictest,效果如图 25所示。

172809_ZUFw_2859455.png

图 25 CGI响应程序执行

可以看到一个网页被展示出来。这样,包含cgic库的的CGI程序就运行起来了。

3. 参考资料

1) http://blog.chinaunix.net/uid-20620288-id-3139686.html

2) http://blog.csdn.net/manchestermi/article/details/50826129

原创粉丝点击