Pass pointers to the log entry to functions which can set the status code, so changes are reflected in the main handle function.

This commit is contained in:
Solderpunk 2020-06-06 12:08:34 +02:00
parent b0a08f8231
commit 2c99228610
2 changed files with 9 additions and 9 deletions

View File

@ -15,7 +15,7 @@ import (
"time" "time"
) )
func handleCGI(config Config, path string, URL *url.URL, log LogEntry, conn net.Conn) { func handleCGI(config Config, path string, URL *url.URL, log *LogEntry, conn net.Conn) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, path) cmd := exec.CommandContext(ctx, path)
@ -51,7 +51,7 @@ func handleCGI(config Config, path string, URL *url.URL, log LogEntry, conn net.
conn.Write(response) conn.Write(response)
} }
func handleSCGI(socket_path string, config Config, URL *url.URL, log LogEntry, conn net.Conn) { func handleSCGI(socket_path string, config Config, URL *url.URL, log *LogEntry, conn net.Conn) {
// Connect to socket // Connect to socket
socket, err := net.Dial("unix", socket_path) socket, err := net.Dial("unix", socket_path)

View File

@ -28,7 +28,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
defer func() { logEntries <- log }() defer func() { logEntries <- log }()
// Read request // Read request
URL, err := readRequest(conn, log) URL, err := readRequest(conn, &log)
if err != nil { if err != nil {
return return
} }
@ -78,7 +78,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
matched, err := regexp.Match(scgi_url, []byte(URL.Path)) matched, err := regexp.Match(scgi_url, []byte(URL.Path))
if matched && err == nil { if matched && err == nil {
fmt.Println("Matched:", scgi_url, scgi_socket) fmt.Println("Matched:", scgi_url, scgi_socket)
handleSCGI(scgi_socket, config, URL, log, conn) handleSCGI(scgi_socket, config, URL, &log, conn)
return return
} }
} }
@ -122,7 +122,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
index_path := filepath.Join(path, "index.gmi") index_path := filepath.Join(path, "index.gmi")
index_info, err := os.Stat(index_path) index_info, err := os.Stat(index_path)
if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 { if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 {
serveFile(index_path, log, conn) serveFile(index_path, &log, conn)
// Serve a generated listing // Serve a generated listing
} else { } else {
conn.Write([]byte("20 text/gemini\r\n")) conn.Write([]byte("20 text/gemini\r\n"))
@ -135,17 +135,17 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
// If this file is executable, get dynamic content // If this file is executable, get dynamic content
inCGIPath, err := regexp.Match(config.CGIPath, []byte(path)) inCGIPath, err := regexp.Match(config.CGIPath, []byte(path))
if inCGIPath && info.Mode().Perm() & 0111 == 0111 { if inCGIPath && info.Mode().Perm() & 0111 == 0111 {
handleCGI(config, path, URL, log, conn) handleCGI(config, path, URL, &log, conn)
return return
} }
// Otherwise, serve the file contents // Otherwise, serve the file contents
serveFile(path, log, conn) serveFile(path, &log, conn)
return return
} }
func readRequest(conn net.Conn, log LogEntry) (*url.URL, error) { func readRequest(conn net.Conn, log *LogEntry) (*url.URL, error) {
reader := bufio.NewReaderSize(conn, 1024) reader := bufio.NewReaderSize(conn, 1024)
request, overflow, err := reader.ReadLine() request, overflow, err := reader.ReadLine()
if overflow { if overflow {
@ -214,7 +214,7 @@ func generateDirectoryListing(path string) string {
return listing return listing
} }
func serveFile(path string, log LogEntry, conn net.Conn) { func serveFile(path string, log *LogEntry, conn net.Conn) {
// Get MIME type of files // Get MIME type of files
ext := filepath.Ext(path) ext := filepath.Ext(path)
var mimeType string var mimeType string