| 1 |
#include "ldapsearchiter.h" |
| 2 |
#include "ldapconnection.h" |
| 3 |
|
| 4 |
|
| 5 |
static void |
| 6 |
ldapsearchiter_dealloc(LDAPSearchIter* self) { |
| 7 |
DEBUG("ldapsearchiter_dealloc (self:%p)", self); |
| 8 |
Py_XDECREF(self->buffer); |
| 9 |
Py_XDECREF(self->conn); |
| 10 |
|
| 11 |
free_search_params(self->params); |
| 12 |
|
| 13 |
|
| 14 |
if (self->vlv_info != NULL) { |
| 15 |
if (self->vlv_info->ldvlv_attrvalue != NULL) { |
| 16 |
free(self->vlv_info->ldvlv_attrvalue->bv_val); |
| 17 |
free(self->vlv_info->ldvlv_attrvalue); |
| 18 |
} |
| 19 |
free(self->vlv_info); |
| 20 |
} |
| 21 |
free(self->cookie); |
| 22 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
static PyObject * |
| 27 |
ldapsearchiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 28 |
LDAPSearchIter *self = NULL; |
| 29 |
|
| 30 |
self = (LDAPSearchIter *)type->tp_alloc(type, 0); |
| 31 |
|
| 32 |
if (self != NULL) { |
| 33 |
self->buffer = NULL; |
| 34 |
self->cookie = NULL; |
| 35 |
self->page_size = 0; |
| 36 |
self->params = NULL; |
| 37 |
self->vlv_info = NULL; |
| 38 |
self->auto_acquire = 0; |
| 39 |
} |
| 40 |
|
| 41 |
DEBUG("ldapsearchiter_new [self:%p]", self); |
| 42 |
return (PyObject *)self; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
LDAPSearchIter * |
| 47 |
LDAPSearchIter_New(LDAPConnection *conn) { |
| 48 |
PyObject *tmp = NULL; |
| 49 |
LDAPSearchIter *self = |
| 50 |
(LDAPSearchIter *)LDAPSearchIterType.tp_new(&LDAPSearchIterType, |
| 51 |
NULL, NULL); |
| 52 |
DEBUG("LDAPSearchIter_New (conn:%p)[self:%p]", conn, self); |
| 53 |
if (conn != NULL && self != NULL) { |
| 54 |
self->params = (ldapsearchparams *)malloc(sizeof(ldapsearchparams)); |
| 55 |
if (self->params == NULL) return NULL; |
| 56 |
Py_INCREF(conn); |
| 57 |
self->conn = conn; |
| 58 |
|
| 59 |
|
| 60 |
tmp = PyObject_GetAttrString(self->conn->client, "auto_page_acquire"); |
| 61 |
if (tmp == NULL) return NULL; |
| 62 |
|
| 63 |
self->auto_acquire = (char)PyObject_IsTrue(tmp); |
| 64 |
Py_DECREF(tmp); |
| 65 |
} |
| 66 |
return self; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static PyObject * |
| 71 |
ldapsearchiter_acquirenextpage(LDAPSearchIter *self) { |
| 72 |
int msgid = -1; |
| 73 |
|
| 74 |
DEBUG("ldapsearchiter_acquirenextpage (self:%p) cookie:%p", self, |
| 75 |
(self != NULL) ? self->cookie : NULL |
| 76 |
); |
| 77 |
|
| 78 |
if (self->cookie != NULL && self->cookie->bv_val != NULL && self->cookie->bv_len > 0) { |
| 79 |
Py_INCREF(self); |
| 80 |
msgid = LDAPConnection_Searching(self->conn, NULL, (PyObject *)self); |
| 81 |
if (msgid < 0) return NULL; |
| 82 |
return PyLong_FromLong((long int)msgid); |
| 83 |
} else { |
| 84 |
ber_bvfree(self->cookie); |
| 85 |
self->cookie = NULL; |
| 86 |
Py_RETURN_NONE; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
static PyObject* |
| 92 |
ldapsearchiter_getiter(LDAPSearchIter *self) { |
| 93 |
Py_INCREF(self); |
| 94 |
return (PyObject*)self; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static PyObject * |
| 99 |
ldapsearchiter_iternext(LDAPSearchIter *self) { |
| 100 |
PyObject *item = NULL; |
| 101 |
PyObject *tmp = NULL, *msg = tmp; |
| 102 |
|
| 103 |
DEBUG("ldapsearchiter_iternext (self:%p)", self); |
| 104 |
if (self->buffer == NULL) return NULL; |
| 105 |
if (Py_SIZE(self->buffer) != 0) { |
| 106 |
|
| 107 |
item = PyList_GetItem(self->buffer, 0); |
| 108 |
if (item == NULL) { |
| 109 |
PyErr_BadInternalCall(); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
Py_INCREF(item); |
| 113 |
|
| 114 |
if (PyList_SetSlice(self->buffer, 0, 1, NULL) != 0) { |
| 115 |
PyErr_BadInternalCall(); |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
return item; |
| 119 |
} else { |
| 120 |
Py_DECREF(self->buffer); |
| 121 |
self->buffer = NULL; |
| 122 |
if (self->auto_acquire == 1 && self->conn->async == 0) { |
| 123 |
|
| 124 |
|
| 125 |
msg = ldapsearchiter_acquirenextpage(self); |
| 126 |
if (msg == NULL) return NULL; |
| 127 |
if (msg == Py_None) { |
| 128 |
Py_DECREF(msg); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
self = (LDAPSearchIter *)PyObject_CallMethod((PyObject *)self->conn, |
| 132 |
"_evaluate", "(O)", msg); |
| 133 |
Py_DECREF(msg); |
| 134 |
if (self == NULL) return NULL; |
| 135 |
Py_DECREF(self); |
| 136 |
return PyIter_Next((PyObject *)self); |
| 137 |
} |
| 138 |
} |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
static Py_ssize_t |
| 143 |
ldapsearchiter_len(LDAPSearchIter *self) { |
| 144 |
if (self->buffer == NULL) return 0; |
| 145 |
return PyObject_Size(self->buffer); |
| 146 |
} |
| 147 |
|
| 148 |
static PyObject * |
| 149 |
ldapsearchiter_anext(LDAPSearchIter *self) { |
| 150 |
PyObject *res = NULL; |
| 151 |
|
| 152 |
DEBUG("ldapsearchiter_anext (self:%p)", self); |
| 153 |
res = PyObject_CallMethod((PyObject *)self->conn, "_search_iter_anext", |
| 154 |
"(O)", (PyObject *)self); |
| 155 |
|
| 156 |
return res; |
| 157 |
} |
| 158 |
|
| 159 |
static PyAsyncMethods ldapsearchiter_async = { |
| 160 |
0, |
| 161 |
(unaryfunc)ldapsearchiter_getiter, |
| 162 |
(unaryfunc)ldapsearchiter_anext |
| 163 |
}; |
| 164 |
|
| 165 |
static PySequenceMethods ldapsearchiter_sequence = { |
| 166 |
(lenfunc)ldapsearchiter_len, |
| 167 |
0, |
| 168 |
0, |
| 169 |
0, |
| 170 |
0, |
| 171 |
0, |
| 172 |
0, |
| 173 |
0, |
| 174 |
0, |
| 175 |
0, |
| 176 |
}; |
| 177 |
|
| 178 |
static PyMethodDef ldapsearchiter_methods[] = { |
| 179 |
{"acquire_next_page", (PyCFunction)ldapsearchiter_acquirenextpage, |
| 180 |
METH_NOARGS, "Get next page of paged LDAP search."}, |
| 181 |
{NULL, NULL, 0, NULL} |
| 182 |
}; |
| 183 |
|
| 184 |
PyTypeObject LDAPSearchIterType = { |
| 185 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 186 |
"_bonsai.ldapsearchiter", |
| 187 |
sizeof(LDAPSearchIter), |
| 188 |
0, |
| 189 |
(destructor)ldapsearchiter_dealloc, |
| 190 |
0, |
| 191 |
0, |
| 192 |
0, |
| 193 |
&ldapsearchiter_async, |
| 194 |
0, |
| 195 |
0, |
| 196 |
&ldapsearchiter_sequence, |
| 197 |
0, |
| 198 |
0, |
| 199 |
0, |
| 200 |
0, |
| 201 |
0, |
| 202 |
0, |
| 203 |
0, |
| 204 |
Py_TPFLAGS_DEFAULT | |
| 205 |
Py_TPFLAGS_BASETYPE, |
| 206 |
"ldapsearchiter object", |
| 207 |
0, |
| 208 |
0, |
| 209 |
0, |
| 210 |
0, |
| 211 |
(getiterfunc)ldapsearchiter_getiter, |
| 212 |
(iternextfunc)ldapsearchiter_iternext, |
| 213 |
ldapsearchiter_methods, |
| 214 |
0, |
| 215 |
0, |
| 216 |
0, |
| 217 |
0, |
| 218 |
0, |
| 219 |
0, |
| 220 |
0, |
| 221 |
0, |
| 222 |
0, |
| 223 |
ldapsearchiter_new, |
| 224 |
}; |