]> Sergey Matveev's repositories - godlighty.git/blobdiff - handler.go
Better URL escaping in output
[godlighty.git] / handler.go
index 4593a2f119262608082414f00e9da6713dd75260..8953a8060713f04cb3dbdd14df575afcb9ff5a02 100644 (file)
@@ -25,6 +25,7 @@ import (
        "io/ioutil"
        "log"
        "net/http"
+       "net/url"
        "os"
        "path"
        "strconv"
@@ -62,6 +63,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 +78,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 +99,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,7 +148,7 @@ 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"),
                )
@@ -149,7 +157,7 @@ func (h Handler) Handle(
 
        if !(r.Method == "" || r.Method == http.MethodGet) {
                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"),
                )
@@ -286,7 +294,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 +302,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"),
        )