<commit_before><?php
namespace Luracast\Restler;

use Luracast\Restler\Format\JsonFormat;

/**
 * Static class for handling redirection
 *
 * @category   Framework
 * @package    Restler
 * @author     <NAME> <<EMAIL>>
 * @copyright  2010 Luracast
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link       http://luracast.com/products/restler/
 * @version    3.0.0rc5
 */
class Redirect
{
    /**
     * Redirect to given url
     *
     * @param string $url relative path or full url
     * @param array $params associative array of query parameters
     * @param array $sessionData associative array of properties to be set in $_SESSION
     *
     * @return array
     */
    public static function to($url, array $params = array(), array $sessionData = array())
    {
        $url = ltrim($url, '/');
        /** @var $r Restler */
        $r = Scope::get('Restler');
        $base = $r->getBaseUrl() . '/';
        if (0 !== strpos($url, 'http'))
            $url = $base . $url;
        if (!empty($sessionData) || $base . $r->url !== $url || Util::getRequestMethod() != 'GET') {
            if ($r->responseFormat instanceof JsonFormat)
                return array('redirect' => $url);
            if (!empty($params)) {
                $url .= '?' . http_build_query($params);
            }
            foreach ($sessionData as $name => $value) {
                $_SESSION[$name] = $value;
            }
            header("Location: $url");
            die('');
        }
        return array();
    }
}<commit_msg>Improves Redirect set http status code. Closes #242<commit_after><?php
namespace Luracast\Restler;

use Luracast\Restler\Format\JsonFormat;

/**
 * Static class for handling redirection
 *
 * @category   Framework
 * @package    Restler
 * @author     <NAME> <<EMAIL>>
 * @copyright  2010 Luracast
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link       http://luracast.com/products/restler/
 * @version    3.0.0rc5
 */
class Redirect
{
    /**
     * Redirect to given url
     *
     * @param string $url relative path or full url
     * @param array $params associative array of query parameters
     * @param array $sessionData associative array of properties to be set in $_SESSION
     * @param int $status http status code to send the response with ideally 301 or 302
     *
     * @return array
     */
    public static function to($url, array $params = array(), array $sessionData = array(), $status = 302)
    {
        $url = ltrim($url, '/');
        /** @var $r Restler */
        $r = Scope::get('Restler');
        $base = $r->getBaseUrl() . '/';
        if (0 !== strpos($url, 'http'))
            $url = $base . $url;
        if (!empty($sessionData) || $base . $r->url !== $url || Util::getRequestMethod() != 'GET') {
            if ($r->responseFormat instanceof JsonFormat)
                return array('redirect' => $url);
            if (!empty($params)) {
                $url .= '?' . http_build_query($params);
            }
            foreach ($sessionData as $name => $value) {
                $_SESSION[$name] = $value;
            }
            header(
                "{$_SERVER['SERVER_PROTOCOL']} $status " .
                (isset(RestException::$codes[$status]) ? RestException::$codes[$status] : '')
            );
            header("Location: $url");
            die('');
        }
        return array();
    }
}
{'fraction_non_alphanumeric': 0.15187663660168751, 'fraction_numerical': 0.008728542333430317, 'mean_word_length': 2.0889487870619945, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2475578', 'n_tokens_mistral': 1028, 'n_tokens_neox': 998, 'n_words': 267}