6
6
use PHPPM \Bootstraps \BootstrapInterface ;
7
7
use PHPPM \Bootstraps \HooksInterface ;
8
8
use PHPPM \React \HttpResponse ;
9
+ use PHPPM \Utils ;
9
10
use React \Http \Request as ReactRequest ;
10
11
use Symfony \Component \HttpFoundation \Cookie ;
11
12
use Symfony \Component \HttpFoundation \Request as SymfonyRequest ;
@@ -65,6 +66,8 @@ public function getStaticDirectory()
65
66
*
66
67
* @param ReactRequest $request
67
68
* @param HttpResponse $response
69
+ *
70
+ * @throws \Exception
68
71
*/
69
72
public function onRequest (ReactRequest $ request , HttpResponse $ response )
70
73
{
@@ -111,18 +114,35 @@ public function onRequest(ReactRequest $request, HttpResponse $response)
111
114
protected function mapRequest (ReactRequest $ reactRequest )
112
115
{
113
116
$ method = $ reactRequest ->getMethod ();
114
- $ headers = array_change_key_case ( $ reactRequest ->getHeaders () );
117
+ $ headers = $ reactRequest ->getHeaders ();
115
118
$ query = $ reactRequest ->getQuery ();
116
119
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 ' ]);
120
127
foreach ($ headersCookie as $ cookie ) {
121
128
list ($ name , $ value ) = explode ('= ' , trim ($ cookie ));
122
129
$ cookies [$ name ] = $ value ;
130
+ $ _COOKIE [$ name ] = $ value ;
131
+
132
+ if ($ name === session_name ()) {
133
+ session_id ($ value );
134
+ $ sessionCookieSet = true ;
135
+ }
123
136
}
124
137
}
125
138
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
+
126
146
$ files = $ reactRequest ->getFiles ();
127
147
$ post = $ reactRequest ->getPost ();
128
148
@@ -144,17 +164,46 @@ protected function mapRequest(ReactRequest $reactRequest)
144
164
*/
145
165
protected function mapResponse (HttpResponse $ reactResponse , SymfonyResponse $ syResponse )
146
166
{
167
+ //end active session
168
+ if (PHP_SESSION_ACTIVE === session_status ()) {
169
+ session_write_close ();
170
+ session_unset (); //reset $_SESSION
171
+ }
172
+
147
173
$ content = $ syResponse ->getContent ();
148
174
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 ());
150
199
$ cookies = [];
151
200
152
201
/** @var Cookie $cookie */
153
202
foreach ($ syResponse ->headers ->getCookies () as $ cookie ) {
154
203
$ cookieHeader = sprintf ('%s=%s ' , $ cookie ->getName (), $ cookie ->getValue ());
155
204
156
205
if ($ cookie ->getPath ()) {
157
- $ cookieHeader .= '; Path= ' . $ cookie ->getPath ();
206
+ $ cookieHeader .= '; Path= ' . urlencode ( $ cookie ->getPath () );
158
207
}
159
208
if ($ cookie ->getDomain ()) {
160
209
$ cookieHeader .= '; Domain= ' . $ cookie ->getDomain ();
@@ -174,7 +223,11 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
174
223
$ cookies [] = $ cookieHeader ;
175
224
}
176
225
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
+ }
178
231
179
232
$ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
180
233
@@ -184,7 +237,6 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
184
237
}
185
238
186
239
$ reactResponse ->end ($ stdOut . $ content );
187
-
188
240
}
189
241
190
242
/**
0 commit comments