]> Sergey Matveev's repositories - godlighty.git/blobdiff - handler.go
FastCGI running example
[godlighty.git] / handler.go
index df16c38d5e11bf05092f7c6b1bcaf606be8a87c7..6fa1d35bbeb0e1a6290900f55ecaaf9a8b91f998 100644 (file)
@@ -24,7 +24,9 @@ import (
        "fmt"
        "io/ioutil"
        "log"
+       "net"
        "net/http"
+       "net/url"
        "os"
        "path"
        "strconv"
@@ -62,6 +64,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(
@@ -69,8 +78,8 @@ func (h Handler) Handle(
        host string, cfg *HostCfg,
 ) {
        notFound := func() {
-               fmt.Printf("%s %s \"%s %s %s\" %d \"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+               fmt.Printf("%s %s \"%s %+q %s\" %d \"%s\"\n",
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        http.StatusNotFound,
                        r.Header.Get("User-Agent"),
                )
@@ -90,8 +99,8 @@ func (h Handler) Handle(
                username = "user:" + username + " "
        }
        printErr := func(code int, err error) {
-               fmt.Printf("%s %s \"%s %s %s\" %d \"%s\" %s\"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+               fmt.Printf("%s %s \"%s %+q %s\" %d \"%s\" %s\"%s\"\n",
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        code, err.Error(),
                        username, r.Header.Get("User-Agent"),
                )
@@ -139,8 +148,8 @@ func (h Handler) Handle(
                }
                wc := &CountResponseWriter{ResponseWriter: w}
                dav.ServeHTTP(wc, r)
-               fmt.Printf("%s %s \"WebDAV %s\" %d %d %s\"%s\"\n",
-                       r.RemoteAddr, host, r.URL.Path,
+               fmt.Printf("%s %s \"WebDAV %+q\" %d %d %s\"%s\"\n",
+                       r.RemoteAddr, host, PathWithQuery(r.URL),
                        wc.Status, wc.Size,
                        username, r.Header.Get("User-Agent"),
                )
@@ -148,8 +157,8 @@ func (h Handler) Handle(
        }
 
        if !(r.Method == "" || r.Method == http.MethodGet) {
-               fmt.Printf("%s %s \"%s %s %s\" %d %s\"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+               fmt.Printf("%s %s \"%s %+q %s\" %d %s\"%s\"\n",
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        http.StatusMethodNotAllowed,
                        username, r.Header.Get("User-Agent"),
                )
@@ -285,16 +294,16 @@ IndexLookup:
                wr := wc.(*gzipResponseWriter)
                w.WriteHeader(wr.status)
                w.Write(bufCompressed.Bytes())
-               fmt.Printf("%s %s \"%s %s %s\" %d %d %s\"%s\"\n",
-                       r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+               fmt.Printf("%s %s \"%s %+q %s\" %d %d %s\"%s\"\n",
+                       r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                        wr.status, size,
                        username, r.Header.Get("User-Agent"),
                )
                return
        }
        wr := wc.(*CountResponseWriter)
-       fmt.Printf("%s %s \"%s %s %s\" %d %d %s\"%s\"\n",
-               r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
+       fmt.Printf("%s %s \"%s %+q %s\" %d %d %s\"%s\"\n",
+               r.RemoteAddr, host, r.Method, PathWithQuery(r.URL), r.Proto,
                wr.Status, wr.Size,
                username, r.Header.Get("User-Agent"),
        )
@@ -305,6 +314,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])
 }