include $(INCLUDE_DIR)/package.mk
-define Package/uhttpd
+define Package/uhttpd/default
SECTION:=net
CATEGORY:=Network
TITLE:=uHTTPd - tiny, single threaded HTTP server
- DEPENDS:=+liblua +libcyassl +zlib
+endef
+
+define Package/uhttpd
+ $(Package/uhttpd/default)
+ MENU:=1
endef
define Package/uhttpd/description
HTTP daemon.
endef
+
+define Package/uhttpd-mod-tls
+ $(Package/uhttpd/default)
+ TITLE+= (TLS plugin)
+ DEPENDS:=uhttpd +libcyassl
+endef
+
+define Package/uhttpd-mod-tls/description
+ The TLS plugin adds HTTPS support to uHTTPd.
+endef
+
+
+define Package/uhttpd-mod-lua
+ $(Package/uhttpd/default)
+ TITLE+= (Lua plugin)
+ DEPENDS:=uhttpd +liblua
+endef
+
+define Package/uhttpd-mod-lua/description
+ The Lua plugin adds a CGI-like Lua runtime interface to uHTTPd.
+endef
+
+
# hack to use CyASSL headers
TARGET_CFLAGS += -I$(firstword $(wildcard $(BUILD_DIR)/cyassl-*/include))
$(INSTALL_BIN) $(PKG_BUILD_DIR)/uhttpd $(1)/usr/sbin/uhttpd
endef
+define Package/uhttpd-mod-tls/install
+ $(INSTALL_DIR) $(1)/usr/lib
+ $(INSTALL_BIN) $(PKG_BUILD_DIR)/uhttpd_tls.so $(1)/usr/lib/
+endef
+
+define Package/uhttpd-mod-lua/install
+ $(INSTALL_DIR) $(1)/usr/lib
+ $(INSTALL_BIN) $(PKG_BUILD_DIR)/uhttpd_lua.so $(1)/usr/lib/
+endef
+
+
$(eval $(call BuildPackage,uhttpd))
+$(eval $(call BuildPackage,uhttpd-mod-tls))
+$(eval $(call BuildPackage,uhttpd-mod-lua))
TLS_SUPPORT ?= 1
CFLAGS ?= -I./lua-5.1.4/src -I./cyassl-1.4.0/include -O0 -ggdb3
-LDFLAGS ?= -L./lua-5.1.4/src -L./cyassl-1.4.0/src/.libs -lm
+LDFLAGS ?= -L./lua-5.1.4/src -L./cyassl-1.4.0/src/.libs
CFLAGS += -Wall --std=gnu99
-LDFLAGS += -lm -lcrypt
OBJ = uhttpd.o uhttpd-file.o uhttpd-utils.o
+LIB = -Wl,--export-dynamic -lcrypt -ldl
+
+TLSLIB =
+LUALIB =
+
+
+world: compile
ifeq ($(CGI_SUPPORT),1)
- OBJ += uhttpd-cgi.o
- CFLAGS += -DHAVE_CGI
+ OBJ += uhttpd-cgi.o
+ CFLAGS += -DHAVE_CGI
endif
ifeq ($(LUA_SUPPORT),1)
- OBJ += uhttpd-lua.o
- CFLAGS += -DHAVE_LUA
- LDFLAGS += -ldl -llua
+ CFLAGS += -DHAVE_LUA
+ LUALIB = uhttpd_lua.so
+
+ $(LUALIB): uhttpd-lua.c
+ $(CC) $(CFLAGS) $(LDFLAGS) $(FPIC) \
+ -shared -lm -llua -ldl \
+ -o $(LUALIB) uhttpd-lua.c
endif
ifeq ($(TLS_SUPPORT),1)
- OBJ += uhttpd-tls.o
- CFLAGS += -DHAVE_TLS
- LDFLAGS += -lpthread -lz -lcyassl
+ CFLAGS += -DHAVE_TLS
+ TLSLIB = uhttpd_tls.so
+
+ $(TLSLIB): uhttpd-tls.c
+ $(CC) $(CFLAGS) $(LDFLAGS) $(FPIC) \
+ -shared -lcyassl \
+ -o $(TLSLIB) uhttpd-tls.c
endif
%.o: %.c
- $(CC) $(CFLAGS) -c -o $@ $<
+ $(CC) $(CFLAGS) -c -o $@ $<
-compile: $(OBJ)
- $(CC) -o uhttpd $(LDFLAGS) $(OBJ)
+compile: $(OBJ) $(TLSLIB) $(LUALIB)
+ $(CC) -o uhttpd $(LDFLAGS) $(LIB) $(OBJ)
clean:
- rm -f *.o uhttpd
+ rm -f *.o *.so uhttpd
char bind[128];
char *port = NULL;
+ /* library handles */
+ void *tls_lib;
+ void *lua_lib;
+
/* clear the master and temp sets */
FD_ZERO(&used_fds);
FD_ZERO(&serv_fds);
memset(bind, 0, sizeof(bind));
#ifdef HAVE_TLS
- /* init SSL context */
- if( ! (conf.tls = uh_tls_ctx_init()) )
+ /* load TLS plugin */
+ if( ! (tls_lib = dlopen("uhttpd_tls.so", RTLD_LAZY | RTLD_GLOBAL)) )
{
- fprintf(stderr, "Failed to initalize SSL context\n");
- exit(1);
+ fprintf(stderr,
+ "Notice: Unable to load TLS plugin - disabling SSL support! "
+ "(Reason: %s)\n", dlerror()
+ );
+ }
+ else
+ {
+ /* resolve functions */
+ if( !(conf.tls_init = dlsym(tls_lib, "uh_tls_ctx_init")) ||
+ !(conf.tls_cert = dlsym(tls_lib, "uh_tls_ctx_cert")) ||
+ !(conf.tls_key = dlsym(tls_lib, "uh_tls_ctx_key")) ||
+ !(conf.tls_free = dlsym(tls_lib, "uh_tls_ctx_free")) ||
+ !(conf.tls_accept = dlsym(tls_lib, "uh_tls_client_accept")) ||
+ !(conf.tls_close = dlsym(tls_lib, "uh_tls_client_close")) ||
+ !(conf.tls_recv = dlsym(tls_lib, "uh_tls_client_recv")) ||
+ !(conf.tls_send = dlsym(tls_lib, "uh_tls_client_send"))
+ ) {
+ fprintf(stderr,
+ "Error: Failed to lookup required symbols "
+ "in TLS plugin: %s\n", dlerror()
+ );
+ exit(1);
+ }
+
+ /* init SSL context */
+ if( ! (conf.tls = conf.tls_init()) )
+ {
+ fprintf(stderr, "Error: Failed to initalize SSL context\n");
+ exit(1);
+ }
}
#endif
}
if( opt == 's' )
+ {
+ if( !conf.tls )
+ {
+ fprintf(stderr,
+ "Notice: TLS support is disabled, "
+ "ignoring '-s %s'\n", optarg
+ );
+ continue;
+ }
+
tls = 1;
+ }
/* bind sockets */
bound += uh_socket_bind(
&serv_fds, &max_fd, bind[0] ? bind : NULL, port,
- &hints, tls, &conf
+ &hints, (opt == 's'), &conf
);
break;
#ifdef HAVE_TLS
/* certificate */
case 'C':
- if( SSL_CTX_use_certificate_file(conf.tls, optarg, SSL_FILETYPE_ASN1) < 1 )
+ if( conf.tls )
{
- fprintf(stderr, "Invalid certificate file given\n");
- exit(1);
+ if( conf.tls_cert(conf.tls, optarg) < 1 )
+ {
+ fprintf(stderr,
+ "Error: Invalid certificate file given\n");
+ exit(1);
+ }
+
+ keys++;
}
- keys++;
break;
/* key */
case 'K':
- if( SSL_CTX_use_PrivateKey_file(conf.tls, optarg, SSL_FILETYPE_ASN1) < 1 )
+ if( conf.tls )
{
- fprintf(stderr, "Invalid private key file given\n");
- exit(1);
+ if( conf.tls_key(conf.tls, optarg) < 1 )
+ {
+ fprintf(stderr,
+ "Error: Invalid private key file given\n");
+ exit(1);
+ }
+
+ keys++;
}
- keys++;
break;
#endif
case 'h':
if( ! realpath(optarg, conf.docroot) )
{
- fprintf(stderr, "Invalid directory %s: %s\n", optarg, strerror(errno));
+ fprintf(stderr, "Error: Invalid directory %s: %s\n",
+ optarg, strerror(errno));
exit(1);
}
break;
#ifdef HAVE_TLS
if( (tls == 1) && (keys < 2) )
{
- fprintf(stderr, "Missing private key or certificate file\n");
+ fprintf(stderr, "Error: Missing private key or certificate file\n");
exit(1);
}
#endif
if( bound < 1 )
{
- fprintf(stderr, "No sockets bound, unable to continue\n");
+ fprintf(stderr, "Error: No sockets bound, unable to continue\n");
exit(1);
}
/* default docroot */
if( !conf.docroot[0] && !realpath(".", conf.docroot) )
{
- fprintf(stderr, "Can not determine default document root: %s\n",
+ fprintf(stderr, "Error: Can not determine default document root: %s\n",
strerror(errno));
exit(1);
}
#endif
#ifdef HAVE_LUA
- /* init Lua runtime if handler is specified */
- if( conf.lua_handler )
+ /* load Lua plugin */
+ if( ! (lua_lib = dlopen("uhttpd_lua.so", RTLD_LAZY | RTLD_GLOBAL)) )
{
- /* default lua prefix */
- if( ! conf.lua_prefix )
- conf.lua_prefix = "/lua";
+ fprintf(stderr,
+ "Notice: Unable to load Lua plugin - disabling Lua support! "
+ "(Reason: %s)\n", dlerror()
+ );
+ }
+ else
+ {
+ /* resolve functions */
+ if( !(conf.lua_init = dlsym(lua_lib, "uh_lua_init")) ||
+ !(conf.lua_close = dlsym(lua_lib, "uh_lua_close")) ||
+ !(conf.lua_request = dlsym(lua_lib, "uh_lua_request"))
+ ) {
+ fprintf(stderr,
+ "Error: Failed to lookup required symbols "
+ "in Lua plugin: %s\n", dlerror()
+ );
+ exit(1);
+ }
+
+ /* init Lua runtime if handler is specified */
+ if( conf.lua_handler )
+ {
+ /* default lua prefix */
+ if( ! conf.lua_prefix )
+ conf.lua_prefix = "/lua";
- L = uh_lua_init(conf.lua_handler);
+ L = conf.lua_init(conf.lua_handler);
+ }
}
#endif
{
#ifdef HAVE_TLS
/* setup client tls context */
- uh_tls_client_accept(cl);
+ if( conf.tls )
+ conf.tls_accept(cl);
#endif
/* add client socket to global fdset */
/* Lua request? */
if( L && uh_path_match(conf.lua_prefix, req->url) )
{
- uh_lua_request(cl, req, L);
+ conf.lua_request(cl, req, L);
}
else
#endif
#ifdef HAVE_TLS
/* free client tls context */
- uh_tls_client_close(cl);
+ if( conf.tls )
+ conf.tls_close(cl);
#endif
cleanup:
#ifdef HAVE_LUA
/* destroy the Lua state */
if( L != NULL )
- lua_close(L);
+ conf.lua_close(L);
#endif
return 0;