X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_env.c;h=4b9ea88313c701fea0c765544dbc9c6f376ea0b6;hp=59442fc4f35e8bc0004faf0fd0699eed1ab81893;hb=dedad9f35cbeaee56e7cca145e378cc6548198e3;hpb=d3485e3b28336153dca690e872ffe1ddc60fedd2 diff --git a/php_http_env.c b/php_http_env.c index 59442fc..4b9ea88 100644 --- a/php_http_env.c +++ b/php_http_env.c @@ -14,11 +14,10 @@ #include "php_http.h" +#include
+ PHP_RINIT_FUNCTION(http_env) { - PHP_HTTP_G->env.response.last_modified = 0; - PHP_HTTP_G->env.response.throttle_chunk = 0; - PHP_HTTP_G->env.response.throttle_delay = 0; PHP_HTTP_G->env.request.time = sapi_get_request_time(TSRMLS_C); return SUCCESS; @@ -34,11 +33,6 @@ PHP_RSHUTDOWN_FUNCTION(http_env) if (PHP_HTTP_G->env.request.body) { php_http_message_body_free(&PHP_HTTP_G->env.request.body); } - if (PHP_HTTP_G->env.response.body) { - php_http_message_body_free(&PHP_HTTP_G->env.response.body); - } - STR_SET(PHP_HTTP_G->env.response.content_type, NULL); - STR_SET(PHP_HTTP_G->env.response.etag, NULL); if (PHP_HTTP_G->env.server_var) { zval_ptr_dtor(&PHP_HTTP_G->env.server_var); @@ -89,7 +83,7 @@ PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t php_http_env_get_request_headers(NULL TSRMLS_CC); if (SUCCESS == zend_hash_find(PHP_HTTP_G->env.request.headers, key, name_len + 1, (void *) &zvalue)) { - zval *zcopy = php_http_zsep(IS_STRING, *zvalue); + zval *zcopy = php_http_ztyp(IS_STRING, *zvalue); val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy)); zval_ptr_dtor(&zcopy); @@ -159,12 +153,13 @@ PHP_HTTP_API php_http_message_body_t *php_http_env_get_request_body(TSRMLS_D) } php_stream_rewind(s); } - } else if (sapi_module.read_post) { + } else if (sapi_module.read_post && !SG(read_post_bytes)) { if ((s = php_stream_temp_new())) { char *buf = emalloc(4096); int len; while (0 < (len = sapi_module.read_post(buf, 4096 TSRMLS_CC))) { + SG(read_post_bytes) += len; php_stream_write(s, buf, len); if (len < 4096) { @@ -338,7 +333,7 @@ static void grab_headers(void *data, void *arg TSRMLS_DC) PHP_HTTP_API STATUS php_http_env_get_response_headers(HashTable *headers_ht TSRMLS_DC) { STATUS status; - php_http_buffer headers; + php_http_buffer_t headers; php_http_buffer_init(&headers); zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers TSRMLS_CC); @@ -361,7 +356,7 @@ PHP_HTTP_API char *php_http_env_get_response_header(const char *name_str, size_t char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); if (SUCCESS == zend_hash_find(&headers, key, name_len + 1, (void *) &zvalue)) { - zval *zcopy = php_http_zsep(IS_STRING, *zvalue); + zval *zcopy = php_http_ztyp(IS_STRING, *zvalue); val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy)); zval_ptr_dtor(&zcopy); @@ -410,6 +405,25 @@ PHP_HTTP_API STATUS php_http_env_set_response_header(long http_code, const char return ret; } +PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend_bool replace TSRMLS_DC, const char *fmt, ...) +{ + va_list args; + STATUS ret = FAILURE; + sapi_header_line h = {NULL, 0, http_code}; + + va_start(args, fmt); + h.line_len = vspprintf(&h.line, 0, fmt, args); + va_end(args); + + if (h.line) { + if (h.line_len) { + ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC); + } + efree(h.line); + } + return ret; +} + PHP_HTTP_API STATUS php_http_env_set_response_header_value(long http_code, const char *name_str, size_t name_len, zval *value, zend_bool replace TSRMLS_DC) { if (!value) { @@ -432,7 +446,7 @@ PHP_HTTP_API STATUS php_http_env_set_response_header_value(long http_code, const return SUCCESS; } else { - zval *data = php_http_zsep(IS_STRING, value); + zval *data = php_http_ztyp(IS_STRING, value); if (!Z_STRLEN_P(data)) { zval_ptr_dtor(&data); @@ -457,597 +471,6 @@ PHP_HTTP_API STATUS php_http_env_set_response_header_value(long http_code, const } } -PHP_HTTP_API void php_http_env_set_response_throttle_rate(zval *container, size_t chunk_size, double delay TSRMLS_CC) -{ - if (Z_TYPE_P(container) == IS_OBJECT) { - zend_update_property_double(Z_OBJCE_P(container), container, ZEND_STRL("throttleDelay"), delay TSRMLS_CC); - zend_update_property_long(Z_OBJCE_P(container), container, ZEND_STRL("throttleChunk"), chunk_size TSRMLS_CC); - } else { - convert_to_array(container); - add_assoc_double_ex(container, ZEND_STRS("throttleDelay"), delay); - add_assoc_long_ex(container, ZEND_STRS("throttleChunk"), chunk_size); - } -} - -static void set_container_value(zval *container, const char *name_str, size_t name_len, int type, const void *value_ptr, size_t value_len TSRMLS_DC) -{ - if (Z_TYPE_P(container) == IS_OBJECT) { - /* stupid non-const api */ - char *name = estrndup(name_str, name_len); - switch (type) { - case IS_LONG: - zend_update_property_long(Z_OBJCE_P(container), container, name, name_len, *(long *)value_ptr TSRMLS_CC); - break; - case IS_STRING: - zend_update_property_stringl(Z_OBJCE_P(container), container, name, name_len, value_ptr, value_len TSRMLS_CC); - break; - } - efree(name); - } else { - convert_to_array(container); - switch (type) { - case IS_LONG: - add_assoc_long_ex(container, name_str, name_len + 1, *(long *)value_ptr); - break; - case IS_STRING: { - char *value = estrndup(value_ptr, value_len); - add_assoc_stringl_ex(container, name_str, name_len + 1, value, value_len, 0); - break; - } - } - } -} - -PHP_HTTP_API STATUS php_http_env_set_response_last_modified(zval *container, time_t t, char **sent_header TSRMLS_DC) -{ - STATUS ret; - char *lm_header_str, *date; - size_t lm_header_len; - - if (t) { - if (!(date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), t, 0 TSRMLS_CC))) { - return FAILURE; - } - - lm_header_len = spprintf(&lm_header_str, 0, "Last-Modified: %s", date); - STR_FREE(date); - } else { - lm_header_str = "Last-Modified:"; - lm_header_len = lenof("Last-Modified:"); - } - - if (SUCCESS == (ret = php_http_env_set_response_header(0, lm_header_str, lm_header_len, 1 TSRMLS_CC))) { - set_container_value(container, ZEND_STRL("lastModified"), IS_LONG, &t, 0 TSRMLS_CC); - } - - if (sent_header) { - *sent_header = lm_header_str; - } else if (t) { - STR_FREE(lm_header_str); - } - - return ret; -} - -PHP_HTTP_API STATUS php_http_env_set_response_etag(zval *container, const char *etag_str, size_t etag_len, char **sent_header TSRMLS_DC) -{ - STATUS ret; - char *etag = NULL, *etag_header_str; - size_t etag_header_len; - - if (etag_len){ - etag_header_len = spprintf(&etag_header_str, 0, "ETag: \"%s\"", etag_str); - } else { - etag_header_str = "ETag:"; - etag_header_len = lenof("ETag:"); - } - - if (SUCCESS == (ret = php_http_env_set_response_header(0, etag_header_str, etag_header_len, 1 TSRMLS_CC))) { - set_container_value(container, ZEND_STRL(etag), IS_STRING, etag_str, etag_len TSRMLS_CC); - } - - if (sent_header) { - *sent_header = etag_header_str; - } else if (etag_len) { - STR_FREE(etag_header_str); - } - - return ret; -} - -PHP_HTTP_API STATUS php_http_env_set_response_content_type(zval *container, const char *ct_str, size_t ct_len, char **sent_header TSRMLS_DC) -{ - STATUS ret; - char *ct_header_str; - size_t ct_header_len; - - if (ct_len) { - PHP_HTTP_CHECK_CONTENT_TYPE(ct_str, return FAILURE); - ct_header_len = spprintf(&ct_header_str, 0, "Content-Type: %s", ct_str); - } else { - ct_header_str = "Content-Type:"; - ct_header_len = lenof("Content-Type:"); - } - - if (SUCCESS == (ret = php_http_env_set_response_header(0, ct_header_str, ct_header_len, 1 TSRMLS_CC))) { - set_container_value(container, ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC); - } - - if (sent_header) { - *sent_header = ct_header_str; - } else if (ct_len) { - STR_FREE(ct_header_str); - } - - return ret; -} - -PHP_HTTP_API STATUS php_http_env_set_response_content_disposition(zval *container, php_http_content_disposition_t d, const char *f_str, size_t f_len, char **sent_header TSRMLS_DC) -{ - STATUS ret; - char *tmp, *cd_header_str, *new_f_str; - int new_f_len; - size_t cd_header_len; - - switch (d) { - case PHP_HTTP_CONTENT_DISPOSITION_NONE: - break; - case PHP_HTTP_CONTENT_DISPOSITION_INLINE: - tmp = "inline"; - break; - case PHP_HTTP_CONTENT_DISPOSITION_ATTACHMENT: - tmp = "attachment"; - break; - default: - php_http_error(HE_WARNING, PHP_HTTP_E_INVALID_PARAM, "Unknown content disposition (%d)", (int) d); - return FAILURE; - } - - if (f_len) { - new_f_str = php_addslashes(estrndup(f_str, f_len), f_len, &new_f_len, 0 TSRMLS_CC); - cd_header_len = spprintf(&cd_header_str, 0, "Content-Disposition: %s; filename=\"%.*s\"", tmp, new_f_len, new_f_str); - STR_FREE(new_f_str); - } else if (d) { - cd_header_len = spprintf(&cd_header_str, 0, "Content-Disposition: %s", tmp); - } else { - cd_header_str = "Content-Disposition:"; - cd_header_len = lenof("Content-Disposition:"); - } - - ret = php_http_env_set_response_header(0, cd_header_str, cd_header_len, 1 TSRMLS_CC); - - if (sent_header) { - *sent_header = cd_header_str; - } else if (f_len || d){ - STR_FREE(cd_header_str); - } - - return ret; -} - -PHP_HTTP_API STATUS php_http_env_set_response_cache_control(zval *container, const char *cc_str, size_t cc_len, char **sent_header TSRMLS_DC) -{ - STATUS ret; - char *cc_header_str; - size_t cc_header_len; - - if (cc_len) { - cc_header_len = spprintf(&cc_header_str, 0, "Cache-Control: %s", cc_str); - } else { - cc_header_str = "Content-Disposition:"; - cc_header_len = lenof("Content-Disposition:"); - } - - ret = php_http_env_set_response_header(0, cc_header_str, cc_header_len, 1 TSRMLS_CC); - - if (sent_header) { - *sent_header = cc_header_str; - } else if (cc_len) { - STR_FREE(cc_header_str); - } - - return ret; -} - -static zval *get_container_value(zval *container, const char *name_str, size_t name_len TSRMLS_CC) -{ - zval *val, **valptr; - - if (Z_TYPE_P(container) == IS_OBJECT) { - char *name = estrndup(name_str, name_len); - val = zend_read_property(Z_OBJCE_P(container), container, name, name_len, 0 TSRMLS_CC); - efree(name); - } else { - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(container), name_str, name_len + 1, (void *) &valptr)) { - val = *valptr; - } else { - val = NULL; - } - } - if (val) { - Z_ADDREF_P(val); - } - return val; -} - -PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *container, const char *header_str, size_t header_len TSRMLS_DC) -{ - int ret, free_etag = 0; - char *header, *etag; - zval *zetag, *zbody = NULL; - - if ( !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC)) - || !(zbody = get_container_value(container, ZEND_STRL("body") TSRMLS_CC)) - || !(Z_TYPE_P(zbody) == IS_OBJECT) - || !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC) - ) { - STR_FREE(header); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return PHP_HTTP_CACHE_NO; - } - - if ((zetag = get_container_value(container, ZEND_STRL("etag") TSRMLS_CC))) { - zval *zetag_copy = php_http_zsep(IS_STRING, zetag); - zval_ptr_dtor(&zetag); - zetag = zetag_copy; - } - - if (zetag && Z_STRLEN_P(zetag)) { - etag = Z_STRVAL_P(zetag); - } else { - etag = php_http_message_body_etag(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body); - php_http_env_set_response_etag(container, etag, strlen(etag), NULL TSRMLS_CC); - free_etag = 1; - } - - if (zetag) { - zval_ptr_dtor(&zetag); - } - - ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD); - - if (free_etag) { - efree(etag); - } - efree(header); - - return ret ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS; -} - -PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *container, const char *header_str, size_t header_len TSRMLS_DC) -{ - char *header; - time_t ums, lm = 0; - zval *zbody = NULL, *zlm; - - if ( !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC)) - || !(zbody = get_container_value(container, ZEND_STRL("body") TSRMLS_CC)) - || !(Z_TYPE_P(zbody) == IS_OBJECT) - || !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC) - ) { - STR_FREE(header); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return PHP_HTTP_CACHE_NO; - } - - if ((zlm = get_container_value(container, ZEND_STRL("lastModified") TSRMLS_CC))) { - zval *zlm_copy = php_http_zsep(IS_LONG, zlm); - zval_ptr_dtor(&zlm); - zlm = zlm_copy; - } - - if (zlm && Z_LVAL_P(zlm) > 0) { - lm = Z_LVAL_P(zlm); - } else { - lm = php_http_message_body_mtime(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body); - php_http_env_set_response_last_modified(container, lm, NULL TSRMLS_CC); - } - - if (zlm) { - zval_ptr_dtor(&zlm); - } - - ums = php_parse_date(header, NULL TSRMLS_CC); - efree(header); - - if (ums > 0 && ums <= lm) { - return PHP_HTTP_CACHE_HIT; - } else { - return PHP_HTTP_CACHE_MISS; - } -} - -PHP_HTTP_API void php_http_env_set_response_body(zval *container, php_http_message_body_t *body) -{ - TSRMLS_FETCH_FROM_CTX(body->ts); - zend_object_value ov = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_copy(body, NULL, 0), NULL TSRMLS_CC); - - set_container_value(container, ZEND_STRL("body"), IS_OBJECT, &ov, 0 TSRMLS_CC); -} - -struct output_ctx { - php_http_buffer *buf; - zval *container; -}; - -static size_t output(void *context, const char *buf, size_t len TSRMLS_DC) -{ - struct output_ctx *ctx = context; - - if (ctx->buf) { - zval *zcs; - size_t chunk_size = PHP_HTTP_SENDBUF_SIZE; - - if ((zcs = get_container_value(ctx->container, ZEND_STRL("throttleChunk") TSRMLS_CC))) { - zval *zcs_copy = php_http_zsep(IS_LONG, zcs); - - zval_ptr_dtor(&zcs); - chunk_size = Z_LVAL_P(zcs_copy); - zval_ptr_dtor(&zcs_copy); - } - php_http_buffer_chunked_output(&ctx->buf, buf, len, buf ? chunk_size : 0, output, NULL TSRMLS_CC); - } else { - zval *ztd; - - - PHPWRITE(buf, len); - - /* we really only need to flush when throttling is enabled, - because we push the data as fast as possible anyway if not */ - if ((ztd = get_container_value(ctx->container, ZEND_STRL("throttleDelay") TSRMLS_CC))) { - double delay; - zval *ztd_copy = php_http_zsep(IS_DOUBLE, ztd); - - zval_ptr_dtor(&ztd); - delay = Z_DVAL_P(ztd_copy); - zval_ptr_dtor(&ztd_copy); - - if (delay >= PHP_HTTP_DIFFSEC) { - if (php_output_get_level(TSRMLS_C)) { - php_output_flush_all(TSRMLS_C); - } - if (!(php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH)) { - sapi_flush(TSRMLS_C); - } - php_http_sleep(delay); - } - } - } - return len; -} - -PHP_HTTP_API STATUS php_http_env_send_response(zval *container TSRMLS_DC) -{ - struct output_ctx ctx = {NULL, container}; - zval *zbody, *zheader, *zrcode, *zversion; - HashTable ranges; - php_http_range_status_t range_status; - php_http_message_body_t *body; - size_t body_size; - - if ( !(zbody = get_container_value(container, ZEND_STRL("body") TSRMLS_CC)) - || !(Z_TYPE_P(zbody) == IS_OBJECT) - || !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC) - ) { - if (zbody) { - zval_ptr_dtor(&zbody); - } - return FAILURE; - } - - if ((zrcode = get_container_value(container, ZEND_STRL("responseCode") TSRMLS_CC))) { - zval *zrcode_copy = php_http_zsep(IS_LONG, zrcode); - - zval_ptr_dtor(&zrcode); - if (Z_LVAL_P(zrcode_copy) > 0) { - php_http_env_set_response_code(Z_LVAL_P(zrcode_copy) TSRMLS_CC); - } - zval_ptr_dtor(&zrcode_copy); - } - - if ((zversion = get_container_value(container, ZEND_STRL("httpVersion") TSRMLS_CC))) { - php_http_version_t v; - zval *zversion_copy = php_http_zsep(IS_STRING, zversion); - - zval_ptr_dtor(&zversion); - if (Z_STRLEN_P(zversion_copy) && php_http_version_parse(&v, Z_STRVAL_P(zversion_copy) TSRMLS_CC)) { - php_http_env_set_response_protocol_version(&v TSRMLS_CC); - php_http_version_dtor(&v); - } - zval_ptr_dtor(&zversion_copy); - } - - if ((zheader = get_container_value(container, ZEND_STRL("headers") TSRMLS_CC))) { - if (Z_TYPE_P(zheader) == IS_ARRAY) { - zval **val; - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - - FOREACH_KEYVAL(pos, zheader, key, val) { - if (key.type == HASH_KEY_IS_STRING) { - php_http_env_set_response_header_value(0, key.str, key.len - 1, *val, 1 TSRMLS_CC); - } - } - } - zval_ptr_dtor(&zheader); - } - - body = ((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body; - body_size = php_http_message_body_size(body); - php_http_env_set_response_header(0, ZEND_STRL("Accept-Ranges: bytes"), 1 TSRMLS_CC); - zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0); - range_status = php_http_env_get_request_ranges(&ranges, body_size TSRMLS_CC); - - switch (range_status) { - case PHP_HTTP_RANGE_ERR: - zend_hash_destroy(&ranges); - if (!php_http_env_got_request_header(ZEND_STRL("If-Range") TSRMLS_CC)) { - char *cr_header_str; - size_t cr_header_len; - - cr_header_len = spprintf(&cr_header_str, 0, "Content-Range: bytes */%zu", body_size); - php_http_env_set_response_header(416, cr_header_str, cr_header_len, 1 TSRMLS_CC); - efree(cr_header_str); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return SUCCESS; - } - break; - - case PHP_HTTP_RANGE_NO: - /* send full entity */ - zend_hash_destroy(&ranges); - break; - - case PHP_HTTP_RANGE_OK: - /* send content-range response */ - if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(container, ZEND_STRL("If-Range") TSRMLS_CC) - || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("If-Range") TSRMLS_CC) - ) { - /* send full entity */ - zend_hash_destroy(&ranges); - break; - } - if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(container, ZEND_STRL("If-Match") TSRMLS_CC) - || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("If-Unmodified-Since") TSRMLS_CC) - || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("Unless-Modified-Since") TSRMLS_CC) - ) { - zend_hash_destroy(&ranges); - php_http_env_set_response_code(412 TSRMLS_CC); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return SUCCESS; - } - if (zend_hash_num_elements(&ranges) == 1) { - /* single range */ - zval **range, **begin, **end; - - if (SUCCESS != zend_hash_index_find(&ranges, 0, (void *) &range) - || SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void *) &begin) - || SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void *) &end) - ) { - /* this should never happen */ - zend_hash_destroy(&ranges); - php_http_env_set_response_code(500 TSRMLS_CC); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return FAILURE; - } else { - char *cr_header_str; - size_t cr_header_len; - - cr_header_len = spprintf(&cr_header_str, 0, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), body_size); - php_http_env_set_response_header(206, cr_header_str, cr_header_len, 1 TSRMLS_CC); - efree(cr_header_str); - - /* send chunk */ - php_http_message_body_to_callback(body, output, &ctx, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); - output(&ctx, NULL, 0 TSRMLS_CC); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return SUCCESS; - } - } else { - /* send multipart/byte-ranges message */ - HashPosition pos; - zval **chunk, *zct; - php_http_buffer preface; - int free_ct = 0; - char *content_type = "application/octet-stream"; - char boundary[32], *ct_header_str = "Content-Type: multipart/byteranges; boundary= "; - - if ((zct = get_container_value(container, ZEND_STRL("contentType") TSRMLS_CC))) { - zval *zct_copy = php_http_zsep(IS_STRING, zct); - - zval_ptr_dtor(&zct); - if (Z_STRLEN_P(zct_copy)) { - content_type = estrndup(Z_STRVAL_P(zct_copy), Z_STRLEN_P(zct_copy)); - free_ct = 1; - } - - zval_ptr_dtor(&zct); - } - - php_http_boundary(boundary, sizeof(boundary)); - strlcpy(&ct_header_str[45], boundary, 32); - - php_http_env_set_response_header(206, ct_header_str, strlen(ct_header_str), 1 TSRMLS_CC); - - php_http_buffer_init(&preface); - FOREACH_HASH_VAL(pos, &ranges, chunk) { - zval **begin, **end; - - if (IS_ARRAY == Z_TYPE_PP(chunk) - && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 0, (void *) &begin) - && IS_LONG == Z_TYPE_PP(begin) - && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 1, (void *) &end) - && IS_LONG == Z_TYPE_PP(end) - ) { - php_http_buffer_appendf(&preface, - PHP_HTTP_CRLF - "--%s" PHP_HTTP_CRLF - "Content-Type: %s" PHP_HTTP_CRLF - "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF, - /* - */ - boundary, - content_type, - Z_LVAL_PP(begin), - Z_LVAL_PP(end), - body_size - ); - php_http_buffer_fix(&preface); - output(&ctx, PHP_HTTP_BUFFER_VAL(&preface), PHP_HTTP_BUFFER_LEN(&preface) TSRMLS_CC); - php_http_buffer_reset(&preface); - - php_http_message_body_to_callback(body, output, &ctx, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); - } - } - php_http_buffer_appendf(&preface, PHP_HTTP_CRLF "--%s--", boundary); - php_http_buffer_fix(&preface); - output(&ctx, PHP_HTTP_BUFFER_VAL(&preface), PHP_HTTP_BUFFER_LEN(&preface) TSRMLS_CC); - php_http_buffer_dtor(&preface); - output(&ctx, NULL, 0 TSRMLS_CC); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return SUCCESS; - } - break; - } - - switch (php_http_env_is_response_cached_by_etag(container, ZEND_STRL("If-None-Match"))) { - case PHP_HTTP_CACHE_MISS: - break; - - case PHP_HTTP_CACHE_NO: - if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("If-Modified-Since"))) { - break; - } - - case PHP_HTTP_CACHE_HIT: - php_http_env_set_response_code(304 TSRMLS_CC); - if (zbody) { - zval_ptr_dtor(&zbody); - } - return SUCCESS; - } - - php_http_message_body_to_callback(body, output, &ctx, 0, 0); - output(&ctx, NULL, 0 TSRMLS_CC); - - if (zbody) { - zval_ptr_dtor(&zbody); - } - return SUCCESS; -} static PHP_HTTP_STRLIST(php_http_env_response_status) = PHP_HTTP_STRLIST_ITEM("Continue") @@ -1138,22 +561,22 @@ PHP_HTTP_BEGIN_ARGS(setResponseCode, 1) PHP_HTTP_ARG_VAL(code, 0) PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(negotiateLanguage, 0) +PHP_HTTP_BEGIN_ARGS(negotiateLanguage, 1) PHP_HTTP_ARG_VAL(supported, 0) PHP_HTTP_ARG_VAL(result_array, 1) PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(negotiateContentType, 0) +PHP_HTTP_BEGIN_ARGS(negotiateContentType, 1) PHP_HTTP_ARG_VAL(supported, 0) PHP_HTTP_ARG_VAL(result_array, 1) PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(negotiateCharset, 0) +PHP_HTTP_BEGIN_ARGS(negotiateCharset, 1) PHP_HTTP_ARG_VAL(supported, 0) PHP_HTTP_ARG_VAL(result_array, 1) PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(negotiate, 0) +PHP_HTTP_BEGIN_ARGS(negotiate, 2) PHP_HTTP_ARG_VAL(value, 0) PHP_HTTP_ARG_VAL(supported, 0) PHP_HTTP_ARG_VAL(result_array, 1) @@ -1163,12 +586,18 @@ PHP_HTTP_EMPTY_ARGS(persistentHandlesStat); PHP_HTTP_BEGIN_ARGS(persistentHandlesClean, 0) PHP_HTTP_ARG_VAL(name, 0) + PHP_HTTP_ARG_VAL(ident, 0) PHP_HTTP_END_ARGS; PHP_HTTP_BEGIN_ARGS(persistentHandlesIdent, 0) PHP_HTTP_ARG_VAL(name, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(parseParams, 1) + PHP_HTTP_ARG_VAL(params, 0) + PHP_HTTP_ARG_VAL(flags, 0) +PHP_HTTP_END_ARGS; + zend_function_entry php_http_env_method_entry[] = { PHP_HTTP_ENV_ME(getRequestHeader) PHP_HTTP_ENV_ME(getRequestBody) @@ -1187,7 +616,8 @@ zend_function_entry php_http_env_method_entry[] = { PHP_HTTP_ENV_ME(persistentHandlesStat) PHP_HTTP_ENV_ME(persistentHandlesClean) - PHP_HTTP_ENV_ME(persistentHandlesIdent) + + PHP_HTTP_ENV_ME(parseParams) EMPTY_FUNCTION_ENTRY }; @@ -1216,7 +646,7 @@ PHP_METHOD(HttpEnv, getRequestHeader) PHP_METHOD(HttpEnv, getRequestBody) { - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { zend_class_entry *class_entry = php_http_message_body_class_entry; if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &class_entry)) { @@ -1314,7 +744,7 @@ PHP_METHOD(HttpEnv, setResponseCode) zval **value_ptr; \ \ FOREACH_HASH_VAL(pos, supported, value_ptr) { \ - zval *value = php_http_zsep(IS_STRING, *value_ptr); \ + zval *value = php_http_ztyp(IS_STRING, *value_ptr); \ add_assoc_double(rs_array, Z_STRVAL_P(value), 1.0); \ zval_ptr_dtor(&value); \ } \ @@ -1343,7 +773,7 @@ PHP_METHOD(HttpEnv, setResponseCode) #define PHP_HTTP_DO_NEGOTIATE(type, supported, rs_array) \ { \ HashTable *result; \ - if ((result = php_http_negotiate_ ##type(supported))) { \ + if ((result = php_http_negotiate_ ##type(supported TSRMLS_CC))) { \ PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \ } else { \ PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \ @@ -1411,7 +841,7 @@ PHP_METHOD(HttpEnv, negotiate) array_init(rs_array); } - if ((rs = php_http_negotiate(value_str, supported, php_http_negotiate_default_func))) { + if ((rs = php_http_negotiate(value_str, supported, php_http_negotiate_default_func TSRMLS_CC))) { PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array); } else { PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); @@ -1424,7 +854,7 @@ PHP_METHOD(HttpEnv, persistentHandlesStat) { if (SUCCESS == zend_parse_parameters_none()) { object_init(return_value); - if (php_http_persistent_handle_statall(HASH_OF(return_value))) { + if (php_http_persistent_handle_statall(HASH_OF(return_value) TSRMLS_CC)) { return; } zval_dtor(return_value); @@ -1434,27 +864,29 @@ PHP_METHOD(HttpEnv, persistentHandlesStat) PHP_METHOD(HttpEnv, persistentHandlesClean) { - char *name_str = NULL; - int name_len = 0; + char *name_str = NULL, *ident_str = NULL; + int name_len = 0, ident_len = 0; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name_str, &name_len)) { - php_http_persistent_handle_cleanup(name_str, name_len, 1 TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &name_str, &name_len, &ident_str, &ident_len)) { + php_http_persistent_handle_cleanup(name_str, name_len, ident_str, ident_len TSRMLS_CC); } } -PHP_METHOD(HttpEnv, persistentHandlesIdent) +PHP_METHOD(HttpEnv, parseParams) { - char *ident_str = NULL; - int ident_len = 0; + char *param_str; + int param_len; + long flags = PHP_HTTP_PARAMS_DEFAULT; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ident_str, &ident_len)) { - RETVAL_STRING(zend_ini_string(ZEND_STRS("http.persistent.handles.ident"), 0), 1); - if (ident_str && ident_len) { - zend_alter_ini_entry(ZEND_STRS("http.persistent.handles.ident"), ident_str, ident_len, ZEND_INI_USER, PHP_INI_STAGE_RUNTIME); - } - return; + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", ¶m_str, ¶m_len, &flags)) { + RETURN_FALSE; + } + + array_init(return_value); + if (SUCCESS != php_http_params_parse(param_str, flags, php_http_params_parse_default_func, Z_ARRVAL_P(return_value) TSRMLS_CC)) { + zval_dtor(return_value); + RETURN_FALSE; } - RETURN_FALSE; } zend_class_entry *php_http_env_request_class_entry; @@ -1475,220 +907,28 @@ zend_function_entry php_http_env_request_method_entry[] = { PHP_METHOD(HttpEnvRequest, __construct) { - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { if (SUCCESS == zend_parse_parameters_none()) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { obj->message = php_http_message_init_env(obj->message, PHP_HTTP_REQUEST TSRMLS_CC); } end_error_handling(); } } end_error_handling(); } - -zend_class_entry *php_http_env_response_class_entry; - -#undef PHP_HTTP_BEGIN_ARGS -#undef PHP_HTTP_EMPTY_ARGS -#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEnvResponse, method, 0, req_args) -#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEnvResponse, method, 0) -#define PHP_HTTP_ENV_RESPONSE_ME(method, visibility) PHP_ME(HttpEnvResponse, method, PHP_HTTP_ARGS(HttpEnvResponse, method), visibility) - -PHP_HTTP_EMPTY_ARGS(__construct); - -PHP_HTTP_BEGIN_ARGS(setContentType, 1) - PHP_HTTP_ARG_VAL(content_type, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(setContentDisposition, 1) - PHP_HTTP_ARG_VAL(content_disposition, 0) - PHP_HTTP_ARG_VAL(filename, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(setCacheControl, 1) - PHP_HTTP_ARG_VAL(cache_control, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(setLastModified, 1) - PHP_HTTP_ARG_VAL(last_modified, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(isCachedByLastModified, 0) - PHP_HTTP_ARG_VAL(header_name, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(setEtag, 1) - PHP_HTTP_ARG_VAL(etag, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(isCachedByEtag, 0) - PHP_HTTP_ARG_VAL(header_name, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(setThrottleRate, 1) - PHP_HTTP_ARG_VAL(chunk_size, 0) - PHP_HTTP_ARG_VAL(delay, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(send); - - -zend_function_entry php_http_env_response_method_entry[] = { - PHP_HTTP_ENV_RESPONSE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) - PHP_HTTP_ENV_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(setCacheControl, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(setLastModified, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(isCachedByLastModified, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(setEtag, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(isCachedByEtag, ZEND_ACC_PUBLIC) - PHP_HTTP_ENV_RESPONSE_ME(setThrottleRate, ZEND_ACC_PUBLIC) - - PHP_HTTP_ENV_RESPONSE_ME(send, ZEND_ACC_PUBLIC) - - EMPTY_FUNCTION_ENTRY -}; - - -PHP_METHOD(HttpEnvResponse, __construct) -{ - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { - if (SUCCESS == zend_parse_parameters_none()) { - php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) { - obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC); - } end_error_handling(); - } - } end_error_handling(); - -} - -PHP_METHOD(HttpEnvResponse, setContentType) -{ - char *ct_str; - int ct_len; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ct_str, &ct_len)) { - RETURN_SUCCESS(php_http_env_set_response_content_type(getThis(), ct_str, ct_len, NULL TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, setContentDisposition) -{ - long cd; - char *file_str = NULL; - int file_len = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s!", &cd, &file_str, &file_len)) { - RETURN_SUCCESS(php_http_env_set_response_content_disposition(getThis(), cd, file_str, file_len, NULL TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, setCacheControl) -{ - char *cc_str; - int cc_len; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cc_str, &cc_len)) { - RETURN_SUCCESS(php_http_env_set_response_cache_control(getThis(), cc_str, cc_len, NULL TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, setLastModified) -{ - long last_modified; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified)) { - RETURN_SUCCESS(php_http_env_set_response_last_modified(getThis(), last_modified, NULL TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, isCachedByLastModified) -{ - char *header_name_str = NULL; - int header_name_len = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) { - if (!header_name_str || !header_name_len) { - header_name_str = "If-Modified-Since"; - header_name_len = lenof("If-Modified-Since"); - } - RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, setEtag) -{ - char *etag_str; - int etag_len; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len)) { - RETURN_SUCCESS(php_http_env_set_response_etag(getThis(), etag_str, etag_len, NULL TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, isCachedByEtag) -{ - char *header_name_str = NULL; - int header_name_len = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header_name_str, &header_name_len)) { - if (!header_name_str || !header_name_len) { - header_name_str = "If-None-Match"; - header_name_len = lenof("If-None-Match"); - } - RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len TSRMLS_CC)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, setThrottleRate) -{ - long chunk_size; - double delay = 1; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay)) { - php_http_env_set_response_throttle_rate(getThis(), chunk_size, delay TSRMLS_CC); - RETURN_TRUE; - } - RETURN_FALSE; -} - -PHP_METHOD(HttpEnvResponse, send) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_SUCCESS(php_http_env_send_response(getThis() TSRMLS_CC)); - } - RETURN_FALSE; -} - - PHP_MINIT_FUNCTION(http_env) { PHP_HTTP_REGISTER_CLASS(http, Env, http_env, NULL, 0); - PHP_HTTP_REGISTER_CLASS(http\\env, Request, http_env_request, php_http_message_class_entry, 0); - PHP_HTTP_REGISTER_CLASS(http\\env, Response, http_env_response, php_http_message_class_entry, 0); - zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_INLINE"), PHP_HTTP_CONTENT_DISPOSITION_INLINE TSRMLS_CC); - zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_ATTACHMENT"), PHP_HTTP_CONTENT_DISPOSITION_ATTACHMENT TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_ALLOW_COMMA"), PHP_HTTP_PARAMS_ALLOW_COMMA TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_ALLOW_FAILURE"), PHP_HTTP_PARAMS_ALLOW_FAILURE TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_RAISE_ERROR"), PHP_HTTP_PARAMS_RAISE_ERROR TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_COLON_SEPARATOR"), PHP_HTTP_PARAMS_COLON_SEPARATOR TSRMLS_CC); - zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC); - zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC); - zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC); - - zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC); + PHP_HTTP_REGISTER_CLASS(http\\env, Request, http_env_request, php_http_message_class_entry, 0); return SUCCESS; }