X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=php_http_env_response.c;h=e6ae66d2a30bdbbd5916cdaba7c328daee5f83e4;hb=1421b2b78469a66ebe3cd2958a608de9a036bca9;hp=40fa9412ce4008500fdab33fc213243c510fe451;hpb=4d708279b6956fc95b253ddc88671fb2f0e5aa39;p=m6w6%2Fext-http diff --git a/php_http_env_response.c b/php_http_env_response.c index 40fa941..e6ae66d 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -6,18 +6,11 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2010, Michael Wallner | + | Copyright (c) 2004-2011, Michael Wallner | +--------------------------------------------------------------------+ */ -/* $Id $ */ - -#include "php_http.h" - -#include
-#include -#include - +#include "php_http_api.h" static void set_option(zval *options, const char *name_str, size_t name_len, int type, const void *value_ptr, size_t value_len TSRMLS_DC) { @@ -61,7 +54,7 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int } } } -static zval *get_option(zval *options, const char *name_str, size_t name_len TSRMLS_CC) +static zval *get_option(zval *options, const char *name_str, size_t name_len TSRMLS_DC) { zval *val, **valptr; @@ -70,7 +63,7 @@ static zval *get_option(zval *options, const char *name_str, size_t name_len TSR val = zend_read_property(Z_OBJCE_P(options), options, name, name_len, 0 TSRMLS_CC); efree(name); } else { - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) { + if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) { val = *valptr; } else { val = NULL; @@ -84,12 +77,11 @@ static zval *get_option(zval *options, const char *name_str, size_t name_len TSR PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len TSRMLS_DC) { - int ret, free_etag = 0; + int ret = 0, 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_option(options, ZEND_STRL("body") TSRMLS_CC)) + if ( !(zbody = get_option(options, 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) ) { @@ -108,8 +100,7 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zva 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); + } else if ((etag = php_http_message_body_etag(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body))) { set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag) TSRMLS_CC); free_etag = 1; } @@ -122,12 +113,14 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zva zval_ptr_dtor(&zetag); } - ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD); + if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL TSRMLS_CC))) { + ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD); + } if (free_etag) { efree(etag); } - efree(header); + STR_FREE(header); return ret ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS; } @@ -138,7 +131,7 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_mod time_t ums, lm = 0; zval *zbody = NULL, *zlm; - if ( !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC)) + if ( !(header = php_http_env_get_request_header(header_str, header_len, NULL TSRMLS_CC)) || !(zbody = get_option(options, 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) @@ -167,7 +160,7 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_mod zval_ptr_dtor(&zlm); } - ums = php_parse_date(header, NULL TSRMLS_CC); + ums = php_parse_date(header, NULL); efree(header); if (ums > 0 && ums <= lm) { @@ -180,7 +173,6 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_mod static size_t output(void *context, const char *buf, size_t len TSRMLS_DC) { php_http_env_response_t *r = context; - TSRMLS_FETCH_FROM_CTX(r->ts); PHPWRITE(buf, len); @@ -363,7 +355,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) ret = php_http_env_set_response_header_format(206, 1 TSRMLS_CC, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length); } } else { - php_http_boundary(r->range.boundary, sizeof(r->range.boundary)); + php_http_boundary(r->range.boundary, sizeof(r->range.boundary) TSRMLS_CC); ret = php_http_env_set_response_header_format(206, 1 TSRMLS_CC, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary); } } else { @@ -393,6 +385,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) switch (Z_LVAL_P(zdisposition_copy)) { case PHP_HTTP_CONTENT_DISPOSITION_NONE: ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Disposition"), NULL, 1 TSRMLS_CC); + break; case PHP_HTTP_CONTENT_DISPOSITION_INLINE: tmp = "inline"; break; @@ -411,7 +404,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s", tmp); } else { int new_f_len; - char *new_f_str = php_addslashes(estrndup(Z_STRVAL_P(zfilename_copy), Z_STRLEN_P(zfilename_copy)), Z_STRLEN_P(zfilename_copy), &new_f_len, 0 TSRMLS_CC); + char *new_f_str = php_addslashes(estrndup(Z_STRVAL_P(zfilename_copy), Z_STRLEN_P(zfilename_copy)), Z_STRLEN_P(zfilename_copy), &new_f_len, 1 TSRMLS_CC); ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s; filename=\"%.*s\"", tmp, new_f_len, new_f_str); STR_FREE(new_f_str); @@ -450,13 +443,13 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) zend_hash_internal_pointer_reset(result); if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key_str, &key_len, NULL, 0, NULL)) { if (!strcmp(key_str, "gzip")) { - if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP))) { + if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC))) { ret = FAILURE; } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: gzip"), 1 TSRMLS_CC))) { r->content.encoding = estrndup(key_str, key_len - 1); } } else if (!strcmp(key_str, "deflate")) { - if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB))) { + if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC))) { ret = FAILURE; } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: deflate"), 1 TSRMLS_CC))) { r->content.encoding = estrndup(key_str, key_len - 1); @@ -489,14 +482,15 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) return ret; } - switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"))) { + switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match") TSRMLS_CC)) { 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(options, ZEND_STRL("If-Modified-Since"))) { + if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since") TSRMLS_CC)) { break; } + /* no break */ case PHP_HTTP_CACHE_HIT: ret = php_http_env_set_response_code(304 TSRMLS_CC); @@ -505,8 +499,15 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) } if ((zoption = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) { - ret = php_http_env_set_response_header_value(0, ZEND_STRL("ETag"), zoption, 1 TSRMLS_CC); + zval *zoption_copy = php_http_ztyp(IS_STRING, zoption); + zval_ptr_dtor(&zoption); + if (*Z_STRVAL_P(zoption_copy) != '"' && strncmp(Z_STRVAL_P(zoption_copy), "W/\"", 3)) { + ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy)); + } else { + ret = php_http_env_set_response_header_value(0, ZEND_STRL("ETag"), zoption_copy, 1 TSRMLS_CC); + } + zval_ptr_dtor(&zoption_copy); } if ((zoption = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) { zval *zoption_copy = php_http_ztyp(IS_LONG, zoption); @@ -530,6 +531,7 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) { STATUS ret = SUCCESS; zval *zbody; + TSRMLS_FETCH_FROM_CTX(r->ts); if (r->done) { return ret; @@ -609,6 +611,7 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) { zval *zbody; + TSRMLS_FETCH_FROM_CTX(r->ts); /* check for ranges */ if ( (zbody = get_option(r->options, ZEND_STRL("body") TSRMLS_CC)) @@ -663,6 +666,8 @@ PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) } break; } + + break; } } } else if (zbody) { @@ -787,7 +792,7 @@ PHP_METHOD(HttpEnvResponse, setContentDisposition) if (file_len) { add_assoc_stringl_ex(arr, ZEND_STRS("filename"), file_str, file_len, 1); } - zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), arr); + zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), arr TSRMLS_CC); zval_ptr_dtor(&arr); } } @@ -850,7 +855,7 @@ 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 (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"); @@ -892,7 +897,7 @@ PHP_METHOD(HttpEnvResponse, send) PHP_MINIT_FUNCTION(http_env_response) { - PHP_HTTP_REGISTER_CLASS(http\\env, Response, http_env_response, 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_NONE"), PHP_HTTP_CONTENT_DISPOSITION_NONE TSRMLS_CC); zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_INLINE"), PHP_HTTP_CONTENT_DISPOSITION_INLINE TSRMLS_CC);