Skip to content

Commit d355832

Browse files
committed
Fix the extra new lines after saving
1 parent 4826077 commit d355832

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

package/src/synoedit/http_response.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewHTTPResponse(statusCode int, statusMessage string) *HTTPResponse {
3737
statusCode: statusCode,
3838
Status: "Status: " + fmt.Sprintf("%v", statusCode) + " " + statusMessage + "\r\n",
3939
Server: "Server: synoedit " + AppVersion + "\r\n",
40-
ContentType: "Content-Type: text/html; charset=utf-8\r\n",
40+
ContentType: "Content-Type: text/plain; charset=utf-8\r\n",
4141
}
4242
return &HTTPResponse
4343
}
@@ -71,14 +71,15 @@ func notFound(str ...string) {
7171

7272
// Return HTML with OK status message
7373
func okHTML(str ...string) {
74+
okPlainRes := NewHTTPResponse(200, "OK")
75+
okPlainRes.ContentType = "Content-Type: text/html; charset=utf-8;\r\n"
76+
okPlainRes.print(strings.Join(str, " "))
7477
NewHTTPResponse(200, "OK").print(strings.Join(str, " "))
7578
os.Exit(0)
7679
}
7780

7881
// Return plain text with OK status message
7982
func okPlain(str ...string) {
80-
okPlainRes := NewHTTPResponse(200, "OK")
81-
okPlainRes.ContentType = "Content-Type: text/plain;\r\n"
82-
okPlainRes.print(strings.Join(str, " "))
83+
NewHTTPResponse(200, "OK").print(strings.Join(str, " "))
8384
os.Exit(0)
8485
}

package/src/synoedit/main.go

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

2020
import (
21+
"bufio"
2122
"flag"
2223
"fmt"
2324
"html/template"
24-
"io/ioutil"
2525
"net/url"
2626
"os"
2727
)
@@ -72,8 +72,11 @@ func renderHTML(fileData string, successMessage string, errorMessage string) {
7272
page.Applications = config.Applications
7373
page.ErrorMessage = errorMessage
7474
page.SuccessMessage = successMessage
75-
fmt.Print("Status: 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nServer: synoedit", AppVersion)
76-
fmt.Print("\r\n\r\n")
75+
fmt.Print(
76+
"Status: 200 OK\r\n",
77+
"Content-Type: text/html; charset=utf-8\r\n",
78+
"Server: synoedit", AppVersion, "\r\n",
79+
"\r\n")
7780
err = tmpl.Execute(os.Stdout, page)
7881
if err != nil {
7982
logError(err.Error())
@@ -92,14 +95,26 @@ func readGet() url.Values {
9295
}
9396

9497
// Read POST parameters and return them as an Object
95-
func readPost() url.Values { // todo: stop on a max size (10mb?)
98+
func readPost() url.Values {
9699
// fixme: check/generate csrf token
97-
bytes, err := ioutil.ReadAll(os.Stdin) // if there is no data the process will block (wait)
98-
if err != nil {
99-
logError(err.Error())
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+
}
100115
}
101116

102-
q, err := url.ParseQuery(string(bytes))
117+
q, err := url.ParseQuery(strings.TrimSpace(string(bytes)))
103118
if err != nil {
104119
logError(err.Error())
105120
}

package/src/synoedit/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func GetFilePath(appName string, fileName string) string {
3131
}
3232
}
3333
logError("File not found in App configuration!")
34-
return "" // exit early (file not found)
34+
return ""
3535
}
3636
logError("App not found in configuration!")
37-
return "" // exit early (app not found)
37+
return ""
3838
}
3939

4040
// Look for command in $PATH or Check that file exits

0 commit comments

Comments
 (0)