/* 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)) {
}
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;
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;
}
}
#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)
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);
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)
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)
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.
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;
}
*/
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)) {
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);
{
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);
}
}
}
/* }}} */
+/* {{{ 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)
/* {{{ 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;
}
/* }}} */
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)
{
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);
}
/* }}} */
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;
}
/* 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);
/* 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) {
}
/* 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);
#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
#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);
} 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);
#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);