Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ZF HTTP Status Code overwritten #3661

Closed
wants to merge 1 commit into from

2 participants

Jamie Burns Matthew Weier O'Phinney
Jamie Burns

Using code as below (in any call order):

$response->setStatusCode(HttpResponse::STATUS_CODE_202);
$response->getHeaders()->addHeaderLine('Location', $location);

In a controller does not work because PHP's header() function sets a 302 code whenever a Location header is sent.

By sending the ZF "status header line" last we can make sure that the correct code is sent to the client. Unfortunately this cannot be tested explicitly because xdebug_get_headers() does not seem to return the status header.

Ref: http://php.net/manual/en/function.header.php

Jamie Burns fantasticjamieburns Status header is sent last
Status header is sent last to prevent header() from overwriting the ZF
status code when a Location header is used
876f95d
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3661' into develop
Forward port #3661
1d55e68
Matthew Weier O'Phinney weierophinney was assigned
Matthew Weier O'Phinney

Nice catch!

Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3661'
Close #3661
53adce5
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3661' into develop
Forward port #3661
d174015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 4, 2013
  1. Jamie Burns

    Status header is sent last

    fantasticjamieburns authored
    Status header is sent last to prevent header() from overwriting the ZF
    status code when a Location header is used
This page is out of date. Refresh to see the latest.
5 library/Zend/Mvc/ResponseSender/AbstractResponseSender.php
View
@@ -26,8 +26,6 @@ public function sendHeaders(SendResponseEvent $event)
}
$response = $event->getResponse();
- $status = $response->renderStatusLine();
- header($status);
foreach ($response->getHeaders() as $header) {
if ($header instanceof MultipleHeaderInterface) {
@@ -37,6 +35,9 @@ public function sendHeaders(SendResponseEvent $event)
header($header->toString());
}
+ $status = $response->renderStatusLine();
+ header($status);
+
$event->setHeadersSent();
return $this;
}
31 tests/ZendTest/Mvc/ResponseSender/AbstractResponseSenderTest.php
View
@@ -11,6 +11,7 @@
namespace ZendTest\Mvc\ResponseSender;
use PHPUnit_Framework_TestCase as TestCase;
+use Zend\Http\Headers;
use Zend\Http\Response;
/**
@@ -53,4 +54,34 @@ public function testSendHeadersTwoTimesSendsOnlyOnce()
$responseSender->sendHeaders($mockSendResponseEvent);
$this->assertEquals(array(), xdebug_get_headers());
}
+
+ /**
+ * @runInSeparateProcess
+ */
+ public function testSendHeadersSendsStatusLast()
+ {
+ if (!function_exists('xdebug_get_headers')) {
+ $this->markTestSkipped('Xdebug extension needed, skipped test');
+ }
+
+ $mockResponse = $this->getMock('Zend\Http\Response');
+ $mockResponse->expects($this->once())->method('getHeaders')->will($this->returnValue(Headers::fromString('Location: example.com')));
+ $mockResponse->expects($this->once())->method('renderStatusLine')->will($this->returnValue('X-Test: HTTP/1.1 202 Accepted'));
+
+ $mockSendResponseEvent = $this->getMock('Zend\Mvc\ResponseSender\SendResponseEvent', array('getResponse'));
+ $mockSendResponseEvent->expects($this->any())->method('getResponse')->will($this->returnValue($mockResponse));
+
+ $responseSender = $this->getMockForAbstractClass('Zend\Mvc\ResponseSender\AbstractResponseSender');
+ $responseSender->sendHeaders($mockSendResponseEvent);
+
+ $sentHeaders = xdebug_get_headers();
+
+ $this->assertCount(2, $sentHeaders);
+ $this->assertEquals('Location: example.com', $sentHeaders[0]);
+ $this->assertEquals(
+ 'X-Test: HTTP/1.1 202 Accepted',
+ $sentHeaders[1],
+ 'Status header is sent last to prevent header() from overwriting the ZF status code when a Location header is used'
+ );
+ }
}
Something went wrong with that request. Please try again.