Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / playground / playground.go
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 Standard with Go 1.11+ runtime.
8 package playground // import "golang.org/x/tools/playground"
9
10 import (
11         "bytes"
12         "context"
13         "fmt"
14         "io"
15         "log"
16         "net/http"
17         "os"
18         "strings"
19         "time"
20
21         "golang.org/x/tools/godoc/golangorgenv"
22 )
23
24 const baseURL = "https://play.golang.org"
25
26 func init() {
27         http.HandleFunc("/compile", bounce)
28         http.HandleFunc("/share", bounce)
29 }
30
31 func bounce(w http.ResponseWriter, r *http.Request) {
32         b := new(bytes.Buffer)
33         if err := passThru(b, r); os.IsPermission(err) {
34                 http.Error(w, "403 Forbidden", http.StatusForbidden)
35                 log.Println(err)
36                 return
37         } else if err != nil {
38                 http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
39                 log.Println(err)
40                 return
41         }
42         io.Copy(w, b)
43 }
44
45 func passThru(w io.Writer, req *http.Request) error {
46         if req.URL.Path == "/share" && googleCN(req) {
47                 return os.ErrPermission
48         }
49         defer req.Body.Close()
50         url := baseURL + req.URL.Path
51         ctx, cancel := context.WithTimeout(req.Context(), 60*time.Second)
52         defer cancel()
53         r, err := post(ctx, url, req.Header.Get("Content-Type"), req.Body)
54         if err != nil {
55                 return fmt.Errorf("making POST request: %v", err)
56         }
57         defer r.Body.Close()
58         if _, err := io.Copy(w, r.Body); err != nil {
59                 return fmt.Errorf("copying response Body: %v", err)
60         }
61         return nil
62 }
63
64 func post(ctx context.Context, url, contentType string, body io.Reader) (*http.Response, error) {
65         req, err := http.NewRequest(http.MethodPost, url, body)
66         if err != nil {
67                 return nil, fmt.Errorf("http.NewRequest: %v", err)
68         }
69         req.Header.Set("Content-Type", contentType)
70         return http.DefaultClient.Do(req.WithContext(ctx))
71 }
72
73 // googleCN reports whether request r is considered
74 // to be served from golang.google.cn.
75 func googleCN(r *http.Request) bool {
76         if r.FormValue("googlecn") != "" {
77                 return true
78         }
79         if strings.HasSuffix(r.Host, ".cn") {
80                 return true
81         }
82         if !golangorgenv.CheckCountry() {
83                 return false
84         }
85         switch r.Header.Get("X-Appengine-Country") {
86         case "", "ZZ", "CN":
87                 return true
88         }
89         return false
90 }