- support for the "right way" (tm) to redirect post requests
authorMichael Wallner <mike@php.net>
Fri, 30 Sep 2005 15:43:15 +0000 (15:43 +0000)
committerMichael Wallner <mike@php.net>
Fri, 30 Sep 2005 15:43:15 +0000 (15:43 +0000)
http.c
http_api.c
http_functions.c
http_headers_api.c
http_response_object.c
php_http.h
php_http_headers_api.h
tests/redirect_003.phpt

diff --git a/http.c b/http.c
index 5d0491a..f7f0f31 100644 (file)
--- a/http.c
+++ b/http.c
@@ -30,6 +30,7 @@
 #include "php_http_api.h"
 #include "php_http_send_api.h"
 #include "php_http_cache_api.h"
+#include "php_http_headers_api.h"
 #include "php_http_request_method_api.h"
 #ifdef HTTP_HAVE_CURL
 #      include "php_http_request_api.h"
@@ -263,6 +264,9 @@ PHP_MINIT_FUNCTION(http)
 
        REGISTER_INI_ENTRIES();
        
+       if (SUCCESS != http_headers_global_init()) {
+               return FAILURE;
+       }
        if (SUCCESS != http_cache_global_init()) {
                return FAILURE;
        }
index eebfeb2..7111261 100644 (file)
@@ -228,6 +228,8 @@ STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header
        {
                case 301:       http_log(HTTP_G(log).redirect, "301-REDIRECT", header);                 break;
                case 302:       http_log(HTTP_G(log).redirect, "302-REDIRECT", header);                 break;
+               case 303:       http_log(HTTP_G(log).redirect, "303-REDIRECT", header);                 break;
+               case 307:       http_log(HTTP_G(log).redirect, "307-REDIRECT", header);                 break;
                case 304:       http_log(HTTP_G(log).cache, "304-CACHE", header);                               break;
                case 405:       http_log(HTTP_G(log).allowed_methods, "405-ALLOWED", header);   break;
                default:        http_log(NULL, header, body);                                                                   break;
index 63a3423..7d7eec4 100644 (file)
@@ -476,29 +476,37 @@ PHP_FUNCTION(http_throttle)
 }
 /* }}} */
 
-/* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
+/* {{{ proto void http_redirect([string url[, array params[, bool session[, int status]]]])
  *
  * Redirect to a given url.
  * The supplied url will be expanded with http_absolute_uri(), the params array will
  * be treated with http_build_query() and the session identification will be appended
  * if session is true.
  *
- * Depending on permanent the redirection will be issued with a permanent
- * ("301 Moved Permanently") or a temporary ("302 Found") redirection
- * status code.
+ * The HTTP response code will be set according to status.
+ * You can use one of the following constants for convenience:
+ *  - HTTP_REDIRECT                    302 Found
+ *  - HTTP_REDIRECT_PERM       301 Moved Permanently
+ *  - HTTP_REDIRECT_POST       303 See Other
+ *  - HTTP_REDIRECT_TEMP       307 Temporary Redirect
+ *
+ * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
+ * for which redirect response code to use in which situation.
  *
  * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
- * if the client doesn't redirect immediatly.
+ * if the client doesn't redirect immediatly, and the request method was
+ * antoher than HEAD.
  */
 PHP_FUNCTION(http_redirect)
 {
        int url_len;
        size_t query_len = 0;
-       zend_bool session = 0, permanent = 0, free_params = 0;
+       zend_bool session = 0, free_params = 0;
        zval *params = NULL;
+       long status = 302;
        char *query = NULL, *url = NULL, *URI, *LOC, *RED = NULL;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bl", &url, &url_len, &params, &session, &status) != SUCCESS) {
                RETURN_FALSE;
        }
 
@@ -580,7 +588,7 @@ PHP_FUNCTION(http_redirect)
                FREE_ZVAL(params);
        }
 
-       RETURN_SUCCESS(http_exit_ex(permanent ? 301 : 302, LOC, RED, 1));
+       RETURN_SUCCESS(http_exit_ex(status, LOC, RED, 1));
 }
 /* }}} */
 
index 63c46ab..b7295ef 100644 (file)
@@ -37,6 +37,18 @@ ZEND_EXTERN_MODULE_GLOBALS(http);
 #      define HTTP_DBG_NEG 0
 #endif
 
+/* {{{ STATUS http_headers_global_init() */
+STATUS _http_headers_global_init(INIT_FUNC_ARGS)
+{
+       HTTP_LONG_CONSTANT("HTTP_REDIRECT", HTTP_REDIRECT);
+       HTTP_LONG_CONSTANT("HTTP_REDIRECT_PERM", HTTP_REDIRECT_PERM);
+       HTTP_LONG_CONSTANT("HTTP_REDIRECT_POST", HTTP_REDIRECT_POST);
+       HTTP_LONG_CONSTANT("HTTP_REDIRECT_TEMP", HTTP_REDIRECT_TEMP);
+       
+       return SUCCESS;
+}
+/* }}} */
+
 /* {{{ static int http_sort_q(const void *, const void *) */
 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
 {
index 93c8f0c..37c45f8 100644 (file)
@@ -241,6 +241,11 @@ static inline void _http_response_object_declare_default_properties(TSRMLS_D)
        DCL_STATIC_PROP(PROTECTED, double, throttleDelay, 0.0);
 
 #ifndef WONKY
+       DCL_CONST(long, "REDIRECT", HTTP_REDIRECT);
+       DCL_CONST(long, "REDIRECT_PERM", HTTP_REDIRECT_PERM);
+       DCL_CONST(long, "REDIRECT_POST", HTTP_REDIRECT_POST);
+       DCL_CONST(long, "REDIRECT_TEMP", HTTP_REDIRECT_TEMP);
+       
        DCL_CONST(long, "ETAG_MD5", HTTP_ETAG_MD5);
        DCL_CONST(long, "ETAG_SHA1", HTTP_ETAG_SHA1);
        DCL_CONST(long, "ETAG_CRC32", HTTP_ETAG_CRC32);
index 87e1f3a..e457d2d 100644 (file)
@@ -18,7 +18,7 @@
 #ifndef PHP_EXT_HTTP_H
 #define PHP_EXT_HTTP_H
 
-#define HTTP_PEXT_VERSION "0.14.1"
+#define HTTP_PEXT_VERSION "0.14.2dev"
 
 /* make compile on Win32 */
 #ifdef HTTP_HAVE_CURL
index 329d650..dbf1a43 100644 (file)
 #include "php_http_std_defs.h"
 #include "php_http_info_api.h"
 
+#define HTTP_REDIRECT          302L
+#define HTTP_REDIRECT_PERM     301L
+#define HTTP_REDIRECT_POST     303L
+#define HTTP_REDIRECT_TEMP     307L
+
 typedef enum {
        RANGE_OK,
        RANGE_NO,
        RANGE_ERR
 } http_range_status;
 
+#define http_headers_global_init() _http_headers_global_init(INIT_FUNC_ARGS_PASSTHRU)
+extern STATUS _http_headers_global_init(INIT_FUNC_ARGS);
+
 #define http_parse_headers(h, a) _http_parse_headers_ex((h), Z_ARRVAL_P(a), 1, http_info_default_callback, NULL TSRMLS_CC)
 #define http_parse_headers_ex(h, ht, p) _http_parse_headers_ex((h), (ht), (p), http_info_default_callback, NULL TSRMLS_CC)
 #define http_parse_headers_cb(h, ht, p, f, d) _http_parse_headers_ex((h), (ht), (p), (f), (d) TSRMLS_CC)
index ba9b83b..0de0195 100644 (file)
@@ -11,7 +11,7 @@ HTTP_HOST=localhost
 <?php
 include 'log.inc';
 log_prepare(_REDIR_LOG);
-http_redirect('redirect', null, false, true);
+http_redirect('redirect', null, false, HTTP_REDIRECT_PERM);
 ?>
 --EXPECTF--
 Status: 301