diff options
Diffstat (limited to '_____')
-rw-r--r-- | _____/http/config.go | 11 | ||||
-rw-r--r-- | _____/http/rpc.go | 34 | ||||
-rw-r--r-- | _____/http/static.go | 68 | ||||
-rw-r--r-- | _____/http/uploads.go | 206 | ||||
-rw-r--r-- | _____/utils/size.go | 28 | ||||
-rw-r--r-- | _____/utils/workers.go | 37 |
6 files changed, 41 insertions, 343 deletions
diff --git a/_____/http/config.go b/_____/http/config.go index 4ea414c1..bd8cec5e 100644 --- a/_____/http/config.go +++ b/_____/http/config.go @@ -7,6 +7,7 @@ import ( "os" "path" "strings" + "github.com/spiral/roadrunner/http" ) // Configures RoadRunner HTTP server. @@ -14,21 +15,23 @@ 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 - // TmpDir contains name of temporary directory to store uploaded files passed to underlying PHP process. + // 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 - // ForbidUploads specifies list of file extensions which are forbidden for uploads. + // Forbid specifies list of file extensions which are forbidden for uploads. // Example: .php, .exe, .bat, .htaccess and etc. ForbidUploads []string } -// ForbidUploads must return true if file extension is not allowed for the upload. +// 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)) @@ -46,7 +49,7 @@ type serviceConfig struct { Host string Port string MaxRequest string - Static struct { + Static struct { Serve bool Root string } diff --git a/_____/http/rpc.go b/_____/http/rpc.go index e54eae7c..673ff2bb 100644 --- a/_____/http/rpc.go +++ b/_____/http/rpc.go @@ -42,3 +42,37 @@ func (rpc *rpcServer) Workers(list bool, r *WorkerList) error { 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/static.go b/_____/http/static.go deleted file mode 100644 index b055099f..00000000 --- a/_____/http/static.go +++ /dev/null @@ -1,68 +0,0 @@ -package http - -import ( - "github.com/sirupsen/logrus" - "net/http" - "os" - "path" - "path/filepath" - "strings" -) - -var forbiddenFiles = []string{".php", ".htaccess"} - -// staticServer serves static files -type staticServer struct { - root http.Dir -} - -// serve attempts to serve static file and returns true in case of success, will return false in case if file not -// found, not allowed or on read error. -func (svr *staticServer) serve(w http.ResponseWriter, r *http.Request) bool { - fpath := r.URL.Path - if !strings.HasPrefix(fpath, "/") { - fpath = "/" + fpath - } - fpath = path.Clean(fpath) - - if svr.forbidden(fpath) { - logrus.Warningf("attempt to access forbidden file %s", fpath) // todo: better logs - return false - } - - f, err := svr.root.Open(fpath) - if err != nil { - if !os.IsNotExist(err) { - logrus.Error(err) //todo: rr or access error - } - - return false - } - defer f.Close() - - d, err := f.Stat() - if err != nil { - logrus.Error(err) //todo: rr or access error - return false - } - - if d.IsDir() { - // do not serve directories - return false - } - - http.ServeContent(w, r, d.Name(), d.ModTime(), f) - return true -} - -// forbidden returns true if file has forbidden extension. -func (svr *staticServer) forbidden(path string) bool { - ext := strings.ToLower(filepath.Ext(path)) - for _, exl := range forbiddenFiles { - if ext == exl { - return true - } - } - - return false -} diff --git a/_____/http/uploads.go b/_____/http/uploads.go deleted file mode 100644 index c3b18169..00000000 --- a/_____/http/uploads.go +++ /dev/null @@ -1,206 +0,0 @@ -package http - -import ( - "encoding/json" - "io" - "io/ioutil" - "mime/multipart" - "net/http" - "os" - "strings" - "sync" -) - -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 - - // ForbidUploads file extension. - UploadErrorExtension = 7 -) - -// FileUpload represents singular file wrapUpload. -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 -} - -func (f *FileUpload) Open(cfg *Config) error { - if cfg.Forbidden(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.TmpDir, "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 -} - -type fileTree map[string]interface{} - -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) -} - -// tree manages uploaded files tree and temporary files. -type Uploads struct { - // 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. @todo: do we need it? -func (u *Uploads) Open(cfg *Config) error { - var wg sync.WaitGroup - for _, f := range u.list { - wg.Add(1) - go func(f *FileUpload) { - defer wg.Done() - f.Open(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) - } - } -} - -// parse incoming dataTree request into JSON (including multipart form dataTree) -func parseUploads(r *http.Request) (*Uploads, error) { - u := &Uploads{ - 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, wrapUpload(f)) - } - - u.list = append(u.list, files...) - u.tree.push(k, files) - } - - return u, nil -} - -func wrapUpload(f *multipart.FileHeader) *FileUpload { - return &FileUpload{ - Name: f.Filename, - MimeType: f.Header.Get("Content-Type"), - Error: UploadErrorOK, - header: f, - } -} - -// 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 -} diff --git a/_____/utils/size.go b/_____/utils/size.go deleted file mode 100644 index 176cc9e1..00000000 --- a/_____/utils/size.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "strconv" - "strings" -) - -func ParseSize(size string) int64 { - if len(size) == 0 { - return 0 - } - - s, err := strconv.Atoi(size[:len(size)-1]) - if err != nil { - return 0 - } - - switch strings.ToLower(size[len(size)-1:]) { - case "k", "kb": - return int64(s * 1024) - case "m", "mb": - return int64(s * 1024 * 1024) - case "g", "gb": - return int64(s * 1024 * 1024 * 1024) - } - - return 0 -} diff --git a/_____/utils/workers.go b/_____/utils/workers.go deleted file mode 100644 index 1024b4c6..00000000 --- a/_____/utils/workers.go +++ /dev/null @@ -1,37 +0,0 @@ -package utils - -import "github.com/spiral/roadrunner" - -// 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 |