Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: sky/tools/skygo/sky_server.go

Issue 1059113004: Teach sky_server not to serve up /.git (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/tools/skygo/README ('k') | sky/tools/skygo/sky_server.sha1 » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "flag" 8 » "flag"
9 "fmt" 9 » "fmt"
10 "io/ioutil" 10 » "io/ioutil"
11 "net/http" 11 » "net/http"
12 "os" 12 » "os"
13 "path" 13 » "path"
14 "path/filepath" 14 » "path/filepath"
15 "strings" 15 » "strings"
16 ) 16 )
17 17
18 type skyHandlerRoot struct { 18 type skyHandlerRoot struct {
19 root string 19 » root string
20 } 20 }
21 21
22 func skyHandler(root string) http.Handler { 22 func skyHandler(root string) http.Handler {
23 return &skyHandlerRoot{root} 23 » return &skyHandlerRoot{root}
24 } 24 }
25 25
26 func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) { 26 func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
27 path := path.Join(handler.root, r.URL.Path) 27 » if strings.HasPrefix(r.URL.Path, "/.git") {
28 if strings.HasSuffix(path, ".sky") { 28 » » w.WriteHeader(http.StatusNotFound)
29 w.Header().Set("Content-Type", "text/sky") 29 » » return
30 } 30 » }
31 w.Header().Set("Cache-Control", "no-cache") 31 » path := path.Join(handler.root, r.URL.Path)
32 http.ServeFile(w, r, path) 32 » if strings.HasSuffix(path, ".sky") {
33 » » w.Header().Set("Content-Type", "text/sky")
34 » }
35 » w.Header().Set("Cache-Control", "no-cache")
36 » http.ServeFile(w, r, path)
33 } 37 }
34 38
35 func usage() { 39 func usage() {
36 fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n") 40 » fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n ")
37 fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\ n") 41 » fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\n")
38 fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n") 42 » fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n")
39 fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:flag.\n \n") 43 » fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:fla g.\n\n")
40 flag.PrintDefaults() 44 » flag.PrintDefaults()
41 os.Exit(2) 45 » os.Exit(2)
42 } 46 }
43 47
44 func addMapping(from_path string, to_path string) { 48 func addMapping(from_path string, to_path string) {
45 // Print to stderr to it's more obvious what this binary does. 49 » // Print to stderr to it's more obvious what this binary does.
46 fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path) 50 » fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path)
47 http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path))) 51 » http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path)))
48 } 52 }
49 53
50 func main() { 54 func main() {
51 var configuration = flag.String("t", "Release", "The target configuration (i .e. Release or Debug)") 55 » var configuration = flag.String("t", "Release", "The target configuratio n (i.e. Release or Debug)")
52 56
53 flag.Parse() 57 » flag.Parse()
54 flag.Usage = usage 58 » flag.Usage = usage
55 // The built-in go:flag is awful. It only allows short-name arguments 59 » // The built-in go:flag is awful. It only allows short-name arguments
56 // and they *must* be before any unnamed arguments. There are better ones: 60 » // and they *must* be before any unnamed arguments. There are better on es:
57 // https://godoc.org/github.com/jessevdk/go-flags 61 » // https://godoc.org/github.com/jessevdk/go-flags
58 // but for now we're using raw-go. 62 » // but for now we're using raw-go.
59 if flag.NArg() != 3 { 63 » if flag.NArg() != 3 {
60 usage() 64 » » usage()
61 } 65 » }
62 66
63 root, _ := filepath.Abs(flag.Arg(0)) 67 » root, _ := filepath.Abs(flag.Arg(0))
64 port := flag.Arg(1) 68 » port := flag.Arg(1)
65 packageRoot := flag.Arg(2) 69 » packageRoot := flag.Arg(2)
66 70
67 // genRoot should not be needed once we figure out how mojom generation 71 » // genRoot should not be needed once we figure out how mojom generation
68 // for sdk users should work. 72 » // for sdk users should work.
69 genRoot := path.Join(root, "out", *configuration, "gen") 73 » genRoot := path.Join(root, "out", *configuration, "gen")
70 74
71 fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port) 75 » fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port)
72 76
73 fmt.Fprintf(os.Stderr, " / -> %s\n", root) 77 » fmt.Fprintf(os.Stderr, " / -> %s\n", root)
74 http.Handle("/", skyHandler(root)) 78 » http.Handle("/", skyHandler(root))
75 79
76 fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n") 80 » fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n")
77 http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) { 81 » http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Reques t) {
78 defer r.Body.Close() 82 » » defer r.Body.Close()
79 body, _ := ioutil.ReadAll(r.Body) 83 » » body, _ := ioutil.ReadAll(r.Body)
80 w.Write(body) 84 » » w.Write(body)
81 }) 85 » })
82 86
83 addMapping("/gen/", genRoot) 87 » addMapping("/gen/", genRoot)
84 addMapping("/packages/", packageRoot) 88 » addMapping("/packages/", packageRoot)
85 89
86 http.ListenAndServe(":" + port, nil) 90 » http.ListenAndServe(":"+port, nil)
87 } 91 }
OLDNEW
« no previous file with comments | « sky/tools/skygo/README ('k') | sky/tools/skygo/sky_server.sha1 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698