66use PHPPM \Bootstraps \BootstrapInterface ;
77use PHPPM \Bootstraps \HooksInterface ;
88use PHPPM \React \HttpResponse ;
9+ use PHPPM \Utils ;
910use React \Http \Request as ReactRequest ;
1011use Symfony \Component \HttpFoundation \Cookie ;
1112use Symfony \Component \HttpFoundation \Request as SymfonyRequest ;
@@ -65,6 +66,8 @@ public function getStaticDirectory()
6566 *
6667 * @param ReactRequest $request
6768 * @param HttpResponse $response
69+ *
70+ * @throws \Exception
6871 */
6972 public function onRequest (ReactRequest $ request , HttpResponse $ response )
7073 {
@@ -111,18 +114,35 @@ public function onRequest(ReactRequest $request, HttpResponse $response)
111114 protected function mapRequest (ReactRequest $ reactRequest )
112115 {
113116 $ method = $ reactRequest ->getMethod ();
114- $ headers = array_change_key_case ( $ reactRequest ->getHeaders () );
117+ $ headers = $ reactRequest ->getHeaders ();
115118 $ query = $ reactRequest ->getQuery ();
116119
117- $ cookies = array ();
118- if (isset ($ headers ['Cookie ' ])) {
119- $ headersCookie = explode ('; ' , $ headers ['Cookie ' ]);
120+ $ cookies = [];
121+ $ _COOKIE = [];
122+
123+ $ sessionCookieSet = false ;
124+
125+ if (isset ($ headers ['Cookie ' ]) || isset ($ headers ['cookie ' ])) {
126+ $ headersCookie = explode ('; ' , isset ($ headers ['Cookie ' ]) ? $ headers ['Cookie ' ] : $ headers ['cookie ' ]);
120127 foreach ($ headersCookie as $ cookie ) {
121128 list ($ name , $ value ) = explode ('= ' , trim ($ cookie ));
122129 $ cookies [$ name ] = $ value ;
130+ $ _COOKIE [$ name ] = $ value ;
131+
132+ if ($ name === session_name ()) {
133+ session_id ($ value );
134+ $ sessionCookieSet = true ;
135+ }
123136 }
124137 }
125138
139+ if (!$ sessionCookieSet && session_id ()) {
140+ //session id already set from the last round but not got from the cookie header,
141+ //so generate a new one, since php is not doing it automatically with session_start() if session
142+ //has already been started.
143+ session_id (Utils::generateSessionId ());
144+ }
145+
126146 $ files = $ reactRequest ->getFiles ();
127147 $ post = $ reactRequest ->getPost ();
128148
@@ -144,17 +164,46 @@ protected function mapRequest(ReactRequest $reactRequest)
144164 */
145165 protected function mapResponse (HttpResponse $ reactResponse , SymfonyResponse $ syResponse )
146166 {
167+ //end active session
168+ if (PHP_SESSION_ACTIVE === session_status ()) {
169+ session_write_close ();
170+ session_unset (); //reset $_SESSION
171+ }
172+
147173 $ content = $ syResponse ->getContent ();
148174
149- $ headers = $ syResponse ->headers ->allPreserveCase ();
175+ $ nativeHeaders = [];
176+
177+ foreach (headers_list () as $ header ) {
178+ if (false !== $ pos = strpos ($ header , ': ' )) {
179+ $ name = substr ($ header , 0 , $ pos );
180+ $ value = trim (substr ($ header , $ pos + 1 ));
181+
182+ if (isset ($ nativeHeaders [$ name ])) {
183+ if (!is_array ($ nativeHeaders [$ name ])) {
184+ $ nativeHeaders [$ name ] = [$ nativeHeaders [$ name ]];
185+ }
186+
187+ $ nativeHeaders [$ name ][] = $ value ;
188+ } else {
189+ $ nativeHeaders [$ name ] = $ value ;
190+ }
191+ }
192+ }
193+
194+ //after reading all headers we need to reset it, so next request
195+ //operates on a clean header.
196+ header_remove ();
197+
198+ $ headers = array_merge ($ nativeHeaders , $ syResponse ->headers ->allPreserveCase ());
150199 $ cookies = [];
151200
152201 /** @var Cookie $cookie */
153202 foreach ($ syResponse ->headers ->getCookies () as $ cookie ) {
154203 $ cookieHeader = sprintf ('%s=%s ' , $ cookie ->getName (), $ cookie ->getValue ());
155204
156205 if ($ cookie ->getPath ()) {
157- $ cookieHeader .= '; Path= ' . $ cookie ->getPath ();
206+ $ cookieHeader .= '; Path= ' . urlencode ( $ cookie ->getPath () );
158207 }
159208 if ($ cookie ->getDomain ()) {
160209 $ cookieHeader .= '; Domain= ' . $ cookie ->getDomain ();
@@ -174,7 +223,11 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
174223 $ cookies [] = $ cookieHeader ;
175224 }
176225
177- $ headers ['Set-Cookie ' ] = $ cookies ;
226+ if (isset ($ headers ['Set-Cookie ' ])) {
227+ $ headers ['Set-Cookie ' ] = array_merge ((array )$ headers ['Set-Cookie ' ], $ cookies );
228+ } else {
229+ $ headers ['Set-Cookie ' ] = $ cookies ;
230+ }
178231
179232 $ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
180233
@@ -184,7 +237,6 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
184237 }
185238
186239 $ reactResponse ->end ($ stdOut . $ content );
187-
188240 }
189241
190242 /**
0 commit comments