http.go 906 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package conn
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. )
  6. func ParseHttp(tee *Tee, reqs chan *http.Request, resps chan *http.Response) {
  7. lastReq := make(chan *http.Request)
  8. go func() {
  9. for {
  10. req, err := http.ReadRequest(tee.ReadBuffer())
  11. if err != nil {
  12. // no more requests to be read, we're done
  13. break
  14. }
  15. lastReq <- req
  16. // make sure we read the body of the request so that
  17. // we don't block the reader
  18. _, _ = httputil.DumpRequest(req, true)
  19. reqs <- req
  20. }
  21. }()
  22. go func() {
  23. for {
  24. req := <-lastReq
  25. resp, err := http.ReadResponse(tee.WriteBuffer(), req)
  26. if err != nil {
  27. tee.Warn("Error reading response from server: %v", err)
  28. // no more responses to be read, we're done
  29. break
  30. }
  31. // make sure we read the body of the response so that
  32. // we don't block the writer
  33. _, _ = httputil.DumpResponse(resp, true)
  34. resps <- resp
  35. }
  36. }()
  37. }