summaryrefslogtreecommitdiff
path: root/plugins/http
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-11-27 16:08:27 +0300
committerValery Piashchynski <[email protected]>2020-11-27 16:08:27 +0300
commit6abfe73b5f7b66a7be094486e29ab3f2ab1ce940 (patch)
tree888b5eb96ab396ef53f77ce3944ed62af9cc878d /plugins/http
parentb5020bfce6b5362400cb9b578fe32c1a6ed5d61a (diff)
Gzip plugin
Diffstat (limited to 'plugins/http')
-rw-r--r--plugins/http/config.go20
-rw-r--r--plugins/http/plugin.go30
-rw-r--r--plugins/http/tests/configs/.rr-echoErr.yaml2
-rw-r--r--plugins/http/tests/configs/.rr-http.yaml2
-rw-r--r--plugins/http/tests/psr-worker.php23
5 files changed, 24 insertions, 53 deletions
diff --git a/plugins/http/config.go b/plugins/http/config.go
index 3f1fa69e..ae18e4ce 100644
--- a/plugins/http/config.go
+++ b/plugins/http/config.go
@@ -32,23 +32,6 @@ func (c *Cidrs) IsTrusted(ip string) bool {
return false
}
-type ServerConfig struct {
- // Command includes command strings with all the parameters, example: "php worker.php pipes".
- Command string
-
- // User under which process will be started
- User string
-
- // Relay defines connection method and factory to be used to connect to workers:
- // "pipes", "tcp://:6001", "unix://pool.sock"
- // This config section must not change on re-configuration.
- Relay string
-
- // RelayTimeout defines for how long socket factory will be waiting for worker connection. This config section
- // must not change on re-configuration.
- RelayTimeout time.Duration
-}
-
// Config configures RoadRunner HTTP server.
type Config struct {
// Port and port to handle as http server.
@@ -78,6 +61,9 @@ type Config struct {
// Env is environment variables passed to the http pool
Env map[string]string
+ Middleware []string
+
+ // slice of net.IPNet
cidrs Cidrs
}
diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go
index 7ce3a70d..19765afc 100644
--- a/plugins/http/plugin.go
+++ b/plugins/http/plugin.go
@@ -13,6 +13,7 @@ import (
"sync"
"github.com/hashicorp/go-multierror"
+ "github.com/spiral/endure"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2"
"github.com/spiral/roadrunner/v2/interfaces/log"
@@ -38,6 +39,8 @@ type Middleware interface {
Middleware(f http.Handler) http.HandlerFunc
}
+type middleware map[string]Middleware
+
// Service manages pool, http servers.
type Plugin struct {
sync.Mutex
@@ -48,7 +51,7 @@ type Plugin struct {
cfg *Config
// middlewares to chain
- mdwr []Middleware
+ mdwr middleware
// Event listener to stdout
listener util.EventListener
@@ -87,6 +90,7 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, server factory.Serv
s.configurer = cfg
s.log = log
+ s.mdwr = make(map[string]Middleware)
if !s.cfg.EnableHTTP() && !s.cfg.EnableTLS() && !s.cfg.EnableFCGI() {
return errors.E(op, errors.Disabled)
@@ -333,8 +337,8 @@ func (s *Plugin) Collects() []interface{} {
}
}
-func (s *Plugin) AddMiddleware(m Middleware) {
- s.mdwr = append(s.mdwr, m)
+func (s *Plugin) AddMiddleware(name endure.Named, m Middleware) {
+ s.mdwr[name.Name()] = m
}
func (s *Plugin) redirect(w http.ResponseWriter, r *http.Request) bool {
@@ -498,19 +502,23 @@ func (s *Plugin) tlsAddr(host string, forcePort bool) string {
func (s *Plugin) addMiddlewares() {
if s.http != nil {
- for i := 0; i < len(s.mdwr); i++ {
- s.http.Handler = s.mdwr[i].Middleware(s.http.Handler)
- }
+ applyMiddlewares(s.http, s.mdwr, s.cfg.Middleware, s.log)
}
if s.https != nil {
- for i := 0; i < len(s.mdwr); i++ {
- s.https.Handler = s.mdwr[i].Middleware(s.https.Handler)
- }
+ applyMiddlewares(s.https, s.mdwr, s.cfg.Middleware, s.log)
}
if s.fcgi != nil {
- for i := 0; i < len(s.mdwr); i++ {
- s.fcgi.Handler = s.mdwr[i].Middleware(s.fcgi.Handler)
+ applyMiddlewares(s.fcgi, s.mdwr, s.cfg.Middleware, s.log)
+ }
+}
+
+func applyMiddlewares(server *http.Server, middlewares map[string]Middleware, order []string, log log.Logger) {
+ for i := 0; i < len(order); i++ {
+ if mdwr, ok := middlewares[order[i]]; ok {
+ server.Handler = mdwr.Middleware(server.Handler)
+ } else {
+ log.Warn("requested middleware does not exist", "requested", order[i])
}
}
}
diff --git a/plugins/http/tests/configs/.rr-echoErr.yaml b/plugins/http/tests/configs/.rr-echoErr.yaml
index 696fc0ae..6ecdbb2a 100644
--- a/plugins/http/tests/configs/.rr-echoErr.yaml
+++ b/plugins/http/tests/configs/.rr-echoErr.yaml
@@ -15,7 +15,7 @@ http:
debug: true
address: 127.0.0.1:8080
maxRequestSize: 1024
- middleware: [ "" ]
+ middleware: [ "pluginMiddleware", "pluginMiddleware2" ]
uploads:
forbid: [ "" ]
trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
diff --git a/plugins/http/tests/configs/.rr-http.yaml b/plugins/http/tests/configs/.rr-http.yaml
index c907c5e7..7b91f735 100644
--- a/plugins/http/tests/configs/.rr-http.yaml
+++ b/plugins/http/tests/configs/.rr-http.yaml
@@ -15,7 +15,7 @@ http:
debug: true
address: 127.0.0.1:18903
maxRequestSize: 1024
- middleware: [ "" ]
+ middleware: [ "pluginMiddleware", "pluginMiddleware2" ]
uploads:
forbid: [ ".php", ".exe", ".bat" ]
trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
diff --git a/plugins/http/tests/psr-worker.php b/plugins/http/tests/psr-worker.php
deleted file mode 100644
index 65fc6bde..00000000
--- a/plugins/http/tests/psr-worker.php
+++ /dev/null
@@ -1,23 +0,0 @@
-<?php
-/**
- * @var Goridge\RelayInterface $relay
- */
-use Spiral\Goridge;
-use Spiral\RoadRunner;
-
-ini_set('display_errors', 'stderr');
-require dirname(__DIR__) . "/../../vendor_php/autoload.php";
-
-$worker = new RoadRunner\Worker(new Goridge\StreamRelay(STDIN, STDOUT));
-$psr7 = new RoadRunner\PSR7Client($worker);
-
-while ($req = $psr7->acceptRequest()) {
- try {
- $resp = new \Zend\Diactoros\Response();
- $resp->getBody()->write("hello world");
-
- $psr7->respond($resp);
- } catch (\Throwable $e) {
- $psr7->getWorker()->error((string)$e);
- }
-} \ No newline at end of file