2014-04-23 19:57:06 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2014-04-23 20:10:18 +00:00
|
|
|
"os"
|
2014-04-23 19:57:06 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UiIndex serves files in the /ui/ prefix from a preconfigured directory
|
|
|
|
func (s *HTTPServer) UiIndex(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
// Invoke the handler
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
s.logger.Printf("[DEBUG] http: Request %v (%v)", req.URL, time.Now().Sub(start))
|
|
|
|
}()
|
|
|
|
|
2014-04-23 20:10:18 +00:00
|
|
|
// Determine the file path
|
2014-04-23 19:57:06 +00:00
|
|
|
file := strings.TrimPrefix(req.URL.Path, "/ui/")
|
|
|
|
if file == "" {
|
|
|
|
file = "index.html"
|
|
|
|
}
|
2014-04-23 20:10:18 +00:00
|
|
|
|
|
|
|
// Reject if it is relative
|
|
|
|
if strings.Contains(file, "..") {
|
|
|
|
s.logger.Printf("[WARN] http: Invalid file %s", file)
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
2014-04-23 19:57:06 +00:00
|
|
|
path := filepath.Join(s.uiDir, file)
|
2014-04-23 20:10:18 +00:00
|
|
|
|
|
|
|
// Attempt to open
|
|
|
|
fh, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("[WARN] http: Failed to serve file %s: %v", path, err)
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
|
|
|
// Get the file info
|
|
|
|
info, err := fh.Stat()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("[WARN] http: Failed to stat file %s: %v", path, err)
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve the file
|
|
|
|
http.ServeContent(resp, req, path, info.ModTime(), fh)
|
2014-04-23 19:57:06 +00:00
|
|
|
}
|