]> Sergey Matveev's repositories - godlighty.git/blobdiff - handler.go
Git's .bundle MIME type
[godlighty.git] / handler.go
index 6fa1d35bbeb0e1a6290900f55ecaaf9a8b91f998..e6d63fc6e76911358a5aa41dc1b266e7d59bc671 100644 (file)
@@ -1,6 +1,6 @@
 /*
 godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
-Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@ package godlighty
 import (
        "bytes"
        "compress/gzip"
+       "encoding/base64"
        "errors"
        "fmt"
        "io/ioutil"
@@ -35,13 +36,13 @@ import (
        "time"
 
        "github.com/klauspost/compress/zstd"
+       "go.stargrave.org/godlighty/meta4"
        "golang.org/x/net/webdav"
 )
 
 const (
-       Index    = "index.html"
-       Readme   = "README"
-       Meta4Ext = ".meta4"
+       Index  = "index.html"
+       Readme = "README"
 )
 
 var (
@@ -156,7 +157,7 @@ func (h Handler) Handle(
                return
        }
 
-       if !(r.Method == "" || r.Method == http.MethodGet) {
+       if !(r.Method == "" || r.Method == http.MethodGet || r.Method == http.MethodHead) {
                fmt.Printf("%s %s \"%s %+q %s\" %d %s\"%s\"\n",
                        r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        http.StatusMethodNotAllowed,
@@ -212,7 +213,7 @@ IndexLookup:
                        }
                        contentType = "text/html; charset=utf-8"
                } else {
-                       for _, index := range append(cfg.Indexes, Index) {
+                       for _, index := range append(cfg.Indices, Index) {
                                p := path.Join(pth, index)
                                if _, err := os.Stat(p); err == nil {
                                        pth = p
@@ -240,9 +241,35 @@ IndexLookup:
        }
        defer fd.Close()
 
-       if _, err = os.Stat(pth + Meta4Ext); err == nil {
-               w.Header().Set("Link", "<"+path.Base(pth)+Meta4Ext+`>; rel=describedby; type="application/metalink4+xml"`)
+       if meta4fi, err := os.Stat(pth + meta4.Ext); err == nil {
+               if meta4fi.Size() > meta4.MaxSize {
+                       goto SkipMeta4
+               }
+               meta4Raw, err := ioutil.ReadFile(pth + meta4.Ext)
+               if err != nil {
+                       goto SkipMeta4
+               }
+               base := path.Base(pth)
+               forHTTP, err := meta4.Parse(base, meta4Raw)
+               if err != nil {
+                       goto SkipMeta4
+               }
+               w.Header().Add("Link", "<"+base+meta4.Ext+
+                       `>; rel=describedby; type="application/metalink4+xml"`,
+               )
+               for _, u := range forHTTP.URLs {
+                       w.Header().Add("Link", "<"+u+">; rel=duplicate")
+               }
+               if forHTTP.SHA256 != nil {
+                       w.Header().Add("Digest", "SHA-256="+
+                               base64.StdEncoding.EncodeToString(forHTTP.SHA256))
+               }
+               if forHTTP.SHA512 != nil {
+                       w.Header().Add("Digest", "SHA-512="+
+                               base64.StdEncoding.EncodeToString(forHTTP.SHA512))
+               }
        }
+SkipMeta4:
 
        if contentType == "" {
                contentType = mediaType(path.Base(pth), cfg.MIMEs)