What you should do, is to create an http.Response object whose body is a streaming writer.
This is not ideal, and we should support creating http responses easily through goproxy. I need to think through what's the best way to expose that through the API.
package main
import (
"flag"
"io"
"log"
"net/http"
"time"
)
func main() {
addr := flag.String("addr", ":8080", "proxy listen address")
flag.Parse()
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = true
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
resp := goproxy.NewResponse(req, "text/plain", http.StatusAccepted, "")
resp.ContentLength = -1
//resp.TransferEncoding = []string{"chunked"}
pr, pw := io.Pipe()
go func() {
io.WriteString(pw, "hello\n")
io.WriteString(pw, "world\n")
for i := 0; i < 10; i++ {
for j := 0; j < 100; j++ {
io.WriteString(pw, "1234567890")
}
io.WriteString(pw, "\n")
time.Sleep(1 * time.Second)
log.Println("sleeping", i)
}
pw.Close()
}()
resp.Body = pr
println("RETURNING")
return nil, resp
})
log.Fatal(http.ListenAndServe(*addr, proxy))
}