summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-07-07 19:23:15 -0700
committerWolfy-J <[email protected]>2018-07-07 19:23:15 -0700
commit98f7ce699b559afd74d4e9d607e64b9a8367d762 (patch)
treea1553b7ed55109a1158f92dbf0eae5520754c5f7
parentafde365ba6210569b7d48dec0c07434a7f8a1fd8 (diff)
attributes support
-rw-r--r--php-src/PSR7Client.php11
-rw-r--r--service/http/attributes.go63
-rw-r--r--service/http/request.go16
-rw-r--r--service/http/service.go1
-rw-r--r--service/rpc/service_test.go2
5 files changed, 85 insertions, 8 deletions
diff --git a/php-src/PSR7Client.php b/php-src/PSR7Client.php
index f8913a8d..d1d8ee8c 100644
--- a/php-src/PSR7Client.php
+++ b/php-src/PSR7Client.php
@@ -9,6 +9,7 @@ namespace Spiral\RoadRunner;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
+use Spiral\RoadRunner\Worker;
use Zend\Diactoros;
/**
@@ -64,7 +65,7 @@ class PSR7Client
$bodyStream->write($body);
}
- return new Diactoros\ServerRequest(
+ $request = new Diactoros\ServerRequest(
$_SERVER,
$this->wrapUploads($ctx['uploads']),
$ctx['uri'],
@@ -76,6 +77,14 @@ class PSR7Client
$parsedBody,
$ctx['protocol']
);
+
+ if (!empty($ctx['attributes'])) {
+ foreach ($ctx['attributes'] as $key => $value) {
+ $request = $request->withAttribute($key, $value);
+ }
+ }
+
+ return $request;
}
/**
diff --git a/service/http/attributes.go b/service/http/attributes.go
new file mode 100644
index 00000000..fe069adf
--- /dev/null
+++ b/service/http/attributes.go
@@ -0,0 +1,63 @@
+package http
+
+import (
+ "context"
+ "net/http"
+)
+
+const contextKey = "psr:attributes"
+
+type attrs map[string]interface{}
+
+// InitAttributes returns request with new context and attribute bag.
+func InitAttributes(r *http.Request) *http.Request {
+ return r.WithContext(context.WithValue(r.Context(), contextKey, attrs{}))
+}
+
+// AllAttributes returns all context attributes.
+func AllAttributes(r *http.Request) map[string]interface{} {
+ v := r.Context().Value(contextKey)
+ if v == nil {
+ return nil
+ }
+
+ return v.(attrs)
+}
+
+// Get gets the value from request context. It replaces any existing
+// values.
+func GetAttribute(r *http.Request, key string) interface{} {
+ v := r.Context().Value(contextKey)
+ if v == nil {
+ return ""
+ }
+
+ return v.(attrs).Get(key)
+}
+
+// Set sets the key to value. It replaces any existing
+// values. Context specific.
+func SetAttribute(r *http.Request, key string, value interface{}) {
+ v := r.Context().Value(contextKey)
+ v.(attrs).Set(key, value)
+}
+
+// Get gets the value associated with the given key.
+func (v attrs) Get(key string) interface{} {
+ if v == nil {
+ return ""
+ }
+
+ return v[key]
+}
+
+// Set sets the key to value. It replaces any existing
+// values.
+func (v attrs) Set(key string, value interface{}) {
+ v[key] = value
+}
+
+// Del deletes the value associated with key.
+func (v attrs) Del(key string) {
+ delete(v, key)
+} \ No newline at end of file
diff --git a/service/http/request.go b/service/http/request.go
index 9281a3f5..21566416 100644
--- a/service/http/request.go
+++ b/service/http/request.go
@@ -44,6 +44,9 @@ type Request struct {
// Uploads contains list of uploaded files, their names, sized and associations with temporary files.
Uploads *Uploads `json:"uploads"`
+ // Attributes can be set by chained middleware to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions.
+ Attributes map[string]interface{} `json:"attributes"`
+
// request body can be parsedData or []byte
body interface{}
}
@@ -51,12 +54,13 @@ type Request struct {
// NewRequest creates new PSR7 compatible request using net/http request.
func NewRequest(r *http.Request, cfg *UploadsConfig) (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,
+ Protocol: r.Proto,
+ Method: r.Method,
+ URI: uri(r),
+ Headers: r.Header,
+ Cookies: make(map[string]string),
+ RawQuery: r.URL.RawQuery,
+ Attributes: AllAttributes(r),
}
for _, c := range r.Cookies() {
diff --git a/service/http/service.go b/service/http/service.go
index cef019b3..460175b9 100644
--- a/service/http/service.go
+++ b/service/http/service.go
@@ -113,6 +113,7 @@ func (s *Service) Stop() {
// middleware handles connection using set of mdws and rr PSR-7 server.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ r = InitAttributes(r)
for _, m := range s.mdws {
if m(w, r) {
return
diff --git a/service/rpc/service_test.go b/service/rpc/service_test.go
index ce85d52f..d4734bb5 100644
--- a/service/rpc/service_test.go
+++ b/service/rpc/service_test.go
@@ -84,8 +84,8 @@ func Test_Serve_Client(t *testing.T) {
assert.NoError(t, s.Register("test", &testService{}))
go func() { assert.NoError(t, s.Serve()) }()
-
time.Sleep(time.Millisecond)
+
client, err := s.Client()
assert.NotNil(t, client)
assert.NoError(t, err)