doesnt work yet!

This commit is contained in:
Hedy Li 2021-08-02 20:28:52 +08:00
parent 070098ad2d
commit 7eff471da0
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
2 changed files with 26 additions and 23 deletions

View File

@ -21,6 +21,7 @@ type Config struct {
DirlistTitles bool
RestrictHostname string
CGIPaths []string
UserCGIEnable bool
}
var defaultConf = &Config{
@ -35,6 +36,7 @@ var defaultConf = &Config{
UserDir: "public_spartan",
RestrictHostname: "",
CGIPaths: []string{"cgi/"},
UserCGIEnable: false, // Turned off by default because scripts are run by server user as of now
}
func LoadConfig(path string) (*Config, error) {

View File

@ -18,7 +18,6 @@ import (
flag "github.com/spf13/pflag"
)
var doneScanningRequest = false
type Request struct {
conn io.ReadWriteCloser
@ -96,9 +95,25 @@ func handleConnection(netConn net.Conn, conf *Config) {
log.Println("Closed connection")
}()
doneScanningRequest := false
// Check the size of the request buffer.
s := bufio.NewScanner(conn)
s.Split(ScanRequest)
s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if doneScanningRequest {
// Return a byte
return 1, data[:1], nil
}
// Read request
if i := bytes.IndexByte(data, '\n'); i >= 0 {
return i + 1, bytes.TrimRight(data[0:i], "\r"), nil
}
return 0, nil, nil
})
// Sanity check incoming request URL content.
if ok := s.Scan(); !ok {
@ -154,14 +169,15 @@ func handleConnection(netConn net.Conn, conf *Config) {
// Check for CGI
for _, cgiPath := range conf.CGIPaths {
if strings.HasPrefix(req.filePath, cgiPath) {
log.Println("Attempting CGI:", req.filePath)
if req.user != "" && conf.UserCGIEnable {
log.Println("Attempting CGI:", req.filePath)
ok := handleCGI(conf, req, cgiPath)
if ok {
return
ok := handleCGI(conf, req, cgiPath)
if ok {
return
}
break // CGI failed. just handle the request as if it's a static file.
}
break // CGI failed. just handle the request as if it's a static file.
}
}
@ -319,18 +335,3 @@ func parseRequest(r string) (host, path string, contentLength int, err error) {
return
}
// ScanRequest returns a line if we haven't scanned request line yet, else, returns a byte
func ScanRequest(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if doneScanningRequest {
// Return a byte
return 1, data[:1], nil
}
// Read request
if i := bytes.IndexByte(data, '\n'); i >= 0 {
return i + 1, bytes.TrimRight(data[0:i], "\r"), nil
}
return 0, nil, nil
}