@@@ lbuf needs test
[mLib-python] / url.pyx
1 ### -*-pyrex-*-
2 ###
3 ### Form-urlencoding functions
4 ###
5 ### (c) 2006 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
12 ### mLib/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### mLib/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 cdef class URLEncode:
27 """URLEncode([strictp = False], [laxp = False], [semip = False])"""
28 cdef url_ectx ctx
29 cdef dstr d
30
31 def __cinit__(me, *hunoz, **hukairz):
32 url_initenc(&me.ctx)
33 DCREATE(&me.d)
34 def __dealloc__(me):
35 dstr_destroy(&me.d)
36 def __init__(me, strictp = False, laxp = False, semip = False):
37 cdef unsigned f
38 f = 0
39 if strictp:
40 f |= URLF_STRICT
41 if laxp:
42 f |= URLF_LAX
43 if semip:
44 f |= URLF_SEMI
45 me.ctx.f = f
46 def encode(me, object name, object value):
47 """UE.encode(NAME, VALUE): encode a key/value pair"""
48 url_enc(&me.ctx, &me.d, TEXT_PTR(name), TEXT_PTR(value))
49 return me
50 @property
51 def result(me):
52 """UE.result -> STR: the encoded string"""
53 return TEXT_FROMSTRLEN(me.d.buf, me.d.len)
54 @property
55 def strictp(me):
56 """UE.strictp -> BOOL: strictly escape non-alphanumerics?"""
57 return <bint>(me.ctx.f&URLF_STRICT)
58 @strictp.setter
59 def strictp(me, val):
60 if val:
61 me.ctx.f |= URLF_STRICT
62 else:
63 me.ctx.f &= ~URLF_STRICT
64 @property
65 def laxp(me):
66 """UE.laxp -> BOOL: only escape obviously unsafe characters?"""
67 return <bint>(me.ctx.f&URLF_LAX)
68 @laxp.setter
69 def laxp(me, val):
70 if val:
71 me.ctx.f |= URLF_LAX
72 else:
73 me.ctx.f &= ~URLF_LAX
74 @property
75 def semip(me):
76 """UE.semip -> BOOL: separate key/value pairs with semicolons?"""
77 return <bint>(me.ctx.f&URLF_SEMI)
78 @semip.setter
79 def semip(me, val):
80 if val:
81 me.ctx.f |= URLF_SEMI
82 else:
83 me.ctx.f &= ~URLF_SEMI
84
85 cdef class URLDecode:
86 """URLDecode(STR, [semip = False]): iterator over (KEY, VALUE) pairs"""
87 cdef url_dctx ctx
88 cdef char *p
89
90 def __cinit__(me, *hunoz, **hukairz):
91 me.p = NULL
92 url_initdec(&me.ctx, me.p)
93 def __dealloc__(me):
94 xfree(me.p)
95 def __init__(me, object string, semip = False):
96 cdef unsigned f
97 f = 0
98 if semip:
99 f |= URLF_SEMI
100 xfree(me.p)
101 me.p = xstrdup(TEXT_PTR(string))
102 me.ctx.p = me.p
103 me.ctx.f = f
104 def __iter__(me):
105 return me
106 def __next__(me):
107 cdef dstr n
108 cdef dstr v
109 cdef object nn
110 cdef object vv
111 cdef int anyp
112 DCREATE(&n)
113 DCREATE(&v)
114 anyp = url_dec(&me.ctx, &n, &v)
115 if anyp:
116 nn = TEXT_FROMSTRLEN(n.buf, n.len)
117 vv = TEXT_FROMSTRLEN(v.buf, v.len)
118 dstr_destroy(&n)
119 dstr_destroy(&v)
120 if not anyp:
121 raise StopIteration
122 return nn, vv
123 @property
124 def semip(me):
125 """UE.semip -> BOOL: separate key/value pairs with semicolons?"""
126 return <bint>(me.ctx.f&URLF_SEMI)
127 @semip.setter
128 def semip(me, val):
129 if val:
130 me.ctx.f |= URLF_SEMI
131 else:
132 me.ctx.f &= ~URLF_SEMI
133
134 ###----- That's all, folks --------------------------------------------------