diff options
Diffstat (limited to '_____')
-rw-r--r-- | _____/http/config.go | 88 | ||||
-rw-r--r-- | _____/http/request.go | 137 | ||||
-rw-r--r-- | _____/http/rpc.go | 78 | ||||
-rw-r--r-- | _____/http/service.go | 80 |
4 files changed, 0 insertions, 383 deletions
diff --git a/_____/http/config.go b/_____/http/config.go deleted file mode 100644 index bd8cec5e..00000000 --- a/_____/http/config.go +++ /dev/null @@ -1,88 +0,0 @@ -package http - -import ( - "fmt" - "github.com/spiral/roadrunner/service" - "github.com/spiral/roadrunner/_____/utils" - "os" - "path" - "strings" - "github.com/spiral/roadrunner/http" -) - -// Configures RoadRunner HTTP server. -type Config struct { - // serve enables static file serving from desired root directory. - ServeStatic bool - - Static *http.FsConfig - - // Root directory, required when serve set to true. - Root string - - // Dir contains name of temporary directory to store uploaded files passed to underlying PHP process. - TmpDir string - - // MaxRequest specified max size for payload body in bytes, set 0 to unlimited. - MaxRequest int64 - - // Forbid specifies list of file extensions which are forbidden for uploads. - // Example: .php, .exe, .bat, .htaccess and etc. - ForbidUploads []string -} - -// Forbid must return true if file extension is not allowed for the upload. -func (cfg Config) Forbidden(filename string) bool { - ext := strings.ToLower(path.Ext(filename)) - - for _, v := range cfg.ForbidUploads { - if ext == v { - return true - } - } - - return false -} - -type serviceConfig struct { - Enabled bool - Host string - Port string - MaxRequest string - Static struct { - Serve bool - Root string - } - - Uploads struct { - TmpDir string - Forbid []string - } - - Pool service.PoolConfig - - //todo: verbose ? -} - -func (cfg *serviceConfig) httpAddr() string { - return fmt.Sprintf("%s:%v", cfg.Host, cfg.Port) -} - -func (cfg *serviceConfig) httpConfig() *Config { - tmpDir := cfg.Uploads.TmpDir - if tmpDir == "" { - tmpDir = os.TempDir() - } - - return &Config{ - ServeStatic: cfg.Static.Serve, - Root: cfg.Static.Root, - TmpDir: tmpDir, - MaxRequest: utils.ParseSize(cfg.MaxRequest), - ForbidUploads: cfg.Uploads.Forbid, - } -} - -func (cfg *serviceConfig) Valid() error { - return nil -} diff --git a/_____/http/request.go b/_____/http/request.go deleted file mode 100644 index fd483744..00000000 --- a/_____/http/request.go +++ /dev/null @@ -1,137 +0,0 @@ -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/_____/http/rpc.go b/_____/http/rpc.go deleted file mode 100644 index 673ff2bb..00000000 --- a/_____/http/rpc.go +++ /dev/null @@ -1,78 +0,0 @@ -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/_____/http/service.go b/_____/http/service.go deleted file mode 100644 index 008aeab8..00000000 --- a/_____/http/service.go +++ /dev/null @@ -1,80 +0,0 @@ -package http - -import ( - "context" - "github.com/sirupsen/logrus" - "github.com/spiral/roadrunner/service" - "net/http" - "github.com/spiral/roadrunner" -) - -const ServiceName = "http" - -type Service struct { - cfg *serviceConfig - http *http.Server - srv *Server -} - -func (s *Service) Name() string { - return ServiceName -} - -func (s *Service) Configure(cfg service.Config) (bool, error) { - config := &serviceConfig{} - if err := cfg.Unmarshal(config); err != nil { - return false, err - } - - if !config.Enabled { - return false, nil - } - - if err := config.Valid(); err != nil { - return false, err - } - - s.cfg = config - return true, nil -} - -func (s *Service) RPC() interface{} { - return &rpcServer{s} -} - -func (s *Service) Serve() error { - logrus.Debugf("http: started") - defer logrus.Debugf("http: stopped") - - rr, term, err := s.cfg.Pool.NewServer() - if err != nil { - return err - } - defer term() - - //todo: remove - rr.Observe(func(event int, ctx interface{}) { - switch event { - case roadrunner.EventPoolError: - logrus.Error(ctx) - case roadrunner.EventWorkerError: - logrus.Errorf("%s: %s", ctx.(roadrunner.WorkerError).Worker, ctx.(roadrunner.WorkerError).Error()) - } - }) - - s.srv = NewServer(s.cfg.httpConfig(), rr) - s.http = &http.Server{ - Addr: s.cfg.httpAddr(), - Handler: s.srv, - } - - if err := s.http.ListenAndServe(); err != nil { - return err - } - - return nil -} - -func (s *Service) Stop() error { - return s.http.Shutdown(context.Background()) -} |