More and better error logging.

This commit is contained in:
Solderpunk 2020-07-01 20:15:52 +02:00
parent 821a862036
commit f7e588dfae
2 changed files with 13 additions and 8 deletions

View File

@ -73,7 +73,7 @@ func handleCGI(config Config, path string, cgiPath string, URL *url.URL, log *Lo
header, _, err := bufio.NewReader(strings.NewReader(string(response))).ReadLine()
status, err2 := strconv.Atoi(strings.Fields(string(header))[0])
if err != nil || err2 != nil {
errorLog.Println("Unable to parse first line of output from CGI process " + path + " as valid Gemini response header.")
errorLog.Println("Unable to parse first line of output from CGI process " + path + " as valid Gemini response header. Line was: " + string(header))
conn.Write([]byte("42 CGI error!\r\n"))
log.Status = 42
return
@ -83,11 +83,12 @@ func handleCGI(config Config, path string, cgiPath string, URL *url.URL, log *Lo
conn.Write(response)
}
func handleSCGI(URL *url.URL, scgiPath string, scgiSocket string, config Config, log *LogEntry, conn net.Conn) {
func handleSCGI(URL *url.URL, scgiPath string, scgiSocket string, config Config, log *LogEntry, errorLog *log.Logger, conn net.Conn) {
// Connect to socket
socket, err := net.Dial("unix", scgiSocket)
if err != nil {
errorLog.Println("Error connecting to SCGI socket " + scgiSocket + ": " + err.Error())
conn.Write([]byte("42 Error connecting to SCGI service!\r\n"))
log.Status = 42
return
@ -119,6 +120,7 @@ func handleSCGI(URL *url.URL, scgiPath string, scgiSocket string, config Config,
break
} else if !first {
// Err
errorLog.Println("Error reading from SCGI socket " + scgiSocket + ": " + err.Error())
conn.Write([]byte("42 Error reading from SCGI service!\r\n"))
log.Status = 42
return

View File

@ -86,7 +86,7 @@ func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan Log
}
// Check for redirects
handleRedirects(URL, config, conn, &log)
handleRedirects(URL, config, conn, &log, errorLog)
if log.Status != 0 {
return
}
@ -94,7 +94,7 @@ func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan Log
// Check whether this URL is mapped to an SCGI app
for scgiPath, scgiSocket := range config.SCGIPaths {
if strings.HasPrefix(URL.Path, scgiPath) {
handleSCGI(URL, scgiPath, scgiSocket, config, &log, conn)
handleSCGI(URL, scgiPath, scgiSocket, config, &log, errorLog, conn)
return
}
}
@ -116,6 +116,7 @@ func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan Log
log.Status = 51
return
} else if err != nil {
errorLog.Println("Error getting info for file " + path + ": " + err.Error())
conn.Write([]byte("40 Temporary failure!\r\n"))
log.Status = 40
return
@ -239,16 +240,17 @@ func parseMollyFiles(path string, config *Config, errorLog *log.Logger) {
}
}
func handleRedirects(URL *url.URL, config Config, conn net.Conn, log *LogEntry) {
handleRedirectsInner(URL, config.TempRedirects, 30, conn, log)
handleRedirectsInner(URL, config.PermRedirects, 31, conn, log)
func handleRedirects(URL *url.URL, config Config, conn net.Conn, log *LogEntry, errorLog *log.Logger) {
handleRedirectsInner(URL, config.TempRedirects, 30, conn, log, errorLog)
handleRedirectsInner(URL, config.PermRedirects, 31, conn, log, errorLog)
}
func handleRedirectsInner(URL *url.URL, redirects map[string]string, status int, conn net.Conn, log *LogEntry) {
func handleRedirectsInner(URL *url.URL, redirects map[string]string, status int, conn net.Conn, log *LogEntry, errorLog *log.Logger) {
strStatus := strconv.Itoa(status)
for src, dst := range redirects {
compiled, err := regexp.Compile(src)
if err != nil {
errorLog.Println("Error compiling redirect regexp " + src + ": " + err.Error())
continue
}
if compiled.MatchString(URL.Path) {
@ -308,6 +310,7 @@ func serveFile(path string, log *LogEntry, conn net.Conn, config Config, errorLo
contents, err := ioutil.ReadFile(path)
if err != nil {
errorLog.Println("Error reading file " + path + ": " + err.Error())
conn.Write([]byte("50 Error!\r\n"))
log.Status = 50
return