github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/godoc/play-local.go (about) 1 // Copyright 2012 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 // Stand-alone godoc Playground functionality. 6 7 // +build !appengine 8 9 package main 10 11 import ( 12 "io" 13 "net/http" 14 "net/url" 15 ) 16 17 var playgroundScheme, playgroundHost string 18 19 func init() { 20 u, err := url.Parse(playgroundBaseURL) 21 if err != nil { 22 panic(err) 23 } 24 playgroundScheme = u.Scheme 25 playgroundHost = u.Host 26 } 27 28 // bounceToPlayground forwards the request to play.golang.org. 29 func bounceToPlayground(w http.ResponseWriter, req *http.Request) { 30 defer req.Body.Close() 31 req.URL.Scheme = playgroundScheme 32 req.URL.Host = playgroundHost 33 resp, err := http.Post(req.URL.String(), req.Header.Get("Content-type"), req.Body) 34 if err != nil { 35 http.Error(w, err.Error(), 500) 36 return 37 } 38 w.WriteHeader(resp.StatusCode) 39 io.Copy(w, resp.Body) 40 resp.Body.Close() 41 }