boa搭建Web服务器详细教程

合集下载

ARM嵌入式Web服务器Boa的软、硬件设计 毕业设计

ARM嵌入式Web服务器Boa的软、硬件设计 毕业设计

摘要论文主要介绍了基于ARM9的嵌入式Web服务器Boa的软、硬件设计及其实现,其中硬件部分的核心是三星的S3C2410X为处理器。

最后成果形式为可以远程访问的WebServer嵌入式服务器。

Boa是一款单任务的HTTP服务器。

与其他传统的Web服务器不同的是当有连接请求到来时,它并不为每个连接单独创建进程, 也不通过复制自身进程来处理多链接。

而是通过建立HTTP请求列表来处理多路HTTP连接请求。

同时它只为CGI程序创建新的进程,这样就在最大程度上节省了系统资源,这对嵌入式系统来说至关重要。

同时它还具有自动生成目录、自动解压文件等功能。

因此, Boa在嵌入式系统中具有很高的应用价值。

关键词:ARM;Linux;嵌入式Web服务器;CGIThis paper has mainly developed the software hardware design and realization of the embedded Web server Boa based on ARM. And the core of the hardware part is S3C2410X microprocessor produced by Samsung . The final achievement is the embedded webserver which can remote visit. Boa is a single-tasking HTTP server. That means that unlike traditional webservers, it does not fork for each incoming connection, nor does it fork many copies of itself to handle multiple connections. It internally multiplexes all of the ongoing HTTP connections. And forks only for CGI programs, this is very important to embedded system. At the same time, it also has the function of automatic directory generation、automatic file gunzipping and so on. So Boa is of highly value in the embedded system application.Keywords: ARM;Linux;Embedded Web server;CGI摘要 (1)Abstract (2)目录 (3)第一章引言 (5)第二章课题背景 (6)2.1 嵌入式系统简介 (6)2.2 嵌入式系统和Linux (6)2.3 ARM9硬件平台 (7)2.3.1 ARM简介 (7)2.3.2 JXARM9-2410 ARM嵌入式教学实验系统 (8)第三章嵌入式Web服务器 (9)3.1 概述 (9)3.2 Web服务器原理 (9)3.3 嵌入式Web服务器实现原理 (9)3.3.1 TCP/IP协议 (9)3.4 嵌入式Web服务器Boa (10)3.4.1 Boa概述 (10)3.4.2 Boa的功能实现 (11)第四章CGI技术 (12)4.1 CGI概述 (12)4.2 CGI工作原理 (12)第五章建立嵌入式系统开发环境 (14)5.1 在Vmware下安装Redhat 9.0 (14)5.2 编译配置主机开发环境 (18)5.2.1 配置以太网 (18)5.2.2 安装配置tftp服务器 (19)5.2.3 配置防火墙 (21)5.2.4 配置NFS服务器 (22)5.3 JXARM9-2410中U-boot烧写 (22)5.3.1 u-boot的功能 (22)5.3.2 使用ADT IDE烧写u-boot (23)第六章系统功能的实现 (25)6.1 GCC简介 (25)6.1.1 GCC概述 (25)6.1.2 GCC基本用法 (25)6.2 Linux内核移植 (26)6.2.1 Linux内核源代码的安装 (26)6.2.2 Linux交叉编译环境的建立和使用 (27)6.2.3 Linux内核的配置和编译 (27)6.3 加载Linux内核映像 (30)6.4 Boa Web服务器的移植 (31)6.4.1 Boa Web服务器的建立 (31)6.5 Linux下动态Web页面的实现 (32)6.5.1 CGI程序分析 (32)6.5.2 Form输入的分析和解码 (34)第七章系统功能的测试 (36)7.1 测试Web服务器 (36)7.2 动态Web页面测试 (37)结束语 (40)致谢 (41)参考文献 (42)附录 (43)第一章引言随着计算机技术的发展,嵌入式系统已成为计算机领域的一格重要组成部分。

11实验十一 web服务器的安装与配置

11实验十一 web服务器的安装与配置

实训十一WEB服务器的安装与配置一、实训目的1、学会使用windows 2000 server建立WEB站点2、掌握WEB服务器的配置与管理二、相关理论IIS概述IIS是Internet信息服务(Internet Infomation Server)的缩写。

它是一种Web服务,主要包括WWW服务器、FTP服务器等。

它使得在Intranet(局域网)或Internet(因特网)上发布信息成了一件很容易的事。

Windows 2000 Advanced Server上提供的为IIS 5.0。

利用这种2K自带的IIS 5.0可以在2K机上建立最常用的WWW和FTP服务器,实现最基本的浏览和文件传输功能,可以满足人们的一般要求。

在本文中,欲设置WWW,为了方便起见,可先定下想要实现的目标:⑴我已在DNS中将域名“”指向了IP地址“191.168.0.48”,要求在浏览器中输入此域名就能调出“D:\Myweb”目录下的网页文件。

⑵我已在DNS中将域名“”指向了IP地址“191.168.0.51”,要求在浏览器中输入此域名就能调出“E:\website\wantong”目录下的网页文件。

⑶我已在DNS中将域名“”指向了IP地址“191.168.0.50”,要求在浏览器中输入此域名就能通过浏览器远程进行IIS管理。

⑷当然,上面所有域名也可全部或部分共用同一个IP地址,这种情况下设置方法又不相同。

具体请参见本教程后文相关内容;对于上面所涉及到的多个域名的添加和DNS 设置部分,请参见本教程前文相关内容。

三、实训操作“”的设置1、打开IIS管理器:选“开始菜单→程序→管理工具→Internet信息服务”。

如下图:2、设置“默认Web站点”项:“默认Web站点”一般用于对应向所有人开放的WWW站点,比如本文的“”,本网中的任何用户都可以无限制地通过浏览器来查看它。

⑴打开“默认Web站点”的属性设置窗口:选“默认Web站点→右键→属性”即可。

BOA服务器的配置

BOA服务器的配置

#避免使用mime.types文件,此时需要用AddType在本文件里指明
MimeTypes /etc/mime.types
#文件扩展名没有或未知的话,使用的缺省MIME类型
DefaultType text/plain
5.#将文件扩展名和MIME类型关联起来,和mime.types文件作用一样。如果用mime.types
/
, 或者

最新发行版本: 0.94.13
下载 boa-0.94.13.tar.gz,注意:从boa上下载的是boa-0.94.13.tar.tar,解压方式一样
2、ScriptAlias的修改 修改 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
为 ScriptAlias /cgi-bin/ /var/www/cgi-bin/
3、ServerName的设置修改 ServerName
.here
第二步 Boa的配置 Boa需要在/etc目录下建立一个boa目录,里面放入Boa的主要配置文件boa.conf。在Boa源码目录下已有一个示例boa.conf,可以在其基础上进行修改。
1、Group的修改 修改 Group nogroup
为 Group 0
由于在/etc/group文件中没有nogroup组,所以设成0。另外在/etc/passwd中有nobody用户,所以User nobody不用修改。
b、 修改 CPP = gcc - E
为 CPP = /usr/local/arm/2.95.3/bin/arm-linux-gcc -E
4、编译 # make
# /usr/local/arm/2.95.3/bin/arm-linux-strip boa

实验WEB服务器的配置

实验WEB服务器的配置

图9.55 FTP匿名连接
⑤ 在【消息】标签处,如图9.56,有【欢迎】、【退出】 和【最大连接数】3个输入框,分别代表别人在登录、退 出时FTP服务器上给出旳提醒信息,可根据自己旳需要设 置。另外,最大连接数是设置同步连接本地FTP旳最大主 机台数。
图9.56 FTP站点欢迎消息
试验:WEB服务器旳配置
Web服务器是网络系统平台旳主要主件。它除了提供用于 Web旳硬件服务器外,还需要web服务器旳软件,目前用于 建立web服务器旳软件主要有基于微软平台IIS和APACHE等。 下面将简介WINDOWS下IIS服务器旳配务器配置 IIS(Internet Information Server)是微软出品旳架设 WEB、FTP、SMTP 服务器旳一套整合软件,捆绑在 Windows2023/NT 中,其以便性和易用性,使它成为最受 欢迎旳Web服务器软件之一。 WINDOWS能够采用第三方软件或系统自带IIS6.0两种方式 架设WEB服务器;同步WINDOWS还有一种web版本,专用于 基于web服务旳多种web接口应用,功能极其完美。我们以 WINDOWS系统自带旳IIS6.0为例,简介IIS6.0配置web服务 器旳过程。
访问设置中可指定哪些资源可访问,哪些资源不可访问, 要注意旳是目录浏览和日志访问;选择统计访问,IIS 会 统计该站点旳访问统计,能够选择统计那些资料,如: 访 问者IP ,访问时间等等。基于安全考虑,提议在 NTFS 磁盘格式下使用 IIS。
图9.50 WEB站点主目录配置
(3)设定默认文档 每个网站都会有默认文档,默认文档就是访问者访问站点 时首先要访问旳那个文件;例如 index.htm、index.asp、 default.asp 等。这里需要指定默认旳文档名称和顺序。 默认文档旳顺序是按照从上到下读取。 默认文档能够是目录旳主页或涉及站点文档目录列表旳索 引页。要添加一种新旳默认文档,请单击“添加”按钮。 能够使用该特征指定多种默认文档。按出目前列表中旳名 称顺序提供默认文档。服务器将返回所找到旳第一种文档。 选择【开启文档页脚】,即将一种 HTML 格式旳页脚附加 到 Web 服务器所发送旳每个文档中。页脚文件不是一种 完整旳 HTML 文档,只涉及需用于格式化页脚内容外观和 功能旳HTML 标签。如图9.51所示:

web服务器Boa移植手册

web服务器Boa移植手册
1
Boa服务器的移植可分成两部分,一个是在x86平台上移植,即在虚拟机的linux下,另一个是 在arm平台上移植,即在开发板上的linux下,本手册的前四步,都是在虚拟机上做移植,第五步 是移植到发开板上。
一、编译 boa 程序
1、下载boa-0.94.13.tar.gz。 2、解压 3、#./configure 4、#make 编译会出错: util.c: 100: 1: pasting “t” and “->” does not give a valid preprocessing token make: [util.o] Error1 解决方法,修改compat.h中的 #define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff 为: #define TIMEZONE_OFFSET(foo) foo->tm_gmtoff
3
MimeTypes /etc/mime.types 将网页文件放在/var/www 目录下,cgi 脚本放在/var/www/cgi-bin 目录下。在开发板上运行 boa, 在主机 IE 上输入开发板 IP,可看到网页。
六、常见错误
这些错误内容可查看/var/log/boa/error_log 文件 1、gethostbyname:: No such file or directory 解决办法: 修改 boa.conf 去掉 ServerName .here 前的注释符号(#)。 2、util.c:100:1: error: pasting "t" and "->" does not give a valid preprocessing token make: *** [util.o] 解决办法: 修改 src/compat.h 找到 #define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff 修改成 #define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff 3、boa.c:211 - getpwuid: No such file or directory 解决办法: 修改 src/boa.c 注释掉下面这段程序: if (passwdbuf == NULL) { DIE(”getpwuid”); } if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) { DIE(”initgroups”); } 即修改为: #if 0 if (passwdbuf == NULL) { DIE(”getpwuid”); } if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) { DIE(”initgroups”); } #endif 4、boa.c:228 - icky Linux kernel bug!: No such file or directory 解决办法: 修改 src/boa.c 注释掉下面语句: if (setuid(0) != -1) { DIE(”icky Linux kernel bug!”);

基于ARM9的Web服务器Boa的软、硬件设计

基于ARM9的Web服务器Boa的软、硬件设计

基于ARM9的Web服务器Boa的软、硬件设计作者:金勇来源:《电子世界》2013年第13期【摘要】文章介绍了基于ARM9的嵌入式Web服务器Boa的软、硬件设计及其实现,硬件部分的核心是三星的S3C2410X处理器。

最后成果形式为可以远程访问的WebServer嵌入式服务器。

Boa是一款单任务的HTTP服务器。

与其他传统的Web服务器不同的是当有连接请求到来时,它并不为每个连接单独创建进程,也不通过复制自身进程来处理多链接。

而是通过建立HTTP请求列表来处理多路HTTP连接请求。

同时它只为CGI程序创建新的进程,这样就在最大程度上节省了系统资源,这对嵌入式系统来说至关重要。

同时它还具有自动生成目录、自动解压文件等功能。

因此,Boa在嵌入式系统中具有很高的应用价值。

【关键词】ARM;Linux;嵌入式Web服务器;CGI1.ARM简介广义地讲,凡是带有微处理器的专用软硬件系统都可以称为嵌入式系统。

它是嵌入到对象体系中的专用计算机系统,以应用为中心,以计算机技术为基础,软硬件可裁剪,适应应用系统对功能、可靠性、成本、体积、功耗等严格要求的专用计算机系统。

目前非常流行的ARM 内核有ARM7TDM1、StrongARM、ARM720T、ARM9TDM1、ARM920T、ARM940T、ARM946T、ARM966T、ARM10TDM1等。

本文所讨论的目标板的CPU为ARM920T内核的三星S3C2410芯片。

ARM9系列微处理器主要应用于无线设备、仪器仪表、安全系统、机顶盒、高端打印机、数字照相机和数字摄像机等。

它包含ARM920T、ARM922T和ARM940T三种类型,广泛应用于PDA、移动通信、路由器、工业控制等领域。

2.嵌入式Web服务器实现原理嵌入式Web服务器是指将Web服务器引入到现场嵌入式设备中,并驻留其中有限的代码空间内。

与传统的Web应用相比,简化了系统结构,并将信息采集和信息发布都集成到现场的嵌入式设备中。

嵌入式WEB服务器BOA的应用研究

嵌入式WEB服务器BOA的应用研究

而 不 会 创 建 (o k m 新 的 进 程 来 处 理 并 发 连 接 请 求 , fr ) 但
B OA 支 持 C , 且 能 为 CG GI并 I程 序 创 建 出 一 个 新 的 进 程 来 执 行 。B OA 服 务 器 的设 计 目标 是 速 度 快 和 安 全 性 好 , 在 其 站 点 公 布 的性 能 测 试 中 , OA 的性 能 要 好 于 Ap c e B a h 服 务 器 的性 能 。 嵌入式 WE B服 务 器 BOA 和 普 通 WE B服 务 器 一 样 ,
⑦ 去 除 其 中的 调 试 信 息 , 以减 小 文 件 的大 小 。
# a m —i x s rp s lt 3 r l nu - ti qie
4 基 于 O 的数 据 库 访 问技 术 [ GI 4
C ( mmo twa ne fc , 共 网 关 接 口) GICo n Gae y I tra e 公 定
① 从 www. qi . r s l e o g下 载 sl e2 8 1 . a. z源 t qi 一 . . 7 tr g t 码 , 解压 。 并
② 解压 。
# tr~ z fb a 0 9 . 3 t r g a x v o ~ . 4 1 . a . z
③ 进 入 源代 码 目录 。
IEl_ 一w 、 : 嵌服 i务 入器 式 B
图 1 嵌 入 式 WE B服务 器 的工 作 过 程
图 2为 硬 件 系 统 结 构 框 图 。嵌 入 式 WE B服 务 器 采 用
¥ C2 1 3 4 0作 为 整 个 系 统 的 处 理 器 。¥ C2 1 3 4 0是 S ms n a ug 公 司 的 一 款 基 于 ARM9 0 内核 的 1 / 2位 R S 2T 63 I C嵌 入 式

Linux下嵌入式Web服务器BOA和CGI编程开发

Linux下嵌入式Web服务器BOA和CGI编程开发

Linux下嵌⼊式Web服务器BOA和CGI编程开发**⽬录**⼀、环境搭建⼆、相关配置(部分)三、调试运⾏四、测试源码参考五、常见错误六、扩展(CCGI,SQLite)# ⼆、相关配置(部分)boa我的配置:/etc/boa$ sudo vi boa.conf# 下⾯⼏个都是关键点,基本就错这⼏个点上# cumentRoot /var/www#将cgi保存的实际位置和⽹站地址做个对应# ScriptAlias /cgi-bin/ /var/www/cgi-bin/#cgi脚本运⾏时能看到的$PATH(可选)# CGIPath /bin:/usr/bin:/usr/local/bin#如果想在任何位置都能运⾏cgi,要添加这个(可选)AddType application/x-httpd-cgi cgi# Boa v0.94 configuration file# File format has not changed from 0.93# File format has changed little from 0.92# version changes are noted in the comments## The Boa configuration file is parsed with a lex/yacc or flex/bison# generated parser. If it reports an error, the line number will be# provided; it should be easy to spot. The syntax of each of these# rules is very simple, and they can occur in any order. Where possible# these directives mimic those of NCSA httpd 1.3; I saw no reason to# introduce gratuitous differences.# $Id: boa.conf,v 1.252002/03/2204:33:09 jnelson Exp $# The "ServerRoot" is not in this configuration file. It can be compiled# into the server (see defines.h) or specified on the command line with# the -c option, for example:## boa -c /usr/local/boa# Port: The port Boa runs on. The default port for http servers is 80.# If it is less than 1024, the server must be started as root.# 端⼝⽼是被占⽤,所以我改掉了Port 88# Listen: the Internet address to bind(2) to. If you leave it out,# it takes the behavior before 0.93.17.2, which is to bind to all# addresses (INADDR_ANY). You only get one "Listen" directive,# if you want service on multiple IP addresses, you have three choices:# 1. Run boa without a "Listen" directive# a. All addresses are treated the same; makes sense if the addresses# are localhost, ppp, and eth0.# b. Use the VirtualHost directive below to point requests to different# files. Should be good for a very large number of addresses (web# hosting clients).# 2. Run one copy of boa per IP address, each has its own configuration# with a "Listen" directive. No big deal up to a few tens of addresses.# Nice separation between clients.# The name you provide gets run through inet_aton(3), so you have to use dotted# quad notation. This configuration is too important to trust some DNS.#Listen 192.68.0.5# User: The name or UID the server should run as.# Group: The group name or GID the server should run as.Group 0# ServerAdmin: The email address where server problems should be sent.# Note: this is not currently used, except as an environment variable# for CGIs.#ServerAdmin root@localhost# ErrorLog: The location of the error log file. If this does not start# with /, it is considered relative to the server root.# Set to /dev/null if you don't want errors logged.# If unset, defaults to /dev/stderrErrorLog /var/log/boa/error_log# Please NOTE: Sending the logs to a pipe ('|'), as shown below,# is somewhat experimental and might fail under heavy load.# "Usual libc implementations of printf will stall the whole# process if the receiving end of a pipe stops reading."#ErrorLog "|/usr/sbin/cronolog --symlink=/var/log/boa/error_log /var/log/boa/error-%Y%m%d.log"# AccessLog: The location of the access log file. If this does not# start with /, it is considered relative to the server root.# Comment out or set to /dev/null (less effective) to disable# Access logging.# AccessLog /var/log/boa/access_log# Please NOTE: Sending the logs to a pipe ('|'), as shown below,# is somewhat experimental and might fail under heavy load.# "Usual libc implementations of printf will stall the whole# process if the receiving end of a pipe stops reading."#AccessLog "|/usr/sbin/cronolog --symlink=/var/log/boa/access_log /var/log/boa/access-%Y%m%d.log" # UseLocaltime: Logical switch. Uncomment to use localtime# instead of UTC time#UseLocaltime# VerboseCGILogs: this is just a logical switch.# It simply notes the start and stop times of cgis in the error log# Comment out to disable.#VerboseCGILogs# ServerName: the name of this server that should be sent back to# clients if different than that returned by gethostname + gethostbynameServerName .here# VirtualHost: a logical switch.# Comment out to disable.# Given DocumentRoot /var/www, requests on interface 'A' or IP 'IP-A'# become /var/www/IP-A.# Example: http://localhost/ becomes /var/www/127.0.0.1## Not used until version 0.93.17.2. This "feature" also breaks commonlog# output rules, it prepends the interface number to each access_log line.# You are expected to fix that problem with a postprocessing script.#VirtualHost# DocumentRoot: The root directory of the HTML documents.# Comment out to disable server non user files.DocumentRoot /var/www# UserDir: The name of the directory which is appended onto a user's home# directory if a ~user request is recieved.UserDir public_html# DirectoryIndex: Name of the file to use as a pre-written HTML# directory index. Please MAKE AND USE THESE FILES. On the# fly creation of directory indexes can be _slow_.DirectoryIndex index.html# DirectoryMaker: Name of program used to create a directory listing.# Comment out to disable directory listings. If both this and# DirectoryIndex are commented out, accessing a directory will give# an error (though accessing files in the directory are still ok). DirectoryMaker /usr/lib/boa/boa_indexer# DirectoryCache: If DirectoryIndex doesn't exist, and DirectoryMaker# has been commented out, the the on-the-fly indexing of Boa can be used # to generate indexes of directories. Be warned that the output is# extremely minimal and can cause delays when slow disks are used.# Note: The DirectoryCache must be writable by the same user/group that # Boa runs as.# DirectoryCache /var/spool/boa/dircache# KeepAliveMax: Number of KeepAlive requests to allow per connection# Comment out, or set to 0 to disable keepalive processing KeepAliveMax 1000# KeepAliveTimeout: seconds to wait before keepalive connection times out KeepAliveTimeout 10# MimeTypes: This is the file that is used to generate mime type pairs# and Content-Type fields for boa.# Set to /dev/null if you do not want to load a mime types file.# Do *not* comment out (better use AddType!)MimeTypes /etc/mime.types# DefaultType: MIME type used if the file extension is unknown, or there# is no file extension.DefaultType text/plain# CGIPath: The value of the $PATH environment variable given to CGI progs. CGIPath /bin:/usr/bin:/usr/local/bin# SinglePostLimit: The maximum allowable number of bytes in# a single POST. Default is normally 1MB.# AddType: adds types without editing mime.types# Example: AddType type extension [extension ...]# Uncomment the next line if you want .cgi files to execute from anywhere#AddType application/x-httpd-cgi cgi# Redirect, Alias, and ScriptAlias all have the same semantics -- they# match the beginning of a request and take appropriate action. Use# Redirect for other servers, Alias for the same server, and ScriptAlias# to enable directories for script execution.# Redirect allows you to tell clients about documents which used to exist in# your server's namespace, but do not anymore. This allows you to tell the # clients where to look for the relocated document.# Example: Redirect /bar http://elsewhere/feh/bar# Aliases: Aliases one path to another.# Example: Alias /path1/bar /path2/fooAlias /doc /usr/doc# ScriptAlias: Maps a virtual path to a directory for serving scripts# Example: ScriptAlias /htbin/ /www/htbin/ScriptAlias /cgi-bin/ /var/www/cgi-bin/View Codecgi我的配置:sudo vi /etc/apache2/sites-enabled/000-defaultServerName 127.0.0.1<VirtualHost *:80>ServerAdmin webmaster@localhostDocumentRoot /var/www<Directory />Options FollowSymLinksAllowOverride None</Directory><Directory /var/www/>Options Indexes FollowSymLinks MultiViews AllowOverride NoneOrder allow,denyallow from all</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/<Directory "/usr/lib/cgi-bin">AllowOverride NoneOptions +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,denyAllow from all</Directory>ErrorLog ${APACHE_LOG_DIR}/error.log# Possible values include: debug, info, notice, warn, error, crit, # alert, emerg.LogLevel warnCustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/"<Directory "/usr/share/doc/">Options Indexes MultiViews FollowSymLinks AllowOverride NoneOrder deny,allowDeny from allAllow from 127.0.0.0/255.0.0.0 ::1/128</Directory></VirtualHost>View Code下⾯提供⼏个测试代码,转⾃其他⼤佬,不过我找不到⽹址了。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

摘要:在详细介绍一种嵌入式Web服务器BOA的实现与配置方法的基础上,以一个Web 在线远程监控GPIO(通用输入/输出)的程序为实例,介绍嵌入式Linux系统下CPU程序设计技术。

关键词:嵌入式系统Linux BOA CGI GPIO1 概述随着互联网应用的普及,越来越多的信息化产品需要接入互联网通过Web页面进行远程访问。

嵌入式Web系统提供了一种经济、实用的互联网嵌入式接入方案。

这里结合一种嵌入式Web Server BOA来介绍嵌入式Linux系统下的CGI程序设计技术。

2 Web Server BOA的实现与配置2.1 uClinux下,主要有三个Web Server:HTTPD、THTTPD和BOA。

HTTPD是最简单的一个Web Server,它的功能最弱,不支持认证,不支持CGI。

THTTPD和BOA都支持认证、CGI等,功能都比较全。

BOA是一个单任务的小型HTTP服务器,源代码开放、性能优秀,特别适合应用在嵌入式系统中。

目前的uClinux的代码中已经包含BOA的源代码。

在uClinux下实现BOA,只需要对BOA做一些配置和修改。

以下是配置的过程。

(1)编译BOA到内核首先,需要把BOA编译到内核,即执行make menuconfig,在应用程序选单中network application项下面选择boa。

该操作需要重新编译内核。

(2)编制配置文件boa.conf在Linux操作系统下,应用程序的配置都是以配置文件的形式提供的,一般都是放在目标板/etc/目录下或者/etc/config目录下。

但boa的配置文件boa.cont一般都旋转在目标板/home/httpd/目录下。

例如,一个典型的boa.conf文件格式如下:ServerName Samsung-ARMDocumentRoot/home/httpdScriptAlias/cgi-bin/home/httpd/cgi-bin/ScriptAlias/index.html/home/httpd/index.html它指定了HTML页面必须放到/home/httpd目录下,cgi外部扩展程序必须放到/home/httpd/cgi-bin目录下。

(3)编译烧写内核重新编译内核后,通过烧写工具烧写内核,就可以在PC上通过IE浏览器访问开发板上的Web Server。

例如,输入开发板的IP地址http://192.168.0.101,即可访问到自己做的网页index.html了。

并且,通过编写CGI外部扩展程序,可以实现动态Web技术,下面将详细介绍。

2.2 具有MMU平台的Linux下B0A的实现与配置对于有MMU(内存管理单元)的平台,如armlinux和ppclinux,可以到网上下载一个主流版本的boa发行包。

因为是运行在目标系统,所以要用交叉编译工具编译,即需要修改boa/src/Makefile里面的编译器。

例如:CC=/LinuxPPC/CDK/bin/powerpc-linux-gccCPP=/LinuxPPC/CDK/bin/powerpc-linux-g++然后直接在boa/src目录下执行make,即可生成BOA可执行文件;将其编译入内核,并烧写到存储设备,就可以实现访问BOA服务器。

3 CGI程序设计技术CGI(Common Gateway Interface)是外部应用扩展应用程序与WWW服务器交互的一个标准接口。

按照CGI标准编写的外部扩展应用程序可以处理客户端浏览器输入的数据,从而完成客户端与服务器的交互操作。

而CGI规范就定义了Web服务器如何向扩展应用程序发送消息,在收到扩展应用程序的信息后又如何进行处理等内容。

通过CGI可以提供许多静态的HTML网页无法实现的功能,比如搜索引擎、基于Web的数据库访问等等。

3.1 工作原理(1)WWW和CGI的工作原理HTTP协议是WWW的基础,它基于客户/服务器模型,一个服务器可以为分布在网络中处的客户提供服务;它是建立在TCP/IP协议之上的“无连接”协议,每次连接只处理一个请求。

在服务器上,运行产着一个守护进程对端口进行监听,等待来自客户的请求。

当一个请求到来时,将创建一个子进程为用户的连接服务。

根据请求的不同,服务器返回HTML文件或者通过CGI调用外部应用程序,返回处理结果。

服务器通过CGI与外部程序和脚本之间进行交互,根据客户端在进行请求时所采取的方法,服务器会收集客户所提供的信息,并将该部分信息发送给指定的CGI扩展程序。

CGI扩展程序进行信息处理并将结果返回服务器,然后服务器对信息进行分析,并将结果发送回客户端。

外部CGI程序与WWW服务器进行通信、传递有关参数和处理结果是通过环境变量、命令行参数和标准输入来进行的。

服务器提供了客户端(浏览器)与CGI扩展程序之间的信息交换的通道。

CGI的标准输入是服务器的标准输出,而CGI的标准输出是服务器的标准输入。

客户的请求通过服务器的标准输出传送给CGI的标准输入,CGI对信息进行处理后,将结果发送到它的标准输入,然后由服务器将处理结果发送给客户端。

(2)URL编码客户端浏览器向服务器发送数据采用编码的形式进行。

该编码就是CRL编码。

编码的主要工作是表单域的名字和值的转义,具体的做法为:每一对域和值里的空格都会被替换为一个加号(+)字符,不是字母或数字的字符将被替换为它们的十六进制数字形式,格式为%HH。

HH是该字符的ASCII十六进制值。

<BR>标签将被替换为“%0D%0A”。

信息是按它们在表单里出现的顺序排列的。

数据域的名字和数据域的值通过等号(=)字符连在一起。

各对名/值再通过“&”字符连接在一起。

经过这些编码处理之后,表单信号就整个成为一个连续的字符流,里面包含着将被送往服务器的全部信息。

因为表单输入信息都是经过编码后传递给脚本程序的,所以CGI扩展程序在使用这些参数之前必须对它们进行解码。

3.2 CGI外部扩展程序编制服务器程序可以通过三种途径接收信息:环境变量、命令行和标准输入。

具体使用哪一种方法要由<FORM>标签的METHOD属性来决定。

在“METHOD=GET”时,向CGI程序传递表单编码信息的正常做法是通过命令来进行的。

大多数表单编码信息都是通过QUERY_STRING的环境变量来传递的。

如果“METHOD=POST”,表单信息将通过标准输入来读取。

还有一种不使用表单就可以向CGI传送信息的方法,那就是把信息直接追回在URL地址后面,信息和URL之间用问号(?)来分隔。

下面结合Web远程监控ARM芯片的GPIO(通用输入/输出)的应用实例详细介绍。

(1)GET方法GET方法是对数据的一个请求,被用于获得静态文档。

当使用GET方法时,CGI程序将会从环境变量QUERY_STRING获取数据。

为了处理客户端的请求,CGI必须对QUERY_STRING中的字符串进行分析。

当需要从服务器获取数据并且不改变服务器上的数据时,应该选用GET 方法;但是如果请求中包含的字符串超过了一定长度,一般是1024字节,那么就只能选用POST方法。

GET方法通过附加在URL后面的参数发送请求信息。

这些参数将被放在环境变量QUERY_STRING中传给CGI程序。

GET方法的表单格式和CGI解码程序可以参考POST方法的实现。

(2)POST方法当浏览器将数据从一个填写的表单传给服务器时一般采用POST方法,而且在发送的数据超过1024字节时也必须采用POST方法。

当使用POST方法时,Web服务器向CGI程序的标准输入STDIN传送数据。

发送的数据长度存在环境变量CONTENT_LENGTH中,并且,POST方法的数据格式为:variable1=value1&variable2=value2&etcCGI程序必须检查REQUEST_METHOD环境变量以确定是否采用了POST方法,并决定是否要读取STDIN。

POST方法在HTML文档中定义的表单如下:<FORM METHOD=POST ACTION="/cgi-bin/cgi_gpio.cgi"><INPUT TYPE="RADIO"NAME=rb VALUE="0">Operate P0<BR><INPUT TYPE="RADIO"NAME=rb VALUE="1">Operate P1<BR><INPUT TYPE="RADIO"NAME=rb VALUE="2">Operate P2<BR><INPUT NAME="ok"TYPE=submit VALUE="OK"><INPUT>NAME="cancel"TYPE=reset VALUE="RESET"></FORM>它调用的服务器脚本程序是/cgi/bin/cgi_gpio.cgi。

CGI扩展程序中FORM表单的解码可参考如下程序:/*function getPOSTvars*/char **getPOSTvars(){int i;int content_length;char **postvars;char *postinput;char **pairlist;int paircount=0;chr *nvpair;char *eqpos;postinput=getenv("CONTENT_LENGTH");//获取传送给程序数据的字节数if(!postinput)exit();if(!content_length=atoi(postinput))) //获取信息长度exit(1);if(!(postinput=(char*)malloc(content_length+1)))exit(1);if(!fread(postinput,content_length,1,stadin))exit(1);postinput[content_length]='0';for(i=0;postinput[i];i++)if(postinput[i]=='+')postinput[i]=''; //对加易进行处理pairlist=(char **)malloc(256*sizeof(char **));paircount=0;nvpair=strtok(postinput,"&");//从出现“&”字符的位置把信息分段,然后对结果依次处理while (nvpair){pairlist[paircount++]=strdup(nvpair);if(!(paircount%256))pairlist=(char**)realloc(pairlist,(paircount+256)*sizeof(char**));nvpair=strtok(NULL,"&");}pairlist[paircount]=0;postvars=(char**)malloc((paircount*2+1)*sizeof(char **));for(i=0;i<paircount;i++){if(eqpos=strchr(pairlist[i],'=')){*eqpos='0';unescape_url(postvars[i*2+1]=strdup(eqpos+1));//调用unescape_url函数继续解码}else{unescape_url(postvars[i*2+1])=strdup(""));}postvars[paircount*2]=0;for(i=0;pairlist[i];i++)free(pairlist[i]);free(pairlist);free(postinput);return postvars;}其中,unescape_url函数再调用x2c函数,把(不是字节或数字的)特殊字符从其%HH表示方式解码为文本字符。

相关文档
最新文档