goscgi

package module
v0.0.0-...-5ee3815 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: BSD-2-Clause Imports: 14 Imported by: 0

README

goscgi

SimpleCGI protocol implementation for Go lang. Allows creation of a basic HTTP server if used with Nginx or other SCGI capable web server.

Nginx configuration

Locate Nginx configuration file. In Ubuntu it may be located at /etc/nginx/sites-enabled/default. Add scgi_pass & include scgi_params directives in the root location.

location / {
	scgi_pass 127.0.0.1:8080;
	#scgi_pass unix:/tmp/goscgi.socket;
	include scgi_params;
}

If you use unix sockets, don't forget to give write permission to www-data (default nginx user) on the socket file (created at runtime). The examples below, use tcp sockets and don't need any special treatment. Save the config file & restart the Nginx service. In Ubuntu: sudo service nginx restart.

Usage

See goscgi/benchmarks/test/main.go.

Documentation

Index

Constants

View Source
const (
	GET byte = iota
	POST
	PUT
	DELETE
)
View Source
const (
	ContentTypeForm          = "application/x-www-form-urlencoded"
	ContentTypeMultipartForm = "multipart/form-data"
)
View Source
const (
	ContentSizeKey   = "CONTENT_LENGTH"
	ContentTypeKey   = "CONTENT_TYPE"
	RequestMethodKey = "REQUEST_METHOD"
	RequestUriKey    = "REQUEST_URI"
	DocumentUriKey   = "DOCUMENT_URI"
	DocumentRootKey  = "DOCUMENT_ROOT"
	QueryStringKey   = "QUERY_STRING"
	RemoteAddrKey    = "REMOTE_ADDR"
	RemotePortKey    = "REMOTE_PORT"
	RequestedWithKey = "HTTP_X_REQUESTED_WITH"
	HttpCookieKey    = "HTTP_COOKIE"
	HttpUpgradeKey   = "HTTP_UPGRADE"
	HttpUserAgentKey = "HTTP_USER_AGENT"
)

Variables

View Source
var (
	InvalidHeaderErr  = errors.New("Invalid header")
	InvalidContentErr = errors.New("Invalid content size")
	UnexpectedEndErr  = errors.New("Unexpected end of stream")
)
View Source
var (
	RespTypeHtml = []byte("text/html")
	RespTypeText = []byte("text/plain")
	RespTypeJson = []byte("text/json")

	RespCodeOK            = []byte("200 OK")
	RespCodeNotFound      = []byte("404 Not found")
	RespCodeBadRequest    = []byte("400 Bad request")
	RespCodeInternalError = []byte("500 Internal error")
)

Functions

Types

type Handler

type Handler struct {
	Path string
	Func HandlerFunc
}

type HandlerFunc

type HandlerFunc func(*Request) *Response

type Request

type Request struct {
	Connection    net.Conn
	Header        http.Header
	RawURI        string
	URL           *url.URL
	Query         url.Values
	Form          url.Values
	Files         map[string][]*multipart.FileHeader
	MultipartForm *multipart.Form
	Cookies       []*http.Cookie
	Method        byte
	IsAJAX        bool
	UserAgent     string
	Content       []byte
	ContentType   string
	ContentSize   int64
	Settings      *Settings // settings used while reading this request
}

func ReadRequest

func ReadRequest(conn net.Conn, settings *Settings) (*Request, error)

type Response

type Response struct {
	ResponseCode []byte
	ContentType  []byte
	Content      []byte
	Cookies      []*http.Cookie
	Header       http.Header
}

func NewResponse

func NewResponse(respCode, contentType []byte, content []byte, cookies ...*http.Cookie) *Response

func (*Response) SetCookie

func (resp *Response) SetCookie(cookie *http.Cookie)

func (*Response) Write

func (resp *Response) Write(conn net.Conn, timeout time.Duration) error

type Server

type Server struct {
	Settings  *Settings
	Handlers  []Handler
	Close     chan bool
	WaitGroup sync.WaitGroup
}

func NewServer

func NewServer(s *Settings) *Server

func (*Server) AddHandler

func (srv *Server) AddHandler(path string, handler HandlerFunc)

func (*Server) ListenTcp

func (srv *Server) ListenTcp(port string) error

func (*Server) ListenUnix

func (srv *Server) ListenUnix(addrStr string) error

type Settings

type Settings struct {
	MaxHeaderSize  int
	MaxContentSize int64
	ListenTimeout  time.Duration
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
}

func NewSettings

func NewSettings() *Settings

Directories

Path Synopsis
benchmarks

Jump to

Keyboard shortcuts

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