From 3a4a31f26f1699b9a4a7d6684a380a990bb9ac86 Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Wed, 10 Aug 2022 09:50:13 +0200
Subject: [PATCH 85/85] uhttpd: update to latest HEAD

Signed-off-by: John Crispin <john@phrozen.org>
---
 package/network/services/uhttpd/Makefile      |  43 +++--
 .../services/uhttpd/files/uhttpd.config       |   8 +
 .../network/services/uhttpd/files/uhttpd.init |  15 ++
 .../services/uhttpd/patches/error.patch       | 165 ------------------
 .../services/uhttpd/patches/path.patch        |  14 --
 5 files changed, 56 insertions(+), 189 deletions(-)
 delete mode 100644 package/network/services/uhttpd/patches/error.patch
 delete mode 100644 package/network/services/uhttpd/patches/path.patch

diff --git a/package/network/services/uhttpd/Makefile b/package/network/services/uhttpd/Makefile
index de666a480d..0ae076ca8b 100644
--- a/package/network/services/uhttpd/Makefile
+++ b/package/network/services/uhttpd/Makefile
@@ -8,19 +8,18 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=uhttpd
-PKG_RELEASE:=2
+PKG_RELEASE:=1
 
 PKG_SOURCE_PROTO:=git
 PKG_SOURCE_URL=$(PROJECT_GIT)/project/uhttpd.git
-PKG_SOURCE_DATE:=2021-03-21
-PKG_SOURCE_VERSION:=15346de8d3ba422002496526ee24c62a3601ab8c
-PKG_MIRROR_HASH:=819424d071ed7c8888f9ca66f679907831becc59a67dd4a5ec521d5fba0a3171
+PKG_SOURCE_DATE:=2022-06-01
+PKG_SOURCE_VERSION:=e3395cd90bed9b7b9fc319e79528fedcc0d947fe
 PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
 PKG_LICENSE:=ISC
 
 PKG_ASLR_PIE_REGULAR:=1
 PKG_BUILD_DEPENDS = ustream-ssl
-PKG_CONFIG_DEPENDS:= CONFIG_uhttpd_lua
+PKG_CONFIG_DEPENDS:= CONFIG_uhttpd_lua CONFIG_uhttpd_ucode
 
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
@@ -49,8 +48,20 @@ define Package/uhttpd/config
     depends on PACKAGE_uhttpd-mod-lua
     bool "Enable Integrated Lua interpreter"
 	default y
+
+  config uhttpd_ucode
+    depends on PACKAGE_uhttpd-mod-ucode
+    bool "Enable Integrated ucode interpreter"
+	default y
 endef
 
+define Package/uhttpd/conffiles
+/etc/config/uhttpd
+/etc/uhttpd.crt
+/etc/uhttpd.key
+endef
+
+
 define Package/uhttpd-mod-lua
   $(Package/uhttpd/default)
   TITLE+= (Lua plugin)
@@ -73,19 +84,25 @@ define Package/uhttpd-mod-ubus/description
  session.* namespace and procedures.
 endef
 
-define Package/uhttpd/conffiles
-/etc/config/uhttpd
-/etc/uhttpd.crt
-/etc/uhttpd.key
+
+define Package/uhttpd-mod-ucode
+  $(Package/uhttpd/default)
+  TITLE+= (ucode plugin)
+  DEPENDS:=uhttpd +libucode
+endef
+
+define Package/uhttpd-mod-ucode/description
+ The ucode plugin adds a CGI-like ucode runtime interface to uHTTPd.
 endef
 
+
 ifneq ($(CONFIG_USE_GLIBC),)
   TARGET_CFLAGS += -D_DEFAULT_SOURCE
 endif
 
 TARGET_LDFLAGS += -lcrypt
 
-CMAKE_OPTIONS = -DTLS_SUPPORT=on
+CMAKE_OPTIONS += -DTLS_SUPPORT=on
 
 define Package/uhttpd/install
 	$(INSTALL_DIR) $(1)/etc/init.d
@@ -108,7 +125,13 @@ define Package/uhttpd-mod-ubus/install
 	$(INSTALL_DATA) ./files/ubus.default $(1)/etc/uci-defaults/00_uhttpd_ubus
 endef
 
+define Package/uhttpd-mod-ucode/install
+	$(INSTALL_DIR) $(1)/usr/lib
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/uhttpd_ucode.so $(1)/usr/lib/
+endef
+
 
 $(eval $(call BuildPackage,uhttpd))
 $(eval $(call BuildPackage,uhttpd-mod-lua))
 $(eval $(call BuildPackage,uhttpd-mod-ubus))
+$(eval $(call BuildPackage,uhttpd-mod-ucode))
diff --git a/package/network/services/uhttpd/files/uhttpd.config b/package/network/services/uhttpd/files/uhttpd.config
index 40ce67fd01..a9b8ff3d15 100644
--- a/package/network/services/uhttpd/files/uhttpd.config
+++ b/package/network/services/uhttpd/files/uhttpd.config
@@ -57,6 +57,14 @@ config uhttpd main
 	# matches have precedence over the CGI prefix.
 	list lua_prefix		"/cgi-bin/luci=/usr/lib/lua/luci/sgi/uhttpd.lua"
 
+	# List of prefix->ucode handler mappings.
+	# Any request to an URL beneath the prefix
+	# will be dispatched to the associated ucode
+	# handler script. Ucode support is disabled when
+	# no handler mappings are specified. Ucode prefix
+	# matches have precedence over the CGI prefix.
+#	list ucode_prefix		"/ucode/example=/usr/share/example.uc"
+
 	# Specify the ubus-rpc prefix and socket path.
 #	option ubus_prefix	/ubus
 #	option ubus_socket	/var/run/ubus/ubus.sock
diff --git a/package/network/services/uhttpd/files/uhttpd.init b/package/network/services/uhttpd/files/uhttpd.init
index 30fd7b4259..8dbc23f59c 100755
--- a/package/network/services/uhttpd/files/uhttpd.init
+++ b/package/network/services/uhttpd/files/uhttpd.init
@@ -91,6 +91,18 @@ append_lua_prefix() {
 	fi
 }
 
+append_ucode_prefix() {
+	local v="$1"
+	local prefix="${v%%=*}"
+	local handler="${v#*=}"
+
+	if [ "$prefix" != "$handler" ] && [ -n "$prefix" ] && [ -f "$handler" ]; then
+		procd_append_param command -o "$prefix" -O "$handler"
+	else
+		echo "Skipping invalid ucode prefix \"$v\"" >&2
+	fi
+}
+
 start_instance()
 {
 	UHTTPD_CERT=""
@@ -142,6 +154,9 @@ start_instance()
 		append_arg "$cfg" ubus_socket "-U"
 		append_bool "$cfg" ubus_cors "-X" 0
 	}
+	[ -f /usr/lib/uhttpd_ucode.so ] && {
+		config_list_foreach "$cfg" ucode_prefix append_ucode_prefix
+	}
 	append_arg "$cfg" script_timeout "-t"
 	append_arg "$cfg" network_timeout "-T"
 	append_arg "$cfg" http_keepalive "-k"

