playground: remove /share registration, add Proxy

The /share registration is not needed by programs like
talks and blog, only by golang.org, so installing it by default
is unnecessary and perhaps more exposure than people running
those servers intend.

Add Proxy to allow recreating the /share registration (unlikely)
or installing the proxy on an alternate mux (more likely).

Change-Id: Ibb33add804d353920d405bf022428e48b3815da3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/293449
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Russ Cox 2021-02-16 23:36:42 -05:00
Родитель 24aca17f68
Коммит 54dc8c5edb
5 изменённых файлов: 22 добавлений и 42 удалений

Просмотреть файл

@ -14,11 +14,9 @@ import (
"golang.org/x/tools/godoc"
"golang.org/x/tools/godoc/redirect"
"golang.org/x/tools/godoc/vfs"
)
// This package registers "/compile" and "/share" handlers
// that redirect to the golang.org playground.
import _ "golang.org/x/tools/playground"
_ "golang.org/x/tools/playground" // register "/compile" playground redirect
)
var (
pres *godoc.Presentation

Просмотреть файл

@ -18,7 +18,7 @@ import (
"golang.org/x/tools/playground/socket"
"golang.org/x/tools/present"
// This will register handlers at /compile and /share that will proxy to the
// This will register a handler at /compile that will proxy to the
// respective endpoints at play.golang.org. This allows the frontend to call
// these endpoints without needing cross-origin request sharing (CORS).
// Note that this is imported regardless of whether the endpoints are used or

Просмотреть файл

@ -517,7 +517,7 @@ function PlaygroundOutput(el) {
sharing = true;
var sharingData = body();
$.ajax('/share', {
$.ajax('https://play.golang.org/share', {
processData: false,
data: sharingData,
type: 'POST',

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package playground registers HTTP handlers at "/compile" and "/share" that
// proxy requests to the golang.org playground service.
// Package playground registers an HTTP handler at "/compile" that
// proxies requests to the golang.org playground service.
// This package may be used unaltered on App Engine Standard with Go 1.11+ runtime.
package playground // import "golang.org/x/tools/playground"
@ -14,27 +14,31 @@ import (
"io"
"log"
"net/http"
"os"
"strings"
"time"
"golang.org/x/tools/godoc/golangorgenv"
)
const baseURL = "https://play.golang.org"
func init() {
http.HandleFunc("/compile", bounce)
http.HandleFunc("/share", bounce)
http.Handle("/compile", Proxy())
}
// Proxy returns a handler that can be registered on /compile to proxy requests to the Go playground.
//
// This package already contains a func init that does:
//
// func init() {
// http.Handle("/compile", Proxy())
// }
//
// Proxy may be useful for servers that use HTTP muxes other than the default mux.
func Proxy() http.Handler {
return http.HandlerFunc(bounce)
}
func bounce(w http.ResponseWriter, r *http.Request) {
b := new(bytes.Buffer)
if err := passThru(b, r); os.IsPermission(err) {
http.Error(w, "403 Forbidden", http.StatusForbidden)
log.Println(err)
return
} else if err != nil {
if err := passThru(b, r); err != nil {
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
log.Println(err)
return
@ -43,9 +47,6 @@ func bounce(w http.ResponseWriter, r *http.Request) {
}
func passThru(w io.Writer, req *http.Request) error {
if req.URL.Path == "/share" && googleCN(req) {
return os.ErrPermission
}
defer req.Body.Close()
url := baseURL + req.URL.Path
ctx, cancel := context.WithTimeout(req.Context(), 60*time.Second)
@ -69,22 +70,3 @@ func post(ctx context.Context, url, contentType string, body io.Reader) (*http.R
req.Header.Set("Content-Type", contentType)
return http.DefaultClient.Do(req.WithContext(ctx))
}
// googleCN reports whether request r is considered
// to be served from golang.google.cn.
func googleCN(r *http.Request) bool {
if r.FormValue("googlecn") != "" {
return true
}
if strings.HasSuffix(r.Host, ".cn") {
return true
}
if !golangorgenv.CheckCountry() {
return false
}
switch r.Header.Get("X-Appengine-Country") {
case "", "ZZ", "CN":
return true
}
return false
}