summaryrefslogtreecommitdiff
path: root/http/config.go
blob: fe5fab36e991a58ab8774458abadf610c6de466f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package http

import (
	"strings"
	"path"
	"github.com/spiral/roadrunner/service"
	"os"
	"github.com/spiral/roadrunner/utils"
	"fmt"
)

// Configures RoadRunner HTTP server.
type Config struct {
	// serve enables static file serving from desired root directory.
	ServeStatic bool

	// 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.
	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.
	// Example: .php, .exe, .bat, .htaccess and etc.
	ForbidUploads []string
}

// ForbidUploads 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
}