コード例 #1
0
// type Attribute struct {
//     Namespace, Key, Val string
// }
func rewriteAttributes(attributes []html.Attribute, r *http.Request) []html.Attribute {

	rew := make([]html.Attribute, 0, len(attributes))

	for i := 0; i < len(attributes); i++ {
		attr := attributes[i]

		if attr.Key == "class" || attr.Key == "style" {
			continue
		}

		if attr.Key == "href" || attr.Key == "src" || attr.Key == "action" { //  make absolute
			attr.Val = absolutize(attr.Val)
		}

		if attr.Key == "href" || attr.Key == "src" {
			if attr.Key == "href" {
				attr.Val = fmt.Sprintf("/%v?url=%v", fetchURL, attr.Val)
			}
		}

		if attr.Key == "action" {
			if attr.Key == "href" || attr.Key == "action" {
				// attr.Val = fmt.Sprintf("/blob2/form-redirector?redirect-to=%v", attr.Val) // appended as form field, thus not needed here
				if util_appengine.IsLocalEnviron() {
					attr.Val = fmt.Sprintf("http://%v/blob2/form-redirector", r.Host)
				} else {
					attr.Val = fmt.Sprintf("https://%v/blob2/form-redirector", r.Host)
				}

			}
		}

		if attr.Key == "method" {
			attr.Val = "post"
		}

		rew = append(rew, attr)
	}

	rew = append(rew, html.Attribute{Key: "was", Val: "rewritten"})
	rew = append(rew, html.Attribute{Key: "method", Val: "post"})

	return rew
}
コード例 #2
0
ファイル: get data.go プロジェクト: mehulsbhatt/tools-old
func regroupFromDatastore01(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	b1 := new(bytes.Buffer)
	defer func() {
		w.Header().Set("Content-Type", "text/html")
		w.Write(b1.Bytes())
	}()

	var vVSrc [][]byte

	if util_appengine.IsLocalEnviron() {
		vVSrc = bq_statified_res1
	} else {
		dsObj1, _ := dsu.BufGet(w, r, "dsu.WrapBlob__bq_res1")
		vVSrc = dsObj1.VVByte
	}

	if r.FormValue("mock") == "1" {
		dsObj1, _ := dsu.BufGet(w, r, "dsu.WrapBlob__bq_res_test")
		vVSrc = dsObj1.VVByte
	}

	var vVDest [][]byte = make([][]byte, len(vVSrc))

	for i0 := 0; i0 < len(vVSrc); i0++ {

		s_row := string(vVSrc[i0])
		v_row := util.SplitByWhitespace(s_row)
		b_row := new(bytes.Buffer)

		b_row.WriteString(fmt.Sprintf("%16.12s   ", v_row[3])) // leading spaces
		b_row.WriteString(fmt.Sprintf("%16.12s   ", v_row[5]))
		b_row.WriteString(fmt.Sprintf("%16.8s", v_row[7]))

		vVDest[i0] = []byte(b_row.Bytes())

	}

	key_combi, _ := dsu.BufPut(w, r, dsu.WrapBlob{Name: "res_processed_01", S: "[][]byte", VVByte: vVDest}, "res_processed_01")
	dsObj2, _ := dsu.BufGet(w, r, key_combi)

	printPlaintextTable(w, r, dsObj2.VVByte)

}
コード例 #3
0
ファイル: get.go プロジェクト: mehulsbhatt/tools-old
func Get(w http.ResponseWriter, r *http.Request, m map[string]interface{}) *Instance {

	c := appengine.NewContext(r)

	startFunc := time.Now()

	if !ii.LastUpdated.IsZero() {

		age := startFunc.Sub(ii.LastUpdated)

		if age < 200*time.Millisecond {
			c.Infof("instance info update too recently: %v, skipping.\n", age)
			return ii
		}

		if age < 1*time.Hour {
			if len(ii.Hostname) > 2 {
				return ii
			}

		}

		c.Infof("instance info update too old: %v, recomputing.\n", age)
	}

	ii.ModuleName = appengine.ModuleName(c)
	ii.InstanceID = appengine.InstanceID()
	ii.VersionFull = appengine.VersionID(c)

	majorMinor := strings.Split(ii.VersionFull, ".")
	if len(majorMinor) != 2 {
		panic("we need a version string of format X.Y")
	}

	ii.VersionMajor = majorMinor[0]
	ii.VersionMinor = majorMinor[1]

	var err = errors.New("dummy creation error message")

	ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionFull)
	if err != nil {
		// this never works with version full
		// we do not log this - but try version major
		err = nil

		if !util_appengine.IsLocalEnviron() {
			ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionMajor)
			util_err.Err_http(w, r, err, true, "get num instances works only live and without autoscale")
		}

	}

	// in auto scaling, google reports "zero" - which can not be true
	// we assume at least 1
	if ii.NumInstances == 0 {
		ii.NumInstances = 1
	}

	// http://[0-2].1.default.libertarian-islands.appspot.com/instance-info

	ii.Hostname, err = appengine.ModuleHostname(c, ii.ModuleName,
		ii.VersionMajor, "")
	util_err.Err_http(w, r, err, false)

	if !util_appengine.IsLocalEnviron() {
		ii.HostnameInst0, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "0")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 0: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameInst1, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "1")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 1: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameMod02, err = appengine.ModuleHostname(c, "mod02", "", "")
		util_err.Err_http(w, r, err, true)

	}

	ii.LastUpdated = time.Now()

	c.Infof("collectInfo() completed, %v.%v.%v.%v, took %v",
		util.Ellipsoider(ii.InstanceID, 4), ii.VersionMajor, ii.ModuleName,
		ii.Hostname,
		ii.LastUpdated.Sub(startFunc))

	return ii
}
コード例 #4
0
func formRedirector(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	var msg, cntnt, rURL string

	w.Header().Set("Content-type", "text/html; charset=utf-8")
	// w.Header().Set("Content-type", "text/html; charset=latin-1")

	rURL = r.FormValue("redirect-to")
	//util_err.LogAndShow(w, r, "url: %q <br>\n", rURL)

	u, err := url.Parse(rURL)
	util_err.Err_http(w, r, err, false)

	host, port, err = net.SplitHostPort(u.Host)
	util_err.Err_http(w, r, err, true)
	if err != nil {
		host = u.Host
	}
	//util_err.LogAndShow(w, r, "host and port: %q : %q of %q<br>\n", host, port, rURL)
	//util_err.LogAndShow(w, r, " &nbsp;  &nbsp;  &nbsp; standalone %q <br>\n", u.Host)

	client := urlfetch.Client(c)

	if len(r.PostForm) > 0 {
		// util_err.LogAndShow(w, r, "post unimplemented:<br> %#v <br>\n", r.PostForm)
		// return
		msg += fmt.Sprintf("post converted to get<br>")
	}

	rURL = fmt.Sprintf("%v?1=2&", rURL)
	for key, vals := range r.Form {
		if key == "redirect-to" {
			continue
		}
		val := vals[0]
		if !util_appengine.IsLocalEnviron() {
			val = strings.Replace(val, " ", "%20", -1)
		}
		rURL = fmt.Sprintf("%v&%v=%v", rURL, key, val)
	}

	resp, err := client.Get(rURL)
	util_err.Err_http(w, r, err, false)

	if resp.StatusCode != http.StatusOK {
		fmt.Fprintf(w, "HTTP GET returned status %v<br>\n\n%v<br>\n\n", resp.Status, rURL)
		return
	}

	defer resp.Body.Close()
	byteContent, err := ioutil.ReadAll(resp.Body)
	util_err.Err_http(w, r, err, false)
	if err != nil {
		return
	} else {
		msg += fmt.Sprintf("%v bytes read<br>", len(byteContent))
		cntnt = string(byteContent)
	}

	cntnt = insertNewlines.Replace(cntnt)
	cntnt = undouble.Replace(cntnt)

	cntnt = ModifyHTML(r, cntnt)

	fmt.Fprintf(w, "%s \n\n", cntnt)
	fmt.Fprintf(w, "%s \n\n", msg)

}
コード例 #5
0
func handleFetchURL(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	if r.URL.Scheme != "https" && !util_appengine.IsLocalEnviron() {
		r.URL.Scheme = "https"
		r.URL.Host = r.Host
		http.Redirect(w, r, r.URL.String(), http.StatusFound)
	}

	rURL := ""
	urlAsPost := ""

	/*
		To distinguish between posted and getted value,
		we check the "post-only" slice of values first.
		If nothing's there, but FormValue *has* a value,
		then it was "getted", otherwise "posted"
	*/
	if r.PostFormValue("url") != "" {
		urlAsPost = "url posted"
		rURL = r.PostFormValue("url")
	}

	if r.FormValue("url") != "" {
		if rURL == "" {
			urlAsPost = "url getted"
			rURL = r.FormValue("url")
		}
	}

	renderInPre := false
	if len(r.FormValue("renderInPre")) > 0 {
		renderInPre = true
	}

	elipseOutput := r.FormValue("elipseOutput")

	var msg, cntnt string

	if len(rURL) == 0 {

		tplAdder, tplExec := tpl_html.FuncTplBuilder(w, r)
		tplAdder("n_html_title", "Fetch some http data", nil)

		m := map[string]string{"protocol": "https", "host": r.Host, "path": fetchURL, "val": "google.com"}
		if util_appengine.IsLocalEnviron() {
			m["protocol"] = "http"
		}
		tplAdder("n_cont_0", c_formFetchURL, m)
		tplExec(w, r)

	} else {

		w.Header().Set("Content-type", "text/html; charset=utf-8")
		// w.Header().Set("Content-type", "text/html; charset=latin-1")

		if !strings.HasPrefix(rURL, "http://") && !strings.HasPrefix(rURL, "https://") {
			rURL = "https://" + rURL
		}

		u, err := url.Parse(rURL)
		if err != nil {
			panic(err)
		}
		host, port, err = net.SplitHostPort(u.Host)
		if err != nil {
			host = u.Host
		}
		log.Println("host and port: ", host, port, "of", rURL, "standalone:", u.Host)

		client := urlfetch.Client(c)
		resp, err := client.Get(rURL)

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if resp.StatusCode != http.StatusOK {
			fmt.Fprintf(w, "HTTP GET returned status %v<br>\n\n", resp.Status)
			return
		}

		defer resp.Body.Close()
		byteContent, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			c.Errorf("%s", err)
			fmt.Fprintf(w, "Error %v<br>\n\n", err.Error())
			return
		} else {
			msg = fmt.Sprintf("%v bytes read<br>", len(byteContent))
			if urlAsPost != "" {
				msg += fmt.Sprintf("get overwritten by post: %v <br>", urlAsPost)
			}
			cntnt = string(byteContent)
		}

		cntnt = insertNewlines.Replace(cntnt)
		cntnt = undouble.Replace(cntnt)

		if len(elipseOutput) > 0 {
			cutoff := util.Min(100, len(cntnt))
			fmt.Fprintf(w, "content is: <pre>"+cntnt[:cutoff]+" ... "+cntnt[len(cntnt)-cutoff:]+"</pre>")
		} else {
			if renderInPre {
				fmt.Fprintf(w, "content is: <pre>"+cntnt+"</pre>")
			} else {
				cntnt = ModifyHTML(r, cntnt)
				fmt.Fprintf(w, cntnt)
			}
		}

	}

	// cntnt = html.EscapeString(cntnt)

	fmt.Fprintf(w, " %s \n\n", msg)

}