]> Sergey Matveev's repositories - godlighty.git/blobdiff - handler.go
Raised copyright years
[godlighty.git] / handler.go
index 4593a2f119262608082414f00e9da6713dd75260..d8e6ee6ccc5df3ba424efc3e7171169206470bf4 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,11 +20,14 @@ package godlighty
 import (
        "bytes"
        "compress/gzip"
+       "encoding/base64"
        "errors"
        "fmt"
        "io/ioutil"
        "log"
+       "net"
        "net/http"
+       "net/url"
        "os"
        "path"
        "strconv"
@@ -33,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 (
@@ -62,6 +65,13 @@ var (
        MainHandler Handler
 )
 
+func PathWithQuery(u *url.URL) string {
+       if u.RawQuery == "" {
+               return u.EscapedPath()
+       }
+       return u.EscapedPath() + "?" + u.RawQuery
+}
+
 type Handler struct{}
 
 func (h Handler) Handle(
@@ -70,7 +80,7 @@ func (h Handler) Handle(
 ) {
        notFound := func() {
                fmt.Printf("%s %s \"%s %+q %s\" %d \"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        http.StatusNotFound,
                        r.Header.Get("User-Agent"),
                )
@@ -91,7 +101,7 @@ func (h Handler) Handle(
        }
        printErr := func(code int, err error) {
                fmt.Printf("%s %s \"%s %+q %s\" %d \"%s\" %s\"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        code, err.Error(),
                        username, r.Header.Get("User-Agent"),
                )
@@ -140,16 +150,16 @@ func (h Handler) Handle(
                wc := &CountResponseWriter{ResponseWriter: w}
                dav.ServeHTTP(wc, r)
                fmt.Printf("%s %s \"WebDAV %+q\" %d %d %s\"%s\"\n",
-                       r.RemoteAddr, host, r.URL.Path,
+                       r.RemoteAddr, host, PathWithQuery(r.URL),
                        wc.Status, wc.Size,
                        username, r.Header.Get("User-Agent"),
                )
                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, r.URL.Path, r.Proto,
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        http.StatusMethodNotAllowed,
                        username, r.Header.Get("User-Agent"),
                )
@@ -231,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)
@@ -286,7 +322,7 @@ IndexLookup:
                w.WriteHeader(wr.status)
                w.Write(bufCompressed.Bytes())
                fmt.Printf("%s %s \"%s %+q %s\" %d %d %s\"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        wr.status, size,
                        username, r.Header.Get("User-Agent"),
                )
@@ -294,7 +330,7 @@ IndexLookup:
        }
        wr := wc.(*CountResponseWriter)
        fmt.Printf("%s %s \"%s %+q %s\" %d %d %s\"%s\"\n",
-               r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+               r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                wr.Status, wr.Size,
                username, r.Header.Get("User-Agent"),
        )
@@ -305,6 +341,9 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
                http.Error(w, "invalid URL path", http.StatusBadRequest)
                return
        }
-       host := strings.SplitN(r.Host, ":", 2)[0]
+       host, _, err := net.SplitHostPort(r.Host)
+       if err != nil {
+               host = r.Host
+       }
        h.Handle(w, r, host, Hosts[host])
 }