C开发的小型Http服务器,GNU libmicrohttpd 发布 0.9.34 版本

jopen 11年前

GNU libmicrohttpd是一个小的C库,实现了嵌入式HTTP服务器功能可集成到其它应用程序中。它是可重入的,速度快,支持HTTP1.1,并允许监听多个端口。该API是​​简单的,仍然强大到足以允许程序员使用整个HTTP功能集。支持SSL / TLS是作为可选项。

下面是一个最为简单的使用例子:

#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;  }

发布说明: This release removes the dependency on Plibc for W32 and makes various other improvements to the W32 build process. IPv4/IPv6 dual stack should now also work on W32. On Linux, support for TCP FASTOPEN was added. Keep-Alive with HTTP 1.0 is now supported if explicitly requested by the client. The release also adds explicit annotations to hide non-exported symbols and provides various other minor code cleanups.