playground: block share functionality from specific countries
This will permit us to serve *.golang.org to Chinese users. Change-Id: If184760d7f4c9e49a3df3785c15af770958413de Reviewed-on: https://go-review.googlesource.com/14190 Reviewed-by: Jason Buberel <jbuberel@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
b9fbbb8e31
commit
4df6ae9fad
|
@ -13,6 +13,10 @@ import (
|
||||||
"appengine/urlfetch"
|
"appengine/urlfetch"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
onAppengine = !appengine.IsDevAppServer()
|
||||||
|
}
|
||||||
|
|
||||||
func client(r *http.Request) *http.Client {
|
func client(r *http.Request) *http.Client {
|
||||||
return urlfetch.Client(appengine.NewContext(r))
|
return urlfetch.Client(appengine.NewContext(r))
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ package playground // import "golang.org/x/tools/playground"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -32,6 +33,9 @@ func bounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func passThru(w io.Writer, req *http.Request) error {
|
func passThru(w io.Writer, req *http.Request) error {
|
||||||
|
if req.URL.Path == "/share" && !allowShare(req) {
|
||||||
|
return errors.New("Forbidden")
|
||||||
|
}
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
url := baseURL + req.URL.Path
|
url := baseURL + req.URL.Path
|
||||||
r, err := client(req).Post(url, req.Header.Get("Content-type"), req.Body)
|
r, err := client(req).Post(url, req.Header.Get("Content-type"), req.Body)
|
||||||
|
@ -44,3 +48,16 @@ func passThru(w io.Writer, req *http.Request) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var onAppengine = false // will be overriden by appengine.go
|
||||||
|
|
||||||
|
func allowShare(r *http.Request) bool {
|
||||||
|
if !onAppengine {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
switch r.Header.Get("X-AppEngine-Country") {
|
||||||
|
case "", "ZZ", "HK", "CN", "RC":
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue