From: Michael Wallner Date: Fri, 18 Mar 2005 12:50:24 +0000 (+0000) Subject: * improve http_send() and http_negotiate_q() code X-Git-Tag: RELEASE_0_7_0~30 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=985d2372a8eb0038e3244496c3f13acede337bdc * improve http_send() and http_negotiate_q() code --- diff --git a/http_api.c b/http_api.c index d48770c..20dc30d 100644 --- a/http_api.c +++ b/http_api.c @@ -604,28 +604,21 @@ PHP_HTTP_API STATUS _http_send_status_header(const int status, const char *heade } /* }}} */ -/* {{{ zval *http_get_server_var(char *) */ -PHP_HTTP_API zval *_http_get_server_var(const char *key TSRMLS_DC) +/* {{{ zval *http_get_server_var_ex(char *, size_t) */ +PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC) { zval **var; - if (SUCCESS == zend_hash_find( - HTTP_SERVER_VARS, - (char *) key, strlen(key) + 1, (void **) &var)) { - return *var; + if (SUCCESS == zend_hash_find(HTTP_SERVER_VARS, (char *) key, key_size, (void **) &var)) { + if (check) { + return Z_STRVAL_PP(var) && Z_STRLEN_PP(var) ? *var : NULL; + } else { + return *var; + } } return NULL; } /* }}} */ -/* {{{ int http_got_server_var(char *) */ -PHP_HTTP_API int _http_got_server_var(const char *key TSRMLS_DC) -{ - zval *dummy; - HTTP_GSC(dummy, key, 0); - return 1; -} -/* }}} */ - /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */ PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC) @@ -705,7 +698,7 @@ PHP_HTTP_API STATUS _http_start_ob_handler(php_output_handler_func_t handler_fun /* }}} */ /* {{{ int http_modified_match(char *, time_t) */ -PHP_HTTP_API int _http_modified_match_ex(const char *entry, const time_t t, +PHP_HTTP_API int _http_modified_match_ex(const char *entry, const time_t t, const int enforce_presence TSRMLS_DC) { int retval; @@ -725,7 +718,7 @@ PHP_HTTP_API int _http_modified_match_ex(const char *entry, const time_t t, /* }}} */ /* {{{ int http_etag_match(char *, char *) */ -PHP_HTTP_API int _http_etag_match_ex(const char *entry, const char *etag, +PHP_HTTP_API int _http_etag_match_ex(const char *entry, const char *etag, const int enforce_presence TSRMLS_DC) { zval *zetag; @@ -947,7 +940,7 @@ PHP_HTTP_API char *_http_absolute_uri_ex( return NULL; } - if (!(purl = php_url_parse(url))) { + if (!(purl = php_url_parse((char *) url))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse supplied URL"); return NULL; } @@ -1061,70 +1054,50 @@ PHP_HTTP_API char *_http_absolute_uri_ex( } /* }}} */ -/* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */ -PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported, - const char *def TSRMLS_DC) +/* {{{ char *http_negotiate_q(char *, HashTable *, char *) */ +PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *supported, const char *def TSRMLS_DC) { - zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp; - char *q_ptr, *result; - int i, c; + zval *zaccept, zdelim, zarray, zentries, **zentry, **zsupp; + char *q_ptr = NULL, *key = NULL; + int i = 0, idx = 0; double qual; HTTP_GSC(zaccept, entry, estrdup(def)); - MAKE_STD_ZVAL(zarray); - array_init(zarray); - - MAKE_STD_ZVAL(zdelim); - ZVAL_STRING(zdelim, ",", 0); - php_explode(zdelim, zaccept, zarray, -1); - efree(zdelim); + array_init(&zarray); + array_init(&zentries); - MAKE_STD_ZVAL(zentries); - array_init(zentries); + Z_STRVAL(zdelim) = ","; + Z_STRLEN(zdelim) = 1; - c = zend_hash_num_elements(Z_ARRVAL_P(zarray)); - for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) { + php_explode(&zdelim, zaccept, &zarray, -1); - if (SUCCESS != zend_hash_get_current_data( - Z_ARRVAL_P(zarray), (void **) &zentry)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, - "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept)); - break; - } - - /* check for qualifier */ - if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) { + FOREACH_HASH_VAL(Z_ARRVAL(zarray), zentry) { + if (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';')) { qual = strtod(q_ptr + 3, NULL); + *q_ptr = 0; + q_ptr = NULL; } else { - qual = 1000.0 - i; + qual = 1000.0 - i++; } - - /* walk through the supported array */ - FOREACH_VAL(supported, zsupp) { + FOREACH_HASH_VAL((HashTable *)supported, zsupp) { if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) { - add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual); + add_assoc_double(&zentries, Z_STRVAL_PP(zsupp), qual); break; } } } + zval_dtor(&zarray); - zval_dtor(zarray); - efree(zarray); - - zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries)); - - if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort, - http_sort_q, 0 TSRMLS_CC)) || - (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key( - Z_ARRVAL_P(zentries), &result, 0, 1))) { - result = estrdup(def); + zend_hash_sort(Z_ARRVAL(zentries), zend_qsort, http_sort_q, 0 TSRMLS_CC); + + FOREACH_HASH_KEY(Z_ARRVAL(zentries), key, idx) { + if (key) { + return estrdup(key); + } } - - zval_dtor(zentries); - efree(zentries); - - return result; + + return estrdup(def); } /* }}} */ @@ -1337,6 +1310,7 @@ PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size, { HashTable ranges; http_range_status range_status; + int cache_etag = 0; if (!data_ptr) { return FAILURE; @@ -1345,62 +1319,59 @@ PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size, return SUCCESS; } + /* stop on-the-fly etag generation */ + if (cache_etag = HTTP_G(etag_started)) { + /* interrupt */ + HTTP_G(etag_started) = 0; + /* never ever use the output to compute the ETag if http_send() is used */ + php_end_ob_buffers(0 TSRMLS_CC); + } + zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0); range_status = http_get_request_ranges(&ranges, data_size); - if (range_status != RANGE_OK) { - zend_hash_destroy(&ranges); - } - if (range_status == RANGE_ERR) { + zend_hash_destroy(&ranges); http_send_status(416); return FAILURE; } - /* etag handling */ - - if (HTTP_G(etag_started)) { - char *etag; + /* Range Request - only send ranges if entity hasn't changed */ + if ( range_status == RANGE_OK && + http_etag_match_ex("HTTP_IF_MATCH", HTTP_G(etag), 0) && + http_modified_match_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod), 0)) { + STATUS result = http_send_ranges(&ranges, data_ptr, data_size, data_mode); + zend_hash_destroy(&ranges); + return result; + } + + zend_hash_destroy(&ranges); - /* interrupt */ - HTTP_G(etag_started) = 0; - /* never ever use the output to compute the ETag if http_send() is used */ - php_end_ob_buffer(0, 0 TSRMLS_CC); + /* send 304 Not Modified if etag matches */ + if (cache_etag) { + char *etag = NULL; + int etag_match = 0; if (!(etag = http_etag(data_ptr, data_size, data_mode))) { return FAILURE; } - /* send 304 Not Modified if etag matches */ - if ((range_status == RANGE_NO) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) { - efree(etag); - return http_send_status(304); - } - http_send_etag(etag, 32); + etag_match = http_etag_match("HTTP_IF_NONE_MATCH", etag); efree(etag); + + if (etag_match) { + return http_send_status(304); + } } - switch (range_status) { - /* breaks intentionally left out */ - - case RANGE_OK: - /* only send ranges if entity hasn't changed */ - if ( http_etag_match_ex("HTTP_IF_MATCH", HTTP_G(etag), 0) && - http_modified_match_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod), 0)) { - STATUS result = http_send_ranges(&ranges, data_ptr, data_size, data_mode); - zend_hash_destroy(&ranges); - return result; - } else { - zend_hash_destroy(&ranges); - } - - case RANGE_NO: - if (http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) { - return http_send_status(304); - } - return http_send_chunk(data_ptr, 0, data_size, data_mode); + /* send 304 Not Modified if last modified matches */ + if (http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) { + return http_send_status(304); } + + /* send full entity */ + return http_send_chunk(data_ptr, 0, data_size, data_mode); } /* }}} */ @@ -1602,25 +1573,26 @@ PHP_HTTP_API STATUS _http_parse_headers_ex(char *header, int header_len, /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */ PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC) { - char *key = NULL; - long idx = 0; - zval array; + char *key = NULL; + long idx = 0; + zval array; - Z_ARRVAL(array) = headers; + Z_ARRVAL(array) = headers; - FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) { - if (key && !strncmp(key, "HTTP_", 5)) { - zval **header; + FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) { + if (key && !strncmp(key, "HTTP_", 5)) { + zval **header; - if (prettify) { - key = pretty_key(key + 5, strlen(key) - 5, 1, 1); - } + key += 5; + if (prettify) { + key = pretty_key(key, strlen(key), 1, 1); + } - zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header); - add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1); - key = NULL; - } - } + zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header); + add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1); + key = NULL; + } + } } /* }}} */ diff --git a/http_functions.c b/http_functions.c index 1cffa17..f42fccb 100644 --- a/http_functions.c +++ b/http_functions.c @@ -373,7 +373,7 @@ PHP_FUNCTION(http_cache_last_modified) */ PHP_FUNCTION(http_cache_etag) { - char *etag; + char *etag = NULL; int etag_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) { diff --git a/php_http_api.h b/php_http_api.h index 9c7f312..38c4802 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -45,8 +45,6 @@ typedef enum { char *pretty_key(char *key, int key_len, int uctitle, int xhyphen); /* {{{ public API */ -#define http_is_range_request() zend_hash_exists(HTTP_SERVER_VARS, "HTTP_RANGE", sizeof("HTTP_RANGE")) - #define http_date(t) _http_date((t) TSRMLS_CC) PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC); @@ -58,10 +56,10 @@ PHP_HTTP_API time_t _http_parse_date(const char *date); #define http_send_status_header(s, h) _http_send_status_header((s), (h) TSRMLS_CC) PHP_HTTP_API STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC); -#define http_get_server_var(v) _http_get_server_var((v) TSRMLS_CC) -PHP_HTTP_API zval *_http_get_server_var(const char *key TSRMLS_DC); -#define http_got_server_var(v) _http_got_server_var((v) TSRMLS_CC) -PHP_HTTP_API int _http_got_server_var(const char *key TSRMLS_DC); +#define http_got_server_var(v) (NULL != _http_get_server_var_ex((v), sizeof(v), 1 TSRMLS_CC)) +#define http_get_server_var(v) http_get_server_var_ex((v), sizeof(v)) +#define http_get_server_var_ex(v, s) _http_get_server_var_ex((v), (s), 0 TSRMLS_CC) +PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC); #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC) PHP_HTTP_API char *_http_etag(const void *data_ptr, const size_t data_len, const http_send_mode data_mode TSRMLS_DC); @@ -108,10 +106,12 @@ PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len, co #define http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port) _http_absolute_uri_ex((url), (url_len), (proto), (proto_len), (host), (host_len), (port) TSRMLS_CC) PHP_HTTP_API char *_http_absolute_uri_ex(const char *url, size_t url_len, const char *proto, size_t proto_len, const char *host, size_t host_len, unsigned port TSRMLS_DC); -#define http_negotiate_language(supported, def) _http_negotiate_q("HTTP_ACCEPT_LANGUAGE", (supported), (def) TSRMLS_CC) -#define http_negotiate_charset(supported, def) _http_negotiate_q("HTTP_ACCEPT_CHARSET", (supported), (def) TSRMLS_CC) -#define http_negotiate_q(e, s, d, t) _http_negotiate_q((e), (s), (d), (t) TSRMLS_CC) -PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported, const char *def TSRMLS_DC); +#define http_negotiate_language(zsupported, def) http_negotiate_language_ex(Z_ARRVAL_P(zsupported), (def)) +#define http_negotiate_language_ex(supported, def) http_negotiate_q("HTTP_ACCEPT_LANGUAGE", (supported), (def)) +#define http_negotiate_charset(zsupported, def) http_negotiate_charset_ex(Z_ARRVAL_P(zsupported), (def)) +#define http_negotiate_charset_ex(supported, def) http_negotiate_q("HTTP_ACCEPT_CHARSET", (supported), (def)) +#define http_negotiate_q(e, s, d) _http_negotiate_q((e), (s), (d) TSRMLS_CC) +PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *supported, const char *def TSRMLS_DC); #define http_get_request_ranges(r, l) _http_get_request_ranges((r), (l) TSRMLS_CC) PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, const size_t length TSRMLS_DC); diff --git a/php_http_std_defs.h b/php_http_std_defs.h index 6a113a7..8854b92 100644 --- a/php_http_std_defs.h +++ b/php_http_std_defs.h @@ -47,10 +47,7 @@ /* {{{ HTTP_GSP(var, name, ret) */ #define HTTP_GSP(var, name, ret) \ - if (!(var = http_get_server_var(name))) { \ - ret; \ - } \ - if (!Z_STRLEN_P(var)) { \ + if (!(var = _http_get_server_var_ex(name, strlen(name)+1, 1 TSRMLS_CC))) { \ ret; \ } /* }}} */