<commit_before>/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, <NAME> <<EMAIL>>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"
#include "php_http_client.h"

#include "ext/spl/spl_observer.h"

/*
 * array of name => php_http_client_driver_t*
 */
static HashTable php_http_client_drivers;

static void php_http_client_driver_hash_dtor(zval *pData)
{
	pefree(Z_PTR_P(pData), 1);
}

ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver)
{
	return zend_hash_add_mem(&php_http_client_drivers, driver->driver_name, (void *) driver, sizeof(php_http_client_driver_t))
			? SUCCESS : FAILURE;
}

php_http_client_driver_t *php_http_client_driver_get(zend_string *name)
{
	zval *ztmp;
	php_http_client_driver_t *tmp;

	if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) {
		return tmp;
	}
	if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) {
		return Z_PTR_P(ztmp);
	}
	return NULL;
}

static int apply_driver_list(zval *p, void *arg)
{
	php_http_client_driver_t *d = Z_PTR_P(p);
	zval zname;

	ZVAL_STR_COPY(&zname, d->driver_name);

	zend_hash_next_index_insert(arg, &zname);
	return ZEND_HASH_APPLY_KEEP;
}

void php_http_client_driver_list(HashTable *ht)
{
	zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht);
}

static zend_class_entry *php_http_client_class_entry;
zend_class_entry *php_http_client_get_class_entry(void)
{
	return php_http_client_class_entry;
}

void php_http_client_options_set_subr(zval *instance, char *key, size_t len, zval *opts, int overwrite)
{
	if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) {
		zend_class_entry *this_ce = Z_OBJCE_P(instance);
		zval old_opts_tmp, *old_opts, new_opts, *entry = NULL;

		array_init(&new_opts);
		old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
		if (Z_TYPE_P(old_opts) == IS_ARRAY) {
			array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
		}

		if (overwrite) {
			if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
				Z_ADDREF_P(opts);
				zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
			} else {
				zend_symtable_str_del(Z_ARRVAL(new_opts), key, len);
			}
		} else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
			if ((entry = zend_symtable_str_find(Z_ARRVAL(new_opts), key, len))) {
				array_join(Z_ARRVAL_P(opts), Z_ARRVAL_P(entry), 0, 0);
			} else {
				Z_ADDREF_P(opts);
				zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
			}
		}

		zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
		zval_ptr_dtor(&new_opts);
	}<commit_msg>avoid COW violation<commit_after>/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, <NAME> <<EMAIL>>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"
#include "php_http_client.h"

#include "ext/spl/spl_observer.h"

/*
 * array of name => php_http_client_driver_t*
 */
static HashTable php_http_client_drivers;

static void php_http_client_driver_hash_dtor(zval *pData)
{
	pefree(Z_PTR_P(pData), 1);
}

ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver)
{
	return zend_hash_add_mem(&php_http_client_drivers, driver->driver_name, (void *) driver, sizeof(php_http_client_driver_t))
			? SUCCESS : FAILURE;
}

php_http_client_driver_t *php_http_client_driver_get(zend_string *name)
{
	zval *ztmp;
	php_http_client_driver_t *tmp;

	if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) {
		return tmp;
	}
	if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) {
		return Z_PTR_P(ztmp);
	}
	return NULL;
}

static int apply_driver_list(zval *p, void *arg)
{
	php_http_client_driver_t *d = Z_PTR_P(p);
	zval zname;

	ZVAL_STR_COPY(&zname, d->driver_name);

	zend_hash_next_index_insert(arg, &zname);
	return ZEND_HASH_APPLY_KEEP;
}

void php_http_client_driver_list(HashTable *ht)
{
	zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht);
}

static zend_class_entry *php_http_client_class_entry;
zend_class_entry *php_http_client_get_class_entry(void)
{
	return php_http_client_class_entry;
}

void php_http_client_options_set_subr(zval *instance, char *key, size_t len, zval *opts, int overwrite)
{
	if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) {
		zend_class_entry *this_ce = Z_OBJCE_P(instance);
		zval old_opts_tmp, *old_opts, new_opts, *entry = NULL;

		array_init(&new_opts);
		old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);

		if (Z_TYPE_P(old_opts) == IS_ARRAY) {
			array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
		}

		if (overwrite) {
			if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
				Z_ADDREF_P(opts);
				zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
			} else {
				zend_symtable_str_del(Z_ARRVAL(new_opts), key, len);
			}
		} else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
			if ((entry = zend_symtable_str_find(Z_ARRVAL(new_opts), key, len))) {
				SEPARATE_ZVAL(entry);
				array_join(Z_ARRVAL_P(opts), Z_ARRVAL_P(entry), 0, 0);
			} else {
				Z_ADDREF_P(opts);
				zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
			}
		}

		zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
		zval_ptr_dtor(&new_opts);
	}
{'fraction_non_alphanumeric': 0.19135706513290843, 'fraction_numerical': 0.003730763251981968, 'mean_word_length': 3.111182108626198, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5964775', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2321, 'n_words': 381}