summaryrefslogtreecommitdiff
path: root/efl/ecore_con/efl.ecore_con.pyx
blob: af14c88953ce04ccfafa359e8b34be3a54001e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright (C) 2007-2015 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Python-EFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.

"""

:mod:`efl.ecore_con` Module
###########################

The ecore_con module provide various utilities to perform different network
related tasks. Everything in a full async ecore way. Most notable are the
:class:`Lookup` class to perform DNS requests, the :class:`Url` class to
perform HTTP requests and the :class:`Server` class to implement your own
server.

Don't forget about the :class:`efl.ecore.FileDownload` class if what you need
is just to fetch some data to file.


Classes
=======

.. toctree::

   class-lookup.rst
   class-url.rst



Enumerations
============

.. _Ecore_Con_Type:

Ecore Con Type
--------------

Types for an ecore_con client/server object.

A correct way to set this type is with an ECORE_CON_$TYPE, optionally OR'ed
with an ECORE_CON_$USE if encryption is desired, and LOAD_CERT if the
previously loaded certificate should be used.

.. data:: ECORE_CON_LOCAL_USER

    Socket in ~/.ecore.

.. data:: ECORE_CON_LOCAL_SYSTEM

   Socket in /tmp.

.. data:: ECORE_CON_LOCAL_ABSTRACT

    Abstract socket.

.. data:: ECORE_CON_REMOTE_TCP

    Remote server using TCP.


.. data:: ECORE_CON_REMOTE_MCAST

    Remote multicast server.


.. data:: ECORE_CON_REMOTE_UDP

    Remote server using UDP.

.. data:: ECORE_CON_REMOTE_BROADCAST

    Remote broadcast using UDP.

.. data:: ECORE_CON_REMOTE_NODELAY

    Remote connection sending packets immediately.

.. data:: ECORE_CON_REMOTE_CORK

    Remote connection sending data in large chunks. (only on linux)

.. data:: ECORE_CON_USE_SSL2

    Use SSL2: UNSUPPORTED.

.. data:: ECORE_CON_USE_SSL3

    Use SSL3: UNSUPPORTED.

.. data:: ECORE_CON_USE_TLS

    Use TLS.

.. data:: ECORE_CON_USE_MIXED

    Use both TLS and SSL3.

.. data:: ECORE_CON_LOAD_CERT

    Attempt to use the loaded certificate.

.. data:: ECORE_CON_NO_PROXY

    Disable all types of proxy on the server.


.. _Ecore_Con_Url_Time:

Ecore Con Url Time
------------------

The type of condition to use when making an HTTP request dependent on time, so
that headers such as "If-Modified-Since" are used.

.. data:: ECORE_CON_URL_TIME_NONE

    Do not place time restrictions on the HTTP requests. 

.. data:: ECORE_CON_URL_TIME_IFMODSINCE

   Add the "If-Modified-Since" HTTP header, so that the request is performed by
   the server only if the target has been modified since the time value passed
   to it in the request.

.. data:: ECORE_CON_URL_TIME_IFUNMODSINCE

    Add the "If-Unmodified-Since" HTTP header, so that the request is performed
    by the server only if the target has NOT been modified since the time value
    passed to it in the request.


.. _Ecore_Con_Url_Http_Version:

Ecore Con Url Http Version
--------------------------

The http version to use.

.. data:: ECORE_CON_URL_HTTP_VERSION_1_0

    HTTP version 1.0.

.. data:: ECORE_CON_URL_HTTP_VERSION_1_1

   HTTP version 1.1 (default)


Module level functions
======================

"""

from libc.stdint cimport uintptr_t
from cpython cimport PyUnicode_AsUTF8String, Py_INCREF, Py_DECREF

import traceback
import atexit


from efl.ecore cimport _event_mapping_register, _event_mapping_get, \
    ecore_event_handler_add, ecore_event_handler_del


cdef int _con_events_registered = 0


def init():
    """Initialize the Ecore Con library

    .. note::
        You never need to call this function, it is automatically called when
        the module is imported.

    .. versionadded:: 1.17

    """

    ecore_con_init()
    ecore_con_url_init()

    global _con_events_registered
    if _con_events_registered == 0:
        _event_mapping_register(ECORE_CON_EVENT_URL_COMPLETE, EventUrlComplete)
        _event_mapping_register(ECORE_CON_EVENT_URL_PROGRESS, EventUrlProgress)
        _event_mapping_register(ECORE_CON_EVENT_URL_DATA, EventUrlData)
        _con_events_registered = 1


def shutdown():
    """Shuts down the Ecore Con library.

    .. note::
        You never need to call this function, it is automatically called atexit.

    .. versionadded:: 1.17

    """
    ecore_con_url_shutdown()
    ecore_con_shutdown()


cdef Eina_Bool _con_event_filter_cb(void *data, int ev_type, void *ev) with gil:
    cdef:
        ConEventFilter filter = <ConEventFilter>data
        object event_cls
        Event py_event
        list cbs
        Eo obj
        object func
        tuple args
        dict kargs

    # create correct "EventAbc" python object, using the global mapping
    event_cls = _event_mapping_get(ev_type)
    if event_cls:
        py_event = event_cls()

        # object_from_instance can fail in _set_obj if the event is
        # generated from an object not managed by us, so just ignore it.
        try:
            py_event._set_obj(ev)
        except:
            return 1

        # do we have callbacks for this object/event ?
        try:
            obj = py_event._get_obj()
            cbs = filter.callbacks.get(ev_type).get(obj)
        except:
            cbs = None

        if cbs:
            cbs = cbs[:] # copy, so we can change filter.callbacks
            for func, args, kargs in cbs:
                try:
                    func(py_event, *args, **kargs)
                except Exception:
                    traceback.print_exc()

    return 1 # always return true, no matter what

cdef class ConEventFilter(object):

    """
    self.callbacks = {
        EV_TYPE: {
            objX: [(cb,args,kargs), ... ]
            objY: [(cb,args,kargs), ... ]
        },
        ...
    }
    """

    def __cinit__(self):
        self.callbacks = {}
        self.handlers = {}

    cdef callback_add(self, int ev_type, Eo obj, object func, tuple args, dict kargs):
        # store the function in the callbacks dict
        if not ev_type in self.callbacks:
            self.callbacks[ev_type] = {}
        if not obj in self.callbacks[ev_type]:
            self.callbacks[ev_type][obj] = []
        self.callbacks[ev_type][obj].append((func,args,kargs))

        # connect a single ecore signal, one per event_type
        cdef Ecore_Event_Handler* ee
        if not ev_type in self.handlers:
            ee = ecore_event_handler_add(ev_type, _con_event_filter_cb,
                                         <void *>self)
            self.handlers[ev_type] = <uintptr_t><void *>ee

    cdef callback_del(self, int ev_type, Eo obj, object func, tuple args, dict kargs):
        try:
            self.callbacks[ev_type][obj].remove((func, args, kargs))
        except ValueError:
            raise ValueError(
                "callback is not registered: %s, args=%s, kargs=%s" %
                (func, args, kargs))

        # can delete the ecore handler?
        if self.callbacks.get(ev_type) and self.callbacks.get(ev_type).get(obj):
            return
        if ev_type in self.handlers:
            handler = self.handlers.pop(ev_type)
            ecore_event_handler_del(<Ecore_Event_Handler *><uintptr_t>handler)

    cdef callback_del_full(self, Eo obj):
        for ev_type in self.callbacks:
            if obj in self.callbacks[ev_type]:
                # remove all the cbs for the obj
                self.callbacks[ev_type].pop(obj, None)

                # can delete the ecore handler?
                if len(self.callbacks[ev_type]) < 1 and ev_type in self.handlers:
                    handler = self.handlers.pop(ev_type)
                    ecore_event_handler_del(<Ecore_Event_Handler *><uintptr_t>handler)

# name suggestions are welcome for this unusual "singleton" instance
cdef ConEventFilter GEF = ConEventFilter()


include "efl.ecore_con_lookup.pxi"
include "efl.ecore_con_url.pxi"


init()
atexit.register(shutdown)