support lang tag on a vhost basis

This commit is contained in:
stryan 2020-06-09 12:35:43 -04:00
parent f1d01ae465
commit 67b3258969
3 changed files with 23 additions and 6 deletions

View File

@ -19,6 +19,7 @@ localhost:
Trusted:
- /private
Whitelist: "whitelist"
Lang: "en,eo"
localhost2:
Hostname: "gemini.foo.bar"
RootDir: "/var/gemini2"

View File

@ -62,6 +62,7 @@ type GeminiConfig struct {
RootDir string
CGIDir string
AccessControl CapsuleAcl
Lang string
}
type CapsuleAcl struct {
@ -75,12 +76,13 @@ func (c *GeminiConfig) String() string {
return fmt.Sprintf("Gemini Config: %v Files:%v CGI:%v", c.Hostname, c.RootDir, c.CGIDir)
}
func generateFile(selector string) Response {
func generateFile(selector, extended_meta string) Response {
meta := mime.TypeByExtension(filepath.Ext(selector))
if meta == "" {
//assume plain UTF-8 text
meta = "text/gemini; charset=utf-8"
}
meta = meta + "; " + extended_meta
file, err := os.Open(selector)
if err != nil {
panic("Failed to read file")
@ -90,7 +92,7 @@ func generateFile(selector string) Response {
return Response{STATUS_SUCCESS, meta, string(buf)}
}
func generateDirectory(path string) Response {
func generateDirectory(path, extended_meta string) Response {
var dirpage string
files, err := ioutil.ReadDir(path)
if err != nil {
@ -105,12 +107,16 @@ func generateDirectory(path string) Response {
}
if file.Name() == "index.gmi" || file.Name() == "index.gemini" {
//Found an index file, return that instead
return generateFile(path + file.Name())
return generateFile(path+file.Name(), extended_meta)
} else {
dirpage += fmt.Sprintf("=> %s %s\r\n", url.PathEscape(file.Name()), file.Name())
}
}
return Response{STATUS_SUCCESS, "text/gemini", dirpage}
meta := "text/gemini"
if extended_meta != "" {
meta = meta + "; " + extended_meta
}
return Response{STATUS_SUCCESS, meta, dirpage}
}
func generateCGI(selector *url.URL, c *conn) Response {

View File

@ -224,7 +224,12 @@ func (s *Server) ParseRequest(req string, c *conn) Response {
return Response{STATUS_TEMPORARY_FAILURE, "Unable to access file", ""}
case fi.IsDir():
if strings.HasSuffix(u.Path, "/") {
return generateDirectory(selector)
//build extended meta first
ext := ""
if capsule.Lang != "" {
ext = "lang=" + capsule.Lang
}
return generateDirectory(selector, ext)
} else {
return Response{STATUS_REDIRECT_PERMANENT, "gemini://" + u.Hostname() + u.Path + "/", ""}
}
@ -236,7 +241,12 @@ func (s *Server) ParseRequest(req string, c *conn) Response {
return generateCGI(u, c)
} else {
//Normal file found
return generateFile(selector)
//build extended meta first
ext := ""
if capsule.Lang != "" {
ext = "lang=" + capsule.Lang
}
return generateFile(selector, ext)
}
}
}