From: Michael Wallner Date: Thu, 20 Dec 2012 16:44:01 +0000 (+0000) Subject: add stream support for http\Env\Response X-Git-Tag: RELEASE_2_1_0_RC3~10^2^2~58^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=4ebb73feef38aea0560abf7c3a9435dda4d77820;p=m6w6%2Fext-http add stream support for http\Env\Response tests++ --- diff --git a/php_http_api.h b/php_http_api.h index ca1e2f7..f48d464 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -79,6 +79,8 @@ typedef int STATUS; #include "php_http.h" #include "php_http_cookie.h" #include "php_http_encoding.h" +#include "php_http_info.h" +#include "php_http_message.h" #include "php_http_env.h" #include "php_http_env_request.h" #include "php_http_env_response.h" @@ -87,8 +89,6 @@ typedef int STATUS; #include "php_http_filter.h" #include "php_http_header_parser.h" #include "php_http_headers.h" -#include "php_http_info.h" -#include "php_http_message.h" #include "php_http_message_body.h" #include "php_http_message_parser.h" #include "php_http_negotiate.h" diff --git a/php_http_buffer.c b/php_http_buffer.c index 5a609be..68e9694 100644 --- a/php_http_buffer.c +++ b/php_http_buffer.c @@ -131,68 +131,6 @@ PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const return append_len; } -PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset) -{ - if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, insert_len)) { - return PHP_HTTP_BUFFER_NOMEM; - } - memmove(buf->data + offset + insert_len, buf->data + offset, insert_len); - memcpy(buf->data + offset, insert, insert_len); - buf->used += insert_len; - buf->free -= insert_len; - return insert_len; -} - -PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) -{ - va_list argv; - char *insert; - size_t insert_len, alloc; - - va_start(argv, format); - insert_len = vspprintf(&insert, 0, format, argv); - va_end(argv); - - alloc = php_http_buffer_insert(buf, insert, insert_len, offset); - efree(insert); - - if (PHP_HTTP_BUFFER_NOMEM == alloc) { - return PHP_HTTP_BUFFER_NOMEM; - } - return insert_len; -} - -PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len) -{ - if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, prepend_len)) { - return PHP_HTTP_BUFFER_NOMEM; - } - memmove(buf->data + prepend_len, buf->data, buf->used); - memcpy(buf->data, prepend, prepend_len); - buf->used += prepend_len; - buf->free -= prepend_len; - return prepend_len; -} - -PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) -{ - va_list argv; - char *prepend; - size_t prepend_len, alloc; - - va_start(argv, format); - prepend_len = vspprintf(&prepend, 0, format, argv); - va_end(argv); - - alloc = php_http_buffer_prepend(buf, prepend, prepend_len); - efree(prepend); - - if (PHP_HTTP_BUFFER_NOMEM == alloc) { - return PHP_HTTP_BUFFER_NOMEM; - } - return prepend_len; -} - PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len) { char *copy = ecalloc(1, buf->used + 1); @@ -206,22 +144,6 @@ PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, cha return copy; } -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to) -{ - int free_to = !to; - - to = php_http_buffer_clone(from, to); - - if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(to, from->data, from->used)) { - if (free_to) { - php_http_buffer_free(&to); - } else { - php_http_buffer_dtor(to); - } - } - return to; -} - PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length) { if (offset > buf->used) { @@ -236,73 +158,6 @@ PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t of return length; } -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t length) -{ - if (offset >= buf->used) { - return NULL; - } else { - size_t need = 1 + ((length + offset) > buf->used ? (buf->used - offset) : (length - offset)); - php_http_buffer_t *sub = php_http_buffer_init_ex(NULL, need, PHP_HTTP_BUFFER_INIT_PREALLOC | (buf->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0)); - if (sub) { - if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(sub, buf->data + offset, need)) { - php_http_buffer_free(&sub); - } else { - sub->size = buf->size; - } - } - return sub; - } -} - -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length) -{ - if (length < buf->used) { - return php_http_buffer_sub(buf, buf->used - length, length); - } else { - return php_http_buffer_sub(buf, 0, buf->used); - } -} - - -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv) -{ - unsigned i = 0; - buf = php_http_buffer_init(buf); - - if (buf) { - while (argc > i++) { - php_http_buffer_free_t f = va_arg(argv, php_http_buffer_free_t); - php_http_buffer_t *current = va_arg(argv, php_http_buffer_t *); - php_http_buffer_append(buf, current->data, current->used); - FREE_PHP_HTTP_BUFFER(f, current); - } - } - - return buf; -} - -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...) -{ - va_list argv; - php_http_buffer_t *ret; - - va_start(argv, argc); - ret = php_http_buffer_merge_va(buf, argc, argv); - va_end(argv); - return ret; -} - -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...) -{ - va_list argv; - php_http_buffer_t *ret; - - va_start(argv, argc); - ret = php_http_buffer_merge_va(NULL, argc, argv); - va_end(argv); - return ret; -} - PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf) { if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize_ex(buf, 1, 1, 0)) { @@ -312,17 +167,6 @@ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *bu return buf; } -PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right) -{ - if (left->used > right->used) { - return -1; - } else if (right->used > left->used) { - return 1; - } else { - return memcmp(left->data, right->data, left->used); - } -} - PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf) { buf->free += buf->used; @@ -442,6 +286,166 @@ PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s, return passed; } +#ifdef PHP_HTTP_BUFFER_EXTENDED + +PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right) +{ + if (left->used > right->used) { + return -1; + } else if (right->used > left->used) { + return 1; + } else { + return memcmp(left->data, right->data, left->used); + } +} + +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to) +{ + int free_to = !to; + + to = php_http_buffer_clone(from, to); + + if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(to, from->data, from->used)) { + if (free_to) { + php_http_buffer_free(&to); + } else { + php_http_buffer_dtor(to); + } + } + return to; +} + +PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset) +{ + if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, insert_len)) { + return PHP_HTTP_BUFFER_NOMEM; + } + memmove(buf->data + offset + insert_len, buf->data + offset, insert_len); + memcpy(buf->data + offset, insert, insert_len); + buf->used += insert_len; + buf->free -= insert_len; + return insert_len; +} + +PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) +{ + va_list argv; + char *insert; + size_t insert_len, alloc; + + va_start(argv, format); + insert_len = vspprintf(&insert, 0, format, argv); + va_end(argv); + + alloc = php_http_buffer_insert(buf, insert, insert_len, offset); + efree(insert); + + if (PHP_HTTP_BUFFER_NOMEM == alloc) { + return PHP_HTTP_BUFFER_NOMEM; + } + return insert_len; +} + +PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len) +{ + if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, prepend_len)) { + return PHP_HTTP_BUFFER_NOMEM; + } + memmove(buf->data + prepend_len, buf->data, buf->used); + memcpy(buf->data, prepend, prepend_len); + buf->used += prepend_len; + buf->free -= prepend_len; + return prepend_len; +} + +PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) +{ + va_list argv; + char *prepend; + size_t prepend_len, alloc; + + va_start(argv, format); + prepend_len = vspprintf(&prepend, 0, format, argv); + va_end(argv); + + alloc = php_http_buffer_prepend(buf, prepend, prepend_len); + efree(prepend); + + if (PHP_HTTP_BUFFER_NOMEM == alloc) { + return PHP_HTTP_BUFFER_NOMEM; + } + return prepend_len; +} + +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t length) +{ + if (offset >= buf->used) { + return NULL; + } else { + size_t need = 1 + ((length + offset) > buf->used ? (buf->used - offset) : (length - offset)); + php_http_buffer_t *sub = php_http_buffer_init_ex(NULL, need, PHP_HTTP_BUFFER_INIT_PREALLOC | (buf->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0)); + if (sub) { + if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(sub, buf->data + offset, need)) { + php_http_buffer_free(&sub); + } else { + sub->size = buf->size; + } + } + return sub; + } +} + +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length) +{ + if (length < buf->used) { + return php_http_buffer_sub(buf, buf->used - length, length); + } else { + return php_http_buffer_sub(buf, 0, buf->used); + } +} + + +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv) +{ + unsigned i = 0; + buf = php_http_buffer_init(buf); + + if (buf) { + while (argc > i++) { + php_http_buffer_free_t f = va_arg(argv, php_http_buffer_free_t); + php_http_buffer_t *current = va_arg(argv, php_http_buffer_t *); + php_http_buffer_append(buf, current->data, current->used); + FREE_PHP_HTTP_BUFFER(f, current); + } + } + + return buf; +} + +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...) +{ + va_list argv; + php_http_buffer_t *ret; + + va_start(argv, argc); + ret = php_http_buffer_merge_va(buf, argc, argv); + va_end(argv); + return ret; +} + +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...) +{ + va_list argv; + php_http_buffer_t *ret; + + va_start(argv, argc); + ret = php_http_buffer_merge_va(NULL, argc, argv); + va_end(argv); + return ret; +} + +#endif + /* * Local variables: * tab-width: 4 diff --git a/php_http_buffer.h b/php_http_buffer.h index 93776d5..aaa4408 100644 --- a/php_http_buffer.h +++ b/php_http_buffer.h @@ -165,51 +165,15 @@ PHP_HTTP_BUFFER_API size_t php_http_buffer_shrink(php_http_buffer_t *buf); PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf, const char *append, size_t append_len); PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3); -/* insert data at a specific position of the php_http_buffer_t */ -#define php_http_buffer_inserts(b, i, o) php_http_buffer_insert((b), (i), sizeof(i)-1, (o)) -#define php_http_buffer_insertl(b, i, o) php_http_buffer_insert((b), (i), strlen(i), (o)) -PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset); -PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 3, 4); - -/* prepend data */ -#define php_http_buffer_prepends(b, p) php_http_buffer_prepend((b), (p), sizeof(p)-1) -#define php_http_buffer_prependl(b, p) php_http_buffer_prepend((b), (p), strlen(p)) -PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len); -PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3); - /* get a zero-terminated string */ PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len); -/* get a part of the php_http_buffer_t */ -#define php_http_buffer_mid(b, o, l) php_http_buffer_sub((b), (o), (l)) -#define php_http_buffer_left(b, l) php_http_buffer_sub((b), 0, (l)) -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length); -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t len); - /* remove a substring */ PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length); -/* get a complete php_http_buffer_t duplicate */ -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to); - -/* merge several php_http_buffer_t objects - use like: - - php_http_buffer_t *final = php_http_buffer_merge(3, - PHP_HTTP_BUFFER_NOT_FREE(&keep), - PHP_HTTP_BUFFER_ALL_FREE(middle_ptr), - PHP_HTTP_BUFFER_VAL_FREE(&local); -*/ -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...); -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...); -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv); - /* sets a trailing NUL byte */ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf); -/* memcmp for php_http_buffer_t objects */ -PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right); - /* reset php_http_buffer_t object */ PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf); @@ -232,7 +196,48 @@ PHP_HTTP_BUFFER_API void php_http_buffer_chunked_output(php_http_buffer_t **s, c /* write chunks directly into php_http_buffer_t buffer */ PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *opaque TSRMLS_DC); -#endif + +# ifdef PHP_HTTP_BUFFER_EXTENDED + +/* memcmp for php_http_buffer_t objects */ +PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right); + +/* get a complete php_http_buffer_t duplicate */ +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to); + +/* merge several php_http_buffer_t objects + use like: + + php_http_buffer_t *final = php_http_buffer_merge(3, + PHP_HTTP_BUFFER_NOT_FREE(&keep), + PHP_HTTP_BUFFER_ALL_FREE(middle_ptr), + PHP_HTTP_BUFFER_VAL_FREE(&local); +*/ +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...); +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...); +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv); + +/* insert data at a specific position of the php_http_buffer_t */ +#define php_http_buffer_inserts(b, i, o) php_http_buffer_insert((b), (i), sizeof(i)-1, (o)) +#define php_http_buffer_insertl(b, i, o) php_http_buffer_insert((b), (i), strlen(i), (o)) +PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset); +PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 3, 4); + +/* prepend data */ +#define php_http_buffer_prepends(b, p) php_http_buffer_prepend((b), (p), sizeof(p)-1) +#define php_http_buffer_prependl(b, p) php_http_buffer_prepend((b), (p), strlen(p)) +PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len); +PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3); + +/* get a part of the php_http_buffer_t */ +#define php_http_buffer_mid(b, o, l) php_http_buffer_sub((b), (o), (l)) +#define php_http_buffer_left(b, l) php_http_buffer_sub((b), 0, (l)) +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length); +PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t len); + +# endif /* PHP_HTTP_BUFFER_EXTENDED */ + +#endif /* PHP_HTTP_BUFFER_H */ /* diff --git a/php_http_env.c b/php_http_env.c index 74194c4..0aeb80d 100644 --- a/php_http_env.c +++ b/php_http_env.c @@ -139,14 +139,20 @@ PHP_HTTP_API void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC) } } -PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len TSRMLS_DC) +PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len, php_http_message_t *request TSRMLS_DC) { - zval **zvalue; + HashTable *request_headers; + zval **zvalue = NULL; char *val = NULL, *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); - php_http_env_get_request_headers(NULL TSRMLS_CC); + if (request) { + request_headers = &request->hdrs; + } else { + php_http_env_get_request_headers(NULL TSRMLS_CC); + request_headers = PHP_HTTP_G->env.request.headers; + } - if (SUCCESS == zend_symtable_find(PHP_HTTP_G->env.request.headers, key, name_len + 1, (void *) &zvalue)) { + if (SUCCESS == zend_symtable_find(request_headers, key, name_len + 1, (void *) &zvalue)) { zval *zcopy = php_http_ztyp(IS_STRING, *zvalue); val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy)); @@ -161,13 +167,19 @@ PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t return val; } -PHP_HTTP_API int php_http_env_got_request_header(const char *name_str, size_t name_len TSRMLS_DC) +PHP_HTTP_API int php_http_env_got_request_header(const char *name_str, size_t name_len, php_http_message_t *request TSRMLS_DC) { + HashTable *request_headers; char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1); int got; - php_http_env_get_request_headers(NULL TSRMLS_CC); - got = zend_symtable_exists(PHP_HTTP_G->env.request.headers, key, name_len + 1); + if (request) { + request_headers = &request->hdrs; + } else { + php_http_env_get_request_headers(NULL TSRMLS_CC); + request_headers = PHP_HTTP_G->env.request.headers; + } + got = zend_symtable_exists(request_headers, key, name_len + 1); efree(key); return got; @@ -255,13 +267,26 @@ PHP_HTTP_API php_http_message_body_t *php_http_env_get_request_body(TSRMLS_D) return PHP_HTTP_G->env.request.body; } -PHP_HTTP_API php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC) +PHP_HTTP_API const char *php_http_env_get_request_method(php_http_message_t *request TSRMLS_DC) +{ + const char *m; + + if (PHP_HTTP_MESSAGE_TYPE(REQUEST, request)) { + m = request->http.info.request.method; + } else { + m = SG(request_info).request_method; + } + + return m ? m : "GET"; +} + +PHP_HTTP_API php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length, php_http_message_t *request TSRMLS_DC) { zval *zentry; char *range, *rp, c; long begin = -1, end = -1, *ptr; - if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL TSRMLS_CC))) { + if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL, request TSRMLS_CC))) { return PHP_HTTP_RANGE_NO; } if (strncmp(range, "bytes=", lenof("bytes="))) { @@ -483,15 +508,12 @@ 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, ...) +PHP_HTTP_API STATUS php_http_env_set_response_header_va(long http_code, zend_bool replace, const char *fmt, va_list argv TSRMLS_DC) { - 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); + h.line_len = vspprintf(&h.line, 0, fmt, argv); if (h.line) { if (h.line_len) { @@ -502,6 +524,18 @@ PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend return ret; } +PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend_bool replace TSRMLS_DC, const char *fmt, ...) +{ + STATUS ret; + va_list args; + + va_start(args, fmt); + ret = php_http_env_set_response_header_va(http_code, replace, fmt, args TSRMLS_CC); + va_end(args); + + 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) { @@ -755,7 +789,7 @@ PHP_METHOD(HttpEnv, getRequestHeader) if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) { if (header_name_str && header_name_len) { size_t header_length; - char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length TSRMLS_CC); + char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length, NULL TSRMLS_CC); if (header_value) { RETURN_STRINGL(header_value, header_length, 0); diff --git a/php_http_env.h b/php_http_env.h index 5bd3fb9..b9b4fd5 100644 --- a/php_http_env.h +++ b/php_http_env.h @@ -38,11 +38,12 @@ typedef enum php_http_range_status { PHP_HTTP_RANGE_ERR } php_http_range_status_t; -PHP_HTTP_API php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t entity_length TSRMLS_DC); +PHP_HTTP_API php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t entity_length, php_http_message_t *request TSRMLS_DC); PHP_HTTP_API void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC); -PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len TSRMLS_DC); -PHP_HTTP_API int php_http_env_got_request_header(const char *name_str, size_t name_len TSRMLS_DC); +PHP_HTTP_API char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len, php_http_message_t *request TSRMLS_DC); +PHP_HTTP_API int php_http_env_got_request_header(const char *name_str, size_t name_len, php_http_message_t *request TSRMLS_DC); PHP_HTTP_API php_http_message_body_t *php_http_env_get_request_body(TSRMLS_D); +PHP_HTTP_API const char *php_http_env_get_request_method(php_http_message_t *request TSRMLS_DC); typedef enum php_http_content_disposition { PHP_HTTP_CONTENT_DISPOSITION_NONE, @@ -65,6 +66,7 @@ PHP_HTTP_API STATUS php_http_env_set_response_protocol_version(php_http_version_ PHP_HTTP_API STATUS php_http_env_set_response_header(long http_code, const char *header_str, size_t header_len, zend_bool replace TSRMLS_DC); 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); PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend_bool replace TSRMLS_DC, const char *fmt, ...); +PHP_HTTP_API STATUS php_http_env_set_response_header_va(long http_code, zend_bool replace, const char *fmt, va_list argv TSRMLS_DC); PHP_HTTP_API zval *php_http_env_get_server_var(const char *key_str, size_t key_len, zend_bool check TSRMLS_DC); #define php_http_env_got_server_var(v) (NULL != php_http_env_get_server_var((v), strlen(v), 1 TSRMLS_CC)) diff --git a/php_http_env_response.c b/php_http_env_response.c index b64aefd..2f75eca 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -12,7 +12,7 @@ #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) +static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len TSRMLS_DC) { if (Z_TYPE_P(options) == IS_OBJECT) { /* stupid non-const api */ @@ -28,6 +28,9 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int case IS_STRING: zend_update_property_stringl(Z_OBJCE_P(options), options, name, name_len, value_ptr, value_len TSRMLS_CC); break; + case IS_OBJECT: + zend_update_property(Z_OBJCE_P(options), options, name, name_len, value_ptr TSRMLS_CC); + break; } } else { zend_update_property_null(Z_OBJCE_P(options), options, name, name_len TSRMLS_CC); @@ -47,6 +50,10 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int char *value = estrndup(value_ptr, value_len); add_assoc_stringl_ex(options, name_str, name_len + 1, value, value_len, 0); break; + case IS_OBJECT: + Z_ADDREF_P(value_ptr); + add_assoc_zval_ex(options, name_str, name_len + 1, value_ptr); + break; } } } else { @@ -74,21 +81,49 @@ static zval *get_option(zval *options, const char *name_str, size_t name_len TSR } return val; } +static php_http_message_body_t *get_body(zval *options TSRMLS_DC) +{ + zval *zbody; + php_http_message_body_t *body = NULL; + + if ((zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))) { + if ((Z_TYPE_P(zbody) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_get_class_entry() TSRMLS_CC)) { + php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC); + + body = body_obj->body; + } + zval_ptr_dtor(&zbody); + } + + return body; +} +static php_http_message_t *get_request(zval *options TSRMLS_DC) +{ + zval *zrequest; + php_http_message_t *request = NULL; + + if ((zrequest = get_option(options, ZEND_STRL("request") TSRMLS_CC))) { + if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_get_class_entry() TSRMLS_CC)) { + php_http_message_object_t *request_obj = zend_object_store_get_object(zrequest TSRMLS_CC); + + request = request_obj->message; + } + zval_ptr_dtor(&zrequest); + } + + return request; +} -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) +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, php_http_message_t *request TSRMLS_DC) { php_http_cache_status_t ret = PHP_HTTP_CACHE_NO; int free_etag = 0; char *header = NULL, *etag; - zval *zetag, *zbody = NULL; + php_http_message_body_t *body; + zval *zetag; - 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_get_class_entry() TSRMLS_CC) - ) { - if (zbody) { - zval_ptr_dtor(&zbody); - } + + if (!(body = get_body(options TSRMLS_CC))) { return ret; } @@ -100,45 +135,37 @@ 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 if ((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(body))) { set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag) TSRMLS_CC); free_etag = 1; } - if (zbody) { - zval_ptr_dtor(&zbody); - } - if (zetag) { zval_ptr_dtor(&zetag); } - if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL TSRMLS_CC))) { + if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) { ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD) ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS; } if (free_etag) { efree(etag); } - STR_FREE(header); + STR_FREE(header); return ret; } -PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len TSRMLS_DC) +PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC) { + php_http_cache_status_t ret = PHP_HTTP_CACHE_NO; char *header; time_t ums, lm = 0; - zval *zbody = NULL, *zlm; + php_http_message_body_t *body; + zval *zlm; - 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_get_class_entry() TSRMLS_CC) - ) { - if (zbody) { - zval_ptr_dtor(&zbody); - } - return PHP_HTTP_CACHE_NO; + if (!(body = get_body(options TSRMLS_CC))) { + return ret; } if ((zlm = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) { @@ -150,49 +177,57 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_mod 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); + lm = php_http_message_body_mtime(body); set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0 TSRMLS_CC); } - zval_ptr_dtor(&zbody); if (zlm) { zval_ptr_dtor(&zlm); } - if (!(header = php_http_env_get_request_header(header_str, header_len, NULL TSRMLS_CC))) { - return PHP_HTTP_CACHE_NO; + if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) { + ums = php_parse_date(header, NULL); + + if (ums > 0 && ums >= lm) { + ret = PHP_HTTP_CACHE_HIT; + } else { + ret = PHP_HTTP_CACHE_MISS; + } + } + + STR_FREE(header); + return ret; +} + +static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (r->ops->get_status(r) >= 400) { + return 0; } - ums = php_parse_date(header, NULL); - efree(header); + if (php_http_env_got_request_header(ZEND_STRL("Authorizsation"), request TSRMLS_CC)) { + return 0; + } - if (ums > 0 && ums >= lm) { - return PHP_HTTP_CACHE_HIT; - } else { - return PHP_HTTP_CACHE_MISS; + if (-1 == php_http_select_str(php_http_env_get_request_method(request TSRMLS_CC), 2, "HEAD", "GET")) { + return 0; } + + return 1; } static size_t output(void *context, char *buf, size_t len TSRMLS_DC) { php_http_env_response_t *r = context; - PHPWRITE(buf, len); + r->ops->write(r, 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 (r->throttle.delay >= PHP_HTTP_DIFFSEC) { -#if PHP_VERSION_ID >= 50400 - 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); - } -#else - php_end_ob_buffer(1, 1 TSRMLS_CC); - sapi_flush(TSRMLS_C); -#endif + r->ops->flush(r); php_http_sleep(r->throttle.delay); } return len; @@ -229,15 +264,21 @@ static STATUS php_http_env_response_send_data(php_http_env_response_t *r, const return SUCCESS; } -PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options TSRMLS_DC) +PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *init_arg TSRMLS_DC) { - php_http_env_response_t *free_r = NULL; + zend_bool free_r; - if (!r) { - r = free_r = emalloc(sizeof(*r)); + if ((free_r = !r)) { + r = emalloc(sizeof(*r)); } memset(r, 0, sizeof(*r)); + if (ops) { + r->ops = ops; + } else { + r->ops = php_http_env_response_get_sapi_ops(); + } + r->buffer = php_http_buffer_init(NULL); Z_ADDREF_P(options); @@ -245,11 +286,23 @@ PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_re TSRMLS_SET_CTX(r->ts); + if (r->ops->init && (SUCCESS != r->ops->init(r, init_arg))) { + if (free_r) { + php_http_env_response_free(&r); + } else { + php_http_env_response_dtor(r); + r = NULL; + } + } + return r; } PHP_HTTP_API void php_http_env_response_dtor(php_http_env_response_t *r) { + if (r->ops->dtor) { + r->ops->dtor(r); + } php_http_buffer_free(&r->buffer); zval_ptr_dtor(&r->options); STR_FREE(r->content.type); @@ -268,7 +321,7 @@ PHP_HTTP_API void php_http_env_response_free(php_http_env_response_t **r) } } -static STATUS php_http_env_response_send_head(php_http_env_response_t *r) +static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request) { STATUS ret = SUCCESS; zval *zoption, *options = r->options; @@ -283,7 +336,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) zval_ptr_dtor(&zoption); if (Z_LVAL_P(zoption_copy) > 0) { - ret = php_http_env_set_response_code(Z_LVAL_P(zoption_copy) TSRMLS_CC); + ret = r->ops->set_status(r, Z_LVAL_P(zoption_copy)); } zval_ptr_dtor(&zoption_copy); } @@ -298,7 +351,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) zval_ptr_dtor(&zoption); if (Z_STRLEN_P(zoption_copy) && php_http_version_parse(&v, Z_STRVAL_P(zoption_copy) TSRMLS_CC)) { - ret = php_http_env_set_response_protocol_version(&v TSRMLS_CC); + ret = r->ops->set_protocol_version(r, &v); php_http_version_dtor(&v); } zval_ptr_dtor(&zoption_copy); @@ -310,17 +363,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) { if (Z_TYPE_P(zoption) == IS_ARRAY) { - zval **val; - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - - FOREACH_KEYVAL(pos, zoption, key, val) { - if (key.type == HASH_KEY_IS_STRING) { - if (SUCCESS != (ret = php_http_env_set_response_header_value(0, key.str, key.len - 1, *val, 1 TSRMLS_CC))) { - break; - } - } - } + php_http_headers_to_callback(Z_ARRVAL_P(zoption), 0, r->ops->set_header, r TSRMLS_CC); } zval_ptr_dtor(&zoption); } @@ -335,7 +378,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) zval_ptr_dtor(&zoption); if (Z_STRLEN_P(zoption_copy)) { PHP_HTTP_CHECK_CONTENT_TYPE(Z_STRVAL_P(zoption_copy), ret = FAILURE) else { - if (SUCCESS == (ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)))) { + if (SUCCESS == (ret = r->ops->set_header(r, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)))) { r->content.type = estrndup(Z_STRVAL_P(zoption_copy), Z_STRLEN_P(zoption_copy)); } } @@ -354,16 +397,19 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) if ( 1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range) && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end) ) { - 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); + if (SUCCESS == (ret = r->ops->set_status(r, 206))) { + ret = r->ops->set_header(r, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length); + } } else { /* this should never happen */ zend_hash_destroy(&r->range.values); - php_http_env_set_response_code(500 TSRMLS_CC); ret = FAILURE; } } else { 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); + if (SUCCESS == (ret = r->ops->set_status(r, 206))) { + ret = r->ops->set_header(r, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary); + } } } else { if ((zoption = get_option(options, ZEND_STRL("cacheControl") TSRMLS_CC))) { @@ -371,7 +417,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) zval_ptr_dtor(&zoption); if (Z_STRLEN_P(zoption_copy)) { - ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy) TSRMLS_CC); + ret = r->ops->set_header(r, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)); } zval_ptr_dtor(&zoption_copy); } @@ -386,7 +432,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) php_http_buffer_init(&buf); if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption_copy), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC)) { - ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s", buf.data); + if (buf.used) { + ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data); + } } php_http_buffer_dtor(&buf); @@ -412,7 +460,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) add_next_index_stringl(&zsupported, ZEND_STRL("gzip"), 1); add_next_index_stringl(&zsupported, ZEND_STRL("deflate"), 1); - if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported) TSRMLS_CC))) { + if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request TSRMLS_CC))) { char *key_str = NULL; uint key_len = 0; @@ -421,21 +469,21 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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 TSRMLS_CC))) { ret = FAILURE; - } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: gzip"), 1 TSRMLS_CC))) { + } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: gzip"))) { 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 TSRMLS_CC))) { ret = FAILURE; - } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: deflate"), 1 TSRMLS_CC))) { + } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: deflate"))) { r->content.encoding = estrndup(key_str, key_len - 1); } } else { - ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Encoding"), NULL, 0 TSRMLS_CC); + ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding")); } if (SUCCESS == ret) { - ret = php_http_env_set_response_header(0, ZEND_STRL("Vary: Accept-Encoding"), 0 TSRMLS_CC); + ret = r->ops->add_header(r, "Vary: Accept-Encoding"); } } @@ -448,7 +496,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) case PHP_HTTP_CONTENT_ENCODING_NONE: default: - ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Encoding"), NULL, 0 TSRMLS_CC); + ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding")); break; } zval_ptr_dtor(&zoption_copy); @@ -458,24 +506,19 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) return ret; } - if (php_http_env_get_response_code(TSRMLS_C) < 400 && !php_http_env_got_request_header(ZEND_STRL("Authorization") TSRMLS_CC) - && ( !SG(request_info).request_method - || !strcasecmp(SG(request_info).request_method, "GET") - || !strcasecmp(SG(request_info).request_method, "HEAD") - ) - ) { - switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match") TSRMLS_CC)) { + if (php_http_env_response_is_cacheable(r, request)) { + switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request 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") TSRMLS_CC)) { + if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request TSRMLS_CC)) { break; } /* no break */ case PHP_HTTP_CACHE_HIT: - ret = php_http_env_set_response_code(304 TSRMLS_CC); + ret = r->ops->set_status(r, 304); r->done = 1; break; } @@ -485,9 +528,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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)); + ret = r->ops->set_header(r, "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); + ret = r->ops->set_header(r, "ETag: %s", Z_STRVAL_P(zoption_copy)); } zval_ptr_dtor(&zoption_copy); } @@ -498,7 +541,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) if (Z_LVAL_P(zoption_copy)) { char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), Z_LVAL_P(zoption_copy), 0 TSRMLS_CC); if (date) { - ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Last-Modified: %s", date); + ret = r->ops->set_header(r, "Last-Modified: %s", date); efree(date); } } @@ -513,19 +556,15 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) static STATUS php_http_env_response_send_body(php_http_env_response_t *r) { STATUS ret = SUCCESS; - zval *zbody, *zoption; + zval *zoption; + php_http_message_body_t *body; TSRMLS_FETCH_FROM_CTX(r->ts); if (r->done) { return ret; } - if ( (zbody = get_option(r->options, ZEND_STRL("body") TSRMLS_CC)) - && (Z_TYPE_P(zbody) == IS_OBJECT) - && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_get_class_entry() TSRMLS_CC) - ) { - php_http_message_body_object_t *obj = zend_object_store_get_object(zbody TSRMLS_CC); - + if ((body = get_body(r->options TSRMLS_CC))) { if ((zoption = get_option(r->options, ZEND_STRL("throttleDelay") TSRMLS_CC))) { if (Z_TYPE_P(zoption) == IS_DOUBLE) { r->throttle.delay = Z_DVAL_P(zoption); @@ -548,15 +587,14 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end) ) { /* send chunk */ - php_http_message_body_to_callback(obj->body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); + php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); php_http_env_response_send_done(r); zend_hash_destroy(&r->range.values); ret = SUCCESS; } else { - fprintf(stderr, "wut?"); /* this should never happen */ zend_hash_destroy(&r->range.values); - php_http_env_set_response_code(500 TSRMLS_CC); + r->ops->set_status(r, 500); ret = FAILURE; } @@ -581,7 +619,7 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) Z_LVAL_PP(end), r->content.length ); - php_http_message_body_to_callback(obj->body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); + php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); } } php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary); @@ -590,36 +628,30 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) } } else { - php_http_message_body_to_callback(obj->body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0); + php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0); php_http_env_response_send_done(r); } } - if (zbody) { - zval_ptr_dtor(&zbody); - } return ret; } PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) { - zval *zbody; + php_http_message_t *request; + php_http_message_body_t *body; TSRMLS_FETCH_FROM_CTX(r->ts); - /* check for ranges */ - if ( (zbody = get_option(r->options, ZEND_STRL("body") TSRMLS_CC)) - && (Z_TYPE_P(zbody) == IS_OBJECT) - && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_get_class_entry() TSRMLS_CC) - ) { - php_http_message_body_object_t *obj = zend_object_store_get_object(zbody TSRMLS_CC); + request = get_request(r->options TSRMLS_CC); - r->content.length = php_http_message_body_size(obj->body); - zval_ptr_dtor(&zbody); + /* check for ranges */ + if ((body = get_body(r->options TSRMLS_CC))) { + r->content.length = php_http_message_body_size(body); - if (SUCCESS != php_http_env_set_response_header(0, ZEND_STRL("Accept-Ranges: bytes"), 1 TSRMLS_CC)) { + if (SUCCESS != r->ops->set_header(r, "Accept-Ranges: bytes")) { return FAILURE; } else { zend_hash_init(&r->range.values, 0, NULL, ZVAL_PTR_DTOR, 0); - r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length TSRMLS_CC); + r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request TSRMLS_CC); switch (r->range.status) { case PHP_HTTP_RANGE_NO: @@ -627,33 +659,36 @@ PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) break; case PHP_HTTP_RANGE_ERR: - if (php_http_env_got_request_header(ZEND_STRL("If-Range") TSRMLS_CC)) { + if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request TSRMLS_CC)) { r->range.status = PHP_HTTP_RANGE_NO; zend_hash_destroy(&r->range.values); } else { r->done = 1; zend_hash_destroy(&r->range.values); - if (SUCCESS != php_http_env_set_response_header_format(416, 1 TSRMLS_CC, "Content-Range: bytes */%zu", r->content.length)) { + if (SUCCESS != r->ops->set_status(r, 416)) { + return FAILURE; + } + if (SUCCESS != r->ops->set_header(r, "Content-Range: bytes */%zu", r->content.length)) { return FAILURE; } } break; case PHP_HTTP_RANGE_OK: - if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range") TSRMLS_CC) - || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range") TSRMLS_CC) + if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC) + || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC) ) { r->range.status = PHP_HTTP_RANGE_NO; zend_hash_destroy(&r->range.values); break; } - if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match") TSRMLS_CC) - || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since") TSRMLS_CC) - || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since") TSRMLS_CC) + if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match"), request TSRMLS_CC) + || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since"), request TSRMLS_CC) + || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since"), request TSRMLS_CC) ) { r->done = 1; zend_hash_destroy(&r->range.values); - if (SUCCESS != php_http_env_set_response_code(412 TSRMLS_CC)) { + if (SUCCESS != r->ops->set_status(r, 412)) { return FAILURE; } break; @@ -662,20 +697,361 @@ PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) break; } } - } else if (zbody) { - zval_ptr_dtor(&zbody); } - if (SUCCESS != php_http_env_response_send_head(r)) { + if (SUCCESS != php_http_env_response_send_head(r, request)) { return FAILURE; } if (SUCCESS != php_http_env_response_send_body(r)) { return FAILURE; } + + if (SUCCESS != r->ops->finish(r)) { + return FAILURE; + } + + return SUCCESS; +} + +static long php_http_env_response_sapi_get_status(php_http_env_response_t *r) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + return php_http_env_get_response_code(TSRMLS_C); +} +static STATUS php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + return php_http_env_set_response_code(http_code TSRMLS_CC); +} +static STATUS php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + return php_http_env_set_response_protocol_version(v TSRMLS_CC); +} +static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list args; + TSRMLS_FETCH_FROM_CTX(r->ts); + + va_start(args, fmt); + ret = php_http_env_set_response_header_va(0, 1, fmt, args TSRMLS_CC); + va_end(args); + + return ret; +} +static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list args; + TSRMLS_FETCH_FROM_CTX(r->ts); + + va_start(args, fmt); + ret = php_http_env_set_response_header_va(0, 0, fmt, args TSRMLS_CC); + va_end(args); + + return ret; +} +static STATUS php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1 TSRMLS_CC); +} +static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (0 < PHPWRITE(data_str, data_len)) { + return SUCCESS; + } + return FAILURE; +} +static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + +#if PHP_VERSION_ID >= 50400 + 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); + } +#else + php_end_ob_buffer(1, 1 TSRMLS_CC); + sapi_flush(TSRMLS_C); +#endif + + return SUCCESS; +} +static STATUS php_http_env_response_sapi_finish(php_http_env_response_t *r) +{ return SUCCESS; } +static php_http_env_response_ops_t php_http_env_response_sapi_ops = { + NULL, + NULL, + php_http_env_response_sapi_get_status, + php_http_env_response_sapi_set_status, + php_http_env_response_sapi_set_protocol_version, + php_http_env_response_sapi_set_header, + php_http_env_response_sapi_add_header, + php_http_env_response_sapi_del_header, + php_http_env_response_sapi_write, + php_http_env_response_sapi_flush, + php_http_env_response_sapi_finish +}; + +PHP_HTTP_API php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void) +{ + return &php_http_env_response_sapi_ops; +} + +typedef struct php_http_env_response_stream_ctx { + HashTable header; + php_http_version_t version; + long status_code; + + php_stream *stream; + + unsigned started:1; + unsigned finished:1; +} php_http_env_response_stream_ctx_t; + +static STATUS php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg) +{ + php_http_env_response_stream_ctx_t *ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + + ctx = ecalloc(1, sizeof(*ctx)); + + ctx->stream = init_arg; + if (SUCCESS != zend_list_addref(ctx->stream->rsrc_id)) { + efree(ctx); + return FAILURE; + } + zend_hash_init(&ctx->header, 0, NULL, ZVAL_PTR_DTOR, 0); + php_http_version_init(&ctx->version, 1, 1 TSRMLS_CC); + ctx->status_code = 200; + + r->ctx = ctx; + + return SUCCESS; +} +static void php_http_env_response_stream_dtor(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + + zend_hash_destroy(&ctx->header); + zend_list_delete(ctx->stream->rsrc_id); + efree(ctx); + r->ctx = NULL; +} +static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header TSRMLS_DC) +{ + HashPosition pos; + zval **val; + + FOREACH_HASH_VAL(pos, &ctx->header, val) { + if (Z_TYPE_PP(val) == IS_ARRAY) { + php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val) TSRMLS_CC); + } else { + php_stream_write(ctx->stream, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); + php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + } + } +} +static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx TSRMLS_DC) +{ + if (ctx->started || ctx->finished) { + return FAILURE; + } + + php_stream_printf(ctx->stream TSRMLS_CC, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF, ctx->version.major, ctx->version.minor, ctx->status_code, php_http_env_get_response_status_for_code(ctx->status_code)); + php_http_env_response_stream_header(ctx, &ctx->header TSRMLS_CC); + php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + ctx->started = 1; + return SUCCESS; +} +static long php_http_env_response_stream_get_status(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *ctx = r->ctx; + + return ctx->status_code; +} +static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + stream_ctx->status_code = http_code; + + return SUCCESS; +} +static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + memcpy(&stream_ctx->version, v, sizeof(stream_ctx->version)); + + return SUCCESS; +} +static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + char *header_end, *header_str = NULL; + size_t header_len = 0; + zval *zheader, **zheader_ptr; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + header_len = vspprintf(&header_str, 0, fmt, argv); + + if (!(header_end = strchr(header_str, ':'))) { + efree(header_str); + return FAILURE; + } + + *header_end = '\0'; + + if (!replace && (SUCCESS == zend_hash_find(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader_ptr))) { + convert_to_array(*zheader_ptr); + *header_end = ':'; + return add_next_index_stringl(*zheader_ptr, header_str, header_len, 0); + } else { + MAKE_STD_ZVAL(zheader); + ZVAL_STRINGL(zheader, header_str, header_len, 0); + + if (SUCCESS != zend_hash_update(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader, sizeof(zval *), NULL)) { + zval_ptr_dtor(&zheader); + return FAILURE; + } + + *header_end = ':'; + return SUCCESS; + } +} +static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list argv; + + va_start(argv, fmt); + ret = php_http_env_response_stream_set_header_ex(r, 1, fmt, argv); + va_end(argv); + + return ret; +} +static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list argv; + + va_start(argv, fmt); + ret = php_http_env_response_stream_set_header_ex(r, 0, fmt, argv); + va_end(argv); + + return ret; +} +static STATUS php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + zend_hash_del(&stream_ctx->header, header_str, header_len + 1); + return SUCCESS; +} +static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (stream_ctx->finished) { + return FAILURE; + } + if (!stream_ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) { + return FAILURE; + } + } + + php_stream_write(stream_ctx->stream, data_str, data_len); + + return SUCCESS; +} +static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (stream_ctx->finished) { + return FAILURE; + } + if (!stream_ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) { + return FAILURE; + } + } + + return php_stream_flush(stream_ctx->stream); +} +static STATUS php_http_env_response_stream_finish(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (stream_ctx->finished) { + return FAILURE; + } + if (!stream_ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) { + return FAILURE; + } + } + + stream_ctx->finished = 1; + + return SUCCESS; +} + +static php_http_env_response_ops_t php_http_env_response_stream_ops = { + php_http_env_response_stream_init, + php_http_env_response_stream_dtor, + php_http_env_response_stream_get_status, + php_http_env_response_stream_set_status, + php_http_env_response_stream_set_protocol_version, + php_http_env_response_stream_set_header, + php_http_env_response_stream_add_header, + php_http_env_response_stream_del_header, + php_http_env_response_stream_write, + php_http_env_response_stream_flush, + php_http_env_response_stream_finish +}; + +PHP_HTTP_API php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void) +{ + return &php_http_env_response_stream_ops; +} + #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) @@ -689,6 +1065,10 @@ PHP_HTTP_BEGIN_ARGS(__invoke, 1) PHP_HTTP_ARG_VAL(ob_flags, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(setEnvRequest, 1) + PHP_HTTP_ARG_OBJ(http\\Message, env_request, 1) +PHP_HTTP_END_ARGS; + PHP_HTTP_BEGIN_ARGS(setContentType, 1) PHP_HTTP_ARG_VAL(content_type, 0) PHP_HTTP_END_ARGS; @@ -738,6 +1118,7 @@ zend_class_entry *php_http_env_response_get_class_entry(void) static 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(__invoke, ZEND_ACC_PUBLIC) + PHP_HTTP_ENV_RESPONSE_ME(setEnvRequest, ZEND_ACC_PUBLIC) PHP_HTTP_ENV_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC) PHP_HTTP_ENV_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC) PHP_HTTP_ENV_RESPONSE_ME(setContentEncoding, ZEND_ACC_PUBLIC) @@ -785,6 +1166,15 @@ PHP_METHOD(HttpEnvResponse, __invoke) } } +PHP_METHOD(HttpEnvResponse, setEnvRequest) +{ + zval *env_req = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &env_req, php_http_message_get_class_entry())) { + set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0 TSRMLS_CC); + } +} + PHP_METHOD(HttpEnvResponse, setContentType) { char *ct_str = NULL; @@ -842,7 +1232,8 @@ PHP_METHOD(HttpEnvResponse, isCachedByLastModified) 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_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC)); } RETURN_FALSE; } @@ -867,7 +1258,7 @@ PHP_METHOD(HttpEnvResponse, isCachedByEtag) 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_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC)); } RETURN_FALSE; } @@ -887,16 +1278,31 @@ PHP_METHOD(HttpEnvResponse, setThrottleRate) PHP_METHOD(HttpEnvResponse, send) { + zval *zstream = NULL; + php_stream *s = NULL; + RETVAL_FALSE; - if (SUCCESS == zend_parse_parameters_none()) { - php_http_env_response_t *r = php_http_env_response_init(NULL, getThis() TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &zstream)) { + if (zstream) { + php_stream_from_zval_no_verify(s, &zstream); - if (r) { - RETVAL_SUCCESS(php_http_env_response_send(r)); - } + if (s) { + php_http_env_response_t *r; + + if ((r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s TSRMLS_CC))) { + RETVAL_SUCCESS(php_http_env_response_send(r)); + php_http_env_response_free(&r); + } + } + } else { + php_http_env_response_t r; - php_http_env_response_free(&r); + if (php_http_env_response_init(&r, getThis(), NULL, NULL TSRMLS_CC)) { + RETVAL_SUCCESS(php_http_env_response_send(&r)); + php_http_env_response_dtor(&r); + } + } } } @@ -911,6 +1317,7 @@ PHP_MINIT_FUNCTION(http_env_response) 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("request"), ZEND_ACC_PROTECTED 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("contentDisposition"), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED TSRMLS_CC); diff --git a/php_http_env_response.h b/php_http_env_response.h index 1031daa..4139656 100644 --- a/php_http_env_response.h +++ b/php_http_env_response.h @@ -13,7 +13,29 @@ #ifndef PHP_HTTP_ENV_RESPONSE_H #define PHP_HTTP_ENV_RESPONSE_H -typedef struct php_http_env_response { +typedef struct php_http_env_response php_http_env_response_t; + +typedef struct php_http_env_response_ops { + STATUS (*init)(php_http_env_response_t *r, void *arg); + void (*dtor)(php_http_env_response_t *r); + long (*get_status)(php_http_env_response_t *r); + STATUS (*set_status)(php_http_env_response_t *r, long http_code); + STATUS (*set_protocol_version)(php_http_env_response_t *r, php_http_version_t *v); + STATUS (*set_header)(php_http_env_response_t *r, const char *fmt, ...); + STATUS (*add_header)(php_http_env_response_t *r, const char *fmt, ...); + STATUS (*del_header)(php_http_env_response_t *r, const char *header_str, size_t header_len); + STATUS (*write)(php_http_env_response_t *r, const char *data_str, size_t data_len); + STATUS (*flush)(php_http_env_response_t *r); + STATUS (*finish)(php_http_env_response_t *r); +} php_http_env_response_ops_t; + +PHP_HTTP_API php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void); +PHP_HTTP_API php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void); + +struct php_http_env_response { + void *ctx; + php_http_env_response_ops_t *ops; + php_http_buffer_t *buffer; zval *options; @@ -41,20 +63,21 @@ typedef struct php_http_env_response { #ifdef ZTS void ***ts; #endif -} php_http_env_response_t; +}; -PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options TSRMLS_DC); +PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *ops_ctx TSRMLS_DC); PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r); PHP_HTTP_API void php_http_env_response_dtor(php_http_env_response_t *r); PHP_HTTP_API void php_http_env_response_free(php_http_env_response_t **r); -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); -PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len TSRMLS_DC); +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, php_http_message_t *request TSRMLS_DC); +PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC); zend_class_entry *php_http_env_response_get_class_entry(void); PHP_METHOD(HttpEnvResponse, __construct); PHP_METHOD(HttpEnvResponse, __invoke); +PHP_METHOD(HttpEnvResponse, setEnvRequest); PHP_METHOD(HttpEnvResponse, setContentType); PHP_METHOD(HttpEnvResponse, setContentDisposition); PHP_METHOD(HttpEnvResponse, setContentEncoding); diff --git a/php_http_headers.c b/php_http_headers.c index 77981b6..71edefd 100644 --- a/php_http_headers.c +++ b/php_http_headers.c @@ -41,6 +41,79 @@ PHP_HTTP_API STATUS php_http_headers_parse(const char *header, size_t length, Ha return SUCCESS; } +PHP_HTTP_API void php_http_headers_to_callback(HashTable *headers, zend_bool crlf, php_http_pass_format_callback_t cb, void *cb_arg TSRMLS_DC) +{ + HashPosition pos1, pos2; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval **header, **single_header; + + FOREACH_HASH_KEYVAL(pos1, headers, key, header) { + if (key.type == HASH_KEY_IS_STRING) { + if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) { + FOREACH_VAL(pos2, *header, single_header) { + if (Z_TYPE_PP(single_header) == IS_ARRAY) { + php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC); + + if (cookie) { + char *buf; + size_t len; + + php_http_cookie_list_to_string(cookie, &buf, &len); + cb(cb_arg, crlf ? "Set-Cookie: %s" PHP_HTTP_CRLF : "Set-Cookie: %s", buf); + php_http_cookie_list_free(&cookie); + efree(buf); + } + } else { + zval *strval = php_http_header_value_to_string(*single_header TSRMLS_CC); + + cb(cb_arg, crlf ? "Set-Cookie: %s" PHP_HTTP_CRLF : "Set-Cookie: %s", Z_STRVAL_P(strval)); + zval_ptr_dtor(&strval); + } + } + } else { + zval *strval = php_http_header_value_to_string(*header TSRMLS_CC); + + cb(cb_arg, crlf ? "%s: %s" PHP_HTTP_CRLF : "%s: %s", key.str, Z_STRVAL_P(strval)); + zval_ptr_dtor(&strval); + } + } + } +} + +PHP_HTTP_API void php_http_headers_to_string(php_http_buffer_t *str, HashTable *headers TSRMLS_DC) +{ + php_http_headers_to_callback(headers, 1, php_http_buffer_appendf, str TSRMLS_CC); +} + +PHP_HTTP_API zval *php_http_header_value_to_string(zval *header TSRMLS_DC) +{ + zval *ret; + + if (Z_TYPE_P(header) == IS_BOOL) { + MAKE_STD_ZVAL(ret); + ZVAL_STRING(ret, Z_BVAL_P(header) ? "true" : "false", 1); + } else if (Z_TYPE_P(header) == IS_ARRAY) { + zval **val; + HashPosition pos; + php_http_buffer_t str; + + php_http_buffer_init(&str); + MAKE_STD_ZVAL(ret); + FOREACH_VAL(pos,header, val) { + zval *strval = php_http_header_value_to_string(*val TSRMLS_CC); + + php_http_buffer_appendf(&str, str.used ? ", %s":"%s", Z_STRVAL_P(strval)); + zval_ptr_dtor(&strval); + } + php_http_buffer_fix(&str); + ZVAL_STRINGL(ret, str.data, str.used, 0); + } else { + ret = php_http_zsep(1, IS_STRING, header); + } + + return ret; +} + #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpHeader, method, 0, req_args) #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpHeader, method, 0) #define PHP_HTTP_HEADER_ME(method, v) PHP_ME(HttpHeader, method, PHP_HTTP_ARGS(HttpHeader, method), v) diff --git a/php_http_headers.h b/php_http_headers.h index 238efb6..8eae254 100644 --- a/php_http_headers.h +++ b/php_http_headers.h @@ -17,6 +17,11 @@ PHP_HTTP_API STATUS php_http_headers_parse(const char *header, size_t length, HashTable *headers, php_http_info_callback_t callback_func, void **callback_data TSRMLS_DC); +PHP_HTTP_API void php_http_headers_to_callback(HashTable *headers, zend_bool crlf, php_http_pass_format_callback_t cb, void *cb_arg TSRMLS_DC); +PHP_HTTP_API void php_http_headers_to_string(php_http_buffer_t *str, HashTable *headers TSRMLS_DC); + +PHP_HTTP_API zval *php_http_header_value_to_string(zval *header TSRMLS_DC); + zend_class_entry *php_http_header_get_class_entry(void); PHP_METHOD(HttpHeader, __construct); diff --git a/php_http_message.c b/php_http_message.c index 39f857d..d063b21 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -12,7 +12,6 @@ #include "php_http_api.h" -static zval *message_header_strval(zval **header TSRMLS_DC); static void message_headers(php_http_message_t *msg, php_http_buffer_t *str); PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC) @@ -148,7 +147,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, return msg; } -PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_str, size_t key_len, int join) +PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join) { zval *ret = NULL, **header; char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1); @@ -157,7 +156,7 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st if (join && Z_TYPE_PP(header) == IS_ARRAY) { TSRMLS_FETCH_FROM_CTX(msg->ts); - ret = message_header_strval(header TSRMLS_CC); + ret = php_http_header_value_to_string(*header TSRMLS_CC); } else { Z_ADDREF_PP(header); ret = *header; @@ -305,40 +304,8 @@ PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg) } } -static zval *message_header_strval(zval **header TSRMLS_DC) -{ - zval *ret; - - if (Z_TYPE_PP(header) == IS_BOOL) { - MAKE_STD_ZVAL(ret); - ZVAL_STRING(ret, Z_BVAL_PP(header) ? "true" : "false", 1); - } else if (Z_TYPE_PP(header) == IS_ARRAY) { - zval **val; - HashPosition pos; - php_http_buffer_t str; - - php_http_buffer_init(&str); - MAKE_STD_ZVAL(ret); - FOREACH_VAL(pos, *header, val) { - zval *strval = message_header_strval(val TSRMLS_CC); - - php_http_buffer_appendf(&str, str.used ? ", %s":"%s", Z_STRVAL_P(strval)); - zval_ptr_dtor(&strval); - } - php_http_buffer_fix(&str); - ZVAL_STRINGL(ret, str.data, str.used, 0); - } else { - ret = php_http_zsep(1, IS_STRING, *header); - } - - return ret; -} - static void message_headers(php_http_message_t *msg, php_http_buffer_t *str) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - HashPosition pos1; - zval **header; TSRMLS_FETCH_FROM_CTX(msg->ts); switch (msg->type) { @@ -355,41 +322,7 @@ static void message_headers(php_http_message_t *msg, php_http_buffer_t *str) } php_http_message_update_headers(msg); - - FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) { - if (key.type == HASH_KEY_IS_STRING) { - if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) { - HashPosition pos2; - zval **single_header; - - FOREACH_VAL(pos2, *header, single_header) { - if (Z_TYPE_PP(single_header) == IS_ARRAY) { - php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC); - - if (cookie) { - char *buf; - size_t len; - - php_http_cookie_list_to_string(cookie, &buf, &len); - php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, buf); - php_http_cookie_list_free(&cookie); - efree(buf); - } - } else { - zval *strval = message_header_strval(single_header TSRMLS_CC); - - php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, Z_STRVAL_P(strval)); - zval_ptr_dtor(&strval); - } - } - } else { - zval *strval = message_header_strval(header TSRMLS_CC); - - php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_P(strval)); - zval_ptr_dtor(&strval); - } - } - } + php_http_headers_to_string(str, &msg->hdrs TSRMLS_CC); } PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg) @@ -1100,11 +1033,14 @@ zend_object_value php_http_message_object_new_ex(zend_class_entry *ce, php_http_ } if (msg) { + php_http_message_body_object_t *body_obj; + o->message = msg; if (msg->parent) { o->parent = php_http_message_object_new_ex(ce, msg->parent, NULL TSRMLS_CC); } - o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), php_http_message_body_copy(&msg->body, NULL, 0), NULL TSRMLS_CC); + o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), &msg->body, &body_obj TSRMLS_CC); + body_obj->shared = 1; } ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC); @@ -1344,8 +1280,12 @@ PHP_METHOD(HttpMessage, getBody) if (!obj->message) { obj->message = php_http_message_init(NULL, 0 TSRMLS_CC); } - - if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC)) { + if (!obj->body.handle) { + php_http_message_body_object_t *body_obj; + obj->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), &obj->message->body, &body_obj TSRMLS_CC); + body_obj->shared = 1; + } + if (obj->body.handle) { RETVAL_OBJVAL(obj->body, 1); } } @@ -1911,7 +1851,7 @@ PHP_METHOD(HttpMessage, detach) obj->message = php_http_message_init(NULL, 0 TSRMLS_CC); } - RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy(obj->message, NULL), NULL TSRMLS_CC), 0); + RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy_ex(obj->message, NULL, 0), NULL TSRMLS_CC), 0); } } end_error_handling(); } diff --git a/php_http_message.h b/php_http_message.h index f4bc22c..cb4c116 100644 --- a/php_http_message.h +++ b/php_http_message.h @@ -52,7 +52,7 @@ PHP_HTTP_API void php_http_message_set_info(php_http_message_t *message, php_htt PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg); -PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_str, size_t key_len, int join); +PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join); PHP_HTTP_API zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary); PHP_HTTP_API void php_http_message_to_string(php_http_message_t *msg, char **string, size_t *length); diff --git a/php_http_message_body.c b/php_http_message_body.c index d094db6..45fe95e 100644 --- a/php_http_message_body.c +++ b/php_http_message_body.c @@ -650,8 +650,9 @@ void php_http_message_body_object_free(void *object TSRMLS_DC) { php_http_message_body_object_t *obj = object; - php_http_message_body_free(&obj->body); - + if (!obj->shared) { + php_http_message_body_free(&obj->body); + } zend_object_std_dtor((zend_object *) obj TSRMLS_CC); efree(obj); } @@ -668,7 +669,7 @@ PHP_METHOD(HttpMessageBody, __construct) php_stream_from_zval(stream, &zstream); if (stream) { - if (obj->body) { + if (obj->body && !obj->shared) { php_http_message_body_dtor(obj->body); } obj->body = php_http_message_body_init(obj->body, stream TSRMLS_CC); diff --git a/php_http_message_body.h b/php_http_message_body.h index 165dc3f..1e6f4ea 100644 --- a/php_http_message_body.h +++ b/php_http_message_body.h @@ -54,6 +54,7 @@ static inline php_stream *php_http_message_body_stream(php_http_message_body_t * typedef struct php_http_message_body_object { zend_object zo; php_http_message_body_t *body; + unsigned shared:1; } php_http_message_body_object_t; zend_class_entry *php_http_message_body_get_class_entry(void); diff --git a/php_http_misc.h b/php_http_misc.h index 3487360..69076e7 100644 --- a/php_http_misc.h +++ b/php_http_misc.h @@ -377,6 +377,7 @@ int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list typedef size_t (*php_http_pass_callback_t)(void *cb_arg, const char *str, size_t len); typedef size_t (*php_http_pass_php_http_buffer_callback_t)(void *cb_arg, php_http_buffer_t *str); +typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...); typedef struct php_http_pass_fcall_arg { zval *fcz; diff --git a/php_http_negotiate.h b/php_http_negotiate.h index 23c03ae..95e2fa1 100644 --- a/php_http_negotiate.h +++ b/php_http_negotiate.h @@ -15,11 +15,11 @@ PHP_HTTP_API HashTable *php_http_negotiate(const char *value_str, size_t value_len, HashTable *supported, const char *primary_sep_str, size_t primary_sep_len TSRMLS_DC); -static inline HashTable *php_http_negotiate_language(HashTable *supported TSRMLS_DC) +static inline HashTable *php_http_negotiate_language(HashTable *supported, php_http_message_t *request TSRMLS_DC) { HashTable *result = NULL; size_t length; - char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Language"), &length TSRMLS_CC); + char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Language"), &length, request TSRMLS_CC); if (value) { result = php_http_negotiate(value, length, supported, "-", 1 TSRMLS_CC); @@ -29,11 +29,11 @@ static inline HashTable *php_http_negotiate_language(HashTable *supported TSRMLS return result; } -static inline HashTable *php_http_negotiate_encoding(HashTable *supported TSRMLS_DC) +static inline HashTable *php_http_negotiate_encoding(HashTable *supported, php_http_message_t *request TSRMLS_DC) { HashTable *result = NULL; size_t length; - char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Encoding"), &length TSRMLS_CC); + char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Encoding"), &length, request TSRMLS_CC); if (value) { result = php_http_negotiate(value, length, supported, NULL, 0 TSRMLS_CC); @@ -43,11 +43,11 @@ static inline HashTable *php_http_negotiate_encoding(HashTable *supported TSRMLS return result; } -static inline HashTable *php_http_negotiate_charset(HashTable *supported TSRMLS_DC) +static inline HashTable *php_http_negotiate_charset(HashTable *supported, php_http_message_t *request TSRMLS_DC) { HashTable *result = NULL; size_t length; - char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Charset"), &length TSRMLS_CC); + char *value = php_http_env_get_request_header(ZEND_STRL("Accept-Charset"), &length, request TSRMLS_CC); if (value) { result = php_http_negotiate(value, length, supported, NULL, 0 TSRMLS_CC); @@ -57,11 +57,11 @@ static inline HashTable *php_http_negotiate_charset(HashTable *supported TSRMLS_ return result; } -static inline HashTable *php_http_negotiate_content_type(HashTable *supported TSRMLS_DC) +static inline HashTable *php_http_negotiate_content_type(HashTable *supported, php_http_message_t *request TSRMLS_DC) { HashTable *result = NULL; size_t length; - char *value = php_http_env_get_request_header(ZEND_STRL("Accept"), &length TSRMLS_CC); + char *value = php_http_env_get_request_header(ZEND_STRL("Accept"), &length, request TSRMLS_CC); if (value) { result = php_http_negotiate(value, length, supported, "/", 1 TSRMLS_CC); @@ -119,7 +119,7 @@ static inline HashTable *php_http_negotiate_content_type(HashTable *supported TS #define PHP_HTTP_DO_NEGOTIATE(type, supported, rs_array) \ { \ HashTable *result; \ - if ((result = php_http_negotiate_ ##type(supported TSRMLS_CC))) { \ + if ((result = php_http_negotiate_ ##type(supported, NULL TSRMLS_CC))) { \ PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \ } else { \ PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \ diff --git a/php_http_params.c b/php_http_params.c index 43447d9..be627dc 100644 --- a/php_http_params.c +++ b/php_http_params.c @@ -599,6 +599,7 @@ static void shift_arg(php_http_buffer_t *buf, char *key_str, size_t key_len, zva php_http_array_hashkey_t key = php_http_array_hashkey_init(0); zval **val; + shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC); FOREACH_KEYVAL(pos, zvalue, key, val) { /* did you mean recursion? */ php_http_array_hashkey_stringify(&key); diff --git a/phpunit/MessageTest.php b/phpunit/MessageTest.php index 6f42524..6dd5b70 100644 --- a/phpunit/MessageTest.php +++ b/phpunit/MessageTest.php @@ -155,5 +155,103 @@ class MessageTest extends PHPUnit_Framework_TestCase $m->toString() ); } + + function testDetach() { + $m = new http\Message( + "HTTP/1.1 200 Ok\r\n". + "HTTP/1.1 201 Created\n". + "HTTP/1.1 302 Found\r\n" + ); + + $this->assertCount(3, $m); + $d = $m->detach(); + $this->assertCount(3, $m); + $this->assertCount(1, $d); + + $this->assertEquals("HTTP/1.1 302 Found\r\n\r\n", $d->toString(true)); + } + + function testPrepend() { + for ($i=0; $i<9; ++$i) { + $a[] = new http\Message("HTTP/1.1 ".($i+200)); + } + + foreach ($a as $m) { + if (isset($p)) $m->prepend($p); + $p = $m; + } + + $this->assertEquals( + "HTTP/1.1 200\r\n\r\n". + "HTTP/1.1 201\r\n\r\n". + "HTTP/1.1 202\r\n\r\n". + "HTTP/1.1 203\r\n\r\n". + "HTTP/1.1 204\r\n\r\n". + "HTTP/1.1 205\r\n\r\n". + "HTTP/1.1 206\r\n\r\n". + "HTTP/1.1 207\r\n\r\n". + "HTTP/1.1 208\r\n\r\n", + $m->toString(true) + ); + } + + function testPrependError() { + $m = new http\Message("HTTP/1.1 200\r\nHTTP/1.1 201"); + try { + $m->prepend($m->getParentMessage()); + $this->assertFalse("this code should not be reached"); + } catch (http\Exception $e) { + $this->assertEquals("Cannot prepend a message located within the same message chain", $e->getMessage()); + } + } + + function testToCallback() { + $m = new http\Message("HTTP/1.1 200 Ok"); + $m->addHeader("Content-Type", "text/plain"); + $m->getBody()->append("this\nis\nthe\ntext"); + + $d = new http\Encoding\Stream\Deflate; + $s = ""; + $m->toCallback(function ($m, $data) use ($d) { + $s.=$d->update($data); + }); + $s.=$d->finish(); + $this->assertEquals($m->toString(), http\Encoding\Stream\Inflate::decode($s)); + } + + function testToStream() { + $m = new http\Message("HTTP/1.1 200 Ok"); + $m->addHeader("Content-Type", "text/plain"); + $m->getBody()->append("this\nis\nthe\ntext"); + + $f = tmpfile(); + $m->toStream($f); + rewind($f); + $this->assertEquals((string) $m, stream_get_contents($f)); + fclose($f); + } + + function testBoundary() { + $p = new http\Message; + $p->addHeader("Content-Type", "text/plain"); + $p->getBody()->append("data"); + + $m = new http\Message("HTTP/1.1 200"); + $m->getBody()->addPart($p); + $this->assertStringMatchesFormat( + "HTTP/1.1 200\r\n". + "Content-Length: 97\r\n". + "Content-Type: multipart/form-data; boundary=\"%x.%x\"\r\n". + "\r\n". + "--%x.%x\r\n". + "Content-Type: text/plain\r\n". + "Content-Length: 4\r\n". + "\r\n". + "data\r\n". + "--%x.%x--\r\n". + "", + str_replace("\r", "", $m->toString()) // phpunit replaces \r\n with \n + ); + } } diff --git a/tests/envresponsebody001.phpt b/tests/envresponsebody001.phpt new file mode 100644 index 0000000..616760e --- /dev/null +++ b/tests/envresponsebody001.phpt @@ -0,0 +1,20 @@ +--TEST-- +env response body +--SKIPIF-- + +--INI-- +output_buffering=1 +--FILE-- +getBody()); +?> +Done +--EXPECTF-- +Test +string(5) "Test +" +Done diff --git a/tests/envresponsebody002.phpt b/tests/envresponsebody002.phpt new file mode 100644 index 0000000..f9b33c5 --- /dev/null +++ b/tests/envresponsebody002.phpt @@ -0,0 +1,24 @@ +--TEST-- +env response body error +--SKIPIF-- + +--INI-- +output_buffering=1 +--FILE-- +getBody()); +} catch (http\Exception $e) { + echo $e->getMessage(),"\n"; +} +?> +Done +--EXPECTF-- +Test +Could not fetch response body, output has already been sent at %senvresponsebody002.php:3 +Done diff --git a/tests/message003.phpt b/tests/message003.phpt index 2f2b514..69be990 100644 --- a/tests/message003.phpt +++ b/tests/message003.phpt @@ -8,15 +8,15 @@ $m = new http\Message(fopen(__DIR__."/data/message_r_multipart_put.txt","rb")); if ($m->isMultipart($boundary)) { var_dump($boundary); - foreach ($m->splitMultipartBody() as $mm) { - echo "===\n",$mm,"===\n"; + foreach ($m->splitMultipartBody() as $i => $mm) { + echo "==$i==\n",$mm,"===\n"; } } ?> DONE --EXPECTF-- string(40) "----------------------------6e182425881c" -=== +==%d== Content-Disposition: form-data; name="composer"; filename="composer.json" Content-Type: application/octet-stream Content-Length: 567 @@ -46,7 +46,7 @@ Content-Length: 567 } === -=== +==%d== Content-Disposition: form-data; name="LICENSE"; filename="LICENSE" Content-Type: application/octet-stream Content-Length: 1354 diff --git a/tests/message005.phpt b/tests/message005.phpt index 4cf3e23..6f7a546 100644 --- a/tests/message005.phpt +++ b/tests/message005.phpt @@ -6,6 +6,7 @@ message cloning +--FILE-- +addHeader("foo", array("bar","baz")); +$r->getBody()->append("foo"); + +$r->send($f); + +rewind($f); +var_dump(stream_get_contents($f)); +?> +Done +--EXPECT-- +Test +string(77) "HTTP/1.1 200 OK +Accept-Ranges: bytes +Foo: bar, baz +ETag: "8c736521" + +foo" +Done diff --git a/tests/response007.phpt b/tests/response007.phpt new file mode 100644 index 0000000..dc97c8e --- /dev/null +++ b/tests/response007.phpt @@ -0,0 +1,39 @@ +--TEST-- +response env request +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setHeader("Range", "bytes=2-4"); + +$res = new http\Env\Response; +$res->setEnvRequest($req); +$res->setContentType("text/plain"); +$res->getBody()->append("012345679"); +$res->send($tmp); + +rewind($tmp); +var_dump(stream_get_contents($tmp)); + +?> +Done +--EXPECTF-- +Test +string(141) "HTTP/1.1 206 Partial Content%c +Accept-Ranges: bytes%c +X-Powered-By: PHP/5.4.11-dev%c +Content-Type: text/plain%c +Content-Range: bytes 2-4/9%c +%c +234" +Done diff --git a/tests/response008.phpt b/tests/response008.phpt new file mode 100644 index 0000000..379ab57 --- /dev/null +++ b/tests/response008.phpt @@ -0,0 +1,30 @@ +--TEST-- +env response stream message +--SKIPIF-- + +--ENV-- +HTTP_ACCEPT_ENCODING=gzip +--FILE-- +setHeader("foo","bar"); +$r->setContentEncoding(http\env\Response::CONTENT_ENCODING_GZIP); +$r->setBody(new http\message\Body(fopen(__FILE__,"r"))); +$r->send($f); + +rewind($f); +var_dump(stream_get_contents($f)); + +--EXPECTREGEX-- +string\(\d+\) "HTTP\/1\.1 200 OK +Accept-Ranges: bytes +Foo: bar +Content-Encoding: gzip +Vary: Accept-Encoding +ETag: "\w+-\w+-\w+" +Last-Modified: \w+, \d+ \w+ \d{4} \d\d:\d\d:\d\d GMT + +\x1f\x8b\x08.+ diff --git a/tests/response009.phpt b/tests/response009.phpt new file mode 100644 index 0000000..4743004 --- /dev/null +++ b/tests/response009.phpt @@ -0,0 +1,39 @@ +--TEST-- +http response stream cache negative +--SKIPIF-- + +--GET-- +a=b +--ENV-- +HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:30 GMT +HTTP_IF_NONE_MATCH=0000-00-0000 +--FILE-- +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +?> +--EXPECTF-- +string(355) "HTTP/1.1 200 OK%c +Accept-Ranges: bytes%c +X-Powered-By: %s%c +ETag: "abc"%c +Last-Modified: %s%c +%c +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +?> +" + diff --git a/tests/response010.phpt b/tests/response010.phpt new file mode 100644 index 0000000..8a3c1ae --- /dev/null +++ b/tests/response010.phpt @@ -0,0 +1,29 @@ +--TEST-- +response stream ranges +--SKIPIF-- + +--ENV-- +HTTP_RANGE=bytes=2-4 +--GET-- +a=b +--FILE-- +setContentType("text/plain"); +$r->setContentDisposition( + array("attachment" => array(array("filename" => basename(__FILE__)))) +); +$r->setBody(new http\Message\Body(fopen(__FILE__, "rb"))); +$r->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +?> +--EXPECTF-- +string(%i) "HTTP/1.1 206 Partial Content%c +Accept-Ranges: bytes%c +X-Powered-By: PHP/%s%c +Content-Type: text/plain%c +Content-Range: bytes 2-4/311%c +%c +php" diff --git a/tests/response011.phpt b/tests/response011.phpt new file mode 100644 index 0000000..701da22 --- /dev/null +++ b/tests/response011.phpt @@ -0,0 +1,24 @@ +--TEST-- +http response cache positive with env message +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setHeader("If-Modified-Since", "Fri, 13 Feb 2009 23:31:32 GMT"); +$r = new http\Env\Response; +$r->setEnvRequest($e); +$r->setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->isCachedByEtag("If-None-Match") and die("Huh? etag? really?\n"); +$r->isCachedByLastModified("If-Modified-Since") or die("yep, I should be cached"); +$r->send(); +?> +--EXPECTHEADERS-- +HTTP/1.1 304 Not Modified +ETag: "abc" +Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT +--EXPECT-- diff --git a/tests/response012.phpt b/tests/response012.phpt new file mode 100644 index 0000000..5e9ed57 --- /dev/null +++ b/tests/response012.phpt @@ -0,0 +1,29 @@ +--TEST-- +response content disposition +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setContentDisposition(array("attachment"=>array("filename"=>basename(__FILE__)))); +$r->setBody(new http\Message\Body(fopen(__FILE__,"r"))); +$r->send(); + +?> +--EXPECTHEADERS-- +Content-Disposition: attachment;filename=response012.php +--EXPECT-- +setContentDisposition(array("attachment"=>array("filename"=>basename(__FILE__)))); +$r->setBody(new http\Message\Body(fopen(__FILE__,"r"))); +$r->send(); + +?> + diff --git a/tests/response013.phpt b/tests/response013.phpt new file mode 100644 index 0000000..75b4163 --- /dev/null +++ b/tests/response013.phpt @@ -0,0 +1,29 @@ +--TEST-- +response deflate +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setHeader("Accept-Encoding", "deflate"); + +$res = new http\Env\Response; +$res->setCacheControl("public, max-age=3600"); +$res->setContentEncoding(http\Env\Response::CONTENT_ENCODING_GZIP); +$res->getBody()->append("foobar"); + +$res->setEnvRequest($req); +$res->send(); +?> +--EXPECTHEADERS-- +Content-Encoding: deflate +Vary: Accept-Encoding +Cache-Control: public, max-age=3600 +--EXPECTREGEX-- +^\x78\x9c.+ + diff --git a/tests/response014.phpt b/tests/response014.phpt new file mode 100644 index 0000000..5478a61 --- /dev/null +++ b/tests/response014.phpt @@ -0,0 +1,24 @@ +--TEST-- +response invalid ranges +--SKIPIF-- + +--FILE-- +setHeader("Range", "bytes=321-123,123-0"); +$res = new http\Env\Response; +$res->getBody()->append("foobar"); +$res->setEnvRequest($req); +$res->send($f); +rewind($f); +var_dump(stream_get_contents($f)); +--EXPECTF-- +string(96) "HTTP/1.1 416 Requested Range Not Satisfiable +Accept-Ranges: bytes +Content-Range: bytes */6 + +" + diff --git a/tests/version001.phpt b/tests/version001.phpt new file mode 100644 index 0000000..513875d --- /dev/null +++ b/tests/version001.phpt @@ -0,0 +1,17 @@ +--TEST-- +version parse error +--SKIPIF-- + +--FILE-- +setHttpVersion("1-1"); +$m->setHttpVersion("one.one"); +?> +--EXPECTF-- +Notice: http\Message::setHttpVersion(): Non-standard version separator '-' in HTTP protocol version '1-1' in %s + +Warning: http\Message::setHttpVersion(): Could not parse HTTP protocol version 'one.one' in %s +