shttpd是一款小型的web server(相比较Apache),但功能却特别丰富,支持CGI、SSL、MD5认证、cookies,不需要配置文件,纯c打造,把需要的功能编译为一个静态库(.a文件),非常适合应用在嵌入式设备中。
这个章节介绍如何在Linux(centos)下编译本身及example文件,后面还会介绍与JQuery easyUI相结合如何使用。
使用的版本为shttpd-1.42.tar.gz。
到:下载
解压:
tar xvf shttpd-1.42.tar.gz
因为不想使用SSL功能,把相关的功能屏蔽掉,在src/Makefile文件的空白处添加:CFLAGS=-DNO_SSL,看起来像这样:
# LIB=c:\path_to_msvs6\lib
# INCLUDE=c:\path_to_msvs6\include
# 3. start console, go to shttpd-VERSION\src\ directory
# 4. type "nmake msvc"
# 5. go to shttpd-VERSION\examples , type "nmake msvc"
CFLAGS=-DNO_SSL
all:
@echo "make (unix|msvc|mingw|rtems)"
@echo 'Linux: "LIBS=-ldl make unix"'
@echo 'BSD: "LIBS=-lpthread make unix"'
@echo 'Solaris: "LIBS="-lpthread -lnsl -lsocket" make unix"'
.c.o:
编译:
LIBS="-lpthread " make unix
成功后,会产生静态库libshttpd.a,后面会用到这个文件。
进到目录examples/下,修改example.c文件,去掉暂时不需要的SSL等功能,看起来如下:
ctx = shttpd_init(argc, argv);
//shttpd_set_option(ctx, "ssl_cert", "shttpd.pem");
shttpd_set_option(ctx, "aliases", ALIAS_URI "=" ALIAS_DIR);
//shttpd_set_option(ctx, "ports", "8080,8081s");
/* Register an index page under two URIs */
shttpd_register_uri(ctx, "/", &show_index, (void *) &data);
shttpd_register_uri(ctx, "/abc.html", &show_index, (void *) &data);
/* Register a callback on wildcard URI */
shttpd_register_uri(ctx, "/users/*/", &show_users, NULL);
/* Show how to use password protection */
shttpd_register_uri(ctx, "/secret", &show_secret, NULL);
shttpd_set_option(ctx, "protect", "/secret=passfile");
/* Show how to use stateful big data transfer */
shttpd_register_uri(ctx, "/huge", &show_huge, NULL);
/* Register URI for file upload */
shttpd_register_uri(ctx, "/post", &show_post, NULL);
/* Register SSI callbacks */
//shttpd_register_ssi_func(ctx, "true", ssi_test_true, NULL);
//shttpd_register_ssi_func(ctx, "false", ssi_test_false, NULL);
//shttpd_register_ssi_func(ctx, "print_stuff", ssi_print_stuff, NULL);
使用下面命令编译:
gcc example.c -I ../src ../src/libshttpd.a -ldl -lpthread
成功后,执行./a.out,在远程机器IE的地址栏中输入:http://192.168.x.x/,看到下面界面,可做简单的验证。