2015-01-30 01:48:35 -05:00
|
|
|
// Forked Jan. 2015 from http://bitbucket.org/PinIdea/fcgi_client
|
|
|
|
// (which is forked from https://code.google.com/p/go-fastcgi-client/)
|
|
|
|
|
|
|
|
// This fork contains several fixes and improvements by Matt Holt and
|
|
|
|
// other contributors to this project.
|
|
|
|
|
|
|
|
// Copyright 2012 Junqing Tan <ivan@mysqlab.net> and The Go Authors
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// Part of source code is from Go fcgi package
|
|
|
|
|
|
|
|
package fastcgi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"mime/multipart"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/textproto"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2015-04-20 18:40:54 -05:00
|
|
|
"strings"
|
2015-01-30 01:48:35 -05:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2015-10-09 17:35:34 -05:00
|
|
|
const FCGIListenSockFileno uint8 = 0
|
|
|
|
const FCGIHeaderLen uint8 = 8
|
|
|
|
const Version1 uint8 = 1
|
|
|
|
const FCGINullRequestID uint8 = 0
|
|
|
|
const FCGIKeepConn uint8 = 1
|
2015-01-30 01:48:35 -05:00
|
|
|
const doubleCRLF = "\r\n\r\n"
|
|
|
|
|
|
|
|
const (
|
2015-10-09 17:35:34 -05:00
|
|
|
BeginRequest uint8 = iota + 1
|
|
|
|
AbortRequest
|
|
|
|
EndRequest
|
|
|
|
Params
|
|
|
|
Stdin
|
|
|
|
Stdout
|
|
|
|
Stderr
|
|
|
|
Data
|
|
|
|
GetValues
|
|
|
|
GetValuesResult
|
|
|
|
UnknownType
|
|
|
|
MaxType = UnknownType
|
2015-01-30 01:48:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-10-09 17:35:34 -05:00
|
|
|
Responder uint8 = iota + 1
|
|
|
|
Authorizer
|
|
|
|
Filter
|
2015-01-30 01:48:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-10-09 17:35:34 -05:00
|
|
|
RequestComplete uint8 = iota
|
|
|
|
CantMultiplexConns
|
|
|
|
Overloaded
|
|
|
|
UnknownRole
|
2015-01-30 01:48:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-10-09 17:35:34 -05:00
|
|
|
MaxConns string = "MAX_CONNS"
|
|
|
|
MaxRequests string = "MAX_REQS"
|
|
|
|
MultiplexConns string = "MPXS_CONNS"
|
2015-01-30 01:48:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxWrite = 65500 // 65530 may work, but for compatibility
|
|
|
|
maxPad = 255
|
|
|
|
)
|
|
|
|
|
|
|
|
type header struct {
|
|
|
|
Version uint8
|
|
|
|
Type uint8
|
2015-10-09 17:35:34 -05:00
|
|
|
ID uint16
|
2015-01-30 01:48:35 -05:00
|
|
|
ContentLength uint16
|
|
|
|
PaddingLength uint8
|
|
|
|
Reserved uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
// for padding so we don't have to allocate all the time
|
|
|
|
// not synchronized because we don't care what the contents are
|
|
|
|
var pad [maxPad]byte
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
func (h *header) init(recType uint8, reqID uint16, contentLength int) {
|
2015-01-30 01:48:35 -05:00
|
|
|
h.Version = 1
|
|
|
|
h.Type = recType
|
2015-10-09 17:35:34 -05:00
|
|
|
h.ID = reqID
|
2015-01-30 01:48:35 -05:00
|
|
|
h.ContentLength = uint16(contentLength)
|
|
|
|
h.PaddingLength = uint8(-contentLength & 7)
|
|
|
|
}
|
|
|
|
|
|
|
|
type record struct {
|
|
|
|
h header
|
|
|
|
rbuf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rec *record) read(r io.Reader) (buf []byte, err error) {
|
|
|
|
if err = binary.Read(r, binary.BigEndian, &rec.h); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rec.h.Version != 1 {
|
|
|
|
err = errors.New("fcgi: invalid header version")
|
|
|
|
return
|
|
|
|
}
|
2015-10-09 17:35:34 -05:00
|
|
|
if rec.h.Type == EndRequest {
|
2015-01-30 01:48:35 -05:00
|
|
|
err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n := int(rec.h.ContentLength) + int(rec.h.PaddingLength)
|
|
|
|
if len(rec.rbuf) < n {
|
|
|
|
rec.rbuf = make([]byte, n)
|
|
|
|
}
|
|
|
|
if n, err = io.ReadFull(r, rec.rbuf[:n]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf = rec.rbuf[:int(rec.h.ContentLength)]
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:35:34 -05:00
|
|
|
// FCGIClient implements a FastCGI client, which is a standard for
|
|
|
|
// interfacing external applications with Web servers.
|
2015-01-30 01:48:35 -05:00
|
|
|
type FCGIClient struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
h header
|
|
|
|
buf bytes.Buffer
|
|
|
|
keepAlive bool
|
2015-10-09 17:35:34 -05:00
|
|
|
reqID uint16
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// Dial connects to the fcgi responder at the specified network address.
|
2015-01-30 01:48:35 -05:00
|
|
|
// See func net.Dial for a description of the network and address parameters.
|
|
|
|
func Dial(network, address string) (fcgi *FCGIClient, err error) {
|
|
|
|
var conn net.Conn
|
|
|
|
|
|
|
|
conn, err = net.Dial(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fcgi = &FCGIClient{
|
|
|
|
rwc: conn,
|
|
|
|
keepAlive: false,
|
2015-10-09 17:35:34 -05:00
|
|
|
reqID: 1,
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// Close closes fcgi connnection
|
|
|
|
func (c *FCGIClient) Close() {
|
|
|
|
c.rwc.Close()
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) writeRecord(recType uint8, content []byte) (err error) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
c.buf.Reset()
|
2015-10-09 17:35:34 -05:00
|
|
|
c.h.init(recType, c.reqID, len(content))
|
2015-05-24 21:52:34 -05:00
|
|
|
if err := binary.Write(&c.buf, binary.BigEndian, c.h); err != nil {
|
2015-01-30 01:48:35 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
if _, err := c.buf.Write(content); err != nil {
|
2015-01-30 01:48:35 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
if _, err := c.buf.Write(pad[:c.h.PaddingLength]); err != nil {
|
2015-01-30 01:48:35 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
_, err = c.rwc.Write(c.buf.Bytes())
|
2015-01-30 01:48:35 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) writeBeginRequest(role uint16, flags uint8) error {
|
2015-01-30 01:48:35 -05:00
|
|
|
b := [8]byte{byte(role >> 8), byte(role), flags}
|
2015-10-09 17:35:34 -05:00
|
|
|
return c.writeRecord(BeginRequest, b[:])
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) writeEndRequest(appStatus int, protocolStatus uint8) error {
|
2015-01-30 01:48:35 -05:00
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(b, uint32(appStatus))
|
|
|
|
b[4] = protocolStatus
|
2015-10-09 17:35:34 -05:00
|
|
|
return c.writeRecord(EndRequest, b)
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) writePairs(recType uint8, pairs map[string]string) error {
|
|
|
|
w := newWriter(c, recType)
|
2015-01-30 01:48:35 -05:00
|
|
|
b := make([]byte, 8)
|
|
|
|
nn := 0
|
|
|
|
for k, v := range pairs {
|
|
|
|
m := 8 + len(k) + len(v)
|
|
|
|
if m > maxWrite {
|
|
|
|
// param data size exceed 65535 bytes"
|
|
|
|
vl := maxWrite - 8 - len(k)
|
|
|
|
v = v[:vl]
|
|
|
|
}
|
|
|
|
n := encodeSize(b, uint32(len(k)))
|
|
|
|
n += encodeSize(b[n:], uint32(len(v)))
|
|
|
|
m = n + len(k) + len(v)
|
|
|
|
if (nn + m) > maxWrite {
|
|
|
|
w.Flush()
|
|
|
|
nn = 0
|
|
|
|
}
|
|
|
|
nn += m
|
|
|
|
if _, err := w.Write(b[:n]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.WriteString(k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.WriteString(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSize(s []byte) (uint32, int) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
size, n := uint32(s[0]), 1
|
|
|
|
if size&(1<<7) != 0 {
|
|
|
|
if len(s) < 4 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
n = 4
|
|
|
|
size = binary.BigEndian.Uint32(s)
|
|
|
|
size &^= 1 << 31
|
|
|
|
}
|
|
|
|
return size, n
|
|
|
|
}
|
|
|
|
|
|
|
|
func readString(s []byte, size uint32) string {
|
|
|
|
if size > uint32(len(s)) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(s[:size])
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeSize(b []byte, size uint32) int {
|
|
|
|
if size > 127 {
|
|
|
|
size |= 1 << 31
|
|
|
|
binary.BigEndian.PutUint32(b, size)
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
b[0] = byte(size)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// bufWriter encapsulates bufio.Writer but also closes the underlying stream when
|
|
|
|
// Closed.
|
|
|
|
type bufWriter struct {
|
|
|
|
closer io.Closer
|
|
|
|
*bufio.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bufWriter) Close() error {
|
|
|
|
if err := w.Writer.Flush(); err != nil {
|
|
|
|
w.closer.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWriter(c *FCGIClient, recType uint8) *bufWriter {
|
|
|
|
s := &streamWriter{c: c, recType: recType}
|
|
|
|
w := bufio.NewWriterSize(s, maxWrite)
|
|
|
|
return &bufWriter{s, w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// streamWriter abstracts out the separation of a stream into discrete records.
|
|
|
|
// It only writes maxWrite bytes at a time.
|
|
|
|
type streamWriter struct {
|
|
|
|
c *FCGIClient
|
|
|
|
recType uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *streamWriter) Write(p []byte) (int, error) {
|
|
|
|
nn := 0
|
|
|
|
for len(p) > 0 {
|
|
|
|
n := len(p)
|
|
|
|
if n > maxWrite {
|
|
|
|
n = maxWrite
|
|
|
|
}
|
|
|
|
if err := w.c.writeRecord(w.recType, p[:n]); err != nil {
|
|
|
|
return nn, err
|
|
|
|
}
|
|
|
|
nn += n
|
|
|
|
p = p[n:]
|
|
|
|
}
|
|
|
|
return nn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *streamWriter) Close() error {
|
|
|
|
// send empty record to close the stream
|
|
|
|
return w.c.writeRecord(w.recType, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type streamReader struct {
|
|
|
|
c *FCGIClient
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *streamReader) Read(p []byte) (n int, err error) {
|
|
|
|
|
|
|
|
if len(p) > 0 {
|
|
|
|
if len(w.buf) == 0 {
|
|
|
|
rec := &record{}
|
|
|
|
w.buf, err = rec.read(w.c.rwc)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = len(p)
|
|
|
|
if n > len(w.buf) {
|
|
|
|
n = len(w.buf)
|
|
|
|
}
|
|
|
|
copy(p, w.buf[:n])
|
|
|
|
w.buf = w.buf[n:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do made the request and returns a io.Reader that translates the data read
|
|
|
|
// from fcgi responder out of fcgi packet before returning it.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Do(p map[string]string, req io.Reader) (r io.Reader, err error) {
|
2015-10-09 17:35:34 -05:00
|
|
|
err = c.writeBeginRequest(uint16(Responder), 0)
|
2015-01-30 01:48:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:35:34 -05:00
|
|
|
err = c.writePairs(Params, p)
|
2015-01-30 01:48:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:35:34 -05:00
|
|
|
body := newWriter(c, Stdin)
|
2015-01-30 01:48:35 -05:00
|
|
|
if req != nil {
|
|
|
|
io.Copy(body, req)
|
|
|
|
}
|
|
|
|
body.Close()
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
r = &streamReader{c: c}
|
2015-01-30 01:48:35 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request returns a HTTP Response with Header and Body
|
|
|
|
// from fcgi responder
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Request(p map[string]string, req io.Reader) (resp *http.Response, err error) {
|
2015-01-30 01:48:35 -05:00
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
r, err := c.Do(p, req)
|
2015-01-30 01:48:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rb := bufio.NewReader(r)
|
|
|
|
tp := textproto.NewReader(rb)
|
|
|
|
resp = new(http.Response)
|
|
|
|
|
|
|
|
// Parse the response headers.
|
|
|
|
mimeHeader, err := tp.ReadMIMEHeader()
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Header = http.Header(mimeHeader)
|
|
|
|
|
2015-04-20 18:40:54 -05:00
|
|
|
if resp.Header.Get("Status") != "" {
|
2015-06-10 22:17:56 -05:00
|
|
|
statusParts := strings.SplitN(resp.Header.Get("Status"), " ", 2)
|
|
|
|
resp.StatusCode, err = strconv.Atoi(statusParts[0])
|
2015-04-25 22:56:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-06-17 18:12:35 -05:00
|
|
|
if len(statusParts) > 1 {
|
2015-09-28 15:11:05 -05:00
|
|
|
resp.Status = statusParts[1]
|
2015-06-10 22:17:56 -05:00
|
|
|
}
|
2015-09-28 15:11:05 -05:00
|
|
|
|
2015-04-20 18:40:54 -05:00
|
|
|
} else {
|
2015-04-25 22:56:14 -05:00
|
|
|
resp.StatusCode = http.StatusOK
|
2015-04-20 18:40:54 -05:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:48:35 -05:00
|
|
|
// TODO: fixTransferEncoding ?
|
|
|
|
resp.TransferEncoding = resp.Header["Transfer-Encoding"]
|
|
|
|
resp.ContentLength, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
|
|
|
|
|
|
|
|
if chunked(resp.TransferEncoding) {
|
|
|
|
resp.Body = ioutil.NopCloser(httputil.NewChunkedReader(rb))
|
|
|
|
} else {
|
|
|
|
resp.Body = ioutil.NopCloser(rb)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues a GET request to the fcgi responder.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Get(p map[string]string) (resp *http.Response, err error) {
|
2015-01-30 01:48:35 -05:00
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = "GET"
|
|
|
|
p["CONTENT_LENGTH"] = "0"
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Request(p, nil)
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
2015-05-03 06:12:18 -05:00
|
|
|
// Head issues a HEAD request to the fcgi responder.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Head(p map[string]string) (resp *http.Response, err error) {
|
2015-05-03 06:12:18 -05:00
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = "HEAD"
|
|
|
|
p["CONTENT_LENGTH"] = "0"
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Request(p, nil)
|
2015-05-03 06:12:18 -05:00
|
|
|
}
|
|
|
|
|
2015-05-06 12:14:02 -05:00
|
|
|
// Options issues an OPTIONS request to the fcgi responder.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Options(p map[string]string) (resp *http.Response, err error) {
|
2015-05-06 12:14:02 -05:00
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = "OPTIONS"
|
|
|
|
p["CONTENT_LENGTH"] = "0"
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Request(p, nil)
|
2015-05-06 12:14:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post issues a POST request to the fcgi responder. with request body
|
2015-01-30 01:48:35 -05:00
|
|
|
// in the format that bodyType specified
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Post(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) {
|
2015-01-30 01:48:35 -05:00
|
|
|
|
|
|
|
if len(p["REQUEST_METHOD"]) == 0 || p["REQUEST_METHOD"] == "GET" {
|
|
|
|
p["REQUEST_METHOD"] = "POST"
|
|
|
|
}
|
|
|
|
p["CONTENT_LENGTH"] = strconv.Itoa(l)
|
|
|
|
if len(bodyType) > 0 {
|
|
|
|
p["CONTENT_TYPE"] = bodyType
|
|
|
|
} else {
|
|
|
|
p["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Request(p, body)
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
2015-05-03 06:12:18 -05:00
|
|
|
// Put issues a PUT request to the fcgi responder.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Put(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) {
|
2015-05-03 06:12:18 -05:00
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = "PUT"
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Post(p, bodyType, body, l)
|
2015-05-03 06:12:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch issues a PATCH request to the fcgi responder.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Patch(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) {
|
2015-05-03 06:12:18 -05:00
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = "PATCH"
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Post(p, bodyType, body, l)
|
2015-05-03 06:12:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete issues a DELETE request to the fcgi responder.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) Delete(p map[string]string, bodyType string, body io.Reader, l int) (resp *http.Response, err error) {
|
2015-05-03 06:12:18 -05:00
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = "DELETE"
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Post(p, bodyType, body, l)
|
2015-05-03 06:12:18 -05:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:48:35 -05:00
|
|
|
// PostForm issues a POST to the fcgi responder, with form
|
|
|
|
// as a string key to a list values (url.Values)
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) PostForm(p map[string]string, data url.Values) (resp *http.Response, err error) {
|
2015-01-30 01:48:35 -05:00
|
|
|
body := bytes.NewReader([]byte(data.Encode()))
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Post(p, "application/x-www-form-urlencoded", body, body.Len())
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostFile issues a POST to the fcgi responder in multipart(RFC 2046) standard,
|
|
|
|
// with form as a string key to a list values (url.Values),
|
|
|
|
// and/or with file as a string key to a list file path.
|
2015-05-24 21:52:34 -05:00
|
|
|
func (c *FCGIClient) PostFile(p map[string]string, data url.Values, file map[string]string) (resp *http.Response, err error) {
|
2015-01-30 01:48:35 -05:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(buf)
|
|
|
|
bodyType := writer.FormDataContentType()
|
|
|
|
|
|
|
|
for key, val := range data {
|
|
|
|
for _, v0 := range val {
|
|
|
|
err = writer.WriteField(key, v0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range file {
|
|
|
|
fd, e := os.Open(val)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
part, e := writer.CreateFormFile(key, filepath.Base(val))
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return c.Post(p, bodyType, buf, buf.Len())
|
2015-01-30 01:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether chunked is part of the encodings stack
|
2015-06-11 20:33:55 -05:00
|
|
|
func chunked(te []string) bool { return len(te) > 0 && te[0] == "chunked" }
|