forked from solderpunk/molly-brown
Check CGI responses for a valid status code.
This commit is contained in:
parent
40ad09f9bc
commit
cadb7b2ea5
17
handler.go
17
handler.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -138,7 +139,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
|
||||||
io.WriteString(stdin, URL.String())
|
io.WriteString(stdin, URL.String())
|
||||||
io.WriteString(stdin, "\r\n")
|
io.WriteString(stdin, "\r\n")
|
||||||
stdin.Close()
|
stdin.Close()
|
||||||
out, err := cmd.Output()
|
response, err := cmd.Output()
|
||||||
if ctx.Err() == context.DeadlineExceeded {
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
conn.Write([]byte("42 CGI process timed out!\r\n"))
|
conn.Write([]byte("42 CGI process timed out!\r\n"))
|
||||||
log.Status = 42
|
log.Status = 42
|
||||||
|
@ -149,8 +150,18 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
|
||||||
log.Status = 42
|
log.Status = 42
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Write([]byte(out))
|
// Extract response header
|
||||||
log.Status = 20 // Do this properly!
|
header, _, err := bufio.NewReader(strings.NewReader(string(response))).ReadLine()
|
||||||
|
status, err2 := strconv.Atoi(strings.Fields(string(header))[0])
|
||||||
|
if err != nil || err2 != nil {
|
||||||
|
conn.Write([]byte("42 CGI error!\r\n"))
|
||||||
|
log.Status = 42
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Status = status
|
||||||
|
// Write response
|
||||||
|
conn.Write(response)
|
||||||
|
|
||||||
// Otherwise, serve the file contents
|
// Otherwise, serve the file contents
|
||||||
} else {
|
} else {
|
||||||
// Get MIME type of files
|
// Get MIME type of files
|
||||||
|
|
Loading…
Reference in New Issue