1.前言
shttpd是一款小型的web server(相比较Apache),但功能却特别丰富,支持CGI、SSL、MD5认证、cookies,不需要配置文件,纯c打造,把需要的功能编译为一个静态库(.a文件),非常适合应用在嵌入式设备中。
2. 下载源码:
官方源码:https://sourceforge.net/projects/shttpd/files/
git上非官方: https://github.com/pepstack/shttpd
3.编译:
3.1 编译shttpd源码的Makefile统一用以下:
3.2 修改
去注释,增加LIBS和CFLAGS定义
LIBS是增加库链接
CFLAGS因为不想使用SSL功能(使用的话需要链接相应库文件),把相关的功能屏蔽掉
进到目录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);
3.3 编译命令
编译shttpd库文件
1.编译静态库
make linux
成功后,会产生静态库libshttpd.a
2.编译动态库
make linuxso
成功后,会产生静态库libshttpd.so
编译example:
1.静态链接方式
gcc example.c -I ../src ../src/libshttpd.a -ldl -lpthread
2.动态库链接方式
gcc example.c -I ../src -lshttpd -ldl -lpthread
成功后,执行./a.out,在远程机器IE的地址栏中输入:http://x.x.x.x/(远程机器IP地址),看到下面界面,可做简单的验证。
参考文档:https://blog.csdn.net/dijkstar/article/details/81981851