gofcgisrv

package module
v0.0.0-...-47ce04e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2013 License: MIT Imports: 18 Imported by: 0

README

gofcgisrv

Go package for the webserver end of the CGI, FastCGI, and SCGI protocols.

The terms "server" and "client" are confusing. "Server" here generally means "webserver," as referred to in (for example) the FastCGI spec. In terms of who is dialing whom, the webserver is the FastCGI or SCGI client. Sorry.

See godoc for usage.

No one really seems to support FastCGI properly and completely.

Bugs and todos

There is nothing here to launch processes. Only TCP connections are supported, not STDIN.

Not all CGI headers are correctly set.

Documentation

Overview

Package gofcgisrv implements the webserver side of the CGI, FastCGI, and SCGI protocols.

CGI: http://tools.ietf.org/html/rfc3875 FastCGI: http://www.fastcgi.com/drupal/node/6?q=node/22 SCGI: http://python.ca/scgi/protocol.txt protocols.

Example (Php)
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"path"
	"strings"
)

func phpCgiHandler(w http.ResponseWriter, r *http.Request) {
	filepath := path.Join("./testdata/", r.URL.Path)
	env := []string{
		"REDIRECT_STATUS=200",
		"SCRIPT_FILENAME=" + filepath,
	}

	ServeHTTP(NewCGI("php-cgi"), env, w, r)
}

func main() {
	server := httptest.NewServer(http.HandlerFunc(phpCgiHandler))
	defer server.Close()
	url := server.URL

	text := "This is a test!"
	resp, err := http.Post(url+"/echo.php", "text/plain", strings.NewReader(text))
	if err == nil {
		fmt.Printf("Status: %v\n", resp.StatusCode)
		body, _ := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		fmt.Printf("Response: %s\n", body)
	}

}
Output:

Status: 200
Response: This is a test!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPEnv

func HTTPEnv(start []string, r *http.Request) []string

HTTPEnv sets up an environment with standard HTTP/CGI variables.

func ProcessResponse

func ProcessResponse(stdout io.Reader, w http.ResponseWriter, r *http.Request) error

ProcessResponse adds any returned header data to the response header and sends the rest to the response body.

func ServeHTTP

func ServeHTTP(s Requester, env []string, w http.ResponseWriter, r *http.Request)

ServeHTTP serves an http request using FastCGI

Types

type CGIRequester

type CGIRequester struct {
	// contains filtered or unexported fields
}

A CGI server.

func NewCGI

func NewCGI(cmd string, args ...string) *CGIRequester

func (*CGIRequester) Request

func (cr *CGIRequester) Request(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error

type Dialer

type Dialer interface {
	Dial() (net.Conn, error)
}

type FCGIRequester

type FCGIRequester struct {

	// Parameters of the application
	CanMultiplex bool
	MaxConns     int
	MaxRequests  int
	// contains filtered or unexported fields
}

Server is the external interface. It manages connections to a single FastCGI application. A server may maintain many connections, each of which may multiplex many requests.

func NewFCGI

func NewFCGI(applicationAddr string) *FCGIRequester

NewServer creates a server that will attempt to connect to the application at the given address over TCP.

func NewFCGIStdin

func NewFCGIStdin(app string, args ...string) *FCGIRequester

NewFCGIStdin creates a server that runs the app and connects over stdin.

func (*FCGIRequester) GetValues

func (s *FCGIRequester) GetValues() error

PHP barfs on FCGI_GET_VALUES. I don't know why. Maybe it expects a different connection. For now don't do it unless asked.

func (*FCGIRequester) Request

func (s *FCGIRequester) Request(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error

Request executes a request using env and stdin as inputs and stdout and stderr as outputs. env should be a slice of name=value pairs. It blocks until the application has finished.

func (*FCGIRequester) ServeHTTP

func (s *FCGIRequester) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves an HTTP request.

type Requester

type Requester interface {
	Request(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error
}

Requester is the interface for any CGI-like protocol server.

type RequesterFunc

type RequesterFunc func(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error

Wrapper for functions

func (RequesterFunc) Request

func (f RequesterFunc) Request(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error

type SCGIRequester

type SCGIRequester struct {
	// contains filtered or unexported fields
}

func NewSCGI

func NewSCGI(addr string) *SCGIRequester

func (*SCGIRequester) Request

func (sr *SCGIRequester) Request(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error

type StdinDialer

type StdinDialer struct {
	// contains filtered or unexported fields
}

StdinDialer managers an app as a child process, creating a socket and passing it through stdin.

func (*StdinDialer) Close

func (sd *StdinDialer) Close()

func (*StdinDialer) Dial

func (sd *StdinDialer) Dial() (net.Conn, error)

func (*StdinDialer) Start

func (sd *StdinDialer) Start() error

type TCPDialer

type TCPDialer struct {
	// contains filtered or unexported fields
}

func (TCPDialer) Dial

func (d TCPDialer) Dial() (net.Conn, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL