8000 Merge branch '2.3' into 2.6 · symfony/symfony@43efb1a · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 43efb1a

Browse files
committed
Merge branch '2.3' into 2.6
* 2.3: [HttpFoundation] MongoDbSessionHandler::read() now checks for valid session age [WebProfilerBundle] Set debug+charset on the ExceptionHandler fallback used HTML5 meta charset tag and removed hardcoded ones Revert "bug #13715 Enforce UTF-8 charset for core controllers (WouterJ)" Conflicts: src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php
2 parents fc009c1 + 374b718 commit 43efb1a

File tree

12 files changed

+125
-151
lines changed

12 files changed

+125
-151
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Resources/views/base.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="UTF-8" />
4+
<meta charset="{{ _charset }}" />
55
<title>{% block title %}Welcome!{% endblock %}</title>
66
{% block stylesheets %}{% endblock %}
77
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />

src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function showAction(Request $request, FlattenException $exception, DebugL
6262

6363
$code = $exception->getStatusCode();
6464

65-
return Response::create($this->twig->render(
65+
return new Response($this->twig->render(
6666
$this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
6767
array(
6868
'status_code' => $code,
@@ -71,7 +71,7 @@ public function showAction(Request $request, FlattenException $exception, DebugL
7171
'logger' => $logger,
7272
'currentContent' => $currentContent,
7373
)
74-
))->setCharset('UTF-8');
74+
));
7575
}
7676

7777
/**

src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="UTF-8" />
4+
<meta charset="{{ _charset }}" />
55
<title>An Error Occurred: {{ status_text }}</title>
66
</head>
77
<body>

src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public function testOnlyClearOwnOutputBuffers()
4040
$request->headers->set('X-Php-Ob-Level', 1);
4141

4242
$controller = new ExceptionController($twig, false);
43-
$response = $controller->showAction($request, $flatten);
44-
$this->assertEquals('UTF-8', $response->getCharset(), 'Request charset is explicitly set to UTF-8');
43+
$controller->showAction($request, $flatten);
4544
}
4645

4746
public function testShowActionCanBeForcedToShowErrorPage()

src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,23 @@ public function showAction($token)
5555
$template = $this->getTemplate();
5656

5757
if (!$this->twig->getLoader()->exists($template)) {
58-
$handler = new ExceptionHandler();
58+
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset());
5959

6060
return new Response($handler->getContent($exception), 200, array('Content-Type' => 'text/html'));
6161
}
6262

6363
$code = $exception->getStatusCode();
6464

65-
return Response::create(
66-
$this->twig->render($template, array(
65+
return new Response($this->twig->render(
66+
$template,
67+
array(
6768
'status_code' => $code,
6869
'status_text' => Response::$statusTexts[$code],
6970
'exception' => $exception,
7071
'logger' => null,
7172
'currentContent' => '',
72-
)),
73-
200,
74-
array('Content-Type' => 'text/html')
75-
)->setCharset('UTF-8');
73+
)
74+
), 200, array('Content-Type' => 'text/html'));
7675
}
7776

7877
/**
@@ -96,16 +95,12 @@ public function cssAction($token)
9695
$template = $this->getTemplate();
9796

9897
if (!$this->templateExists($template)) {
99-
$handler = new ExceptionHandler();
98+
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset());
10099

101-
$response = new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
102-
} else {
103-
$response = new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'), 200, array('Content-Type' => 'text/css'));
100+
return new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
104101
}
105102

106-
$response->setCharset('UTF-8');
107-
108-
return $response;
103+
return new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'), 200, array('Content-Type' => 'text/css'));
109104
}
110105

111106
protected function getTemplate()

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,16 @@ public function panelAction(Request $request, $token)
9999
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
100100
}
101101

102-
return Response::create(
103-
$this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
104-
'token' => $token,
105-
'profile' => $profile,
106-
'collector' => $profile->getCollector($panel),
107-
'panel' => $panel,
108-
'page' => $page,
109-
'request' => $request,
110-
'templates' => $this->getTemplateManager()->getTemplates($profile),
111-
'is_ajax' => $request->isXmlHttpRequest(),
112-
)),
113-
200,
114-
array('Content-Type' => 'text/html')
115-
)->setCharset('UTF-8');
102+
return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
103+
'token' => $token,
104+
'profile' => $profile,
105+
'collector' => $profile->getCollector($panel),
106+
'panel' => $panel,
107+
'page' => $page,
108+
'request' => $request,
109+
'templates' => $this->getTemplateManager()->getTemplates($profile),
110+
'is_ajax' => $request->isXmlHttpRequest(),
111+
)), 200, array('Content-Type' => 'text/html'));
116112
}
117113

118114
/**
@@ -151,13 +147,9 @@ public function infoAction($about)
151147

152148
$this->profiler->disable();
153149

154-
return Response::create(
155-
$this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
156-
'about' => $about,
157-
)),
158-
200,
159-
array('Content-Type' => 'text/html')
160-
)->setCharset('UTF-8');
150+
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
151+
'about' => $about,
152+
)), 200, array('Content-Type' => 'text/html'));
161153
}
162154

163155
/**
@@ -205,17 +197,13 @@ public function toolbarAction(Request $request, $token)
205197
// the profiler is not enabled
206198
}
207199

208-
return Response::create(
209-
$this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
210-
'position' => $position,
211-
'profile' => $profile,
212-
'templates' => $this->getTemplateManager()->getTemplates($profile),
213-
'profiler_url' => $url,
214-
'token' => $token,
215-
)),
216-
200,
217-
array('Content-Type' => 'text/html')
218-
)->setCharset('UTF-8');
200+
return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
201+
'position' => $position,
202+
'profile' => $profile,
203+
'templates' => $this->getTemplateManager()->getTemplates($profile),
204+
'profiler_url' => $url,
205+
'token' => $token,
206+
)), 200, array('Content-Type' => 'text/html'));
219207
}
220208

221209
/**
@@ -253,7 +241,7 @@ public function searchBarAction(Request $request)
253241
$token = $session->get('_profiler_search_token');
254242
}
255243

256-
return Response::create(
244+
return new Response(
257245
$this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
258246
'token' => $token,
259247
'ip' => $ip,
@@ -266,7 +254,7 @@ public function searchBarAction(Request $request)
266254
)),
267255
200,
268256
array('Content-Type' => 'text/html')
269-
)->setCharset('UTF-8');
257+
);
270258
}
271259

272260
/**
@@ -296,22 +284,18 @@ public function searchResultsAction(Request $request, $token)
296284
$end = $request->query->get('end', null);
297285
$limit = $request->query->get('limit');
298286

299-
return Response::create(
300-
$this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
301-
'token' => $token,
302-
'profile' => $profile,
303-
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
304-
'ip' => $ip,
305-
'method' => $method,
306-
'url' => $url,
307-
'start' => $start,
308-
'end' => $end,
309-
'limit' => $limit,
310-
'panel' => null,
311-
)),
312-
200,
313-
array('Content-Type' => 'text/html')
314-
)->setCharset('UTF-8');
287+
return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
288+
'token' => $token,
289+
'profile' => $profile,
290+
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
291+
'ip' => $ip,
292+
'method' => $method,
293+
'url' => $url,
294+
'start' => $start,
295+
'end' => $end,
296+
'limit' => $limit,
297+
'panel' => null,
298+
)), 200, array('Content-Type' => 'text/html'));
315299
}
316300

317301
/**
@@ -385,7 +369,7 @@ public function phpinfoAction()
385369
phpinfo();
386370
$phpinfo = ob_get_clean();
387371

388-
return Response::create($phpinfo, 200, array('Content-Type' => 'text/html'))->setCharset('UTF-8');
372+
return new Response($phpinfo, 200, array('Content-Type' => 'text/html'));
389373
}
390374

391375
/**

src/Symfony/Bundle/WebProfilerBundle/Controller/RouterController.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,10 @@ public function panelAction($token)
6868

6969
$request = $profile->getCollector('request');
7070

71-
return Response::create(
72-
$this->twig->render('@WebProfiler/Router/panel.html.twig', array(
73-
'request' => $request,
74-
'router' => $profile->getCollector('router'),
75-
'traces' => $matcher->getTraces($request->getPathInfo()),
76-
)),
77-
200,
78-
array('Content-Type' => 'text/html')
79-
)->setCharset('UTF-8');
71+
return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
72+
'request' => $request,
73+
'router' => $profile->getCollector('router'),
74+
'traces' => $matcher->getTraces($request->getPathInfo()),
75+
)), 200, array('Content-Type' => 'text/html'));
8076
}
8177
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
5-
<meta name="robots" content="noindex,nofollow">
4+
<meta charset="{{ _charset }}" />
5+
<meta name="robots" content="noindex,nofollow" />
66
<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dXN0+V5bXf+Pjo//0tLS/0hFR/9vbG7/Ozg6/zEuMP8yLzH/Mi8xqTIvMUcyLzHrMi8x/zIvMf8xLjD/Ozg6/Do3OfwwLS//REFD/728vP9nZWb/TktN/4mIif05Nzn/Mi8x7jMwMkwyLzEGMi8xjDIvMf4yLzH/Mi8x/zIvMf8yLzH/Mi8x/zAtL/9XVFb/goGC+Hx6e+6qqanwOzg6/DMwMpJDQEIIMi8xADIvMRUyLzGrMi8x/jIvMf8yLzH/Mi8x/zIvMf8yLzH/MS4w/zg2N/xBPkD3OTY4/DIvMa8yLzEYMi8xAAAAAAAyLzEAMi8xGDIvMY0yLzHqMi8x/zIvMf8yLzH/Mi8x/zIvMf8yLzH/MS4w7DEuMJEyLzEaMi8xAAAAAAAAAAAAAAAAADIvMQAyLzEGMi8xQzIvMZ4yLzHdMi8x+jIvMfoyLzHeMi8xoDIvMUUyLzEGMi8xAAAAAAAAAAAA4AcAAMADAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIABAADAAwAA4AcAAA==">
88
<style>

src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ProfilerControllerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function testReturns404onTokenNotFound()
6969

7070
$response = $controller->toolbarAction(Request::create('/_wdt/found'), 'found');
7171
$this->assertEquals(200, $response->getStatusCode());
72-
$this->assertEquals('UTF-8', $response->getCharset(), 'Request charset is explicitly set to UTF-8');
7372

7473
$response = $controller->toolbarAction(Request::create('/_wdt/notFound'), 'notFound');
7574
$this->assertEquals(404, $response->getStatusCode());

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public function sendPhpResponse($exception)
177177
foreach ($exception->getHeaders() as $name => $value) {
178178
header($name.': '.$value, false);
179179
}
180+
header('Content-Type: text/html; charset='.$this->charset);
180181
}
181182

182183
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
@@ -195,7 +196,7 @@ public function createResponse($exception)
195196
$exception = FlattenException::create($exception);
196197
}
197198

198-
return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders());
199+
return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
199200
}
200201

201202
/**

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
4242
* * id_field: The field name for storing the session id [default: _id]
4343
* * data_field: The field name for storing the session data [default: data]
4444
* * time_field: The field name for storing the timestamp [default: time]
45+
* * expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
46+
*
47+
* It is strongly recommended to put an index on the `expiry_field` for
48+
* garbage-collection. Alternatively it's possible to automatically expire
49+
* the sessions in the database as described below:
50+
*
51+
* A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions
52+
* automatically. Such an index can for example look like this:
53+
*
54+
* db.<session-collection>.ensureIndex(
55+
* { "<expiry-field>": 1 },
56+
* { "expireAfterSeconds": 0 }
57+
* )
58+
*
59+
* More details on: http://docs.mongodb.org/manual/tutorial/expire-data/
60+
*
61+
* If you use such an index, you can drop `gc_probability` to 0 since
62+
* no garbage-collection is required.
4563
*
4664
* @param \Mongo|\MongoClient $mongo A MongoClient or Mongo instance
4765
* @param array $options An associative array of field options
@@ -65,7 +83,7 @@ public function __construct($mongo, array $options)
6583
'id_field' => '_id',
6684
'data_field' => 'data',
6785
'time_field' => 'time',
68-
'expiry_field' => false,
86+
'expiry_field' => 'expires_at',
6987
), $options);
7088
}
7189

@@ -102,21 +120,8 @@ public function destroy($sessionId)
102120
*/
103121
public function gc($maxlifetime)
104122
{
105-
/* Note: MongoDB 2.2+ supports TTL collections, which may be used in
106-
* place of this method by indexing the "time_field" field with an
107-
* "expireAfterSeconds" option. Regardless of whether TTL collections
108-
* are used, consider indexing this field to make the remove query more
109-
* efficient.
110-
*
111-
* See: http://docs.mongodb.org/manual/tutorial/expire-data/
112-
*/
113-
if (false !== $this->options['expiry_field']) {
114-
return true;
115-
}
116-
$time = new \MongoDate(time() - $maxlifetime);
117-
118123
$this->getCollection()->remove(array(
119-
$this->options['time_field'] => array('$lt' => $time),
124+
$this->options['expiry_field'] => array('$lt' => new \MongoDate()),
120125
));
121126

122127
return true;
@@ -127,24 +132,14 @@ public function gc($maxlifetime)
127132
*/
128133
public function write($sessionId, $data)
129134
{
135+
$expiry = new \MongoDate(time() + (int) ini_get('session.gc_maxlifetime'));
136+
130137
$fields = array(
131138
$this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY),
132139
$this->options['time_field'] => new \MongoDate(),
140+
$this->options['expiry_field'] => $expiry,
133141
);
134142

135-
/* Note: As discussed in the gc method of this class. You can utilise
136-
* TTL collections in MongoDB 2.2+
137-
* We are setting the "expiry_field" as part of the write operation here
138-
* You will need to create the index on your collection that expires documents
139-
* at that time
140-
* e.g.
141-
* db.MySessionCollection.ensureIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )
142-
*/
143-
if (false !== $this->options['expiry_field']) {
144-
$expiry = new \MongoDate(time() + (int) ini_get('session.gc_maxlifetime'));
145-
$fields[$this->options['expiry_field']] = $expiry;
146-
}
147-
148143
$this->getCollection()->update(
149144
array($this->options['id_field'] => $sessionId),
150145
array('$set' => $fields),
@@ -160,7 +155,8 @@ public function write($sessionId, $data)
160155
public function read($sessionId)
161156
{
162157
$dbData = $this->getCollection()->findOne(array(
163-
$this->options['id_field'] => $sessionId,
158+
$this->options['id_field'] => $sessionId,
159+
$this->options['expiry_field'] => array('$gte' => new \MongoDate()),
164160
));
165161

166162
return null === $dbData ? '' : $dbData[$this->options['data_field']]->bin;

0 commit comments

Comments
 (0)
0