// Will receive an input stream which would convert the response to utf-8 // The given function must close the reader r, in order to close the response body. func HandleStringReader(f func(r io.Reader, ctx *goproxy.ProxyCtx) io.Reader) goproxy.RespHandler { return goproxy.FuncRespHandler(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { if ctx.Error != nil { return nil } charsetName := ctx.Charset() if charsetName == "" { charsetName = "utf-8" } if strings.ToLower(charsetName) != "utf-8" { r, err := charset.NewReader(charsetName, resp.Body) if err != nil { ctx.Warnf("Cannot convert from %v to utf-8: %v", charsetName, err) return resp } tr, err := charset.TranslatorTo(charsetName) if err != nil { ctx.Warnf("Can't translate to %v from utf-8: %v", charsetName, err) return resp } if err != nil { ctx.Warnf("Cannot translate to %v: %v", charsetName, err) return resp } newr := charset.NewTranslatingReader(f(r, ctx), tr) resp.Body = &readFirstCloseBoth{ioutil.NopCloser(newr), resp.Body} } else { //no translation is needed, already at utf-8 resp.Body = &readFirstCloseBoth{ioutil.NopCloser(f(resp.Body, ctx)), resp.Body} } return resp }) }
func main() { proxy := goproxy.NewProxyHttpServer() rp := NewRProxy() proxy.OnRequest().Do(goproxy.FuncReqHandler(rp.addSignature)) proxy.OnResponse().Do(goproxy.FuncRespHandler(rp.patch)) http.Handle("/", proxy) log.Println("Starting listening on :2424") log.Fatal(http.ListenAndServe(":2424", nil)) }
func main() { proxy := goproxy.NewProxyHttpServer() rp := NewRproxy() proxy.OnRequest().Do(goproxy.FuncReqHandler(rp.storeSig)) proxy.OnResponse().Do(goproxy.FuncRespHandler(rp.delta)) http.Handle("/", proxy) log.Println("Starting listening on :8080") log.Fatal(http.ListenAndServe(":8080", nil)) }