Skip to content

Commit 3e073e0

Browse files
committed
Improve CGI / DSM support
1 parent d355832 commit 3e073e0

2 files changed

Lines changed: 26 additions & 63 deletions

File tree

package/src/synoedit/main.go

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
package main
1919

2020
import (
21-
"bufio"
2221
"flag"
2322
"fmt"
2423
"html/template"
25-
"net/url"
24+
"net/http/cgi"
2625
"os"
26+
"strings"
2727
)
2828

2929
const (
@@ -44,18 +44,6 @@ type Page struct {
4444
Applications map[string]ApplicationConfig
4545
}
4646

47-
// https://stackoverflow.com/questions/44675087/golang-template-variable-isset
48-
// func templateIsset(name string, data interface{}) bool {
49-
// v := reflect.ValueOf(data)
50-
// if v.Kind() == reflect.Ptr {
51-
// v = v.Elem()
52-
// }
53-
// if v.Kind() != reflect.Struct {
54-
// return false
55-
// }
56-
// return v.FieldByName(name).IsValid()
57-
// }
58-
5947
// Return HTML from layout.html.
6048
func renderHTML(fileData string, successMessage string, errorMessage string) {
6149
var page Page
@@ -75,7 +63,7 @@ func renderHTML(fileData string, successMessage string, errorMessage string) {
7563
fmt.Print(
7664
"Status: 200 OK\r\n",
7765
"Content-Type: text/html; charset=utf-8\r\n",
78-
"Server: synoedit", AppVersion, "\r\n",
66+
"Server: synoedit ", AppVersion, "\r\n",
7967
"\r\n")
8068
err = tmpl.Execute(os.Stdout, page)
8169
if err != nil {
@@ -84,43 +72,6 @@ func renderHTML(fileData string, successMessage string, errorMessage string) {
8472
os.Exit(0)
8573
}
8674

87-
// Read GET parameters and return them as an Object
88-
func readGet() url.Values {
89-
queryStr := os.Getenv("QUERY_STRING")
90-
q, err := url.ParseQuery(queryStr)
91-
if err != nil {
92-
logError(err.Error())
93-
}
94-
return q
95-
}
96-
97-
// Read POST parameters and return them as an Object
98-
func readPost() url.Values {
99-
// fixme: check/generate csrf token
100-
var bytes []byte
101-
size := 0
102-
reader := bufio.NewReader(os.Stdin)
103-
for {
104-
b, err := reader.ReadByte()
105-
if err != nil {
106-
break
107-
}
108-
109-
bytes = append(bytes, b)
110-
size++
111-
if (size > 10000000) { // stop reading at 10 megabytes
112-
logError("Stopped reading POST data at 10Mb. Too Much Data!" + err.Error())
113-
break
114-
}
115-
}
116-
117-
q, err := url.ParseQuery(strings.TrimSpace(string(bytes)))
118-
if err != nil {
119-
logError(err.Error())
120-
}
121-
return q
122-
}
123-
12475
func main() {
12576
// Todo:
12677
// fix-up error handling with correct http responses (add --debug flag?/Synology's notifications?)
@@ -148,15 +99,24 @@ func main() {
14899
rootDir = "/var/packages/"
149100
}
150101

102+
// Retrieve Form Values
103+
httpReqest, err := cgi.Request()
104+
if err != nil {
105+
logError(err.Error())
106+
}
107+
if err = httpReqest.ParseForm(); err != nil {
108+
logError(err.Error())
109+
}
110+
111+
ajax := strings.TrimSpace(httpReqest.FormValue("ajax"))
112+
appName := strings.TrimSpace(httpReqest.FormValue("app"))
113+
fileName := strings.TrimSpace(httpReqest.FormValue("file"))
114+
action := strings.TrimSpace(httpReqest.FormValue("action"))
115+
fileData := strings.TrimSpace(httpReqest.FormValue("fileContent"))
116+
151117
// Http
152118
method := os.Getenv("REQUEST_METHOD")
153119
if method == "POST" || method == "PUT" || method == "PATCH" { // POST
154-
postData := readPost()
155-
fileData := postData.Get("fileContent")
156-
ajax := postData.Get("ajax")
157-
appName := postData.Get("app")
158-
fileName := postData.Get("file")
159-
action := postData.Get("action")
160120
if action != "" && appName != "" {
161121
output := ExecuteAction(appName)
162122

@@ -179,14 +139,11 @@ func main() {
179139
}
180140

181141
if method == "GET" { // GET
182-
var fileData = ""
183-
appName := readGet().Get("app")
184-
fileName := readGet().Get("file")
185142
if appName != "" && fileName != "" {
186143
fileData = ReadFile(GetFilePath(appName, fileName))
187144
}
188145

189-
if ajax := readGet().Get("ajax"); ajax == "true" {
146+
if ajax == "true" {
190147
// expect an ajax response
191148
okPlain(fileData)
192149
}

package/ui/test.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export QUERY_STRING="app=dnscrypt-proxy&file=domains-whitelist.txt"
4545
fixLinks test/file2.html
4646

4747
export REQUEST_METHOD=POST
48+
export CONTENT_TYPE="application/x-www-form-urlencoded"
49+
# echo "jax=true&app=dnscrypt-proxy&file=domains-whitelist.txt&fileContent=google.com%0A" | wc -c
50+
export CONTENT_LENGTH=81
4851
# data="$(urlencode "$(cat test/dnscrypt-proxy/target/var/domains-whitelist.txt)")"
4952
data="$(urlencode "google.com")"
5053
# echo "$data" > post.txt
@@ -54,4 +57,7 @@ echo "ajax=true&app=dnscrypt-proxy&file=domains-whitelist.txt&fileContent=$data"
5457
fixLinks test/post.html
5558

5659
export REQUEST_METHOD=POST
57-
echo "ajax=true&action=true&app=dnscrypt-proxy" | ./index.cgi --dev > test/action.html
60+
export CONTENT_TYPE="application/x-www-form-urlencoded"
61+
# echo "ajax=true&action=true&app=dnscrypt-proxy" | wc -c
62+
export CONTENT_LENGTH=41
63+
echo "ajax=true&action=true&app=dnscrypt-proxy" | ./index.cgi --dev | tail -n +5 > test/action.html

0 commit comments

Comments
 (0)