Skip to content

Commit 4826077

Browse files
committed
Code review
1 parent 8493392 commit 4826077

3 files changed

Lines changed: 25 additions & 18 deletions

File tree

package/src/synoedit/http_response.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
type HTTPResponse struct {
2828
statusCode int
2929
Status string
30+
Server string
3031
ContentType string
3132
}
3233

@@ -35,15 +36,16 @@ func NewHTTPResponse(statusCode int, statusMessage string) *HTTPResponse {
3536
HTTPResponse := HTTPResponse{
3637
statusCode: statusCode,
3738
Status: "Status: " + fmt.Sprintf("%v", statusCode) + " " + statusMessage + "\r\n",
39+
Server: "Server: synoedit " + AppVersion + "\r\n",
3840
ContentType: "Content-Type: text/html; charset=utf-8\r\n",
3941
}
4042
return &HTTPResponse
4143
}
4244

4345
// Print http response to stdout
4446
func (HTTPResponse *HTTPResponse) print(str ...string) {
45-
fmt.Print(HTTPResponse.Status + HTTPResponse.ContentType + "\r\n\r\n")
46-
fmt.Println(strings.Join(str, " "))
47+
fmt.Print(HTTPResponse.Status + HTTPResponse.ContentType + HTTPResponse.Server + "\r\n")
48+
fmt.Print(strings.Join(str, " "))
4749
os.Exit(0)
4850
}
4951

@@ -66,3 +68,17 @@ func notFound(str ...string) {
6668
NewHTTPResponse(404, "Not Found").print(strings.Join(str, " "))
6769
os.Exit(0)
6870
}
71+
72+
// Return HTML with OK status message
73+
func okHTML(str ...string) {
74+
NewHTTPResponse(200, "OK").print(strings.Join(str, " "))
75+
os.Exit(0)
76+
}
77+
78+
// Return plain text with OK status message
79+
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+
os.Exit(0)
84+
}

package/src/synoedit/main.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,28 +146,21 @@ func main() {
146146
output := ExecuteAction(appName)
147147

148148
if ajax == "true" {
149-
fmt.Print("Status: 200 OK\r\nContent-Type: text/plain;\r\n\r\n")
150-
fmt.Print(output)
151-
return
149+
okPlain(output)
152150
}
153151
renderHTML(fileData, "Not implemented", "")
154-
return
155152
}
156153

157154
if fileData != "" && appName != "" && fileName != "" {
158155
filePath := GetFilePath(appName, fileName)
159156
SaveFile(filePath, fileData)
160157

161158
if ajax == "true" {
162-
fmt.Print("Status: 200 OK\r\nContent-Type: text/plain;\r\n\r\n")
163-
fmt.Print("File saved successfully!")
164-
return
159+
okPlain("File saved successfully!")
165160
}
166161
renderHTML(fileData, "File saved successfully!", "") // not complete
167-
return
168162
}
169163
logError("No valid data submitted.")
170-
return
171164
}
172165

173166
if method == "GET" { // GET
@@ -180,9 +173,7 @@ func main() {
180173

181174
if ajax := readGet().Get("ajax"); ajax == "true" {
182175
// expect an ajax response
183-
fmt.Print("Status: 200 OK\r\nContent-Type: text/plain;\r\n\r\n")
184-
fmt.Print(fileData)
185-
return
176+
okPlain(fileData)
186177
}
187178
// else respond with full html
188179
renderHTML(fileData, "", "") // not complete

package/ui/test.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ echo "example.com" > test/dnscrypt-proxy/target/var/domains-whitelist.txt
3030
export REQUEST_METHOD=GET
3131
export SERVER_PROTOCOL=HTTP/1.1
3232
mkdir -p test
33-
./index.cgi --dev | tail -n +4 > test/index.html
33+
./index.cgi --dev | tail -n +5 > test/index.html
3434
fixLinks test/index.html
3535

3636
## test GET file
3737
export REQUEST_METHOD=GET
3838
export QUERY_STRING="ajax=true&app=dnscrypt-proxy&file=domains-whitelist.txt"
39-
./index.cgi --dev | tail -n +4 > test/file.html
39+
./index.cgi --dev | tail -n +5 > test/file.html
4040
fixLinks test/file.html
4141

4242
export REQUEST_METHOD=GET
4343
export QUERY_STRING="app=dnscrypt-proxy&file=domains-whitelist.txt"
44-
./index.cgi --dev | tail -n +4 > test/file2.html
44+
./index.cgi --dev | tail -n +5 > test/file2.html
4545
fixLinks test/file2.html
4646

4747
export REQUEST_METHOD=POST
@@ -50,7 +50,7 @@ data="$(urlencode "google.com")"
5050
# echo "$data" > post.txt
5151

5252
# echo "ListenAddresses=0.0.0.0%3A1053+&ServerNames=cloudflare+google+ " | ./index.cgi --dev
53-
echo "ajax=true&app=dnscrypt-proxy&file=domains-whitelist.txt&fileContent=$data" | ./index.cgi --dev | tail -n +4 > test/post.html
53+
echo "ajax=true&app=dnscrypt-proxy&file=domains-whitelist.txt&fileContent=$data" | ./index.cgi --dev | tail -n +5 > test/post.html
5454
fixLinks test/post.html
5555

5656
export REQUEST_METHOD=POST

0 commit comments

Comments
 (0)