小型的嵌入式 HTTP 服務器 GNU libmicrohttpd 0.9.21 發布

openkk 12年前發布 | 10K 次閱讀 HTTP 服務器

GNU libmicrohttpd 0.9.21 發布,GNU libmicrohttpd 是一個小型的嵌入式 HTTP 服務器 的 C 類庫,支持 HTTP 1.1 可以同時偵聽多個端口。

  • C library: fast and small
  • API is simple, expressive and fully reentrant
  • Implementation is HTTP 1.1 compliant
  • HTTP server can listen on multiple ports
  • Four different threading models (select, poll, pthread, thread pool)
  • Supported platforms include GNU/Linux, FreeBSD, OpenBSD, NetBSD, OS X, W32, Symbian and z/OS
  • Support for IPv6
  • Support for SHOUTcast
  • Support for incremental processing of POST data (optional)
  • Support for basic and digest authentication (optional)
  • Support for SSL3 and TLS (requires libgcrypt and libgnutls, optional)
  • Binary is only about 32k (without TLS/SSL support and other optional features)

下面是一個最為簡單的使用例子:

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

#define PAGE "<html><head><title>libmicrohttpd demo</title>"\
             "</head><body>libmicrohttpd demo</body></html>"

static int ahc_echo(void * cls,
            struct MHD_Connection * connection,
            const char * url,
            const char * method,
                    const char * version,
            const char * upload_data,
            size_t * upload_data_size,
                    void ** ptr) {
  static int dummy;
  const char * page = cls;
  struct MHD_Response * response;
  int ret;

  if (0 != strcmp(method, "GET"))
    return MHD_NO; /* unexpected method */
  if (&dummy != *ptr) 
    {
      /* The first time only the headers are valid,
         do not respond in the first round... */
      *ptr = &dummy;
      return MHD_YES;
    }
  if (0 != *upload_data_size)
    return MHD_NO; /* upload data in a GET!? */
  *ptr = NULL; /* clear context pointer */
  response = MHD_create_response_from_data(strlen(page),
                       (void*) page,
                       MHD_NO,
                       MHD_NO);
  ret = MHD_queue_response(connection,
               MHD_HTTP_OK,
               response);
  MHD_destroy_response(response);
  return ret;
}

int main(int argc,
     char ** argv) {
  struct MHD_Daemon * d;
  if (argc != 2) {
    printf("%s PORT\n",
       argv[0]);
    return 1;
  }
  d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
               atoi(argv[1]),
               NULL,
               NULL,
               &ahc_echo,
               PAGE,
               MHD_OPTION_END);
  if (d == NULL)
    return 1;
  (void) getc ();
  MHD_stop_daemon(d);
  return 0;
}

0.9.21:

This release fixes potential problems with non-monotonic increasing system time values, makes sockets non-inheritable by default, and enables MHD to operate without a "listen" socket (for special circumstances).

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!