1
0
Fork 0
mirror of https://git.sr.ht/~yotam/shavit synced 2024-06-13 00:56:28 +02:00
shavit/handler.go

103 lines
2.2 KiB
Go
Raw Normal View History

2019-11-01 12:29:44 +01:00
package main
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strings"
2019-11-02 13:51:43 +01:00
gemini "git.sr.ht/~yotam/go-gemini"
2019-11-01 12:29:44 +01:00
)
2019-11-02 13:51:43 +01:00
// GeminiError wrap the standard Go error with a Gemini status code
2019-11-01 12:29:44 +01:00
type GeminiError struct {
Err error
Status int
}
2019-11-02 13:51:43 +01:00
// Error return the string of the inner error to fulfill the error interface
2019-11-01 12:29:44 +01:00
func (e GeminiError) Error() string {
return e.Err.Error()
}
2019-11-02 13:51:43 +01:00
// Handler is the main handler of the server
type Handler struct {
2019-11-01 12:29:44 +01:00
source string
}
2019-11-02 13:51:43 +01:00
func (h Handler) urlAbsPath(rawURL string) (string, error) {
2019-11-01 12:29:44 +01:00
u, err := url.Parse(rawURL)
if err != nil {
return "", GeminiError{err, gemini.StatusBadRequest}
}
itemPath, err := filepath.Abs(filepath.Join(h.source, u.Path))
if err != nil {
return "", GeminiError{err, gemini.StatusTemporaryFailure}
}
if !strings.HasPrefix(itemPath, h.source) {
return "", GeminiError{fmt.Errorf("Permission Denied"), gemini.StatusBadRequest}
}
return itemPath, nil
}
2019-11-02 13:51:43 +01:00
func (h Handler) isFile(path string) bool {
2019-11-01 12:29:44 +01:00
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.Mode().IsRegular()
}
2019-11-02 13:51:43 +01:00
func (h Handler) getFilePath(rawURL string) (string, error) {
2019-11-01 12:29:44 +01:00
itemPath, err := h.urlAbsPath(rawURL)
if err != nil {
return "", err
}
if h.isFile(itemPath) {
return itemPath, nil
}
indexPath := filepath.Join(itemPath, "index.gemi")
if h.isFile(indexPath) {
return indexPath, nil
}
return "", GeminiError{fmt.Errorf("File Not Found"), gemini.StatusNotFound}
}
2019-11-02 13:51:43 +01:00
func (h Handler) errorResponse(err error) gemini.Response {
2019-11-01 12:29:44 +01:00
if err == nil {
panic("nil error is not a valid parameter")
}
if ge, ok := err.(GeminiError); ok {
2019-11-02 13:51:43 +01:00
return gemini.Response{Status: ge.Status, Meta: ge.Error(), Body: nil}
2019-11-01 12:29:44 +01:00
}
2019-11-02 13:51:43 +01:00
return gemini.Response{Status: gemini.StatusTemporaryFailure, Meta: err.Error(), Body: nil}
2019-11-01 12:29:44 +01:00
}
2019-11-02 13:51:43 +01:00
// Handle implement the gemini.Handler interface by serving files from a given source directory
func (h Handler) Handle(r gemini.Request) gemini.Response {
2019-11-01 12:29:44 +01:00
itemPath, err := h.getFilePath(r.URL)
if err != nil {
return h.errorResponse(err)
}
log.Println("Serving file from", itemPath)
file, err := os.Open(itemPath)
if err != nil {
return h.errorResponse(err)
}
2019-11-02 13:51:43 +01:00
return gemini.Response{Status: gemini.StatusSuccess, Meta: "text/gemini", Body: file}
2019-11-01 12:29:44 +01:00
}