diff options
author | Wolfy-J <[email protected]> | 2018-06-10 16:44:41 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-10 16:44:41 +0300 |
commit | 4c292ee46f5505b00b16186e8f30e9bc1be25895 (patch) | |
tree | 818dffc7ce5e890875b147b97e298d4c7c48cbd9 /service/http | |
parent | a62237fa5afc310453e709837e363f0bb4d7ecf3 (diff) |
fs config
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/config.go | 34 | ||||
-rw-r--r-- | service/http/fs_config.go | 29 | ||||
-rw-r--r-- | service/http/fs_config_test.go | 15 | ||||
-rw-r--r-- | service/http/handler.go | 71 | ||||
-rw-r--r-- | service/http/parse.go | 147 | ||||
-rw-r--r-- | service/http/request.go | 137 | ||||
-rw-r--r-- | service/http/response.go | 54 | ||||
-rw-r--r-- | service/http/rpc.go | 78 | ||||
-rw-r--r-- | service/http/service.go | 86 | ||||
-rw-r--r-- | service/http/uploads.go | 130 |
10 files changed, 781 insertions, 0 deletions
diff --git a/service/http/config.go b/service/http/config.go new file mode 100644 index 00000000..b828eb08 --- /dev/null +++ b/service/http/config.go @@ -0,0 +1,34 @@ +package http + +import ( + "github.com/spiral/roadrunner" + "fmt" +) + +// Configures RoadRunner HTTP server. +type Config struct { + // Enable enables http service. + Enable bool + + // Host and port to handle as http server. + Host, Port string + + // MaxRequest specified max size for payload body in bytes, set 0 to unlimited. + MaxRequest int64 + + // Uploads configures uploads configuration. + Uploads *FsConfig + + // Workers configures roadrunner server and worker pool. + Workers *roadrunner.ServerConfig +} + +// Valid validates the configuration. +func (cfg *Config) Valid() error { + return nil +} + +// httpAddr returns prepared http listen address. +func (cfg *Config) httpAddr() string { + return fmt.Sprintf("%s:%v", cfg.Host, cfg.Port) +} diff --git a/service/http/fs_config.go b/service/http/fs_config.go new file mode 100644 index 00000000..de5b1389 --- /dev/null +++ b/service/http/fs_config.go @@ -0,0 +1,29 @@ +package http + +import ( + "strings" + "path" +) + +// FsConfig describes file location and controls access to them. +type FsConfig 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 +} + +// Forbid must return true if file extension is not allowed for the upload. +func (cfg FsConfig) Forbids(filename string) bool { + ext := strings.ToLower(path.Ext(filename)) + + for _, v := range cfg.Forbid { + if ext == v { + return true + } + } + + return false +} diff --git a/service/http/fs_config_test.go b/service/http/fs_config_test.go new file mode 100644 index 00000000..05f568e5 --- /dev/null +++ b/service/http/fs_config_test.go @@ -0,0 +1,15 @@ +package http + +import ( + "testing" + "github.com/stretchr/testify/assert" +) + +func TestFsConfig_Forbids(t *testing.T) { + cfg := FsConfig{Forbid: []string{".php"}} + + assert.True(t, cfg.Forbids("index.php")) + assert.True(t, cfg.Forbids("index.PHP")) + assert.True(t, cfg.Forbids("phpadmin/index.bak.php")) + assert.False(t, cfg.Forbids("index.html")) +} diff --git a/service/http/handler.go b/service/http/handler.go new file mode 100644 index 00000000..1319200c --- /dev/null +++ b/service/http/handler.go @@ -0,0 +1,71 @@ +package http + +import ( + "net/http" + "strconv" + "github.com/sirupsen/logrus" + "github.com/spiral/roadrunner" + "github.com/pkg/errors" +) + +// 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). +type Handler struct { + cfg *Config + rr *roadrunner.Server +} + +// Handle serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled. +func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) { + // validating request size + if h.cfg.MaxRequest != 0 { + if length := r.Header.Get("content-length"); length != "" { + if size, err := strconv.ParseInt(length, 10, 64); err != nil { + h.sendError(w, r, err) + return + } else if size > h.cfg.MaxRequest { + h.sendError(w, r, errors.New("request body max size is exceeded")) + return + } + } + } + + req, err := NewRequest(r) + if err != nil { + h.sendError(w, r, err) + return + } + + if err = req.Open(h.cfg); err != nil { + h.sendError(w, r, err) + return + } + defer req.Close() + + p, err := req.Payload() + if err != nil { + h.sendError(w, r, err) + return + } + + rsp, err := h.rr.Exec(p) + if err != nil { + h.sendError(w, r, err) + return + } + + resp, err := NewResponse(rsp) + if err != nil { + h.sendError(w, r, err) + return + } + + resp.Write(w) +} + +// sendError sends error +func (h *Handler) sendError(w http.ResponseWriter, r *http.Request, err error) { + logrus.Errorf("http: %s", err) + w.WriteHeader(500) + w.Write([]byte(err.Error())) +} diff --git a/service/http/parse.go b/service/http/parse.go new file mode 100644 index 00000000..898f39a1 --- /dev/null +++ b/service/http/parse.go @@ -0,0 +1,147 @@ +package http + +import ( + "strings" + "net/http" + "os" +) + +const maxLevel = 127 + +type dataTree map[string]interface{} +type fileTree map[string]interface{} + +// parseData parses incoming request body into data tree. +func parseData(r *http.Request) (dataTree, error) { + data := make(dataTree) + for k, v := range r.PostForm { + data.push(k, v) + } + + for k, v := range r.MultipartForm.Value { + data.push(k, v) + } + + return data, nil +} + +// pushes value into data tree. +func (d dataTree) push(k string, v []string) { + if len(v) == 0 { + // skip empty values + return + } + + indexes := make([]string, 0) + for _, index := range strings.Split(k, "[") { + indexes = append(indexes, strings.Trim(index, "]")) + } + + if len(indexes) <= maxLevel { + d.mount(indexes, v) + } +} + +// mount mounts data tree recursively. +func (d dataTree) mount(i []string, v []string) { + if len(v) == 0 { + return + } + + if len(i) == 1 { + // single value context + d[i[0]] = v[0] + return + } + + if len(i) == 2 && i[1] == "" { + // non associated array of elements + d[i[0]] = v + return + } + + if p, ok := d[i[0]]; ok { + p.(dataTree).mount(i[1:], v) + } + + d[i[0]] = make(dataTree) + d[i[0]].(dataTree).mount(i[1:], v) +} + +// parse incoming dataTree request into JSON (including multipart form dataTree) +func parseUploads(r *http.Request, cfg *FsConfig) (*Uploads, error) { + u := &Uploads{ + cfg: cfg, + tree: make(fileTree), + list: make([]*FileUpload, 0), + } + + for k, v := range r.MultipartForm.File { + files := make([]*FileUpload, 0, len(v)) + for _, f := range v { + files = append(files, NewUpload(f)) + } + + u.list = append(u.list, files...) + u.tree.push(k, files) + } + + return u, nil +} + +// exists if file exists. +func exists(path string) bool { + _, err := os.Stat(path) + if err == nil { + return true + } + + if os.IsNotExist(err) { + return false + } + + return false +} + +// pushes new file upload into it's proper place. +func (d fileTree) push(k string, v []*FileUpload) { + if len(v) == 0 { + // skip empty values + return + } + + indexes := make([]string, 0) + for _, index := range strings.Split(k, "[") { + indexes = append(indexes, strings.Trim(index, "]")) + } + + if len(indexes) <= maxLevel { + d.mount(indexes, v) + } +} + +// mount mounts data tree recursively. +func (d fileTree) mount(i []string, v []*FileUpload) { + if len(v) == 0 { + return + } + + if len(i) == 1 { + // single value context + d[i[0]] = v[0] + return + } + + if len(i) == 2 && i[1] == "" { + // non associated array of elements + d[i[0]] = v + return + } + + if p, ok := d[i[0]]; ok { + p.(fileTree).mount(i[1:], v) + } + + d[i[0]] = make(fileTree) + d[i[0]].(fileTree).mount(i[1:], v) +} diff --git a/service/http/request.go b/service/http/request.go new file mode 100644 index 00000000..fd483744 --- /dev/null +++ b/service/http/request.go @@ -0,0 +1,137 @@ +package http + +import ( + "encoding/json" + "fmt" + "github.com/spiral/roadrunner" + "io/ioutil" + "net/http" + "strings" +) + +const ( + defaultMaxMemory = 32 << 20 // 32 MB +) + +// Request maps net/http requests to PSR7 compatible structure and managed state of temporary uploaded files. +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"` + + // request body can be parsedData or []byte + body interface{} +} + +// NewRequest creates new PSR7 compatible request using net/http request. +func NewRequest(r *http.Request) (req *Request, err error) { + req = &Request{ + Protocol: r.Proto, + Method: r.Method, + Uri: uri(r), + Headers: r.Header, + Cookies: make(map[string]string), + RawQuery: r.URL.RawQuery, + } + + for _, c := range r.Cookies() { + req.Cookies[c.Name] = c.Value + } + + if !req.parsable() { + req.body, err = ioutil.ReadAll(r.Body) + return req, err + } + + if err = r.ParseMultipartForm(defaultMaxMemory); err != nil { + return nil, err + } + + if req.body, err = parsePost(r); err != nil { + return nil, err + } + + if req.Uploads, err = parseUploads(r); err != nil { + return nil, err + } + + req.Parsed = true + return req, nil +} + +// Open moves all uploaded files to temporary directory so it can be given to php later. +func (r *Request) Open(cfg *Config) error { + if r.Uploads == nil { + return nil + } + + return r.Uploads.Open(cfg) +} + +// Close clears all temp file uploads +func (r *Request) Close() { + if r.Uploads == nil { + return + } + + r.Uploads.Clear() +} + +// Payload request marshaled RoadRunner payload based on PSR7 data. Default encode method is JSON. Make sure to open +// files prior to calling this method. +func (r *Request) Payload() (p *roadrunner.Payload, err error) { + p = &roadrunner.Payload{} + + if p.Context, err = json.Marshal(r); err != nil { + return nil, err + } + + if r.Parsed { + if p.Body, err = json.Marshal(r.body); err != nil { + return nil, err + } + } else if r.body != nil { + p.Body = r.body.([]byte) + } + + return p, nil +} + +// parsable returns true if request payload can be parsed (POST dataTree, file tree). +func (r *Request) parsable() bool { + if r.Method != "POST" && r.Method != "PUT" && r.Method != "PATCH" { + return false + } + + ct := r.Headers.Get("content-type") + return strings.Contains(ct, "multipart/form-data") || ct == "application/x-www-form-urlencoded" +} + +// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled). +func uri(r *http.Request) string { + if r.TLS != nil { + return fmt.Sprintf("https://%s%s", r.Host, r.URL.String()) + } + + return fmt.Sprintf("http://%s%s", r.Host, r.URL.String()) +} diff --git a/service/http/response.go b/service/http/response.go new file mode 100644 index 00000000..dd092353 --- /dev/null +++ b/service/http/response.go @@ -0,0 +1,54 @@ +package http + +import ( + "encoding/json" + "github.com/spiral/roadrunner" + "net/http" + "io" +) + +// Response handles PSR7 response logic. +type Response struct { + // Status contains response status. + Status int `json:"status"` + + // Headers contains list of response headers. + Headers map[string][]string `json:"headers"` + + // associated body payload. + body interface{} +} + +// NewResponse creates new response based on given roadrunner payload. +func NewResponse(p *roadrunner.Payload) (*Response, error) { + r := &Response{body: p.Body} + if err := json.Unmarshal(p.Context, r); err != nil { + return nil, err + } + + return r, nil +} + +// Write writes response headers, status and body into ResponseWriter. +func (r *Response) Write(w http.ResponseWriter) error { + for k, v := range r.Headers { + for _, h := range v { + w.Header().Add(k, h) + + } + } + + w.WriteHeader(r.Status) + + if data, ok := r.body.([]byte); ok { + w.Write(data) + } + + if rc, ok := r.body.(io.Reader); ok { + if _, err := io.Copy(w, rc); err != nil { + return err + } + } + + return nil +} diff --git a/service/http/rpc.go b/service/http/rpc.go new file mode 100644 index 00000000..673ff2bb --- /dev/null +++ b/service/http/rpc.go @@ -0,0 +1,78 @@ +package http + +import ( + "github.com/sirupsen/logrus" + "github.com/spiral/roadrunner/_____/utils" + "github.com/pkg/errors" +) + +type rpcServer struct { + service *Service +} + +// WorkerList contains list of workers. +type WorkerList struct { + // Workers is list of workers. + Workers []utils.Worker `json:"workers"` +} + +// Reset resets underlying RR worker pool and restarts all of it's workers. +func (rpc *rpcServer) Reset(reset bool, r *string) error { + if rpc.service.srv == nil { + return errors.New("no http server") + } + + logrus.Info("http: restarting worker pool") + *r = "OK" + + err := rpc.service.srv.rr.Reset() + if err != nil { + logrus.Errorf("http: %s", err) + } + + return err +} + +// Workers returns list of active workers and their stats. +func (rpc *rpcServer) Workers(list bool, r *WorkerList) error { + if rpc.service.srv == nil { + return errors.New("no http server") + } + + r.Workers = utils.FetchWorkers(rpc.service.srv.rr) + return nil +} + +// Worker provides information about specific worker. +type Worker struct { + // Pid contains process id. + Pid int `json:"pid"` + + // Status of the worker. + Status string `json:"status"` + + // Number of worker executions. + NumExecs uint64 `json:"numExecs"` + + // Created is unix nano timestamp of worker creation time. + Created int64 `json:"created"` + + // Updated is unix nano timestamp of last worker execution. + Updated int64 `json:"updated"` +} + +// FetchWorkers fetches list of workers from RR Server. +func FetchWorkers(srv *roadrunner.Server) (result []Worker) { + for _, w := range srv.Workers() { + state := w.State() + result = append(result, Worker{ + Pid: *w.Pid, + Status: state.String(), + NumExecs: state.NumExecs(), + Created: w.Created.UnixNano(), + Updated: state.Updated().UnixNano(), + }) + } + + return +}
\ No newline at end of file diff --git a/service/http/service.go b/service/http/service.go new file mode 100644 index 00000000..5a0d4c16 --- /dev/null +++ b/service/http/service.go @@ -0,0 +1,86 @@ +package http + +import ( + "net/http" + "github.com/spiral/roadrunner/service" + "context" + "github.com/spiral/roadrunner" +) + +// Name contains default service name. +const Name = "http" + +type Middleware interface { + // Handle must return true if request/response pair is handled withing the middleware. + Handle(w http.ResponseWriter, r *http.Request) bool +} + +// Service manages rr, http servers. +type Service struct { + middleware []Middleware + cfg *Config + rr *roadrunner.Server + handler *Handler + http *http.Server +} + +func (s *Service) Add(m Middleware) { + s.middleware = append(s.middleware, m) +} + +// Configure must return configure service and return true if service hasStatus enabled. Must return error in case of +// misconfiguration. Services must not be used without proper configuration pushed first. +func (s *Service) Configure(cfg service.Config, c service.Container) (bool, error) { + config := &Config{} + if err := cfg.Unmarshal(config); err != nil { + return false, err + } + + if !config.Enable { + return false, nil + } + + if err := config.Valid(); err != nil { + return false, err + } + + s.cfg = config + return true, nil +} + +// Serve serves the service. +func (s *Service) Serve() error { + rr := roadrunner.NewServer(s.cfg.Workers) + if err := rr.Start(); err != nil { + return err + } + defer s.rr.Stop() + + // todo: observer + + s.rr = rr + s.handler = &Handler{cfg: s.cfg, rr: s.rr} + s.http = &http.Server{Addr: s.cfg.httpAddr(), Handler: s} + + if err := s.http.ListenAndServe(); err != nil { + return err + } + + return nil +} + +// Stop stops the service. +func (s *Service) Stop() { + s.http.Shutdown(context.Background()) +} + +// Handle handles connection using set of middleware and rr PSR-7 server. +func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { + for _, m := range s.middleware { + if m.Handle(w, r) { + return + } + } + + s.handler.Handle(w, r) +} diff --git a/service/http/uploads.go b/service/http/uploads.go new file mode 100644 index 00000000..cdd3e52c --- /dev/null +++ b/service/http/uploads.go @@ -0,0 +1,130 @@ +package http + +import ( + "encoding/json" + "os" + "sync" + "mime/multipart" + "io/ioutil" + "io" +) + +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 +) + +// tree manages uploaded files tree and temporary files. +type Uploads struct { + // associated temp directory and forbidden extensions. + cfg *FsConfig + + // pre processed data tree for Uploads. + tree fileTree + + // flat list of all file Uploads. + list []*FileUpload +} + +// MarshalJSON marshal tree tree into JSON. +func (u *Uploads) MarshalJSON() ([]byte, error) { + return json.Marshal(u.tree) +} + +// Open moves all uploaded files to temp directory, return error in case of issue with temp directory. File errors +// will be handled individually. +func (u *Uploads) Open() error { + var wg sync.WaitGroup + for _, f := range u.list { + wg.Add(1) + go func(f *FileUpload) { + defer wg.Done() + f.Open(u.cfg) + }(f) + } + + wg.Wait() + return nil +} + +// Clear deletes all temporary files. +func (u *Uploads) Clear() { + for _, f := range u.list { + if f.TempFilename != "" && exists(f.TempFilename) { + os.Remove(f.TempFilename) + } + } +} + +// FileUpload represents singular file NewUpload. +type FileUpload struct { + // Name contains filename specified by the client. + Name string `json:"name"` + + // MimeType contains mime-type provided by the client. + MimeType string `json:"type"` + + // 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"` + + // associated file header + header *multipart.FileHeader +} + +// NewUpload wraps net/http upload into PRS-7 compatible structure. +func NewUpload(f *multipart.FileHeader) *FileUpload { + return &FileUpload{ + Name: f.Filename, + MimeType: f.Header.Get("Content-Type"), + Error: UploadErrorOK, + header: f, + } +} + +func (f *FileUpload) Open(cfg *FsConfig) error { + if cfg.Forbids(f.Name) { + f.Error = UploadErrorExtension + return nil + } + + file, err := f.header.Open() + if err != nil { + f.Error = UploadErrorNoFile + return err + } + defer file.Close() + + tmp, err := ioutil.TempFile(cfg.Dir, "upload") + if err != nil { + // most likely cause of this issue is missing tmp dir + f.Error = UploadErrorNoTmpDir + return err + } + + f.TempFilename = tmp.Name() + defer tmp.Close() + + if f.Size, err = io.Copy(tmp, file); err != nil { + f.Error = UploadErrorCantWrite + } + + return err +} |