2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
16 #include "php_http_api.h"
18 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
19 #include "php_http_persistent_handle_api.h"
21 static HashTable http_persistent_handles_hash
;
23 # define LOCK() tsrm_mutex_lock(http_persistent_handles_lock)
24 # define UNLOCK() tsrm_mutex_unlock(http_persistent_handles_lock)
25 static MUTEX_T http_persistent_handles_lock
;
31 typedef struct _http_persistent_handles_hash_entry_t
{
33 http_persistent_handle_ctor ctor
;
34 http_persistent_handle_dtor dtor
;
35 } http_persistent_handles_hash_entry
;
37 typedef struct _http_persistent_handles_list_entry_t
{
39 } http_persistent_handles_list_entry
;
41 static inline void http_persistent_handles_hash_dtor_ex(http_persistent_handles_hash_entry
*hentry
, void (*list_dtor
)(HashTable
*))
43 http_persistent_handles_list_entry
*lentry
;
45 for ( zend_hash_internal_pointer_reset(&hentry
->list
);
46 SUCCESS
== zend_hash_get_current_data(&hentry
->list
, (void *) &lentry
);
47 zend_hash_move_forward(&hentry
->list
)) {
48 hentry
->dtor(lentry
->handle
);
52 list_dtor(&hentry
->list
);
56 static void http_persistent_handles_hash_dtor(void *h
)
58 http_persistent_handles_hash_dtor_ex(h
, zend_hash_destroy
);
61 PHP_MINIT_FUNCTION(http_persistent_handle
)
63 zend_hash_init(&http_persistent_handles_hash
, 0, NULL
, http_persistent_handles_hash_dtor
, 1);
65 http_persistent_handles_lock
= tsrm_mutex_alloc();
70 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle
)
72 zend_hash_destroy(&http_persistent_handles_hash
);
74 tsrm_mutex_free(http_persistent_handles_lock
);
79 PHP_HTTP_API STATUS
_http_persistent_handle_provide_ex(const char *name_str
, size_t name_len
, http_persistent_handle_ctor ctor
, http_persistent_handle_dtor dtor
)
81 STATUS status
= SUCCESS
;
82 http_persistent_handles_hash_entry e
;
84 zend_hash_init(&e
.list
, 0, NULL
, NULL
, 1);
89 if (SUCCESS
!= zend_hash_add(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &e
, sizeof(http_persistent_handles_hash_entry
), NULL
)) {
90 zend_hash_destroy(&e
.list
);
98 PHP_HTTP_API
void _http_persistent_handle_cleanup_ex(const char *name_str
, size_t name_len
)
100 http_persistent_handles_hash_entry
*hentry
;
103 if (name_str
&& name_len
) {
104 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &hentry
)) {
105 http_persistent_handles_hash_dtor_ex(hentry
, zend_hash_clean
);
108 for ( zend_hash_internal_pointer_reset(&http_persistent_handles_hash
);
109 SUCCESS
== zend_hash_get_current_data(&http_persistent_handles_hash
, (void *) &hentry
);
110 zend_hash_move_forward(&http_persistent_handles_hash
)) {
111 http_persistent_handles_hash_dtor_ex(hentry
, zend_hash_clean
);
117 PHP_HTTP_API HashTable
*_http_persistent_handle_statall_ex(HashTable
*ht
)
121 HashKey key
= initHashKey(0);
122 http_persistent_handles_hash_entry
*hentry
;
125 if (zend_hash_num_elements(&http_persistent_handles_hash
)) {
128 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
131 FOREACH_HASH_KEYVAL(pos
, &http_persistent_handles_hash
, key
, hentry
) {
133 ZVAL_LONG(tmp
, zend_hash_num_elements(&hentry
->list
));
134 zend_hash_add(ht
, key
.str
, key
.len
, (void *) &tmp
, sizeof(zval
*), NULL
);
144 PHP_HTTP_API STATUS
_http_persistent_handle_acquire_ex(const char *name_str
, size_t name_len
, void **handle
)
146 STATUS status
= FAILURE
;
148 http_persistent_handles_hash_entry
*hentry
;
149 http_persistent_handles_list_entry
*lentry
;
152 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &hentry
)) {
153 zend_hash_internal_pointer_end(&hentry
->list
);
154 if ( HASH_KEY_NON_EXISTANT
!= zend_hash_get_current_key(&hentry
->list
, NULL
, &index
, 0) &&
155 SUCCESS
== zend_hash_get_current_data(&hentry
->list
, (void *) &lentry
)) {
156 *handle
= lentry
->handle
;
157 zend_hash_index_del(&hentry
->list
, index
);
159 } else if ((*handle
= hentry
->ctor())) {
168 PHP_HTTP_API STATUS
_http_persistent_handle_release_ex(const char *name_str
, size_t name_len
, void **handle
)
170 STATUS status
= FAILURE
;
171 http_persistent_handles_hash_entry
*hentry
;
172 http_persistent_handles_list_entry lentry
;
175 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &hentry
)) {
176 lentry
.handle
= *handle
;
177 if (SUCCESS
== zend_hash_next_index_insert(&hentry
->list
, (void *) &lentry
, sizeof(http_persistent_handles_list_entry
), NULL
)) {
186 #endif /* HTTP_HAVE_PERSISTENT_HANDLES */
193 * vim600: noet sw=4 ts=4 fdm=marker
194 * vim<600: noet sw=4 ts=4