学习libmicrohttpd - Go语言中文社区

学习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: %sn", 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.55w32相关的版本进行打开工程,进行编译使用;
    注意:Windows平台使用该库时,包含的头文件路径为:libmicrohttpd-0.9.55w32common
              否则会出现各种编译、使用问题;

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,
      ...
    };

待续...

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lsyyff23/article/details/79703823
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-27 23:33:54
  • 阅读 ( 1319 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢