Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use recommended reason phrases for redirect #1179

Merged
merged 2 commits into from Sep 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion ext/http/response.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,18 @@ PHP_METHOD(Phalcon_Http_Response, redirect){
zval *header = NULL, *dependency_injector, *service;
zval *url, *status_text, *header_name;

static const char* redirect_phrases[] = {
/* 300 */ "Multiple Choices",
/* 301 */ "Moved Permanently",
/* 302 */ "Found",
/* 303 */ "See Other",
/* 304 */ "Not Modified",
/* 305 */ "Use Proxy",
/* 306 */ "Switch Proxy",
/* 307 */ "Temporary Redirect",
/* 308 */ "Permanent Redirect"
};

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 3, &location, &external_redirect, &status_code);
Expand All @@ -540,6 +552,10 @@ PHP_METHOD(Phalcon_Http_Response, redirect){
PHALCON_INIT_VAR(status_code);
ZVAL_LONG(status_code, 302);
}
else if (unlikely(Z_TYPE_P(status_code) != IS_LONG)) {
PHALCON_SEPARATE_PARAM(status_code);
convert_to_long(status_code);
}

if (zend_is_true(external_redirect)) {
PHALCON_CPY_WRT(header, location);
Expand All @@ -562,7 +578,13 @@ PHP_METHOD(Phalcon_Http_Response, redirect){
* The HTTP status is 302 by default, a temporary redirection
*/
PHALCON_INIT_VAR(status_text);
ZVAL_STRING(status_text, "Redirect", 1);
if (Z_LVAL_P(status_code) < 300 || Z_LVAL_P(status_code) > 308) {
ZVAL_STRING(status_text, "Redirect", 1);
}
else {
ZVAL_STRING(status_text, redirect_phrases[Z_LVAL_P(status_code) - 300], 1);
}

phalcon_call_method_p2_noret(this_ptr, "setstatuscode", status_code, status_text);

/**
Expand Down
15 changes: 6 additions & 9 deletions unit-tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,15 @@ public function testNotModifiedHeader()

public function testRedirect()
{


//local URI
$this->_response->resetHeaders();

$this->_response->redirect("some/local/uri");

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 302 Redirect' => false,
'Status' => '302 Redirect',
'HTTP/1.1 302 Found' => false,
'Status' => '302 Found',
'Location' => '/some/local/uri'
)
)), $this->_response->getHeaders());
Expand All @@ -159,8 +157,8 @@ public function testRedirect()

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 302 Redirect' => false,
'Status' => '302 Redirect',
'HTTP/1.1 302 Found' => false,
'Status' => '302 Found',
'Location' => 'http://google.com'
)
)), $this->_response->getHeaders());
Expand All @@ -172,12 +170,11 @@ public function testRedirect()

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 301 Redirect' => false,
'Status' => '301 Redirect',
'HTTP/1.1 301 Moved Permanently' => false,
'Status' => '301 Moved Permanently',
'Location' => 'http://google.com'
)
)), $this->_response->getHeaders());

}

public function testContent()
Expand Down