#include "php.h"
#include "php_version.h"
#include "php_streams.h"
+#include "php_output.h"
#include "snprintf.h"
#include "ext/standard/md5.h"
#include "ext/standard/url.h"
#include "ext/standard/base64.h"
#include "ext/standard/php_string.h"
-#include "ext/standard/php_smart_str.h"
#include "ext/standard/php_lcg.h"
#include "SAPI.h"
static int http_sort_q(const void *a, const void *b TSRMLS_DC);
#define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
-static STATUS _http_send_chunk(const void *data, const size_t begin, const size_t end, const http_send_mode mode TSRMLS_DC);
+static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_send_mode mode TSRMLS_DC);
static int check_day(char *day, size_t len);
static int check_month(char *month);
/* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
http_send_mode) */
-static STATUS _http_send_chunk(const void *data, const size_t begin,
- const size_t end, const http_send_mode mode TSRMLS_DC)
+static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_send_mode mode TSRMLS_DC)
{
- char *buf;
- size_t read = 0;
long len = end - begin;
- php_stream *s;
switch (mode)
{
case SEND_RSRC:
- s = (php_stream *) data;
+ {
+ char *buf;
+ size_t read = 0;
+ php_stream *s = (php_stream *) data;
+
if (php_stream_seek(s, begin, SEEK_SET)) {
return FAILURE;
}
+
buf = (char *) ecalloc(1, HTTP_SENDBUF_SIZE);
/* read into buf and write out */
while ((len -= HTTP_SENDBUF_SIZE) >= 0) {
efree(buf);
return FAILURE;
}
+ /* ob_flush() && flush() */
+ php_end_ob_buffer(1, 1 TSRMLS_CC);
+ sapi_flush(TSRMLS_C);
}
/* read & write left over */
efree(buf);
return FAILURE;
}
+ /* ob_flush() & flush() */
+ php_end_ob_buffer(1, 1 TSRMLS_CC);
+ sapi_flush(TSRMLS_C);
}
efree(buf);
return SUCCESS;
- break;
+ }
case SEND_DATA:
- return len == php_body_write(((char *)data) + begin, len TSRMLS_CC)
- ? SUCCESS : FAILURE;
- break;
+ {
+ char *s = (char *) data + begin;
+
+ while ((len -= HTTP_SENDBUF_SIZE) >= 0) {
+ if (HTTP_SENDBUF_SIZE - php_body_write(s, HTTP_SENDBUF_SIZE TSRMLS_CC)) {
+ return FAILURE;
+ }
+ s += HTTP_SENDBUF_SIZE;
+ /* ob_flush() & flush() */
+ php_end_ob_buffer(1, 1 TSRMLS_CC);
+ sapi_flush(TSRMLS_C);
+ }
+
+ /* write left over */
+ if (len) {
+ if (HTTP_SENDBUF_SIZE + len - php_body_write(s, HTTP_SENDBUF_SIZE + len TSRMLS_CC)) {
+ return FAILURE;
+ }
+ /* ob_flush() & flush() */
+ php_end_ob_buffer(1, 1 TSRMLS_CC);
+ sapi_flush(TSRMLS_C);
+ }
+ return SUCCESS;
+ }
default:
return FAILURE;
}
/* }}} */
-/* char *pretty_key(char *, int, int, int) */
-char *pretty_key(char *key, int key_len, int uctitle, int xhyphen)
+/* char *pretty_key(char *, size_t, zend_bool, zebd_bool) */
+char *pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
{
if (key && key_len) {
- int i, wasalpha;
+ unsigned i, wasalpha;
if (wasalpha = isalpha(key[0])) {
key[0] = uctitle ? toupper(key[0]) : tolower(key[0]);
}
/* }}} */
/* {{{ char *http_etag(void *, size_t, http_send_mode) */
-PHP_HTTP_API char *_http_etag(const void *data_ptr, const size_t data_len,
- const http_send_mode data_mode TSRMLS_DC)
+PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
{
char ssb_buf[128] = {0};
unsigned char digest[16];
/* }}} */
/* {{{ time_t http_lmod(void *, http_send_mode) */
-PHP_HTTP_API time_t _http_lmod(const void *data_ptr, const http_send_mode data_mode TSRMLS_DC)
+PHP_HTTP_API time_t _http_lmod(const void *data_ptr, http_send_mode data_mode TSRMLS_DC)
{
switch (data_mode)
{
/* }}} */
/* {{{ STATUS http_send_status_header(int, char *) */
-PHP_HTTP_API STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_status_header(int status, const char *header TSRMLS_DC)
{
sapi_header_line h = {(char *) header, strlen(header), status};
return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
}
/* }}} */
-/* {{{ zval *http_get_server_var(char *) */
-PHP_HTTP_API zval *_http_get_server_var(const char *key TSRMLS_DC)
+/* {{{ zval *http_get_server_var_ex(char *, size_t) */
+PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
{
zval **var;
- if (SUCCESS == zend_hash_find(
- HTTP_SERVER_VARS,
- (char *) key, strlen(key) + 1, (void **) &var)) {
- return *var;
+ if (SUCCESS == zend_hash_find(HTTP_SERVER_VARS, (char *) key, key_size, (void **) &var)) {
+ if (check) {
+ return Z_STRVAL_PP(var) && Z_STRLEN_PP(var) ? *var : NULL;
+ } else {
+ return *var;
+ }
}
return NULL;
}
if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
http_send_status(304);
+ zend_bailout();
} else {
http_send_etag(etag, 32);
}
}
/* }}} */
-/* {{{ int http_modified_match(char *, int) */
-PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
+/* {{{ zend_bool http_modified_match(char *, time_t) */
+PHP_HTTP_API zend_bool _http_modified_match_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC)
{
- int retval;
+ zend_bool retval;
zval *zmodified;
char *modified, *chr_ptr;
- HTTP_GSC(zmodified, entry, 0);
+ HTTP_GSC(zmodified, entry, !enforce_presence);
modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
if (chr_ptr = strrchr(modified, ';')) {
}
/* }}} */
-/* {{{ int http_etag_match(char *, char *) */
-PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
+/* {{{ zend_bool http_etag_match(char *, char *) */
+PHP_HTTP_API zend_bool _http_etag_match_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC)
{
zval *zetag;
char *quoted_etag;
- STATUS result;
+ zend_bool result;
- HTTP_GSC(zetag, entry, 0);
+ HTTP_GSC(zetag, entry, !enforce_presence);
if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
return 1;
/* }}} */
/* {{{ STATUS http_send_last_modified(int) */
-PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_last_modified(time_t t TSRMLS_DC)
{
char *date = NULL;
if (date = http_date(t)) {
}
/* }}} */
-/* {{{ static STATUS http_send_etag(char *, int) */
-PHP_HTTP_API STATUS _http_send_etag(const char *etag,
- const int etag_len TSRMLS_DC)
+/* {{{ STATUS http_send_etag(char *, size_t) */
+PHP_HTTP_API STATUS _http_send_etag(const char *etag, size_t etag_len TSRMLS_DC)
{
STATUS status;
char *etag_header;
/* }}} */
/* {{{ STATUS http_send_cache_control(char *, size_t) */
-PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control,
- const size_t cc_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_cache_control(const char *cache_control, size_t cc_len TSRMLS_DC)
{
STATUS status;
char *cc_header = ecalloc(1, sizeof("Cache-Control: ") + cc_len);
/* }}} */
/* {{{ STATUS http_send_content_type(char *, size_t) */
-PHP_HTTP_API STATUS _http_send_content_type(const char *content_type,
- const size_t ct_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC)
{
STATUS status;
char *ct_header;
/* }}} */
/* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
-PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename,
- const size_t f_len, const int send_inline TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t f_len, zend_bool send_inline TSRMLS_DC)
{
STATUS status;
char *cd_header;
/* }}} */
/* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
-PHP_HTTP_API STATUS _http_cache_last_modified(const time_t last_modified,
- const time_t send_modified, const char *cache_control, const size_t cc_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
+ time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
{
if (cc_len) {
http_send_cache_control(cache_control, cc_len);
/* }}} */
/* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
-PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len,
- const char *cache_control, const size_t cc_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
+ const char *cache_control, size_t cc_len TSRMLS_DC)
{
if (cc_len) {
http_send_cache_control(cache_control, cc_len);
return NULL;
}
- if (!(purl = php_url_parse(url))) {
+ if (!(purl = php_url_parse((char *) url))) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse supplied URL");
return NULL;
}
furl.query = purl->query;
furl.fragment = purl->fragment;
- if (proto) {
+ if (proto && proto_len) {
furl.scheme = scheme = estrdup(proto);
} else if (purl->scheme) {
furl.scheme = purl->scheme;
#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
if (se = getservbyname(furl.scheme, "tcp")) {
furl.port = se->s_port;
- } else
+ }
#endif
- furl.port = 80;
} else {
- furl.port = (furl.scheme[5] == 's') ? 443 : 80;
+ furl.port = (furl.scheme[4] == 's') ? 443 : 80;
}
if (host) {
}
/* }}} */
-/* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
-PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
- const char *def TSRMLS_DC)
+/* {{{ char *http_negotiate_q(char *, HashTable *, char *) */
+PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *supported, const char *def TSRMLS_DC)
{
- zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
- char *q_ptr, *result;
- int i, c;
+ zval *zaccept, zdelim, zarray, zentries, **zentry, **zsupp;
+ char *q_ptr = NULL, *key = NULL;
+ int i = 0;
+ ulong idx = 0;
double qual;
HTTP_GSC(zaccept, entry, estrdup(def));
- MAKE_STD_ZVAL(zarray);
- array_init(zarray);
-
- MAKE_STD_ZVAL(zdelim);
- ZVAL_STRING(zdelim, ",", 0);
- php_explode(zdelim, zaccept, zarray, -1);
- efree(zdelim);
+ array_init(&zarray);
+ array_init(&zentries);
- MAKE_STD_ZVAL(zentries);
- array_init(zentries);
+ Z_STRVAL(zdelim) = ",";
+ Z_STRLEN(zdelim) = 1;
- c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
- for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
-
- if (SUCCESS != zend_hash_get_current_data(
- Z_ARRVAL_P(zarray), (void **) &zentry)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
- break;
- }
+ php_explode(&zdelim, zaccept, &zarray, -1);
- /* check for qualifier */
- if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
+ FOREACH_HASH_VAL(Z_ARRVAL(zarray), zentry) {
+ if (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';')) {
qual = strtod(q_ptr + 3, NULL);
+ *q_ptr = 0;
+ q_ptr = NULL;
} else {
- qual = 1000.0 - i;
+ qual = 1000.0 - i++;
}
-
- /* walk through the supported array */
- FOREACH_VAL(supported, zsupp) {
+ FOREACH_HASH_VAL((HashTable *)supported, zsupp) {
if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
- add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);
+ add_assoc_double(&zentries, Z_STRVAL_PP(zsupp), qual);
break;
}
}
}
+ zval_dtor(&zarray);
- zval_dtor(zarray);
- efree(zarray);
+ zend_hash_sort(Z_ARRVAL(zentries), zend_qsort, http_sort_q, 0 TSRMLS_CC);
- zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
-
- if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
- http_sort_q, 0 TSRMLS_CC)) ||
- (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
- Z_ARRVAL_P(zentries), &result, 0, 1))) {
- result = estrdup(def);
+ FOREACH_HASH_KEY(Z_ARRVAL(zentries), key, idx) {
+ if (key) {
+ key = estrdup(key);
+ zval_dtor(&zentries);
+ return key;
+ }
}
+ zval_dtor(&zentries);
- zval_dtor(zentries);
- efree(zentries);
-
- return result;
+ return estrdup(def);
}
/* }}} */
/* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
-PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges,
- const size_t length TSRMLS_DC)
+PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC)
{
zval *zrange;
char *range, c;
{
/* "0-12345" */
case -10:
- if ((length - end) < 1) {
+ /* "0-" */
+ if (end == -1) {
+ return RANGE_NO;
+ }
+ /* "0-0" or overflow */
+ if (end == -10 || length <= end) {
return RANGE_ERR;
}
begin = 0;
/* "-12345" */
case -1:
- if ((length - end) < 1) {
+ /* "-", "-0" or overflow */
+ if (end == -1 || end == -10 || length <= end) {
return RANGE_ERR;
}
begin = length - end;
- end = length;
+ end = length - 1;
break;
/* "12345-(xxx)" */
default:
switch (end)
{
+ /* "12345-0" */
+ case -10:
+ return RANGE_ERR;
+ break;
+
/* "12345-" */
case -1:
- if ((length - begin) < 1) {
+ if (length <= begin) {
return RANGE_ERR;
}
end = length - 1;
/* "12345-67890" */
default:
- if ( ((length - begin) < 1) ||
- ((length - end) < 1) ||
- ((begin - end) >= 0)) {
+ if ( (length <= begin) ||
+ (length <= end) ||
+ (end < begin)) {
return RANGE_ERR;
}
break;
/* }}} */
/* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
-PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_t size, http_send_mode mode TSRMLS_DC)
{
long **begin, **end;
zval **zrange;
/* }}} */
/* {{{ STATUS http_send(void *, size_t, http_send_mode) */
-PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
- const http_send_mode data_mode TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send(const void *data_ptr, size_t data_size, http_send_mode data_mode TSRMLS_DC)
{
- int is_range_request = http_is_range_request();
+ HashTable ranges;
+ http_range_status range_status;
+ int cache_etag = 0;
if (!data_ptr) {
return FAILURE;
return SUCCESS;
}
- /* etag handling */
- if (HTTP_G(etag_started)) {
- char *etag;
+ /* stop on-the-fly etag generation */
+ if (cache_etag = HTTP_G(etag_started)) {
/* interrupt */
HTTP_G(etag_started) = 0;
/* never ever use the output to compute the ETag if http_send() is used */
- php_end_ob_buffer(0, 0 TSRMLS_CC);
- if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
- return FAILURE;
- }
-
- /* send 304 Not Modified if etag matches */
- if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
- efree(etag);
- return http_send_status(304);
- }
-
- http_send_etag(etag, 32);
- efree(etag);
+ php_end_ob_buffers(0 TSRMLS_CC);
}
- /* send 304 Not Modified if last-modified matches*/
- if ((!is_range_request) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
- return http_send_status(304);
- }
+ zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
+ range_status = http_get_request_ranges(&ranges, data_size);
- if (is_range_request) {
+ if (range_status == RANGE_ERR) {
+ zend_hash_destroy(&ranges);
+ http_send_status(416);
+ return FAILURE;
+ }
- /* only send ranges if entity hasn't changed */
- if (
- ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
- http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
- &&
- ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
- http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
- ) {
+ /* Range Request - only send ranges if entity hasn't changed */
+ if ( range_status == RANGE_OK &&
+ http_etag_match_ex("HTTP_IF_MATCH", HTTP_G(etag), 0) &&
+ http_modified_match_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod), 0)) {
+ STATUS result = http_send_ranges(&ranges, data_ptr, data_size, data_mode);
+ zend_hash_destroy(&ranges);
+ return result;
+ }
- STATUS result = FAILURE;
- HashTable ranges;
- zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
+ zend_hash_destroy(&ranges);
- switch (http_get_request_ranges(&ranges, data_size))
- {
- case RANGE_NO:
- zend_hash_destroy(&ranges);
- /* go ahead and send all */
- break;
+ /* send 304 Not Modified if etag matches */
+ if (cache_etag) {
+ char *etag = NULL;
+ int etag_match = 0;
- case RANGE_OK:
- result = http_send_ranges(&ranges, data_ptr, data_size, data_mode);
- zend_hash_destroy(&ranges);
- return result;
- break;
+ if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
+ return FAILURE;
+ }
- case RANGE_ERR:
- zend_hash_destroy(&ranges);
- http_send_status(416);
- return FAILURE;
- break;
+ http_send_etag(etag, 32);
+ etag_match = http_etag_match("HTTP_IF_NONE_MATCH", etag);
+ efree(etag);
- default:
- return FAILURE;
- break;
- }
+ if (etag_match) {
+ return http_send_status(304);
}
}
- /* send all */
+
+ /* send 304 Not Modified if last modified matches */
+ if (http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
+ return http_send_status(304);
+ }
+
+ /* send full entity */
return http_send_chunk(data_ptr, 0, data_size, data_mode);
}
/* }}} */
/* {{{ STATUS http_send_stream(php_stream *) */
-PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file,
- zend_bool close_stream TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file, zend_bool close_stream TSRMLS_DC)
{
STATUS status;
/* }}} */
/* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
-PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
- const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded, size_t encoded_len,
+ char **decoded, size_t *decoded_len TSRMLS_DC)
{
const char *e_ptr;
char *d_ptr;
/* }}} */
/* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
-PHP_HTTP_API STATUS _http_split_response_ex(char *response,
- size_t response_len, HashTable *headers, char **body, size_t *body_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_split_response_ex(char *response, size_t response_len,
+ HashTable *headers, char **body, size_t *body_len TSRMLS_DC)
{
char *header = response, *real_body = NULL;
}
/* }}} */
-/* {{{ STATUS http_parse_headers(char *, long, zval *) */
-PHP_HTTP_API STATUS _http_parse_headers_ex(char *header, int header_len,
+/* {{{ STATUS http_parse_headers(char *, size_t, HashTable *, zend_bool) */
+PHP_HTTP_API STATUS _http_parse_headers_ex(char *header, size_t header_len,
HashTable *headers, zend_bool prettify TSRMLS_DC)
{
char *colon = NULL, *line = NULL, *begin = header;
/* skip empty key */
if (header != colon) {
- char *key = estrndup(header, colon - header);
+ zval **previous = NULL;
+ int keylen = colon - header;
+ char *key = estrndup(header, keylen);
if (prettify) {
- key = pretty_key(key, colon - header, 1, 1);
+ key = pretty_key(key, keylen, 1, 1);
}
value_len += line - colon - 1;
/* skip trailing ws */
while (isspace(colon[value_len - 1])) --value_len;
- if (value_len < 1) {
+ /* if we already have got such a header make an array of those */
+ if (SUCCESS == zend_hash_find(headers, key, keylen + 1, (void **) &previous)) {
+ /* already an array? - just add */
+ if (Z_TYPE_PP(previous) == IS_ARRAY) {
+ if (value_len > 0) {
+ add_next_index_stringl(*previous, colon, value_len, 1);
+ } else {
+ add_next_index_stringl(*previous, "", 0, 1);
+ }
+ } else {
+ /* create the array */
+ zval *new_array;
+ MAKE_STD_ZVAL(new_array);
+ array_init(new_array);
+
+ add_next_index_stringl(new_array, Z_STRVAL_PP(previous), Z_STRLEN_PP(previous), 1);
+ if (value_len > 0) {
+ add_next_index_stringl(new_array, colon, value_len, 1);
+ } else {
+ add_next_index_stringl(new_array, "", 0, 1);
+ }
+
+ add_assoc_zval(&array, key, new_array);
+ }
+
+ previous = NULL;
+ } else if (value_len > 0) {
+ add_assoc_stringl(&array, key, colon, value_len, 1);
+ } else {
/* hm, empty header? */
add_assoc_stringl(&array, key, "", 0, 1);
- } else {
- add_assoc_stringl(&array, key, colon, value_len, 1);
}
efree(key);
}
}
/* }}} */
+/* {{{ */
+PHP_HTTP_API STATUS _http_parse_cookie(const char *cookie, HashTable *values TSRMLS_DC)
+{
+ const char *key = cookie, *val = NULL;
+ int vallen = 0, keylen = 0, done = 0;
+ zval array;
+
+ Z_ARRVAL(array) = values;
+
+ if (!(val = strchr(cookie, '='))) {
+ return FAILURE;
+ }
+
+#define HTTP_COOKIE_VAL(array, k, str, len) \
+ { \
+ const char *encoded = str; \
+ char *decoded = NULL; \
+ int decoded_len = 0, encoded_len = len; \
+ decoded = estrndup(encoded, encoded_len); \
+ decoded_len = php_url_decode(decoded, encoded_len); \
+ add_assoc_stringl(array, k, decoded, decoded_len, 0); \
+ }
+#define HTTP_COOKIE_FIXKEY() \
+ { \
+ while (isspace(*key)) ++key; \
+ keylen = val - key; \
+ while (isspace(key[keylen - 1])) --keylen; \
+ }
+#define HTTP_COOKIE_FIXVAL() \
+ { \
+ ++val; \
+ while (isspace(*val)) ++val; \
+ vallen = key - val; \
+ while (isspace(val[vallen - 1])) --vallen; \
+ }
+
+ HTTP_COOKIE_FIXKEY();
+ HTTP_COOKIE_VAL(&array, "name", key, keylen);
+
+ /* just a name=value cookie */
+ if (!(key = strchr(val, ';'))) {
+ key = val + strlen(val);
+ HTTP_COOKIE_FIXVAL();
+ HTTP_COOKIE_VAL(&array, "value", val, vallen);
+ }
+ /* additional info appended */
+ else {
+ char *keydup = NULL;
+
+ HTTP_COOKIE_FIXVAL();
+ HTTP_COOKIE_VAL(&array, "value", val, vallen);
+
+ do {
+ if (!(val = strchr(key, '='))) {
+ break;
+ }
+ ++key;
+ HTTP_COOKIE_FIXKEY();
+ keydup = estrndup(key, keylen);
+ if (!(key = strchr(val, ';'))) {
+ done = 1;
+ key = val + strlen(val);
+ }
+ HTTP_COOKIE_FIXVAL();
+ HTTP_COOKIE_VAL(&array, keydup, val, vallen);
+ efree(keydup);
+ } while (!done);
+ }
+ return SUCCESS;
+}
+/* }}} */
+
/* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC)
{
- char *key = NULL;
- long idx = 0;
- zval array;
+ char *key = NULL;
+ long idx = 0;
+ zval array;
- Z_ARRVAL(array) = headers;
+ Z_ARRVAL(array) = headers;
- FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) {
- if (key && !strncmp(key, "HTTP_", 5)) {
- zval **header;
+ FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) {
+ if (key && !strncmp(key, "HTTP_", 5)) {
+ zval **header;
- if (prettify) {
- key = pretty_key(key + 5, strlen(key) - 5, 1, 1);
- }
+ key += 5;
+ if (prettify) {
+ key = pretty_key(key, strlen(key), 1, 1);
+ }
- zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
- add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
- key = NULL;
- }
- }
+ zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
+ add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+ key = NULL;
+ }
+ }
}
/* }}} */
-/* {{{ STATUS http_urlencode_hash_ex(HashTable *, int, char **, size_t *) */
-PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, int override_argsep,
+/* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
+PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, zend_bool override_argsep,
char *pre_encoded_data, size_t pre_encoded_len,
char **encoded_data, size_t *encoded_len TSRMLS_DC)
{
- smart_str qstr = {0};
+ char *arg_sep;
+ phpstr *qstr = phpstr_new();
- if (override_argsep) {
- HTTP_URL_ARGSEP_OVERRIDE;
+ if (override_argsep || !strlen(arg_sep = INI_STR("arg_separator.output"))) {
+ arg_sep = HTTP_URL_ARGSEP_DEFAULT;
}
if (pre_encoded_len && pre_encoded_data) {
- smart_str_appendl(&qstr, pre_encoded_data, pre_encoded_len);
+ phpstr_append(qstr, pre_encoded_data, pre_encoded_len);
}
- if (SUCCESS != php_url_encode_hash_ex(hash, &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) {
+ if (SUCCESS != http_urlencode_hash_implementation(hash, qstr, arg_sep)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data");
- if (qstr.c) {
- efree(qstr.c);
- }
- if (override_argsep) {
- HTTP_URL_ARGSEP_RESTORE;
- }
+ phpstr_free(qstr);
return FAILURE;
}
- if (override_argsep) {
- HTTP_URL_ARGSEP_RESTORE;
- }
-
- smart_str_0(&qstr);
-
- *encoded_data = qstr.c;
- if (encoded_len) {
- *encoded_len = qstr.len;
- }
+ phpstr_data(qstr, encoded_data, encoded_len);
+ phpstr_free(qstr);
return SUCCESS;
}
}
/* }}} */
-#ifndef ZEND_ENGINE_2
-/* {{{ php_url_encode_hash
- Author: Sara Golemon <pollita@php.net> */
-PHP_HTTP_API STATUS php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
+/* {{{ http_urlencode_hash_implementation
+ Original Author: Sara Golemon <pollita@php.net> */
+PHP_HTTP_API STATUS _http_urlencode_hash_implementation_ex(
+ HashTable *ht, phpstr *formstr, char *arg_sep,
const char *num_prefix, int num_prefix_len,
const char *key_prefix, int key_prefix_len,
const char *key_suffix, int key_suffix_len,
zval *type TSRMLS_DC)
{
- char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p;
+ char *key = NULL, *ekey, *newprefix, *p;
int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
ulong idx;
zval **zdata = NULL, *copyzval;
- if (!ht) {
+ if (!ht || !formstr) {
return FAILURE;
}
return SUCCESS;
}
- arg_sep = INI_STR("arg_separator.output");
if (!arg_sep || !strlen(arg_sep)) {
arg_sep = HTTP_URL_ARGSEP_DEFAULT;
}
*p = '\0';
}
ht->nApplyCount++;
- php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL) TSRMLS_CC);
+ http_urlencode_hash_implementation_ex(HASH_OF(*zdata), formstr, arg_sep,
+ NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL));
ht->nApplyCount--;
efree(newprefix);
} else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
/* Skip these types */
continue;
} else {
- if (formstr->len) {
- smart_str_appendl(formstr, arg_sep, arg_sep_len);
+ if (formstr->used) {
+ phpstr_append(formstr, arg_sep, arg_sep_len);
}
/* Simple key=value */
- smart_str_appendl(formstr, key_prefix, key_prefix_len);
+ phpstr_append(formstr, key_prefix, key_prefix_len);
if (key_type == HASH_KEY_IS_STRING) {
ekey = php_url_encode(key, key_len, &ekey_len);
- smart_str_appendl(formstr, ekey, ekey_len);
+ phpstr_append(formstr, ekey, ekey_len);
efree(ekey);
} else {
/* Numeric key */
if (num_prefix) {
- smart_str_appendl(formstr, num_prefix, num_prefix_len);
+ phpstr_append(formstr, num_prefix, num_prefix_len);
}
ekey_len = spprintf(&ekey, 12, "%ld", idx);
- smart_str_appendl(formstr, ekey, ekey_len);
+ phpstr_append(formstr, ekey, ekey_len);
efree(ekey);
}
- smart_str_appendl(formstr, key_suffix, key_suffix_len);
- smart_str_appendl(formstr, "=", 1);
+ phpstr_append(formstr, key_suffix, key_suffix_len);
+ phpstr_appends(formstr, "=");
switch (Z_TYPE_PP(zdata)) {
case IS_STRING:
ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
zval_ptr_dtor(©zval);
}
- smart_str_appendl(formstr, ekey, ekey_len);
+ phpstr_append(formstr, ekey, ekey_len);
efree(ekey);
}
}
return SUCCESS;
}
/* }}} */
-#endif /* !ZEND_ENDGINE_2 */
/* }}} public API */