github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/_vendor/src/golang.org/x/tools/playground/common.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package playground registers HTTP handlers at "/compile" and "/share" that 6 // proxy requests to the golang.org playground service. 7 // This package may be used unaltered on App Engine. 8 package playground // import "golang.org/x/tools/playground" 9 10 import ( 11 "bytes" 12 "errors" 13 "fmt" 14 "io" 15 "net/http" 16 ) 17 18 const baseURL = "https://golang.org" 19 20 func init() { 21 http.HandleFunc("/compile", bounce) 22 http.HandleFunc("/share", bounce) 23 } 24 25 func bounce(w http.ResponseWriter, r *http.Request) { 26 b := new(bytes.Buffer) 27 if err := passThru(b, r); err != nil { 28 http.Error(w, "Server error.", http.StatusInternalServerError) 29 report(r, err) 30 return 31 } 32 io.Copy(w, b) 33 } 34 35 func passThru(w io.Writer, req *http.Request) error { 36 if req.URL.Path == "/share" && !allowShare(req) { 37 return errors.New("Forbidden") 38 } 39 defer req.Body.Close() 40 url := baseURL + req.URL.Path 41 r, err := client(req).Post(url, req.Header.Get("Content-type"), req.Body) 42 if err != nil { 43 return fmt.Errorf("making POST request: %v", err) 44 } 45 defer r.Body.Close() 46 if _, err := io.Copy(w, r.Body); err != nil { 47 return fmt.Errorf("copying response Body: %v", err) 48 } 49 return nil 50 } 51 52 var onAppengine = false // will be overriden by appengine.go and appenginevm.go 53 54 func allowShare(r *http.Request) bool { 55 if !onAppengine { 56 return true 57 } 58 switch r.Header.Get("X-AppEngine-Country") { 59 case "", "ZZ", "CN": 60 return false 61 } 62 return true 63 }