diff options
author | Yotam Nachum <me@yotam.net> | 2020-03-14 12:59:10 +0200 |
---|---|---|
committer | Yotam Nachum <me@yotam.net> | 2020-03-14 12:59:10 +0200 |
commit | 129b3e7fc700d02843c4fbd3e7cc73bf714f9cc2 (patch) | |
tree | d88f24982190823c89a3af189e6a5b1a21a16bf8 | |
parent | Add an option to toggle execution feature (diff) | |
download | shavit-129b3e7fc700d02843c4fbd3e7cc73bf714f9cc2.tar.gz shavit-129b3e7fc700d02843c4fbd3e7cc73bf714f9cc2.zip |
Remove executable content support
-rw-r--r-- | fileutils.go | 9 | ||||
-rw-r--r-- | handler.go | 39 | ||||
-rw-r--r-- | input.go | 10 |
3 files changed, 0 insertions, 58 deletions
diff --git a/fileutils.go b/fileutils.go index 03e71fb..557d21c 100644 --- a/fileutils.go +++ b/fileutils.go @@ -19,12 +19,3 @@ func isFile(path string) bool { return fileInfo.Mode().IsRegular() } - -func isExecutable(path string) bool { - stat, err := os.Stat(path) - if err != nil { - return false - } - - return stat.Mode().Perm()&0111 == 0111 -} @@ -1,17 +1,12 @@ package main import ( - "bytes" - "context" "fmt" - "io/ioutil" "log" "net/url" "os" - "os/exec" "path/filepath" "strings" - "time" gemini "git.sr.ht/~yotam/go-gemini" ) @@ -73,36 +68,6 @@ func (h Handler) getFilePath(rawURL string) (string, error) { return "", gemini.Error{Err: fmt.Errorf("file not found"), Status: gemini.StatusNotFound} } -func (h Handler) serveExecutable(r gemini.Request, path string) gemini.Response { - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(h.cfg.ExecTimeout)*time.Second) - defer cancel() - - cmd := exec.CommandContext(ctx, path) - - stdin, err := cmd.StdinPipe() - if err != nil { - return gemini.ErrorResponse(err) - } - defer stdin.Close() - - _, err = fmt.Fprintf(stdin, "%s\r\n", r.URL) - if err != nil { - return gemini.ErrorResponse(err) - } - - // The gemini library api make it hard to stream stdout instead of reading it into memory - out, err := cmd.Output() - if err != nil { - return gemini.ErrorResponse(err) - } - - if ctx.Err() == context.DeadlineExceeded { - return gemini.ErrorResponse(ctx.Err()) - } - - return gemini.Response{Status: 20, Meta: "text/gemini", Body: ioutil.NopCloser(bytes.NewReader(out))} -} - func (h Handler) serveFile(path string) gemini.Response { log.Println("Serving file from", path) @@ -127,9 +92,5 @@ func (h Handler) Handle(r gemini.Request) gemini.Response { return gemini.ErrorResponse(err) } - if h.cfg.ExecuteFiles && isExecutable(path) { - return h.serveExecutable(r, path) - } - return h.serveFile(path) } @@ -39,12 +39,6 @@ type Config struct { // default to ["index.gmi"] IndexFiles []string `toml:"index_files"` - // default to 5 - ExecTimeout int64 `toml:"exec_timeout"` - - // default to false because the content might not be trusted - ExecuteFiles bool - TLSCert string `toml:"tls_certificate"` TLSKey string `toml:"tls_key"` } @@ -70,9 +64,5 @@ func getConfig(path string) (Config, error) { cfg.IndexFiles = []string{"index.gmi"} } - if cfg.ExecTimeout == 0 { - cfg.ExecTimeout = 5 - } - return cfg, nil } |