Returns the cloned request and the tee body to be used on the main request.
(u *url.URL, req *http.Request)
| 38 | |
| 39 | // Returns the cloned request and the tee body to be used on the main request. |
| 40 | func cloneRequestForSplit(u *url.URL, req *http.Request) (*http.Request, io.ReadCloser, error) { |
| 41 | h := make(http.Header) |
| 42 | for k, v := range req.Header { |
| 43 | h[k] = v |
| 44 | } |
| 45 | |
| 46 | var teeBody io.ReadCloser |
| 47 | mainBody := req.Body |
| 48 | |
| 49 | if req.ContentLength != 0 { |
| 50 | pr, pw := io.Pipe() |
| 51 | teeBody = pr |
| 52 | mainBody = &teeTie{mainBody, pw} |
| 53 | } |
| 54 | |
| 55 | clone, err := http.NewRequest(req.Method, u.String(), teeBody) |
| 56 | if err != nil { |
| 57 | return nil, nil, err |
| 58 | } |
| 59 | |
| 60 | clone.RequestURI = req.RequestURI |
| 61 | clone.Header = h |
| 62 | clone.ContentLength = req.ContentLength |
| 63 | clone.RemoteAddr = req.RemoteAddr |
| 64 | |
| 65 | return clone, mainBody, nil |
| 66 | } |