From 8364320dfe3ad76b3e079095a71c93a48277cd2f Mon Sep 17 00:00:00 2001 From: James Robinson Date: Tue, 21 Apr 2015 16:01:17 -0700 Subject: [PATCH] Teach sky_server not to serve up /.git Sky doesn't need the git repo of the mojo repo. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/1059113004 --- engine/src/flutter/tools/skygo/README | 2 +- engine/src/flutter/tools/skygo/sky_server.go | 110 +++++++++--------- .../src/flutter/tools/skygo/sky_server.sha1 | 2 +- 3 files changed, 59 insertions(+), 55 deletions(-) diff --git a/engine/src/flutter/tools/skygo/README b/engine/src/flutter/tools/skygo/README index bf3bc2caf8..312c791b05 100644 --- a/engine/src/flutter/tools/skygo/README +++ b/engine/src/flutter/tools/skygo/README @@ -16,4 +16,4 @@ Uploading the locally built server upload_to_google_storage.py is in depot_tools. It will overwrite sky_server.sha1. Include that change in your code review. -sky_server was last built using go version go1.3 linux/amd64. +sky_server was last built using go version go1.4.2 linux/amd64. diff --git a/engine/src/flutter/tools/skygo/sky_server.go b/engine/src/flutter/tools/skygo/sky_server.go index 482e6694a2..1327a19d3c 100644 --- a/engine/src/flutter/tools/skygo/sky_server.go +++ b/engine/src/flutter/tools/skygo/sky_server.go @@ -5,83 +5,87 @@ package main import ( - "flag" - "fmt" - "io/ioutil" - "net/http" - "os" - "path" - "path/filepath" - "strings" + "flag" + "fmt" + "io/ioutil" + "net/http" + "os" + "path" + "path/filepath" + "strings" ) type skyHandlerRoot struct { - root string + root string } func skyHandler(root string) http.Handler { - return &skyHandlerRoot{root} + return &skyHandlerRoot{root} } func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) { - path := path.Join(handler.root, r.URL.Path) - if strings.HasSuffix(path, ".sky") { - w.Header().Set("Content-Type", "text/sky") - } - w.Header().Set("Cache-Control", "no-cache") - http.ServeFile(w, r, path) + if strings.HasPrefix(r.URL.Path, "/.git") { + w.WriteHeader(http.StatusNotFound) + return + } + path := path.Join(handler.root, r.URL.Path) + if strings.HasSuffix(path, ".sky") { + w.Header().Set("Content-Type", "text/sky") + } + w.Header().Set("Cache-Control", "no-cache") + http.ServeFile(w, r, path) } func usage() { - fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n") - fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\n") - fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n") - fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:flag.\n\n") - flag.PrintDefaults() - os.Exit(2) + fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n") + fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\n") + fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n") + fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:flag.\n\n") + flag.PrintDefaults() + os.Exit(2) } func addMapping(from_path string, to_path string) { - // Print to stderr to it's more obvious what this binary does. - fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path) - http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path))) + // Print to stderr to it's more obvious what this binary does. + fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path) + http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path))) } func main() { - var configuration = flag.String("t", "Release", "The target configuration (i.e. Release or Debug)") + var configuration = flag.String("t", "Release", "The target configuration (i.e. Release or Debug)") - flag.Parse() - flag.Usage = usage - // The built-in go:flag is awful. It only allows short-name arguments - // and they *must* be before any unnamed arguments. There are better ones: - // https://godoc.org/github.com/jessevdk/go-flags - // but for now we're using raw-go. - if flag.NArg() != 3 { - usage() - } + flag.Parse() + flag.Usage = usage + // The built-in go:flag is awful. It only allows short-name arguments + // and they *must* be before any unnamed arguments. There are better ones: + // https://godoc.org/github.com/jessevdk/go-flags + // but for now we're using raw-go. + if flag.NArg() != 3 { + usage() + } - root, _ := filepath.Abs(flag.Arg(0)) - port := flag.Arg(1) - packageRoot := flag.Arg(2) + root, _ := filepath.Abs(flag.Arg(0)) + port := flag.Arg(1) + packageRoot := flag.Arg(2) - // genRoot should not be needed once we figure out how mojom generation - // for sdk users should work. - genRoot := path.Join(root, "out", *configuration, "gen") + // genRoot should not be needed once we figure out how mojom generation + // for sdk users should work. + genRoot := path.Join(root, "out", *configuration, "gen") - fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port) + fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port) - fmt.Fprintf(os.Stderr, " / -> %s\n", root) - http.Handle("/", skyHandler(root)) + fmt.Fprintf(os.Stderr, " / -> %s\n", root) + http.Handle("/", skyHandler(root)) - fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n") - http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - body, _ := ioutil.ReadAll(r.Body) - w.Write(body) - }) + fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n") + http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + body, _ := ioutil.ReadAll(r.Body) + w.Write(body) + }) - addMapping("/gen/", genRoot) - addMapping("/packages/", packageRoot) + addMapping("/gen/", genRoot) + addMapping("/packages/", packageRoot) - http.ListenAndServe(":" + port, nil) + http.ListenAndServe(":"+port, nil) } diff --git a/engine/src/flutter/tools/skygo/sky_server.sha1 b/engine/src/flutter/tools/skygo/sky_server.sha1 index 063c6d0e3c..214818e6f8 100644 --- a/engine/src/flutter/tools/skygo/sky_server.sha1 +++ b/engine/src/flutter/tools/skygo/sky_server.sha1 @@ -1 +1 @@ -6d1d975f16d5ff09aeb2f7b6ee97eea33d0c9fa8 \ No newline at end of file +02d2a03fedfb85053f7f94a0f9464b228609658a \ No newline at end of file