Do makes the request and returns an io.Reader that translates the data read from the FastCGI responder out of FastCGI packets before returning it.
(p map[string]string, req io.Reader)
| 138 | // Do makes the request and returns an io.Reader that translates the data read |
| 139 | // from the FastCGI responder out of FastCGI packets before returning it. |
| 140 | func (c *client) Do(p map[string]string, req io.Reader) (r io.Reader, err error) { |
| 141 | // check for CONTENT_LENGTH, since the lack of it or wrong value will cause the backend to hang |
| 142 | if clStr, ok := p["CONTENT_LENGTH"]; !ok { |
| 143 | return nil, caddyhttp.Error(http.StatusLengthRequired, nil) |
| 144 | } else if _, err := strconv.ParseUint(clStr, 10, 64); err != nil { |
| 145 | // stdlib won't return a negative Content-Length, but we check just in case, |
| 146 | // the most likely cause is from a missing content length, which is -1 |
| 147 | return nil, caddyhttp.Error(http.StatusLengthRequired, err) |
| 148 | } |
| 149 | |
| 150 | writer := &streamWriter{c: c} |
| 151 | writer.buf = bufPool.Get().(*bytes.Buffer) |
| 152 | writer.buf.Reset() |
| 153 | defer bufPool.Put(writer.buf) |
| 154 | |
| 155 | err = writer.writeBeginRequest(uint16(Responder), 0) |
| 156 | if err != nil { |
| 157 | return r, err |
| 158 | } |
| 159 | |
| 160 | writer.recType = Params |
| 161 | err = writer.writePairs(p) |
| 162 | if err != nil { |
| 163 | return r, err |
| 164 | } |
| 165 | |
| 166 | writer.recType = Stdin |
| 167 | if req != nil { |
| 168 | _, err = io.Copy(writer, req) |
| 169 | if err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | } |
| 173 | err = writer.FlushStream() |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | |
| 178 | r = &streamReader{c: c} |
| 179 | return r, err |
| 180 | } |
| 181 | |
| 182 | // clientCloser is an io.ReadCloser. It wraps an io.Reader with a Closer |
| 183 | // that closes the client connection. |