Skip to content

Commit

Permalink
Merge branch '2.1' into 2.2
Browse files Browse the repository at this point in the history
* 2.1:
  [HttpFoundation][BrowserKit] fixed path when converting a cookie to a string
  [BrowserKit] removed dead code
  [HttpFoundation] fixed empty domain= in Cookie::__toString()
  fixed detection of secure cookies received over https
  [Translation] removed an uneeded class property
  [Translation] removed unneeded getter/setter
  [Translator] added additional conversion for encodings other than utf-8
  fix a DI circular reference recognition bug
  [HttpFoundation] fixed the creation of sub-requests under some circumstances for IIS
  • Loading branch information
fabpot committed Apr 20, 2013
2 parents 64ac4d1 + 499c9e6 commit e81f792
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -266,7 +266,7 @@ public function request($method, $uri, array $parameters = array(), array $files

$response = $this->filterResponse($this->response);

$this->cookieJar->updateFromResponse($response);
$this->cookieJar->updateFromResponse($response, $uri);

$this->redirect = $response->getHeader('Location');

Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Component/BrowserKit/Cookie.php
Expand Up @@ -95,7 +95,7 @@ public function __toString()
$cookie .= '; domain='.$this->domain;
}

if ('/' !== $this->path) {
if ($this->path) {
$cookie .= '; path='.$this->path;
}

Expand Down Expand Up @@ -147,10 +147,9 @@ public static function fromString($cookie, $url = null)
if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host']) || !isset($urlParts['path'])) {
throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
}
$parts = array_merge($urlParts, $parts);

$values['domain'] = $parts['host'];
$values['path'] = substr($parts['path'], 0, strrpos($parts['path'], '/'));
$values['domain'] = $urlParts['host'];
$values['path'] = substr($urlParts['path'], 0, strrpos($urlParts['path'], '/'));
}

foreach ($parts as $part) {
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -205,6 +205,15 @@ public function testRequestCookies()
$this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->allValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
}

public function testRequestSecureCookies()
{
$client = new TestClient();
$client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>', 200, array('Set-Cookie' => 'foo=bar; path=/; secure')));
$client->request('GET', 'https://www.example.com/foo/foobar');

$this->assertTrue($client->getCookieJar()->get('foo', '/', 'www.example.com')->isSecure());
}

public function testClick()
{
if (!class_exists('Symfony\Component\DomCrawler\Crawler')) {
Expand Down
24 changes: 12 additions & 12 deletions src/Symfony/Component/BrowserKit/Tests/CookieTest.php
Expand Up @@ -26,14 +26,14 @@ public function testToFromString($cookie, $url = null)
public function getTestsForToFromString()
{
return array(
array('foo=bar'),
array('foo=bar; path=/'),
array('foo=bar; path=/foo'),
array('foo=bar; domain=google.com'),
array('foo=bar; domain=example.com; secure', 'https://example.com/'),
array('foo=bar; httponly'),
array('foo=bar; domain=google.com; path=/'),
array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
array('foo=bar; path=/; httponly'),
array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
array('foo=bar=baz'),
array('foo=bar%3Dbaz'),
array('foo=bar=baz; path=/'),
array('foo=bar%3Dbaz; path=/'),
);
}

Expand Down Expand Up @@ -67,17 +67,17 @@ public function getExpireCookieStrings()

public function testFromStringWithCapitalization()
{
$this->assertEquals('Foo=Bar', (string) Cookie::fromString('Foo=Bar'));
$this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT'));
$this->assertEquals('foo=bar; domain=www.example.org; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
$this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
$this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT; path=/', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT'));
$this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
}

public function testFromStringWithUrl()
{
$this->assertEquals('foo=bar; domain=www.example.com', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
$this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
$this->assertEquals('foo=bar; domain=www.example.com', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
$this->assertEquals('foo=bar; domain=www.myotherexample.com', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
$this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
}

public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
Expand Down
Expand Up @@ -56,11 +56,13 @@ public function process(ContainerBuilder $container)
private function checkOutEdges(array $edges)
{
foreach ($edges as $edge) {
$node = $edge->getDestNode();
$this->currentPath[] = $id = $node->getId();
$node = $edge->getDestNode();
$id = $node->getId();
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;

if ($this->currentId === $id) {
throw new ServiceCircularReferenceException($this->currentId, $this->currentPath);
if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
}

$this->checkOutEdges($node->getOutEdges());
Expand Down
Expand Up @@ -24,7 +24,7 @@
class CheckCircularReferencesPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testProcess()
{
Expand All @@ -36,7 +36,7 @@ public function testProcess()
}

/**
* @expectedException \RuntimeException
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testProcessWithAliases()
{
Expand All @@ -49,7 +49,7 @@ public function testProcessWithAliases()
}

/**
* @expectedException \RuntimeException
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testProcessDetectsIndirectCircularReference()
{
Expand All @@ -61,6 +61,19 @@ public function testProcessDetectsIndirectCircularReference()
$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testDeepCircularReference()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b')->addArgument(new Reference('c'));
$container->register('c')->addArgument(new Reference('b'));

$this->process($container);
}

public function testProcessIgnoresMethodCalls()
{
$container = new ContainerBuilder();
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/Cookie.php
Expand Up @@ -93,11 +93,11 @@ public function __toString()
}
}

if ('/' !== $this->path) {
if ($this->path) {
$str .= '; path='.$this->path;
}

if (null !== $this->getDomain()) {
if ($this->getDomain()) {
$str .= '; domain='.$this->getDomain();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -1489,11 +1489,14 @@ protected function prepareRequestUri()
{
$requestUri = '';

if ($this->headers->has('X_ORIGINAL_URL') && false !== stripos(PHP_OS, 'WIN')) {
if ($this->headers->has('X_ORIGINAL_URL')) {
// IIS with Microsoft Rewrite Module
$requestUri = $this->headers->get('X_ORIGINAL_URL');
$this->headers->remove('X_ORIGINAL_URL');
} elseif ($this->headers->has('X_REWRITE_URL') && false !== stripos(PHP_OS, 'WIN')) {
$this->server->remove('HTTP_X_ORIGINAL_URL');
$this->server->remove('UNENCODED_URL');
$this->server->remove('IIS_WasUrlRewritten');
} elseif ($this->headers->has('X_REWRITE_URL')) {
// IIS with ISAPI_Rewrite
$requestUri = $this->headers->get('X_REWRITE_URL');
$this->headers->remove('X_REWRITE_URL');
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/HttpFoundation/Tests/CookieTest.php
Expand Up @@ -134,11 +134,12 @@ public function testCookieIsCleared()
public function testToString()
{
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);

$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');

$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');

$this->assertEquals('foo=deleted; expires=' . gmdate("D, d-M-Y H:i:s T", time()-31536001) . '; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');

$cookie = new Cookie('foo', 'bar', 0, '/', '');
$this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
}
}
89 changes: 89 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -1391,6 +1391,95 @@ public function testTrustedProxies()
// reset
Request::setTrustedProxies(array());
}

/**
* @dataProvider iisRequestUriProvider
*/
public function testIISRequestUri($headers, $server, $expectedRequestUri)
{
$request = new Request();
$request->headers->replace($headers);
$request->server->replace($server);

$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');

$subRequestUri = '/bar/foo';
$subRequest = $request::create($subRequestUri, 'get', array(), array(), array(), $request->server->all());
$this->assertEquals($subRequestUri, $subRequest->getRequestUri(), '->getRequestUri() is correct in sub request');
}

public function iisRequestUriProvider()
{
return array(
array(
array(
'X_ORIGINAL_URL' => '/foo/bar',
),
array(),
'/foo/bar'
),
array(
array(
'X_REWRITE_URL' => '/foo/bar',
),
array(),
'/foo/bar'
),
array(
array(),
array(
'IIS_WasUrlRewritten' => '1',
'UNENCODED_URL' => '/foo/bar'
),
'/foo/bar'
),
array(
array(
'X_ORIGINAL_URL' => '/foo/bar',
),
array(
'HTTP_X_ORIGINAL_URL' => '/foo/bar'
),
'/foo/bar'
),
array(
array(
'X_ORIGINAL_URL' => '/foo/bar',
),
array(
'IIS_WasUrlRewritten' => '1',
'UNENCODED_URL' => '/foo/bar'
),
'/foo/bar'
),
array(
array(
'X_ORIGINAL_URL' => '/foo/bar',
),
array(
'HTTP_X_ORIGINAL_URL' => '/foo/bar',
'IIS_WasUrlRewritten' => '1',
'UNENCODED_URL' => '/foo/bar'
),
'/foo/bar'
),
array(
array(),
array(
'ORIG_PATH_INFO' => '/foo/bar',
),
'/foo/bar'
),
array(
array(),
array(
'ORIG_PATH_INFO' => '/foo/bar',
'QUERY_STRING' => 'foo=bar',
),
'/foo/bar?foo=bar'
)
);
}
}

class RequestContentProxy extends Request
Expand Down
Expand Up @@ -114,11 +114,11 @@ public function testToStringIncludesCookieHeaders()
$bag = new ResponseHeaderBag(array());
$bag->setCookie(new Cookie('foo', 'bar'));

$this->assertContains("Set-Cookie: foo=bar; httponly", explode("\r\n", $bag->__toString()));
$this->assertContains("Set-Cookie: foo=bar; path=/; httponly", explode("\r\n", $bag->__toString()));

$bag->clearCookie('foo');

$this->assertContains("Set-Cookie: foo=deleted; expires=".gmdate("D, d-M-Y H:i:s T", time() - 31536001)."; httponly", explode("\r\n", $bag->__toString()));
$this->assertContains("Set-Cookie: foo=deleted; expires=".gmdate("D, d-M-Y H:i:s T", time() - 31536001)."; path=/; httponly", explode("\r\n", $bag->__toString()));
}

public function testReplace()
Expand Down Expand Up @@ -158,7 +158,7 @@ public function testCookiesWithSameNames()
$this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; path=/path/bar; domain=bar.foo; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; httponly", $headers);
$this->assertContains("Set-Cookie: foo=bar; path=/; httponly", $headers);

$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
Expand Down
20 changes: 17 additions & 3 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Expand Up @@ -40,7 +40,7 @@ public function load($resource, $locale, $domain = 'messages')
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}

$xml = $this->parseFile($resource);
list($xml, $encoding) = $this->parseFile($resource);
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');

$catalogue = new MessageCatalogue($locale);
Expand All @@ -52,7 +52,21 @@ public function load($resource, $locale, $domain = 'messages')
}

$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
$catalogue->set((string) $source, (string) $translation->target, $domain);
$target = (string) $translation->target;

// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
if ('UTF-8' !== $encoding && !empty($encoding)) {
if (function_exists('mb_convert_encoding')) {
$target = mb_convert_encoding($target, $encoding, 'UTF-8');
} elseif (function_exists('iconv')) {
$target = iconv('UTF-8', $encoding, $target);
} else {
throw new \RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
}
}

$catalogue->set((string) $source, $target, $domain);
}
$catalogue->addResource(new FileResource($resource));

Expand Down Expand Up @@ -117,7 +131,7 @@ private function parseFile($file)

libxml_use_internal_errors($internalErrors);

return simplexml_import_dom($dom);
return array(simplexml_import_dom($dom), strtoupper($dom->encoding));
}

/**
Expand Down
Expand Up @@ -50,6 +50,19 @@ public function testIncompleteResource()
$this->assertFalse($catalogue->has('extra', 'domain1'));
}

public function testEncoding()
{
if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
$this->markTestSkipped('The iconv and mbstring extensions are not available.');
}

$loader = $this->createLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');

$this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
$this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
}

/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Translation/Tests/fixtures/encoding.xlf
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1" resname="foo">
<source>foo</source>
<target>bär</target>
</trans-unit>
<trans-unit id="2" resname="bar">
<source>bar</source>
<target>föö</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit e81f792

Please sign in to comment.