Skip to content

Commit

Permalink
Added -500 option: for return 500 error if shell exit code != 0 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
msoap committed Mar 11, 2023
1 parent 16130c9 commit 80a0f4e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Usage
-one-thread : run each shell command in one thread
-show-errors : show the standard output even if the command exits with a non-zero exit code
-include-stderr : include stderr to output (default is stdout only)
-500 : return 500 error if shell exit code != 0
-cert=cert.pem : SSL certificate path (if specified -cert/-key options - run https server)
-key=key.pem : SSL private key path
-basic-auth="" : setup HTTP Basic Authentication ("user_name:password"), can be used several times
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Config struct {
oneThread bool // run each shell commands in one thread
showErrors bool // returns the standard output even if the command exits with a non-zero exit code
includeStderr bool // also returns output written to stderr (default is stdout only)
intServerErr bool // return 500 error if shell status code != 0
formCheckRe *regexp.Regexp // regexp for check form fields
}

Expand Down Expand Up @@ -102,6 +103,7 @@ func getConfig() (*Config, error) {
flag.BoolVar(&cfg.oneThread, "one-thread", false, "run each shell command in one thread")
flag.BoolVar(&cfg.showErrors, "show-errors", false, "show the standard output even if the command exits with a non-zero exit code")
flag.BoolVar(&cfg.includeStderr, "include-stderr", false, "include stderr to output (default is stdout only)")
flag.BoolVar(&cfg.intServerErr, "500", false, "return 500 error if shell exit code != 0")
flag.StringVar(&cfg.cert, "cert", "", "SSL certificate `path` (if specified -cert/-key options - run https server)")
flag.StringVar(&cfg.key, "key", "", "SSL private key `/path/...`")
flag.Var(&cfg.auth, "basic-auth", "setup HTTP Basic Authentication (\"user_name:password\"), can be used several times")
Expand Down
3 changes: 2 additions & 1 deletion shell2http.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "SHELL2HTTP" "" "February 2023" "" ""
.TH "SHELL2HTTP" "" "March 2023" "" ""
HTTP\-server to execute shell commands\. Designed for development, prototyping or remote control\. Settings through two command line arguments, path and shell command\.
.
.SH "Usage"
Expand Down Expand Up @@ -29,6 +29,7 @@ options:
\-one\-thread : run each shell command in one thread
\-show\-errors : show the standard output even if the command exits with a non\-zero exit code
\-include\-stderr : include stderr to output (default is stdout only)
\-500 : return 500 error if shell exit code != 0
\-cert=cert\.pem : SSL certificate path (if specified \-cert/\-key options \- run https server)
\-key=key\.pem : SSL private key path
\-basic\-auth="" : setup HTTP Basic Authentication ("user_name:password"), can be used several times
Expand Down
25 changes: 14 additions & 11 deletions shell2http.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,15 @@ func getShellHandler(appConfig Config, shell string, params []string, cacheTTL r
log.Printf("out: %s, exec error: %s", string(shellOut), err)
}

rw.Header().Set("X-Shell2http-Exit-Code", strconv.Itoa(exitCode))
customStatusCode := 0
outText := string(shellOut)

if err != nil && !appConfig.showErrors {
responseWrite(rw, fmt.Sprintf("%s\nexec error: %s", string(shellOut), err))
outText = fmt.Sprintf("%s\nexec error: %s", string(shellOut), err)
} else {
outText := string(shellOut)
if appConfig.setCGI {
var headers map[string]string
outText, headers = parseCGIHeaders(outText)
customStatusCode := 0

for headerKey, headerValue := range headers {
switch headerKey {
Expand All @@ -170,14 +169,18 @@ func getShellHandler(appConfig Config, shell string, params []string, cacheTTL r

rw.Header().Set(headerKey, headerValue)
}

if customStatusCode > 0 {
rw.WriteHeader(customStatusCode)
}
}
}

responseWrite(rw, outText)
rw.Header().Set("X-Shell2http-Exit-Code", strconv.Itoa(exitCode))

if customStatusCode > 0 {
rw.WriteHeader(customStatusCode)
} else if exitCode > 0 && appConfig.intServerErr {
rw.WriteHeader(http.StatusInternalServerError)
}

responseWrite(rw, outText)
}
}

Expand Down Expand Up @@ -408,15 +411,15 @@ func parseCGIHeaders(shellOut string) (string, map[string]string) {
headersMap[headerParts[1]] = headerParts[2]
} else {
// headers is not valid, return all text
return shellOut, map[string]string{}
return shellOut, nil
}
}

return parts[1], headersMap
}

// headers don't found, return all text
return shellOut, map[string]string{}
return shellOut, nil
}

// getForm - parse form into environment vars, also handle uploaded files
Expand Down
10 changes: 5 additions & 5 deletions shell2http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_parseCGIHeaders(t *testing.T) {
{
in: "Some text",
out: "Some text",
headers: map[string]string{},
headers: nil,
},
{
in: "Location: url\n\nSome text",
Expand All @@ -42,12 +42,12 @@ func Test_parseCGIHeaders(t *testing.T) {
{
in: "Some text\nText\n\ntext",
out: "Some text\nText\n\ntext",
headers: map[string]string{},
headers: nil,
},
{
in: "Some text\nText: value in text\n\ntext",
out: "Some text\nText: value in text\n\ntext",
headers: map[string]string{},
headers: nil,
},
{
in: "Text::::\n\ntext",
Expand All @@ -62,12 +62,12 @@ func Test_parseCGIHeaders(t *testing.T) {
{
in: "Text: \n\ntext",
out: "Text: \n\ntext",
headers: map[string]string{},
headers: nil,
},
{
in: "Header: value\nText: \n\ntext",
out: "Header: value\nText: \n\ntext",
headers: map[string]string{},
headers: nil,
},
{
in: "Location: url\r\nX-Name: x-value\r\n\r\nOn Windows",
Expand Down

0 comments on commit 80a0f4e

Please sign in to comment.