轻量级嵌入式web服务器 --libhttpd

一、嵌入式web服务器

      在网上找了很多关于嵌入式web server的资料,比如shttpd、appweb、boa、go ahead、server等等, 但都有网友反映移植不方便、资源要求高等等。如果用于运行ucLinux的arm9上,就没有什么问题,而用于运行ucOS的arm7上,则有很多限制,需要进行很多的移植工作,今天找到一个叫做LibHttpd的,采用ANSIC编写,仅从介绍上来看,非常适合arm7的嵌入式设备使用。

      嵌入式设备中使用Web server的主要目的就是用来进行参数设置,使用这种方式,有很多优点:

      1、可以给用户熟悉的用户界面,减少用户的学习负担。

      2、具有B/S方式的优点,不再需要在每个客户机上安装配套的软件,不需要在客户机上升级软件,也不再需要支持由于客户机环境带来的种种问题。

      LibHttpd是一个开源轻量级嵌入式Web server,LibHttpd实现了下述功能:

      1)实现了HTTP的子集;

      2)使用表格技术自动处理Html Form数据;

      3)产生的内容既可以是静态的网页,也可以是调用C函数动态产生的(callback);

      LibHttpd提供API,利用这些API,用户可以很方便地将自己的Web内容加入到程序当中。


 二、libhttpd中带有英文的使用手册,也增加了使用实例源码,在编程中可以做一个很好的模板:

#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

#ifdef _WIN32
#  include <getopt.h>
#else
#  include <sys/time.h>
#endif

#include "httpd.h"

/*
** This is a static page of HTML.  It is loaded into the content
** tree using httpdAddStaticContent( ).
*/
#define test1_html "<HTML><BODY>This is just a test</BODY>"

#define ember_code1 "printf(\"This is from ember in process %d\n\",getpid());"
#define ember_code "load \"/usr/local/nk/www/content.lib\"; dispatchWebPage(\"/index.html\");"

/*
** Below are 2 dynamic pages, each generated by a C function.  The first
** is a simple page that offers a little dynamic info (the process ID)
** and the setups up a test link and a simple form.
** 
** The second page processes the form.  As you can see, you can access
** the form data from within your C code by accessing the symbol table
** using httpdGetVariableByName() (and other similar functions).  You
** can also include variables in the string passed to httpdOutput( ) and
** they will be expanded automatically.
*/
void index_html(server)
    httpd   *server;
{
    httpdPrintf(server,
        "Welcome to the httpd server running in process number %d<P>\n",
        getpid());
    httpdPrintf(server,
        "Click <A HREF=/test1.html>here</A> to view a test page<P>\n");
    httpdPrintf(server,
        "Click <A HREF=/login.html>here</A> to authenticate<P>\n");
    httpdPrintf(server,
        "Or <A HREF=/wildcard/foo>here</A> for a test wildcard page<P>\n");
    httpdPrintf(server, "<P><FORM ACTION=test2.html METHOD=POST>\n");
    httpdPrintf(server, "Enter your name <INPUT NAME=name SIZE=10>\n");
    httpdPrintf(server, "<INPUT TYPE=SUBMIT VALUE=Click!><P></FORM>\n");
    return;
}

void test2_html(server)
    httpd   *server;
{
    httpVar *variable;

    /*
    ** Grab the symbol table entry to see if the variable exists
    */
    variable = httpdGetVariableByName(server, "name");
    if (variable == NULL)
    {
        httpdPrintf(server,"Missing form data!");
        return;
    }

    /*
    ** Use httpdOutput() rather than httpdPrintf() so that the variable
    ** embedded in the text is expanded automatically
    */
    httpdOutput(server,"Hello $name");
}

void test3_html(server)
    httpd   *server;
{
    char    *path;
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值