From 9c12b0d0fd94e5c026cc4d7e899cee0b61859d4f Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 30 Sep 2005 15:43:15 +0000 Subject: [PATCH] - support for the "right way" (tm) to redirect post requests --- http.c | 4 ++++ http_api.c | 2 ++ http_functions.c | 24 ++++++++++++++++-------- http_headers_api.c | 12 ++++++++++++ http_response_object.c | 5 +++++ php_http.h | 2 +- php_http_headers_api.h | 8 ++++++++ tests/redirect_003.phpt | 2 +- 8 files changed, 49 insertions(+), 10 deletions(-) diff --git a/http.c b/http.c index 5d0491a..f7f0f31 100644 --- 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; } diff --git a/http_api.c b/http_api.c index eebfeb2..7111261 100644 --- a/http_api.c +++ b/http_api.c @@ -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; diff --git a/http_functions.c b/http_functions.c index 63a3423..7d7eec4 100644 --- a/http_functions.c +++ b/http_functions.c @@ -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 URI." 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, ¶ms, &session, &permanent) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bl", &url, &url_len, ¶ms, &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)); } /* }}} */ diff --git a/http_headers_api.c b/http_headers_api.c index 63c46ab..b7295ef 100644 --- a/http_headers_api.c +++ b/http_headers_api.c @@ -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) { diff --git a/http_response_object.c b/http_response_object.c index 93c8f0c..37c45f8 100644 --- a/http_response_object.c +++ b/http_response_object.c @@ -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); diff --git a/php_http.h b/php_http.h index 87e1f3a..e457d2d 100644 --- a/php_http.h +++ b/php_http.h @@ -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 diff --git a/php_http_headers_api.h b/php_http_headers_api.h index 329d650..dbf1a43 100644 --- a/php_http_headers_api.h +++ b/php_http_headers_api.h @@ -21,12 +21,20 @@ #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) diff --git a/tests/redirect_003.phpt b/tests/redirect_003.phpt index ba9b83b..0de0195 100644 --- a/tests/redirect_003.phpt +++ b/tests/redirect_003.phpt @@ -11,7 +11,7 @@ HTTP_HOST=localhost --EXPECTF-- Status: 301 -- 2.30.2