学习libmicrohttpd

  该库是GNU系统下的一款C语言http协议服务端,提供http和https服务,支持Linux、Windows等多平台;

1 主页

      https://www.gnu.org/software/libmicrohttpd/

2 介绍

2.1 使用范围

    a Embedded HTTP server on a cortex M3 (128 KB code space)
       嵌入式http server;
    b Large-scale multimedia server (reportedly serving at the simulator limit of 7.5 GB/s)
       大规模的多媒体服务
    c Administrative console (via HTTP/HTTPS) for network appliances
       为网络应用软件提供http/https服务

2.2 线程模式和事件循环

    MHD支持四种基本的线程模式和最多三种事件循环样式。
    a  四种线程模式
    1> external (MHD creates no threads, event loop is fully managed by the application),
         扩展线程模式,由调用进程管理线程;
    2> internal (MHD creates one thread for all connections), 
         内部线程模式,1条线程管理所有的连接;
    3> thread pool (MHD creates a thread pool which is used to process all connections) 
         线程池模式,MHD创建一个线程池处理所有的连接;
    4> thread-per-connection (MHD creates one listen thread and then one thread per accepted connection).
         1连接1线程模式,MHD创建1条监听线程和为每条连接创建1个线程;

    b 线程模式被组合在事件循环风格里
    MHD 支持select、poll、epoll三种事件循环模式;
    epoll   只linux系统支持;
    poll     可在多种系统环境中支持;
    select  在各种系统中都支持;
    注意:MHD可能使用epoll 和 扩展的select-based 事件模式;
    (These thread modes are then combined with the event loop styles. MHD support select, poll and epoll. epoll is only available on Linux, poll may not be available on some platforms. Note that it is possible to combine MHD using epoll with an external select-based event loop.)

    c 默认
    external 线程 + select事件 模式, 其他组合如下:
    线程、事件组合              |select |poll   |epoll
   -----------------------------------------------
    external                           |yes    |no     |yes
    internal                           |yes    |yes    |yes
    thread pool                     |yes    |yes    |yes
    thread-per-connection   |yes    |yes    |no

                                Table1.1

    The default (if no other option is passed) is “external select”. The highest performance can typically be obtained with a thread pool using epoll. Apache Benchmark (ab) was used to compare the performance of select and epoll when using a thread pool and a large number of connections. Figure 1.1 shows the resulting plot from the benchmark.c example, which measures the latency between an incoming request and the completion of the transmission of the response. In this setting, the epoll thread pool with four threads was able to handle more than 45,000 connections per second on loopback (with Apache Benchmark running three processes on the same machine).

   

     Figure 1.1 

    epoll thread pool with four threads模式最多可以支持45000条连接;
   (https://www.gnu.org/software/libmicrohttpd/manual/libmicrohttpd.html)

2.3 包含头文件

     #include <microhttpd.h>之前需要包含uint64_t, size_t, fd_set, socklen_t and struct sockaddr 这些数据类型的头文件;

2.4 不处理SIGPIPE的信号

  可由使用者自定义如:

static void catcher (int sig)
{
}


static void ignore_sigpipe ()
{
    struct sigaction oldsig;
    struct sigaction sig;

    sig.sa_handler = &catcher;
    sigemptyset (&sig.sa_mask);
#ifdef SA_INTERRUPT
    sig.sa_flags = SA_INTERRUPT;  /* SunOS */
#else
    sig.sa_flags = SA_RESTART;
#endif
    if (0 != sigaction (SIGPIPE, &sig, &oldsig))
        fprintf (stderr, "Failed to install SIGPIPE handler: %s\n", strerror (errno));
}

2.5 出现不支持long long的情况    

    可以通过改变"platform.h",
    define MHD_LONG_LONG and MHD_UNSIGNED_LONG_LONG
    define MHD_LONG_LONG_PRINTF and MHD_UNSIGNED_LONG_LONG_PRINTF 

2.6 Portability to z/OS   

To compile MHD on z/OS, extract the archive and run
iconv -f UTF-8 -t IBM-1047 contrib/ascebc > /tmp/ascebc.sh
chmod +x /tmp/ascebc.sh
for n in `find * -type f`
do
   /tmp/ascebc.sh $n
done

3 安装

    a Linux平台:
    $ ./configure
    $ make
    $ make install

    支持如下编译选项,
--disable-curl
disable running testcases using libcurl

--disable-largefile
disable support for 64-bit files

--disable-messages
disable logging of error messages (smaller binary size, not so much fun for debugging)

--disable-https
disable HTTPS support, even if GNUtls is found; this option must be used if eCOS license is desired as an option (in all cases the resulting binary falls under a GNU LGPL-only license)

--disable-postprocessor
do not include the post processor API (results in binary incompatibility)

--disable-dauth
do not include the authentication APIs (results in binary incompatibility)

--disable-epoll
do not include epoll support, even on Linux (minimally smaller binary size, good for testing portability to non-Linux systems)

--enable-coverage
set flags for analysis of code-coverage with gcc/gcov (results in slow, large binaries)

--with-gcrypt=PATH
specifies path to libgcrypt installation

--with-gnutls=PATH
specifies path to libgnutls installation
    b Windows平台;
    使用 libmicrohttpd-0.9.55\w32\相关的版本进行打开工程,进行编译使用;
    注意:Windows平台使用该库时,包含的头文件路径为:libmicrohttpd-0.9.55\w32\common
              否则会出现各种编译、使用问题;

4. 使用介绍

1 MHD_FLAG 和 MHD_OPTION
    MHD_start_daemon(unsigned int flags,
                  uint16_t port,
                  MHD_AcceptPolicyCallback apc,
                  void *apc_cls,
                  MHD_AccessHandlerCallback dh,
                  void *dh_cls,
                  ...)
    的flag为:

    enum MHD_FLAG
    {
        MHD_NO_FLAG = 0,
        MHD_USE_ERROR_LOG = 1,
        MHD_USE_DEBUG = 1,
        MHD_USE_TLS = 2,

         MHD_USE_THREAD_PER_CONNECTION = 4,

         MHD_USE_INTERNAL_POLLING_THREAD = 8,
         ...
         MHD_USE_EPOLL_INTERNAL_THREAD = MHD_USE_EPOLL | MHD_USE_INTERNAL_POLLING_THREAD,
        /** @deprecated */
        MHD_USE_EPOLL_INTERNALLY = MHD_USE_EPOLL | MHD_USE_INTERNAL_POLLING_THREAD,
        /** @deprecated */
        MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY = MHD_USE_EPOLL | MHD_USE_INTERNAL_POLLING_THREAD,
        ...

        /** * Automatically use best available polling function.
        * Choice of polling function is also depend on other daemon options.
        * If #MHD_USE_INTERNAL_POLLING_THREAD is specified then epoll, poll() or
        * select() will be used (listed in decreasing preference order, first
        * function available on system will be used).
        * If #MHD_USE_THREAD_PER_CONNECTION is specified then poll() or select()
        * will be used.
        * If those flags are not specified then epoll or select() will be
        * used (as the only suitable for MHD_get_fdset())   */
        MHD_USE_AUTO = 65536,

        
        /*** Run using an internal thread (or thread pool) with best available on
        * system polling function.
        * This is combination of #MHD_USE_AUTO and #MHD_USE_INTERNAL_POLLING_THREAD
        * flags.*/  
        MHD_USE_AUTO_INTERNAL_THREAD = MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
    };

    MHD_start_daemon(unsigned int flags,
                      uint16_t port,
                      MHD_AcceptPolicyCallback apc,
                      void *apc_cls,
                      MHD_AccessHandlerCallback dh,
                      void *dh_cls,
                      ...)
        的... 为如下:
    enum MHD_OPTION
    {
      /**
       * No more options / last option.  This is used
       * to terminate the VARARGs list.
       */
      MHD_OPTION_END = 0,
      ...
      /**
       * Additional options given in an array of `struct MHD_OptionItem`.
       * The array must be terminated with an entry `{MHD_OPTION_END, 0, NULL}`.
       * An example for code using #MHD_OPTION_ARRAY is:
       *
       *     struct MHD_OptionItem ops[] = {
       *       { MHD_OPTION_CONNECTION_LIMIT, 100, NULL },
       *       { MHD_OPTION_CONNECTION_TIMEOUT, 10, NULL },
       *       { MHD_OPTION_END, 0, NULL }
       *     };
       *     d = MHD_start_daemon (0, 8080, NULL, NULL, dh, NULL,
       *                           MHD_OPTION_ARRAY, ops,
       *                           MHD_OPTION_END);
       *
       * For options that expect a single pointer argument, the
       * second member of the `struct MHD_OptionItem` is ignored.
       * For options that expect two pointer arguments, the first
       * argument must be cast to `intptr_t`.
       */
      MHD_OPTION_ARRAY = 15,
      ...
    };

待续...

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
libmicrohttpd是一种用于创建简单而灵活的HTTP服务器的C库。在安装libmicrohttpd之前,需要确保系统上已经安装了以下依赖项: 1. GCC:作为C语言的编译器,GCC是必需的。可以通过在终端中运行以下命令来检查GCC是否安装:gcc --version 如果GCC未安装,可以使用以下命令在Ubuntu上安装GCC: sudo apt-get install build-essential 2. GNU Autotools:用于自动配置和构建软件的工具集,包括autoconf、automake和libtool。可以使用以下命令安装GNU Autotools: sudo apt-get install autoconf automake libtool 3. GNU Make:用于自动化构建过程的工具。可以使用以下命令安装GNU Make: sudo apt-get install make 4. zlib库:用于处理压缩和解压缩的库函数。可以使用以下命令安装zlib库: sudo apt-get install zlib1g-dev 5. GNU TLS库:用于处理加密和TLS通信的库函数。可以使用以下命令安装GNU TLS库: sudo apt-get install libgnutls28-dev 安装以上依赖项后,可以按照以下步骤安装libmicrohttpd: 1. 下载libmicrohttpd的源代码包,可以从官方网站(https://www.gnu.org/software/libmicrohttpd/)或其他可靠的软件源下载。 2. 解压源代码包,进入解压后的目录。 3. 运行以下命令以生成配置文件: ./configure 如果出现错误提示缺少其他依赖项,可以根据错误信息安装相应的依赖库。 4. 运行以下命令以编译并安装libmicrohttpd: make sudo make install 编译过程可能需要一些时间,取决于系统的性能。 5. 完成安装后,可以在系统上编写和运行使用libmicrohttpd的程序了。 希望以上回答能够帮助您顺利安装libmicrohttpd及其依赖项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值