From: Michael Wallner Date: Mon, 1 Aug 2005 20:31:24 +0000 (+0000) Subject: - simplify http_send_header API X-Git-Tag: RELEASE_0_12_0~51 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=3177ba2e5ef642f38c01568afcaa547df65f3e74;p=m6w6%2Fext-http - simplify http_send_header API - remove HttpResponse::sendHeader() - rename HttpResponse::sendStatus() to HttpResponse::status() - add HttpResponse::setHeader() and HttpResponse::getHeader() --- diff --git a/http_cache_api.c b/http_cache_api.c index 56ca0f6..43540b3 100644 --- a/http_cache_api.c +++ b/http_cache_api.c @@ -237,7 +237,7 @@ PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len, /* just do that if desired */ if (HTTP_G(etag).started) { make_digest(etag, digest); - http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL); + http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)); http_send_etag(etag, 32); if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) { diff --git a/http_functions.c b/http_functions.c index 737ae32..ee8fba4 100644 --- a/http_functions.c +++ b/http_functions.c @@ -497,8 +497,10 @@ PHP_FUNCTION(http_redirect) } efree(URI); - if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) { - php_body_write(RED, strlen(RED) TSRMLS_CC); + if ((SUCCESS == http_send_header_string(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) { + if (SG(request_info).request_method && strcmp(SG(request_info).request_method, "HEAD")) { + PHPWRITE(RED, strlen(RED)); + } RETURN_TRUE; } RETURN_FALSE; diff --git a/http_message_api.c b/http_message_api.c index 3dc2341..8d062b1 100644 --- a/http_message_api.c +++ b/http_message_api.c @@ -329,10 +329,17 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) { if (key) { - char *header; - spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val)); - http_send_header(header); - efree(header); + if (Z_TYPE_PP(val) == IS_ARRAY) { + zend_bool first = 1; + zval **data; + + FOREACH_VAL(*val, data) { + http_send_header_ex(key, strlen(key), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first); + first = 0; + } + } else { + http_send_header_ex(key, strlen(key), Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); + } key = NULL; } } diff --git a/http_response_object.c b/http_response_object.c index 5bdbf14..77fe123 100644 --- a/http_response_object.c +++ b/http_response_object.c @@ -50,6 +50,16 @@ ZEND_EXTERN_MODULE_GLOBALS(http); #define HTTP_RESPONSE_ME(method, visibility) PHP_ME(HttpResponse, method, HTTP_ARGS(HttpResponse, method), visibility|ZEND_ACC_STATIC) #define HTTP_RESPONSE_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpResponse, method)) +HTTP_BEGIN_ARGS(setHeader, 2) + HTTP_ARG_VAL(name, 0) + HTTP_ARG_VAL(value, 0) + HTTP_ARG_VAL(replace, 0) +HTTP_END_ARGS; + +HTTP_BEGIN_ARGS(getHeader, 0) + HTTP_ARG_VAL(name, 0) +HTTP_END_ARGS; + HTTP_EMPTY_ARGS(getETag, 0); HTTP_BEGIN_ARGS(setETag, 1) HTTP_ARG_VAL(etag, 0) @@ -120,14 +130,8 @@ HTTP_BEGIN_ARGS(redirect, 0) HTTP_ARG_VAL(permanent, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(sendStatus, 1) - HTTP_ARG_VAL(status, 0) -HTTP_END_ARGS; - -HTTP_BEGIN_ARGS(sendHeader, 1) - HTTP_ARG_VAL(header, 0) - HTTP_ARG_VAL(replace, 0) - HTTP_ARG_VAL(status, 0) +HTTP_BEGIN_ARGS(status, 1) + HTTP_ARG_VAL(code, 0) HTTP_END_ARGS; HTTP_EMPTY_ARGS(getRequestHeaders, 0); @@ -139,6 +143,9 @@ static inline void _http_response_object_declare_default_properties(TSRMLS_D); zend_class_entry *http_response_object_ce; zend_function_entry http_response_object_fe[] = { + HTTP_RESPONSE_ME(setHeader, ZEND_ACC_PUBLIC) + HTTP_RESPONSE_ME(getHeader, ZEND_ACC_PUBLIC) + HTTP_RESPONSE_ME(setETag, ZEND_ACC_PUBLIC) HTTP_RESPONSE_ME(getETag, ZEND_ACC_PUBLIC) @@ -176,10 +183,7 @@ zend_function_entry http_response_object_fe[] = { HTTP_RESPONSE_ME(capture, ZEND_ACC_PUBLIC) HTTP_RESPONSE_ALIAS(redirect, http_redirect) - - HTTP_RESPONSE_ALIAS(sendStatus, http_send_status) - HTTP_RESPONSE_ALIAS(sendHeader, header) - + HTTP_RESPONSE_ALIAS(status, http_send_status) HTTP_RESPONSE_ALIAS(getRequestHeaders, http_get_request_headers) HTTP_RESPONSE_ALIAS(getRequestBody, http_get_request_body) @@ -211,10 +215,82 @@ static inline void _http_response_object_declare_default_properties(TSRMLS_D) DCL_STATIC_PROP_N(PROTECTED, contentDisposition); DCL_STATIC_PROP(PROTECTED, long, bufferSize, HTTP_SENDBUF_SIZE); DCL_STATIC_PROP(PROTECTED, double, throttleDelay, 0.0); + DCL_STATIC_PROP_N(PROTECTED, headers); } /* ### USERLAND ### */ +/* {{{ proto static bool HttpResponse::setHeader(string name, mixed value[, bool replace = true) + */ +PHP_METHOD(HttpResponse, setHeader) +{ + zend_bool replace = 1; + char *name; + int name_len = 0; + zval *value = NULL, *headers, **header; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/!|b", &name, &name_len, &value, &replace)) { + RETURN_FALSE; + } + if (!name_len) { + http_error(HE_WARNING, HTTP_E_HEADER, "Cannot send anonymous headers"); + RETURN_FALSE; + } + + USE_STATIC_PROP(); + headers = GET_STATIC_PROP(headers); + + if (Z_TYPE_P(headers) != IS_ARRAY) { + convert_to_array(headers); + } + + /* delete header if value == null */ + if (!value || Z_TYPE_P(value) == IS_NULL) { + RETURN_SUCCESS(zend_hash_del(Z_ARRVAL_P(headers), name, name_len + 1)); + } + + if (Z_TYPE_P(value) != IS_STRING) { + convert_to_string_ex(&value); + } + + /* convert old header to an array and add new one if header exists and replace == false */ + if (replace || SUCCESS != zend_hash_find(Z_ARRVAL_P(headers), name, name_len + 1, (void **) &header)) { + RETURN_SUCCESS(add_assoc_stringl_ex(headers, name, name_len + 1, Z_STRVAL_P(value), Z_STRLEN_P(value), 1)); + } else { + convert_to_array(*header); + RETURN_SUCCESS(add_next_index_stringl(*header, Z_STRVAL_P(value), Z_STRLEN_P(value), 1)); + } +} +/* }}} */ + +/* {{{ proto static mixed HttpResponse::getHeader([string name]) + */ +PHP_METHOD(HttpResponse, getHeader) +{ + char *name = NULL; + int name_len = 0; + zval *headers, **header; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len)) { + RETURN_FALSE; + } + + headers = GET_STATIC_PROP(headers); + if (Z_TYPE_P(headers) != IS_ARRAY) { + convert_to_array(headers); + } + + if (!name || !name_len) { + array_init(return_value); + array_copy(headers, return_value); + } else if (SUCCESS == zend_hash_find(Z_ARRVAL_P(headers), name, name_len + 1, (void **) &header)) { + RETURN_ZVAL(*header, ZVAL_PTR_DTOR, 1); + } else { + RETURN_NULL(); + } +} +/* }}} */ + /* {{{ proto static bool HttpResponse::setCache(bool cache) * * Whether it sould be attempted to cache the entitity. @@ -378,13 +454,12 @@ PHP_METHOD(HttpResponse, setContentDisposition) int file_len; zend_bool send_inline = 0; -#define HTTP_CONTENTDISPOSITION_TEMPLATE "%s; filename=\"%s\"" - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &send_inline)) { RETURN_FALSE; } - spprintf(&cd, 0, HTTP_CONTENTDISPOSITION_TEMPLATE, send_inline ? "inline" : "attachment", file); + spprintf(&cd, 0, "%s; filename=\"%s\"", send_inline ? "inline" : "attachment", file); + USE_STATIC_PROP(); SET_STATIC_PROP_STRING(contentDisposition, cd, 0); RETURN_TRUE; } @@ -629,7 +704,7 @@ PHP_METHOD(HttpResponse, getFile) */ PHP_METHOD(HttpResponse, send) { - zval *sent; + zval *sent, *headers; zend_bool clean_ob = 1; if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) { @@ -674,6 +749,31 @@ PHP_METHOD(HttpResponse, send) php_end_ob_buffers(0 TSRMLS_CC); } + /* custom headers */ + headers = GET_STATIC_PROP(headers); + if (Z_TYPE_P(headers) == IS_ARRAY) { + char *name = NULL; + ulong idx = 0; + zval **value; + + FOREACH_KEYVAL(headers, name, idx, value) { + if (name) { + if (Z_TYPE_PP(value) == IS_ARRAY) { + zend_bool first = 1; + zval **data; + + FOREACH_VAL(*value, data) { + http_send_header_ex(name, strlen(name), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first); + first = 0; + } + } else { + http_send_header_ex(name, strlen(name), Z_STRVAL_PP(value), Z_STRLEN_PP(value), 1); + } + name = NULL; + } + } + } + /* gzip */ if (Z_LVAL_P(GET_STATIC_PROP(gzip))) { php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC); @@ -714,11 +814,7 @@ PHP_METHOD(HttpResponse, send) { zval *cd = GET_STATIC_PROP(contentDisposition); if (Z_STRLEN_P(cd)) { - char *cds; - - spprintf(&cds, 0, "Content-Disposition: %s", Z_STRVAL_P(cd)); - http_send_header(cds); - efree(cds); + http_send_header_ex("Content-Disposition", lenof("Content-Disposition"), Z_STRVAL_P(cd), Z_STRLEN_P(cd), 1); } } diff --git a/http_send_api.c b/http_send_api.c index 73aa0b6..59b67d4 100644 --- a/http_send_api.c +++ b/http_send_api.c @@ -148,6 +148,20 @@ static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_ } /* }}} */ +/* {{{ STATUS http_send_header(char *, char *, zend_bool) */ +PHP_HTTP_API STATUS _http_send_header_ex(const char *name, size_t name_len, const char *value, size_t value_len, zend_bool replace TSRMLS_DC) +{ + STATUS ret; + size_t header_len = 1 + lenof(": ") + name_len + value_len; + char *header = emalloc(header_len); + + header[header_len - 1] = '\0'; + snprintf(header, header_len, "%s: %s", name, value); + ret = http_send_header_string_ex(header, replace); + efree(header); + return ret; +} +/* }}} */ /* {{{ STATUS http_send_status_header(int, char *) */ PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, zend_bool replace TSRMLS_DC) @@ -164,18 +178,20 @@ PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, /* {{{ STATUS http_send_last_modified(int) */ PHP_HTTP_API STATUS _http_send_last_modified(time_t t TSRMLS_DC) { - char *date = NULL; - if (date = http_date(t)) { - char modified[96] = "Last-Modified: "; - strcat(modified, date); - efree(date); - - /* remember */ - HTTP_G(send).last_modified = t; + STATUS ret; + char *date = http_date(t); - return http_send_header(modified); + if (!date) { + return FAILURE; } - return FAILURE; + + ret = http_send_header("Last-Modified", date, 1); + efree(date); + + /* remember */ + HTTP_G(send).last_modified = t; + + return ret; } /* }}} */ @@ -196,25 +212,12 @@ PHP_HTTP_API STATUS _http_send_etag(const char *etag, size_t etag_len TSRMLS_DC) etag_header = ecalloc(1, sizeof("ETag: \"\"") + etag_len); sprintf(etag_header, "ETag: \"%s\"", etag); - status = http_send_header(etag_header); + status = http_send_header_string(etag_header); efree(etag_header); return status; } /* }}} */ -/* {{{ STATUS http_send_cache_control(char *, size_t) */ -PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control, size_t cc_len TSRMLS_DC) -{ - STATUS status; - char *cc_header = ecalloc(1, sizeof("Cache-Control: ") + cc_len); - - sprintf(cc_header, "Cache-Control: %s", cache_control); - status = http_send_header(cc_header); - efree(cc_header); - return status; -} -/* }}} */ - /* {{{ STATUS http_send_content_type(char *, size_t) */ PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC) { @@ -230,11 +233,7 @@ PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_ STR_FREE(HTTP_G(send).content_type); HTTP_G(send).content_type = estrndup(content_type, ct_len); - ct_header = ecalloc(1, sizeof("Content-Type: ") + ct_len); - sprintf(ct_header, "Content-Type: %s", content_type); - status = http_send_header(ct_header); - efree(ct_header); - return status; + return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type, ct_len, 1); } /* }}} */ @@ -252,7 +251,7 @@ PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename); } - status = http_send_header(cd_header); + status = http_send_header_string(cd_header); efree(cd_header); return status; } @@ -280,7 +279,7 @@ PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_ /* send content range header */ snprintf(range_header, 255, "Content-Range: bytes %ld-%ld/%lu", **begin, **end, (ulong) size); - http_send_header(range_header); + http_send_header_string(range_header); /* send requested chunk */ return http_send_chunk(data, **begin, **end + 1, mode); @@ -297,7 +296,7 @@ PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_ /* send multipart/byteranges header */ snprintf(bound, 22, "--%lu%0.9f", (ulong) time(NULL), php_combined_lcg(TSRMLS_C)); strncat(multi_header, bound + 2, 21); - http_send_header(multi_header); + http_send_header_string(multi_header); /* send each requested chunk */ FOREACH_HASH_VAL(ranges, zrange) { @@ -355,7 +354,7 @@ PHP_HTTP_API STATUS _http_send(const void *data_ptr, size_t data_size, http_send } /* enable partial dl and resume */ - http_send_header("Accept-Ranges: bytes"); + http_send_header_string("Accept-Ranges: bytes"); zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0); range_status = http_get_request_ranges(&ranges, data_size); diff --git a/php_http.h b/php_http.h index fff7bdc..2cc6d58 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.11.0" +#define HTTP_PEXT_VERSION "0.12.0dev" /* make compile on Win32 */ #ifdef HTTP_HAVE_CURL diff --git a/php_http_response_object.h b/php_http_response_object.h index b114538..fbc1527 100644 --- a/php_http_response_object.h +++ b/php_http_response_object.h @@ -25,6 +25,8 @@ extern zend_function_entry http_response_object_fe[]; #define http_response_object_init() _http_response_object_init(INIT_FUNC_ARGS_PASSTHRU) extern void _http_response_object_init(INIT_FUNC_ARGS); +PHP_METHOD(HttpResponse, setHeader); +PHP_METHOD(HttpResponse, getHeader); PHP_METHOD(HttpResponse, setETag); PHP_METHOD(HttpResponse, getETag); PHP_METHOD(HttpResponse, setContentDisposition); diff --git a/php_http_send_api.h b/php_http_send_api.h index c9de4d4..5b9e016 100644 --- a/php_http_send_api.h +++ b/php_http_send_api.h @@ -28,8 +28,11 @@ typedef enum { } http_send_mode; #define http_send_status(s) sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (s) TSRMLS_CC) -#define http_send_header(h) http_send_status_header(0, (h)) -#define http_send_header_ex(h, r) http_send_status_header_ex(0, (h), (r)) +#define http_send_header(n, v, r) _http_send_header_ex((n), strlen(n), (v), strlen(v), (r) TSRMLS_CC) +#define http_send_header_ex(n, nl, v, vl, r) _http_send_header_ex((n), (nl), (v), (vl), (r) TSRMLS_CC) +PHP_HTTP_API STATUS _http_send_header_ex(const char *name, size_t name_len, const char *value, size_t value_len, zend_bool replace TSRMLS_DC); +#define http_send_header_string(h) _http_send_status_header_ex(0, (h), 1 TSRMLS_CC) +#define http_send_header_string_ex(h, r) _http_send_status_header_ex(0, (h), (r) TSRMLS_CC) #define http_send_status_header(s, h) _http_send_status_header_ex((s), (h), 1 TSRMLS_CC) #define http_send_status_header_ex(s, h, r) _http_send_status_header_ex((s), (h), (r) TSRMLS_CC) PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, zend_bool replace TSRMLS_DC); @@ -40,8 +43,7 @@ PHP_HTTP_API STATUS _http_send_last_modified(time_t t TSRMLS_DC); #define http_send_etag(e, l) _http_send_etag((e), (l) TSRMLS_CC) PHP_HTTP_API STATUS _http_send_etag(const char *etag, size_t etag_len TSRMLS_DC); -#define http_send_cache_control(c, l) _http_send_cache_control((c), (l) TSRMLS_CC) -PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control, size_t cc_len TSRMLS_DC); +#define http_send_cache_control(cc, cl) http_send_header_ex("Cache-Control", lenof("Cache-Control"), (cc), (cl), 1) #define http_send_content_type(c, l) _http_send_content_type((c), (l) TSRMLS_CC) PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC);