mirror of
https://tildegit.org/solderpunk/molly-brown
synced 2024-05-27 01:26:06 +02:00
Basic implementation of getting dynamic content from executables.
This commit is contained in:
parent
10a169a7f3
commit
efc1ecf7a7
59
handler.go
59
handler.go
|
@ -3,12 +3,14 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -120,25 +122,46 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
|
||||||
log.Status = 20
|
log.Status = 20
|
||||||
conn.Write([]byte(generateDirectoryListing(path)))
|
conn.Write([]byte(generateDirectoryListing(path)))
|
||||||
return
|
return
|
||||||
}
|
// If this file is executable, get dynamic content
|
||||||
|
} else if info.Mode().Perm() & 0111 == 0111 {
|
||||||
// Get MIME type of files
|
cmd := exec.Command(path)
|
||||||
ext := filepath.Ext(path)
|
stdin, err := cmd.StdinPipe()
|
||||||
var mimeType string
|
if err != nil {
|
||||||
if ext == ".gmi" {
|
conn.Write([]byte("42 CGI error!\r\n"))
|
||||||
mimeType = "text/gemini"
|
log.Status = 42
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer stdin.Close()
|
||||||
|
io.WriteString(stdin, URL.String())
|
||||||
|
io.WriteString(stdin, "\r\n")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
conn.Write([]byte("42 CGI error!\r\n"))
|
||||||
|
log.Status = 42
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn.Write([]byte(out))
|
||||||
|
log.Status = 20 // Do this properly!
|
||||||
|
// Otherwise, serve the file contents
|
||||||
} else {
|
} else {
|
||||||
mimeType = mime.TypeByExtension(ext)
|
// Get MIME type of files
|
||||||
}
|
ext := filepath.Ext(path)
|
||||||
fmt.Println(path, ext, mimeType)
|
var mimeType string
|
||||||
contents, err := ioutil.ReadFile(path)
|
if ext == ".gmi" {
|
||||||
if err != nil {
|
mimeType = "text/gemini"
|
||||||
conn.Write([]byte("50 Error!\r\n"))
|
} else {
|
||||||
log.Status = 50
|
mimeType = mime.TypeByExtension(ext)
|
||||||
} else {
|
}
|
||||||
conn.Write([]byte(fmt.Sprintf("20 %s\r\n", mimeType)))
|
fmt.Println(path, ext, mimeType)
|
||||||
log.Status = 20
|
contents, err := ioutil.ReadFile(path)
|
||||||
conn.Write(contents)
|
if err != nil {
|
||||||
|
conn.Write([]byte("50 Error!\r\n"))
|
||||||
|
log.Status = 50
|
||||||
|
} else {
|
||||||
|
conn.Write([]byte(fmt.Sprintf("20 %s\r\n", mimeType)))
|
||||||
|
log.Status = 20
|
||||||
|
conn.Write(contents)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue