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_handle_t
{
33 } http_persistent_handle
;
35 typedef HashTable
*http_persistent_handle_list
;
37 typedef struct _http_persistent_handle_provider_t
{
38 http_persistent_handle_list list
; /* "ident" => array(handles) entries */
39 http_persistent_handle_ctor ctor
;
40 http_persistent_handle_dtor dtor
;
41 } http_persistent_handle_provider
;
43 static void http_persistent_handles_hash_dtor(void *p
)
45 http_persistent_handle_provider
*provider
= (http_persistent_handle_provider
*) p
;
46 http_persistent_handle_list
*list
;
47 http_persistent_handle
*handle
;
48 HashPosition pos1
, pos2
;
50 FOREACH_HASH_VAL(pos1
, provider
->list
, list
) {
51 FOREACH_HASH_VAL(pos2
, *list
, handle
) {
52 provider
->dtor(handle
->ptr
);
54 zend_hash_destroy(*list
);
57 zend_hash_destroy(provider
->list
);
58 pefree(provider
->list
, 1);
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
= FAILURE
;
82 http_persistent_handle_provider provider
;
85 provider
.list
= pemalloc(sizeof(HashTable
), 1);
89 zend_hash_init(provider
.list
, 0, NULL
, NULL
, 1);
90 if (SUCCESS
== zend_hash_add(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
, sizeof(http_persistent_handle_provider
), NULL
)) {
93 pefree(provider
.list
, 1);
101 PHP_HTTP_API STATUS
_http_persistent_handle_acquire_ex(const char *name_str
, size_t name_len
, void **handle_ptr TSRMLS_DC
)
103 STATUS status
= FAILURE
;
105 http_persistent_handle_provider
*provider
;
106 http_persistent_handle_list
*list
;
107 http_persistent_handle
*handle
;
111 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
112 if (SUCCESS
== zend_hash_quick_find(provider
->list
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &list
)) {
113 zend_hash_internal_pointer_end(*list
);
114 if (HASH_KEY_NON_EXISTANT
!= zend_hash_get_current_key(*list
, NULL
, &index
, 0) && SUCCESS
== zend_hash_get_current_data(*list
, (void *) &handle
)) {
115 *handle_ptr
= handle
->ptr
;
116 zend_hash_index_del(*list
, index
);
119 if (*handle_ptr
|| (*handle_ptr
= provider
->ctor())) {
128 PHP_HTTP_API STATUS
_http_persistent_handle_release_ex(const char *name_str
, size_t name_len
, void **handle_ptr TSRMLS_DC
)
130 STATUS status
= FAILURE
;
131 http_persistent_handle_provider
*provider
;
132 http_persistent_handle_list
*list
, new_list
;
133 http_persistent_handle handle
= {*handle_ptr
};
136 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
137 if (SUCCESS
== zend_hash_quick_find(provider
->list
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &list
)) {
138 status
= zend_hash_next_index_insert(*list
, (void *) &handle
, sizeof(http_persistent_handle
), NULL
);
139 } else if ((new_list
= pemalloc(sizeof(HashTable
), 1))) {
140 zend_hash_init(new_list
, 0, NULL
, NULL
, 1);
141 if ( SUCCESS
== zend_hash_next_index_insert(new_list
, (void *) &handle
, sizeof(http_persistent_handle
), NULL
) &&
142 SUCCESS
== zend_hash_quick_add(provider
->list
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &new_list
, sizeof(http_persistent_handle_list
*), NULL
)) {
154 PHP_HTTP_API
void _http_persistent_handle_cleanup_ex(const char *name_str
, size_t name_len
, int current_ident_only TSRMLS_DC
)
156 http_persistent_handle_provider
*provider
;
157 http_persistent_handle_list
*list
;
158 http_persistent_handle
*handle
;
159 HashPosition pos1
, pos2
, pos3
;
162 if (name_str
&& name_len
) {
163 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
164 if (current_ident_only
) {
165 if (SUCCESS
== zend_hash_quick_find(provider
->list
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &list
)) {
166 FOREACH_HASH_VAL(pos1
, *list
, handle
) {
167 provider
->dtor(handle
->ptr
);
169 zend_hash_clean(*list
);
172 FOREACH_HASH_VAL(pos1
, provider
->list
, list
) {
173 FOREACH_HASH_VAL(pos2
, *list
, handle
) {
174 provider
->dtor(handle
->ptr
);
176 zend_hash_clean(*list
);
181 FOREACH_HASH_VAL(pos1
, &http_persistent_handles_hash
, provider
) {
182 if (current_ident_only
) {
183 if (SUCCESS
== zend_hash_quick_find(provider
->list
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &list
)) {
184 FOREACH_HASH_VAL(pos2
, *list
, handle
) {
185 provider
->dtor(handle
->ptr
);
187 zend_hash_clean(*list
);
190 FOREACH_HASH_VAL(pos2
, provider
->list
, list
) {
191 FOREACH_HASH_VAL(pos3
, *list
, handle
) {
192 provider
->dtor(handle
->ptr
);
194 zend_hash_clean(*list
);
202 PHP_HTTP_API HashTable
*_http_persistent_handle_statall_ex(HashTable
*ht
)
204 zval
*zlist
, *zentry
;
205 HashPosition pos1
, pos2
;
206 HashKey key1
= initHashKey(0), key2
= initHashKey(0);
207 http_persistent_handle_provider
*provider
;
208 http_persistent_handle_list
*list
;
211 if (zend_hash_num_elements(&http_persistent_handles_hash
)) {
214 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
217 FOREACH_HASH_KEYVAL(pos1
, &http_persistent_handles_hash
, key1
, provider
) {
218 MAKE_STD_ZVAL(zlist
);
220 FOREACH_HASH_KEYVAL(pos2
, provider
->list
, key2
, list
) {
221 MAKE_STD_ZVAL(zentry
);
222 ZVAL_LONG(zentry
, zend_hash_num_elements(*list
));
223 zend_hash_quick_add(Z_ARRVAL_P(zlist
), key2
.str
, key2
.len
, key2
.num
, (void *) &zentry
, sizeof(zval
*), NULL
);
225 zend_hash_quick_add(ht
, key1
.str
, key1
.len
, key1
.num
, (void *) &zlist
, sizeof(zval
*), NULL
);
235 #endif /* HTTP_HAVE_PERSISTENT_HANDLES */
242 * vim600: noet sw=4 ts=4 fdm=marker
243 * vim<600: noet sw=4 ts=4