handler

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Trailer   string = "Trailer"
	HTTP2Push string = "Http2-Push"
)
View Source
const (
	// UploadErrorOK - no error, the file uploaded with success.
	UploadErrorOK = 0

	// UploadErrorNoFile - no file was uploaded.
	UploadErrorNoFile = 4

	// UploadErrorNoTmpDir - missing a temporary folder.
	UploadErrorNoTmpDir = 6

	// UploadErrorCantWrite - failed to write file to disk.
	UploadErrorCantWrite = 7

	// UploadErrorExtension - forbidden file extension.
	UploadErrorExtension = 8
)
View Source
const MaxLevel = 127

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

Variables

This section is empty.

Functions

func FetchIP

func FetchIP(pair string, log *zap.Logger) string

func URI

func URI(r *http.Request) string

URI fetches full uri from request in a form of string (including https scheme if TLS connection is enabled).

Types

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, uid, gid int) *FileUpload

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

func (*FileUpload) Open

func (f *FileUpload) Open(dir string, forbid, allow map[string]struct{}) error

Open moves file content into temporary file available for PHP. NOTE: There is 2 deferred functions, and in case of getting 2 errors from both functions error from close of temp file would be overwritten by error from the main file STACK DEFER FILE CLOSE (2) DEFER TMP CLOSE (1)

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 NewHandler

func NewHandler(cfg *config.Config, pool common.Pool, log *zap.Logger) (*Handler, error)

NewHandler return handle interface implementation

func (*Handler) Dispose

func (h *Handler) Dispose()

func (*Handler) ServeHTTP

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

ServeHTTP transform original request to the PSR-7 passed then to the underlying application. Attempts to serve static files first if enabled.

func (*Handler) Write

func (h *Handler) Write(pld *payload.Payload, w http.ResponseWriter) error

Write writes response headers, status and body into ResponseWriter.

type Request

type Request struct {
	// RemoteAddr contains ip address of client, make sure to check X-Real-Ip and X-Forwarded-For for real client address.
	RemoteAddr string `json:"remoteAddr"`

	// 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"`

	// Header contains list of request headers.
	Header 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"`

	// Attributes can be set by chained mdwr to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions.
	Attributes map[string]any `json:"attributes"`
	// contains filtered or unexported fields
}

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

func (*Request) Close

func (r *Request) Close(log *zap.Logger, hr *http.Request)

Close clears all temp file uploads

func (*Request) Open

func (r *Request) Open(log *zap.Logger, dir string, forbid, allow map[string]struct{})

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

func (*Request) Payload

func (r *Request) Payload(p *payload.Payload, sendRawBody bool) error

Payload request marshaled RoadRunner payload based on PSR7 data. values 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"`

	// Header contains list of response headers.
	Headers map[string][]string `json:"headers"`
}

Response handles PSR7 response logic.

type Uploads

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

Uploads tree manages uploaded files tree and temporary files.

func (*Uploads) Clear

func (u *Uploads) Clear(log *zap.Logger)

Clear deletes all temporary files.

func (*Uploads) MarshalJSON

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

MarshalJSON marshal tree into JSON.

func (*Uploads) Open

func (u *Uploads) Open(log *zap.Logger, dir string, forbid, allow map[string]struct{})

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

Jump to

Keyboard shortcuts

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