Code

Simplified URL parsing and un-escape URIs.
authorSebastian Harl <sh@tokkee.org>
Sat, 22 Nov 2014 12:07:00 +0000 (13:07 +0100)
committerSebastian Harl <sh@tokkee.org>
Sat, 22 Nov 2014 12:08:00 +0000 (13:08 +0100)
This avoids subsequent string-joins in various handlers and generally unifies
URL handling.

Also, it fixes handling of URLs containing (escaped) spaces.

server/graph.go
server/server.go

index 11b55b84fef483d16ff545bf50894444fdd58576..9406f760158029519ecf32edb07f7993c6dfc632 100644 (file)
@@ -33,7 +33,6 @@ import (
        "fmt"
        "io"
        "net/http"
        "fmt"
        "io"
        "net/http"
-       "strings"
        "time"
 
        "code.google.com/p/plotinum/plot"
        "time"
 
        "code.google.com/p/plotinum/plot"
@@ -46,12 +45,12 @@ import (
 )
 
 func (s *Server) graph(w http.ResponseWriter, req request) {
 )
 
 func (s *Server) graph(w http.ResponseWriter, req request) {
-       if len(req.args) < 2 {
+       if len(req.args) != 2 {
                s.internal(w, fmt.Errorf("Missing host/metric information"))
        }
 
        host := proto.EscapeString(req.args[0])
                s.internal(w, fmt.Errorf("Missing host/metric information"))
        }
 
        host := proto.EscapeString(req.args[0])
-       metric := proto.EscapeString(strings.Join(req.args[1:], "/"))
+       metric := proto.EscapeString(req.args[1])
        res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
        if err != nil {
                s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
        res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
        if err != nil {
                s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
index b7cd37a3c5ed3e3dc209cbd4a6010d0b7f2da622..a8e34d9c32a063c5d183070e4cb48e258e5250de 100644 (file)
@@ -34,6 +34,7 @@ import (
        "io"
        "log"
        "net/http"
        "io"
        "log"
        "net/http"
+       "net/url"
        "path/filepath"
        "strings"
 
        "path/filepath"
        "strings"
 
@@ -145,11 +146,19 @@ var content = map[string]func(request, *Server) (*page, error){
 // ServeHTTP implements the http.Handler interface and serves
 // the SysDB user interface.
 func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 // ServeHTTP implements the http.Handler interface and serves
 // the SysDB user interface.
 func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-       path := r.URL.Path
+       path := r.RequestURI
        if len(path) > 0 && path[0] == '/' {
                path = path[1:]
        }
        if len(path) > 0 && path[0] == '/' {
                path = path[1:]
        }
-       fields := strings.Split(path, "/")
+       var fields []string
+       for _, f := range strings.Split(path, "/") {
+               f, err := url.QueryUnescape(f)
+               if err != nil {
+                       s.err(w, http.StatusBadRequest, fmt.Errorf("Error: %v", err))
+                       return
+               }
+               fields = append(fields, f)
+       }
 
        req := request{
                r:   r,
 
        req := request{
                r:   r,
@@ -251,11 +260,11 @@ func fetch(req request, s *Server) (*page, error) {
                }
                q = fmt.Sprintf("FETCH host %s", proto.EscapeString(req.args[0]))
        case "service", "metric":
                }
                q = fmt.Sprintf("FETCH host %s", proto.EscapeString(req.args[0]))
        case "service", "metric":
-               if len(req.args) < 2 {
+               if len(req.args) != 2 {
                        return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
                }
                host := proto.EscapeString(req.args[0])
                        return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
                }
                host := proto.EscapeString(req.args[0])
-               name := proto.EscapeString(strings.Join(req.args[1:], "/"))
+               name := proto.EscapeString(req.args[1])
                q = fmt.Sprintf("FETCH %s %s.%s", req.cmd, host, name)
        default:
                panic("Unknown request: fetch(" + req.cmd + ")")
                q = fmt.Sprintf("FETCH %s %s.%s", req.cmd, host, name)
        default:
                panic("Unknown request: fetch(" + req.cmd + ")")