@@ -18,12 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1818package main
1919
2020import (
21- "bufio"
2221 "flag"
2322 "fmt"
2423 "html/template"
25- "net/url "
24+ "net/http/cgi "
2625 "os"
26+ "strings"
2727)
2828
2929const (
@@ -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.
6048func 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-
12475func 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 }
0 commit comments