uhttpd: add support for configuration option ubus_cors
[openwrt/staging/chunkeey.git] / package / network / services / uhttpd / files / uhttpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010 Jo-Philipp Wich
3
4 START=50
5
6 USE_PROCD=1
7
8 UHTTPD_BIN="/usr/sbin/uhttpd"
9 PX5G_BIN="/usr/sbin/px5g"
10
11 append_arg() {
12 local cfg="$1"
13 local var="$2"
14 local opt="$3"
15 local def="$4"
16 local val
17
18 config_get val "$cfg" "$var"
19 [ -n "$val" -o -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
20 }
21
22 append_bool() {
23 local cfg="$1"
24 local var="$2"
25 local opt="$3"
26 local def="$4"
27 local val
28
29 config_get_bool val "$cfg" "$var" "$def"
30 [ "$val" = 1 ] && procd_append_param command "$opt"
31 }
32
33 generate_keys() {
34 local cfg="$1"
35 local key="$2"
36 local crt="$3"
37 local days bits country state location commonname
38
39 config_get days "$cfg" days
40 config_get bits "$cfg" bits
41 config_get country "$cfg" country
42 config_get state "$cfg" state
43 config_get location "$cfg" location
44 config_get commonname "$cfg" commonname
45
46 [ -x "$PX5G_BIN" ] && {
47 $PX5G_BIN selfsigned -der \
48 -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "${UHTTPD_KEY}.new" -out "${UHTTPD_CERT}.new" \
49 -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-OpenWrt}"
50 sync
51 mv "${UHTTPD_KEY}.new" "${UHTTPD_KEY}"
52 mv "${UHTTPD_CERT}.new" "${UHTTPD_CERT}"
53 }
54 }
55
56 start_instance()
57 {
58 UHTTPD_CERT=""
59 UHTTPD_KEY=""
60
61 local cfg="$1"
62 local realm="$(uci_get system.@system[0].hostname)"
63 local listen http https interpreter indexes path handler
64
65 procd_open_instance
66 procd_set_param respawn
67 procd_set_param stderr 1
68 procd_set_param command "$UHTTPD_BIN" -f
69
70 append_arg "$cfg" home "-h"
71 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
72 append_arg "$cfg" config "-c"
73 append_arg "$cfg" cgi_prefix "-x"
74 [ -f /usr/lib/uhttpd_lua.so ] && {
75 config_get handler "$cfg" lua_handler
76 [ -f "$handler" ] && append_arg "$cfg" lua_prefix "-l" && {
77 procd_append_param command "-L" "$handler"
78 }
79 }
80 [ -f /usr/lib/uhttpd_ubus.so ] && {
81 append_arg "$cfg" ubus_prefix "-u"
82 append_arg "$cfg" ubus_socket "-U"
83 append_bool "$cfg" ubus_cors "-X" 0
84 }
85 append_arg "$cfg" script_timeout "-t"
86 append_arg "$cfg" network_timeout "-T"
87 append_arg "$cfg" http_keepalive "-k"
88 append_arg "$cfg" tcp_keepalive "-A"
89 append_arg "$cfg" error_page "-E"
90 append_arg "$cfg" max_requests "-n" 3
91 append_arg "$cfg" max_connections "-N"
92
93 append_bool "$cfg" no_ubusauth "-a" 0
94 append_bool "$cfg" no_symlinks "-S" 0
95 append_bool "$cfg" no_dirlists "-D" 0
96 append_bool "$cfg" rfc1918_filter "-R" 0
97
98 config_get alias_list "$cfg" alias
99 for alias in $alias_list; do
100 procd_append_param command -y "$alias"
101 done
102
103 config_get http "$cfg" listen_http
104 for listen in $http; do
105 procd_append_param command -p "$listen"
106 done
107
108 config_get interpreter "$cfg" interpreter
109 for path in $interpreter; do
110 procd_append_param command -i "$path"
111 done
112
113 config_get indexes "$cfg" index_page
114 for path in $indexes; do
115 procd_append_param command -I "$path"
116 done
117
118 config_get https "$cfg" listen_https
119 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
120 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
121
122 [ -f /lib/libustream-ssl.so ] && [ -n "$https" ] && {
123 [ -s "$UHTTPD_CERT" -a -s "$UHTTPD_KEY" ] || {
124 config_foreach generate_keys cert
125 }
126
127 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
128 append_arg "$cfg" cert "-C"
129 append_arg "$cfg" key "-K"
130
131 for listen in $https; do
132 procd_append_param command -s "$listen"
133 done
134 }
135
136 append_bool "$cfg" redirect_https "-q" 0
137 }
138
139 for file in /etc/uhttpd/*.json; do
140 [ -s "$file" ] && procd_append_param command -H "$file"
141 done
142
143 procd_close_instance
144 }
145
146 service_triggers()
147 {
148 procd_add_reload_trigger "uhttpd"
149 }
150
151 start_service() {
152 config_load uhttpd
153 config_foreach start_instance uhttpd
154 }