app: hide and block share functionality from specific countries

This will permit us to serve *.golang.org to Chinese users.

Change-Id: I6a9f012346cb332deaba8eae4375040a00e08e57
Reviewed-on: https://go-review.googlesource.com/14191
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/app/goplay/edit.go b/app/goplay/edit.go
index d2a423e..d0fb6e1 100644
--- a/app/goplay/edit.go
+++ b/app/goplay/edit.go
@@ -23,17 +23,22 @@
 
 type editData struct {
 	Snippet *Snippet
+	Share   bool
 }
 
 func edit(w http.ResponseWriter, r *http.Request) {
 	// Redirect foo.play.golang.org to play.golang.org.
 	if strings.HasSuffix(r.Host, "."+hostname) {
-		http.Redirect(w, r, "http://"+hostname, http.StatusFound)
+		http.Redirect(w, r, "https://"+hostname, http.StatusFound)
 		return
 	}
 
 	snip := &Snippet{Body: []byte(hello)}
 	if strings.HasPrefix(r.URL.Path, "/p/") {
+		if !allowShare(r) {
+			http.Error(w, "Forbidden", http.StatusForbidden)
+			return
+		}
 		c := appengine.NewContext(r)
 		id := r.URL.Path[3:]
 		serveText := false
@@ -56,7 +61,7 @@
 			return
 		}
 	}
-	editTemplate.Execute(w, &editData{snip})
+	editTemplate.Execute(w, &editData{snip, allowShare(r)})
 }
 
 const hello = `package main
diff --git a/app/goplay/edit.html b/app/goplay/edit.html
index 277a6ee..e939e1a 100644
--- a/app/goplay/edit.html
+++ b/app/goplay/edit.html
@@ -15,13 +15,17 @@
 				'runEl':        '#run',
 				'fmtEl':        '#fmt',
 				'fmtImportEl':  '#imports',
+				{{if $.Share}}
 				'shareEl':      '#share',
 				'shareURLEl':   '#shareURL',
+				{{end}}
 				'enableHistory': true
 			});
 			playgroundEmbed({
 				'codeEl':       '#code',
+				{{if $.Share}}
 				'shareEl':      '#share',
+				{{end}}
 				'embedEl':      '#embed',
 				'embedLabelEl': '#embedLabel',
 				'embedHTMLEl':  '#shareURL'
@@ -88,13 +92,14 @@
 						Imports
 					</label>
 				</div>
+				{{if $.Share}}
 				<input type="button" value="Share" id="share">
 				<input type="text" id="shareURL">
-
 				<label id="embedLabel">
 					<input type="checkbox" id="embed">
 					embed
 				</label>
+				{{end}}
 			</div>
 			<div id="aboutControls">
 				<input type="button" value="About" id="aboutButton">
diff --git a/app/goplay/share.go b/app/goplay/share.go
index cbdb7bf..cce9e2a 100644
--- a/app/goplay/share.go
+++ b/app/goplay/share.go
@@ -39,7 +39,7 @@
 }
 
 func share(w http.ResponseWriter, r *http.Request) {
-	if r.Method != "POST" {
+	if !allowShare(r) || r.Method != "POST" {
 		http.Error(w, "Forbidden", http.StatusForbidden)
 		return
 	}
@@ -70,3 +70,14 @@
 
 	fmt.Fprint(w, id)
 }
+
+func allowShare(r *http.Request) bool {
+	if appengine.IsDevAppServer() {
+		return true
+	}
+	switch r.Header.Get("X-AppEngine-Country") {
+	case "", "ZZ", "HK", "CN", "RC":
+		return false
+	}
+	return true
+}