2929class DrupalKernel extends SymfonyBridge implements BridgeInterface {
3030
3131 /**
32- * Handle a request using a HttpKernelInterface implementing application.
33- *
34- * @param \React\Http\Request $request
35- * @param \React\Http\Response $response
32+ * {@inheritdoc}
3633 */
37- public function onRequest (ReactRequest $ request , ReactResponse $ response )
38- {
39- if (null === $ this ->application ) {
40- return ;
41- }
34+ public function onRequest (ReactRequest $ request , ReactResponse $ response ) {
35+
36+ if (NULL === $ this ->application ) {
37+ return ;
38+ }
39+
40+ $ content = '' ;
41+ $ headers = $ request ->getHeaders ();
42+ $ contentLength = isset ($ headers ['Content-Length ' ]) ? (int ) $ headers ['Content-Length ' ] : 0 ;
43+
44+ $ request ->on ('data ' , function ($ data )
45+ use ($ request , $ response , &$ content , $ contentLength ) {
46+
47+ // Read data (may be empty for GET request).
48+ $ content .= $ data ;
49+
50+ // Handle request after receive.
51+ if (strlen ($ content ) >= $ contentLength ) {
52+ $ syRequest = self ::mapRequest ($ request , $ content );
4253
43- $ content = '' ;
44- $ headers = $ request ->getHeaders ();
45- $ contentLength = isset ($ headers ['Content-Length ' ]) ? (int ) $ headers ['Content-Length ' ] : 0 ;
46-
47- $ request ->on ('data ' , function ($ data )
48- use ($ request , $ response , &$ content , $ contentLength )
49- {
50- // read data (may be empty for GET request)
51- $ content .= $ data ;
52-
53- // handle request after receive
54- if (strlen ($ content ) >= $ contentLength ) {
55- $ syRequest = self ::mapRequest ($ request , $ content );
56-
57- try {
58- $ syResponse = $ this ->application ->handle ($ syRequest );
59- } catch (\Exception $ exception ) {
60- $ response ->writeHead (500 ); // internal server error
61- $ response ->end ();
62- return ;
63- }
64-
65- self ::mapResponse ($ response , $ syResponse );
66-
67- if ($ this ->application instanceof TerminableInterface) {
68- $ this ->application ->terminate ($ syRequest , $ syResponse );
69- }
70- }
71- });
54+ try {
55+ $ syResponse = $ this ->application ->handle ($ syRequest );
56+ }
57+ catch (\Exception $ exception ) {
58+ // Internal server error.
59+ $ response ->writeHead (500 );
60+ $ response ->end ();
61+ return ;
62+ }
63+
64+ self ::mapResponse ($ response , $ syResponse );
65+
66+ if ($ this ->application instanceof TerminableInterface) {
67+ $ this ->application ->terminate ($ syRequest , $ syResponse );
68+ }
69+ }
70+ });
7271 }
7372
7473 /**
75- * Convert React\Http\Request to Symfony\Component\HttpFoundation\Request
76- *
77- * @param ReactRequest $reactRequest
78- * @return SymfonyRequest $syRequest
74+ * {@inheritdoc}
7975 */
80- protected static function mapRequest (ReactRequest $ reactRequest , $ content )
81- {
82- $ method = $ reactRequest ->getMethod ();
83- $ headers = $ reactRequest ->getHeaders ();
84- $ query = $ reactRequest ->getQuery ();
85- $ post = array ();
86-
87- // parse body?
88- if (isset ($ headers ['Content-Type ' ]) && (0 === strpos ($ headers ['Content-Type ' ], 'application/x-www-form-urlencoded ' ))
89- && in_array (strtoupper ($ method ), array ('POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' ))
90- ) {
91- parse_str ($ content , $ post );
92- }
76+ protected static function mapRequest (ReactRequest $ reactRequest , $ content ) {
9377
94- $ cookies = array ();
95- if (isset ($ headers ['Cookie ' ])) {
96- $ headersCookie = explode ('; ' , $ headers ['Cookie ' ]);
97- foreach ($ headersCookie as $ cookie ) {
98- list ($ name , $ value ) = explode ('= ' , trim ($ cookie ));
99- $ cookies [$ name ] = $ value ;
100- }
78+ $ method = strtoupper ($ reactRequest ->getMethod ());
79+ $ headers = $ reactRequest ->getHeaders ();
80+ $ query = $ reactRequest ->getQuery ();
81+ $ post = array ();
82+
83+ $ requestIsPostType = in_array ($ method , array ('POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' ));
84+
85+ // Parse body?
86+ if (isset ($ headers ['Content-Type ' ]) && (0 === strpos ($ headers ['Content-Type ' ], 'application/x-www-form-urlencoded ' ))
87+ && $ requestIsPostType
88+ ) {
89+ parse_str ($ content , $ post );
90+ }
91+
92+ $ cookies = array ();
93+ if (isset ($ headers ['Cookie ' ])) {
94+ $ headersCookie = explode ('; ' , $ headers ['Cookie ' ]);
95+ foreach ($ headersCookie as $ cookie ) {
96+ list ($ name , $ value ) = explode ('= ' , trim ($ cookie ));
97+ $ cookies [$ name ] = $ value ;
10198 }
99+ }
102100
103- $ parameters =
104- in_array (strtoupper ($ method ), array ('POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' ))
105- ? $ post
106- : $ query ;
107- $ syRequest = SymfonyRequest::create (
108- // $uri, $method, $parameters, $cookies, $files, $server, $content
101+ $ parameters = $ requestIsPostType ? $ post : $ query ;
102+ $ syRequest = SymfonyRequest::create (
103+ // $uri, $method, $parameters, $cookies, $files, $server, $content.
109104 $ reactRequest ->getPath (),
110105 $ method ,
111106 $ parameters ,
@@ -114,18 +109,18 @@ protected static function mapRequest(ReactRequest $reactRequest, $content)
114109 array (),
115110 $ content
116111 );
117- $ syRequest ->headers ->replace ($ headers );
118-
119- // Set CGI/1.1 (RFC 3875) server vars.
120- if (empty ($ _ENV )) {
121- // In some cases with cli, $_ENV isn't set, so get with getenv().
122- // @todo: Make this more efficient to eliminate running per request.
123- // Static variable?
124- $ _ENV ['DOCUMENT_ROOT ' ] = getenv ('DOCUMENT_ROOT ' );
125- $ _ENV ['SCRIPT_NAME ' ] = getenv ('SCRIPT_NAME ' );
126- }
127- $ serverVars = array_merge (
128- $ syRequest ->server ->all (),
112+ $ syRequest ->headers ->replace ($ headers );
113+
114+ // Set CGI/1.1 (RFC 3875) server vars.
115+ if (empty ($ _ENV )) {
116+ // In some cases with cli, $_ENV isn't set, so get with getenv().
117+ // @todo: Make this more efficient to eliminate running per request.
118+ // Static variable?
119+ $ _ENV ['DOCUMENT_ROOT ' ] = getenv ('DOCUMENT_ROOT ' );
120+ $ _ENV ['SCRIPT_NAME ' ] = getenv ('SCRIPT_NAME ' );
121+ }
122+ $ serverVars = array_merge (
123+ $ syRequest ->server ->all (),
129124 array (
130125 'DOCUMENT_ROOT ' => $ _ENV ['DOCUMENT_ROOT ' ],
131126 'GATEWAY_INTERFACE ' => 'CGI/1.1 ' ,
@@ -135,36 +130,33 @@ protected static function mapRequest(ReactRequest $reactRequest, $content)
135130 'SCRIPT_FILENAME ' => $ _ENV ['DOCUMENT_ROOT ' ] . $ _ENV ['SCRIPT_NAME ' ],
136131 )
137132 );
138- $ syRequest ->server ->replace ($ serverVars );
133+ $ syRequest ->server ->replace ($ serverVars );
139134
140- return $ syRequest ;
135+ return $ syRequest ;
141136 }
142137
143138
144139 /**
145- * Convert Symfony\Component\HttpFoundation\Response to React\Http\Response
146- *
147- * @param ReactResponse $reactResponse
148- * @param SymfonyResponse $syResponse
140+ * {@inheritdoc}
149141 */
150142 protected static function mapResponse (ReactResponse $ reactResponse ,
151- SymfonyResponse $ syResponse )
152- {
153- $ headers = $ syResponse ->headers ->all ();
154- $ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
155-
156- // @TODO convert StreamedResponse in an async manner
157- if ($ syResponse instanceof SymfonyStreamedResponse) {
158- ob_start ();
159- $ syResponse ->sendContent ();
160- $ content = ob_get_contents ();
161- ob_end_clean ();
162- }
163- else {
164- $ content = $ syResponse ->getContent ();
165- }
166-
167- $ reactResponse ->end ($ content );
143+ SymfonyResponse $ syResponse ) {
144+
145+ $ headers = $ syResponse ->headers ->all ();
146+ $ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
147+
148+ // @TODO convert StreamedResponse in an async manner
149+ if ($ syResponse instanceof SymfonyStreamedResponse) {
150+ ob_start ();
151+ $ syResponse ->sendContent ();
152+ $ content = ob_get_contents ();
153+ ob_end_clean ();
154+ }
155+ else {
156+ $ content = $ syResponse ->getContent ();
157+ }
158+
159+ $ reactResponse ->end ($ content );
168160 }
169161
170162}
0 commit comments