码迷,mamicode.com
首页 > 其他好文 > 详细

分享一个嵌入式httpd服务器开发库 - boahttpd library

时间:2014-07-09 11:08:59      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   os   2014   art   

http://sourceforge.net/projects/boahttpd/

    一个C接口的开发库,适用于 windows/linux/或其他嵌入式平台,支持CGI扩展,支持多线程。采用面向对象开发,一个应用里可以同时开多个http server,只要端口不同就互不影响。

    目前只写了英文帮助,大家凑合看吧。

 

boahttpd Library Development Manual

 

Last Modified: 2014-07-08

1.     Introduction
 The library, boahttpd, is designed to create a cgi-featured http server (normally called as boa-server)easy and fast. That means you can easily add CGI support to your server application. It support file downloading by default, like any other web servers.

Its written in C++ inside, but provides a C-Style API which you will find it to be Object-Oriented essentially. Multiple platforms are supported with a uqique set of interface, including Windows, Linux, or Embedded systems. It will take a few minutes to get to know how to use it.

 2.     Getting Started
 First you have to decide which platform your application is running on. For windows you have to take  ‘ boahttpd.lib/.dll’  into your project, plus the header file ‘boahttpd.h’.  For linux(x86),  a release built under Fedora14 is provided , which will work correctly under  those systems similar  (I mean the kernel version ) to Fedora 14.

 For embedded systems, such as linux (arm), your have to provide the specific toolchains and ask for a specific build for your target platform.

 2.1         A ‘helloworld’ application
A most simple usage of the library would be like this:

 #include <stdio.h>
#include <stdlib.h>

/* include boahttpd library */
#include "../boahttpd.h"
#pragma comment(lib, "../boahttpd.lib")
/* start a simple web server */
int main()
{
    boa_server_t server;
    if(boa_server_create(&server, NULL,
  "wwwroot",
  8888, "0.0.0.0") < 0)
    {
       printf("failed to create server!\n");
       return 0;
    }

    getchar();
    boa_server_destroy(&server);
    return 0;
}
The code above  sets up a http server running on port 8888, and the root directory of web pages is ‘wwwroot’ folder which should be located on the current directory.  So to make things right, your have to put some files in your ‘wwwroot’ folder, for example, a ‘index.html’ as the default home page.

Now run the program,  then try the following url in your web browser:
    
http://127.0.0.1:8888
or
    
http://127.0.0.1:8888/image/abc.jpg
if there is a picture file located in that sub-directory: wwwroot/image/abc.jpg.

 2.2         Easy CGI Support
Now we get to the key feature of the library: CGI support.  It also easy to know and to use, see  the  following snippet :

… … …
static int cgi_handler(void* context, void* userdata)
{
    … handle the cgi request …
    return 0;
}
int main()
{
    boa_cgi_t cgi ;
    boa_cgi_create(&cgi);
    boa_cgi_register(&cgi, "test", &cgi_handler, NULL);
 
    boa_server_t server;
    if(boa_server_create(&server,&cgi,
  wwwroot", 8888, "0.0.0.0") < 0)
    {
       printf("failed to create server!\n");
       return 0;
    }
     getchar();

    boa_cgi_destroy(&cgi);
    boa_server_destroy(&server);
    return 0;
}

 By register a callback function , cgi_handler, you could have the chance to handle the CGI request. A number of such callbacks can be register to the single boa_cgi_t object .
(Note: each CGI request is called in a NEW thread, thus the callback handler is called in that NEW thread)

 Take the follow request as an example.  When you type
  
http://127.0.0.1:8888/cgi/mytest?name=shaofa&height=175
in your browser, your httpd server will got a CGI request with:

 Name
 Value
 Meaning
 
method
 GET
 The HTTP method, ‘GET’ or ‘POST’
 
service
 mytest
 The name of the CGI service
 
argument name=shaofa&height=175
 The argument passed with the URL
 
content 
 Vaild if the method is ‘POST’
 
HTTP FIELD 
 Any other HTTP head field
 
Socket  
 The socket handle
 

   The following snippet shows how to retrieve  those values:
static int cgi_handler(void* context, void* userdata)
{
    const char* service = boa_cgi_name(context);
    const char* method = boa_cgi_method(context);
    const char* argument = boa_cgi_args(context);
    const char* content = boa_cgi_content(context);
    const char* anyfield = boa_cgi_field(context,”Content-Length”);
      int sock = boa_cgi_socket(context);
}

 

If you want to reply an error message , just call:
     boa_cgi_send_error ( context, status, reason);
If you want to reply an 200 OK message with a text content, just call:
     boa_cgi_send_text(context, “……”, CONTENT_TYPE_TXT);

 
2.3         Advanced CGI Support

3.     License & Registration
The file-downloading function is always free, which means that you can always browse you web files without any limitation.

The CGI support is limited to 100 times if no legal license key is provided. So when you have finally tested your program and decide to release it, you are supposed to acquire  a license key.

3.1         CGI File & License Key
Firstly, you should provide a  *.cgi  text file, in which the desired CGI services’ name and argument should be listed in specific format. Invoke the boa_license() to make your library licensed.
 boa_license(cgi_file, license_key)

    If you just want to test if your CGI file is correctly written, just pass a NULL pointer as license_key.  A NULL license key tells the library to check CGI format and permit accesses for 100 times.

3.2         Format of CGI File
Following is a example of such CGI file:

test?name,age,what,ever 
getfile?filename,size 

This text file has two lines: the first line takes ‘test’ as the service name, and have 4 argument:  name, age what, ever. The second line takes ‘getfile` as service name and 2 argument:  ‘filename, size`.

According to this file, cgi request will be judged as legal or illegal.For examples:
 
http://.../cgi/test?what=xyz    [ legal ]
http://.../cgi/test?what=xyz&thing=abc [ illegal , as ‘thing` no permitted]
 

3.3         Acquire License Key
When you have tested your *.cgi  file, you may want to get a license key and then release your program. The following procedures describes how to get a license key.
(1)         Write and test your *.cgi file thoroughly ;
(2)         Email the *.cgi  file to the Author ;
(3)         Retrieve the license key from Author’s reply ;
(4)         Call bao_license(“xxx.cgi”, “License Key”) in your main() function

4.     Help Information
E-Mail:  
1926583112@qq.com

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                                         

 

分享一个嵌入式httpd服务器开发库 - boahttpd library,布布扣,bubuko.com

分享一个嵌入式httpd服务器开发库 - boahttpd library

标签:des   style   http   os   2014   art   

原文地址:http://blog.csdn.net/iamshaofa/article/details/37563287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!