mirror of
https://git.sr.ht/~yotam/shavit
synced 2024-05-09 07:16:02 +02:00
Remove executable content support
This commit is contained in:
parent
3a9ce46566
commit
129b3e7fc7
|
@ -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
|
||||
}
|
||||
|
|
39
handler.go
39
handler.go
|
@ -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)
|
||||
}
|
||||
|
|
10
input.go
10
input.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue