示例#1
0
文件: http.go 项目: bitland/ngrok
func (h *WebHttpView) register() {
	http.HandleFunc("/http/in/replay", func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		txnid := r.Form.Get("txnid")
		if txn, ok := h.idToTxn[txnid]; ok {
			bodyBytes, err := httputil.DumpRequestOut(txn.HttpTxn.Req.Request, true)
			if err != nil {
				panic(err)
			}
			h.ctl.Cmds <- ui.CmdRequest{Payload: bodyBytes}
			w.Write([]byte(http.StatusText(200)))
		} else {
			// XXX: 400
			http.NotFound(w, r)
		}
	})

	http.HandleFunc("/http/in", func(w http.ResponseWriter, r *http.Request) {
		tmpl := template.Must(template.New("page.html").Delims("{%", "%}").Parse(string(static.PageHtml())))

		payloadData := SerializedPayload{
			Txns:    h.HttpRequests.Slice(),
			UiState: <-h.state,
		}

		payload, err := json.Marshal(payloadData)
		if err != nil {
			panic(err)
		}

		// write the response
		if err := tmpl.Execute(w, string(payload)); err != nil {
			panic(err)
		}
	})
}
示例#2
0
文件: http.go 项目: kyleconroy/ngrok
func (h *WebHttpView) register() {
	http.HandleFunc("/http/in/replay", func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		txnid := r.Form.Get("txnid")
		if txn, ok := h.idToTxn[txnid]; ok {
			bodyBytes, err := httputil.DumpRequestOut(txn.Req.Request, true)
			if err != nil {
				panic(err)
			}
			h.ctl.Cmds <- ui.CmdRequest{Payload: bodyBytes}
			w.Write([]byte(http.StatusText(200)))
		} else {
			// XXX: 400
			http.NotFound(w, r)
		}
	})

	http.HandleFunc("/http/in", func(w http.ResponseWriter, r *http.Request) {
		funcMap := template.FuncMap{
			"classForStatus": func(status string) string {
				switch status[0] {
				case '2':
					return "text-info"
				case '3':
					return "muted"
				case '4':
					return "text-warning"
				case '5':
					return "text-error"
				}
				return ""
			},
			"dumpResponse": func(resp *proto.HttpResponse) (interface{}, error) {
				b, err := httputil.DumpResponse(resp.Response, true)
				return string(b), err
			},
			"dumpRequest": func(req *proto.HttpRequest) (interface{}, error) {
				b, err := httputil.DumpRequestOut(req.Request, true)
				return string(b), err
			},
			"handleForm": func(b []byte, h http.Header) (values interface{}, err error) {
				if !isContentType(h, "application/x-www-form-urlencoded") {
					return
				}

				if b != nil {
					values, err = url.ParseQuery(string(b))
				}
				return
			},
			"handleJson": func(raw []byte, h http.Header) interface{} {
				if !isContentType(h, "application/json") {
					return nil
				}

				var err error
				pretty := new(bytes.Buffer)
				out := raw
				if raw != nil {
					err = json.Indent(pretty, raw, "", "    ")
					if err == nil {
						out = pretty.Bytes()
					}
				}

				return struct {
					Str string
					Err error
				}{
					string(out),
					err,
				}
			},
			"handleOther": func(b []byte, h http.Header) interface{} {
				if isContentType(h, "application/json", "application/x-www-form-urlencoded") {
					return nil
				}

				if len(b) == 0 {
					return nil
				}

				syntaxMap := map[string]string{
					"text/xml":               "xml",
					"application/xml":        "xml",
					"text/html":              "xml",
					"text/css":               "css",
					"text/javascript":        "json",
					"application/javascript": "javascript",
				}

				ctype := strings.Split(h.Get("Content-Type"), ";")[0]
				return struct {
					Body   string
					Syntax string
				}{
					string(b),
					syntaxMap[strings.TrimSpace(ctype)],
				}
			},
		}

		tmpl := template.Must(
			template.New("page.html").Funcs(funcMap).Parse(string(static.PageHtml())))
		template.Must(tmpl.Parse(string(static.BodyHtml())))

		// write the response
		if err := tmpl.Execute(w, h); err != nil {
			panic(err)
		}
	})
}