http

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2018 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EventResponse thrown after the request been processed. See Event as payload.
	EventResponse = iota + 500

	// EventError thrown on any non job error provided by road runner server.
	EventError
)
View Source
const (
	// There is no error, the file uploaded with success.
	UploadErrorOK = 0

	// No file was uploaded.
	UploadErrorNoFile = 4

	// Missing a temporary folder.
	UploadErrorNoTmpDir = 5

	// Failed to write file to disk.
	UploadErrorCantWrite = 6

	// Forbid file extension.
	UploadErrorExtension = 7
)
View Source
const ID = "http"

ID contains default svc name.

View Source
const MaxLevel = 127

MaxLevel defines maximum tree depth for incoming request data and files.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Enable enables http svc.
	Enable bool

	// Address and port to handle as http server.
	Address string

	// MaxRequest specified max size for payload body in megabytes, set 0 to unlimited.
	MaxRequest int64

	// Uploads configures uploads configuration.
	Uploads *UploadsConfig

	// Workers configures roadrunner server and worker pool.
	Workers *roadrunner.ServerConfig
}

Configures RoadRunner HTTP server.

func (*Config) Valid

func (cfg *Config) Valid() error

Valid validates the configuration.

type Event

type Event struct {
	// Method of the request.
	Method string

	// Uri requested by the client.
	Uri string

	// Status is response status.
	Status int

	// Associated error, if any.
	Error error
}

Event represents singular http response event.

type FileUpload

type FileUpload struct {
	// ID contains filename specified by the client.
	Name string `json:"name"`

	// Mime contains mime-type provided by the client.
	Mime string `json:"mime"`

	// Size of the uploaded file.
	Size int64 `json:"size"`

	// Error indicates file upload error (if any). See http://php.net/manual/en/features.file-upload.errors.php
	Error int `json:"error"`

	// TempFilename points to temporary file location.
	TempFilename string `json:"tmpName"`
	// contains filtered or unexported fields
}

FileUpload represents singular file NewUpload.

func NewUpload

func NewUpload(f *multipart.FileHeader) *FileUpload

NewUpload wraps net/http upload into PRS-7 compatible structure.

func (*FileUpload) Open

func (f *FileUpload) Open(cfg *UploadsConfig) error

type Handler

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

Handler serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers, parsed files and query, payload will include parsed form dataTree (if any).

func (*Handler) Listen

func (h *Handler) Listen(l func(event int, ctx interface{}))

AddListener attaches pool event watcher.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

middleware serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled.

type Request

type Request struct {
	// Protocol includes HTTP protocol version.
	Protocol string `json:"protocol"`

	// Method contains name of HTTP method used for the request.
	Method string `json:"method"`

	// Uri contains full request Uri with scheme and query.
	Uri string `json:"uri"`

	// Headers contains list of request headers.
	Headers http.Header `json:"headers"`

	// Cookies contains list of request cookies.
	Cookies map[string]string `json:"cookies"`

	// RawQuery contains non parsed query string (to be parsed on php end).
	RawQuery string `json:"rawQuery"`

	// Parsed indicates that request body has been parsed on RR end.
	Parsed bool `json:"parsed"`

	// Uploads contains list of uploaded files, their names, sized and associations with temporary files.
	Uploads *Uploads `json:"uploads"`
	// contains filtered or unexported fields
}

Request maps net/http requests to PSR7 compatible structure and managed state of temporary uploaded files.

func NewRequest

func NewRequest(r *http.Request, cfg *UploadsConfig) (req *Request, err error)

NewRequest creates new PSR7 compatible request using net/http request.

func (*Request) Close

func (r *Request) Close()

Close clears all temp file uploads

func (*Request) Open

func (r *Request) Open() error

Open moves all uploaded files to temporary directory so it can be given to php later.

func (*Request) Payload

func (r *Request) Payload() (p *roadrunner.Payload, err error)

Payload request marshaled RoadRunner payload based on PSR7 data. Default encode method is JSON. Make sure to open files prior to calling this method.

type Response

type Response struct {
	// Status contains response status.
	Status int `json:"status"`

	// Headers contains list of response headers.
	Headers map[string][]string `json:"headers"`
	// contains filtered or unexported fields
}

Response handles PSR7 response logic.

func NewResponse

func NewResponse(p *roadrunner.Payload) (*Response, error)

NewResponse creates new response based on given roadrunner payload.

func (*Response) Write

func (r *Response) Write(w http.ResponseWriter) error

Write writes response headers, status and body into ResponseWriter.

type Service

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

Service manages rr, http servers.

func (*Service) AddListener

func (s *Service) AddListener(l func(event int, ctx interface{}))

AddListener attaches server event watcher.

func (*Service) AddMiddleware

func (s *Service) AddMiddleware(m middleware)

func (*Service) Init

func (s *Service) Init(cfg service.Config, c service.Container) (bool, error)

Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of misconfiguration. Services must not be used without proper configuration pushed first.

func (*Service) Serve

func (s *Service) Serve() error

Serve serves the svc.

func (*Service) ServeHTTP

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

middleware handles connection using set of mdws and rr PSR-7 server.

func (*Service) Stop

func (s *Service) Stop()

Stop stops the svc.

type Uploads

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

tree manages uploaded files tree and temporary files.

func (*Uploads) Clear

func (u *Uploads) Clear()

Clear deletes all temporary files.

func (*Uploads) MarshalJSON

func (u *Uploads) MarshalJSON() ([]byte, error)

MarshalJSON marshal tree tree into JSON.

func (*Uploads) Open

func (u *Uploads) Open() error

Open moves all uploaded files to temp directory, return error in case of issue with temp directory. File errors will be handled individually.

type UploadsConfig

type UploadsConfig struct {
	// Dir contains name of directory to control access to.
	Dir string

	// Forbid specifies list of file extensions which are forbidden for access.
	// Example: .php, .exe, .bat, .htaccess and etc.
	Forbid []string
}

UploadsConfig describes file location and controls access to them.

func (*UploadsConfig) Forbids

func (cfg *UploadsConfig) Forbids(filename string) bool

Forbid must return true if file extension is not allowed for the upload.

func (*UploadsConfig) TmpDir

func (cfg *UploadsConfig) TmpDir() string

TmpDir returns temporary directory.

type Worker

type Worker struct {
	// Pid contains process id.
	Pid int `json:"pid"`

	// Status of the worker.
	Status string `json:"status"`

	// Number of worker executions.
	NumJobs int64 `json:"numExecs"`

	// Created is unix nano timestamp of worker creation time.
	Created int64 `json:"created"`
}

Worker provides information about specific worker.

type WorkerList

type WorkerList struct {
	// Workers is list of workers.
	Workers []Worker `json:"workers"`
}

WorkerList contains list of workers.

Jump to

Keyboard shortcuts

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