Commit | Line | Data |
---|---|---|
5b1830f3 MW |
1 | ### -*-pyrex-*- |
2 | ### | |
3 | ### Form-urlencoding functions | |
4 | ### | |
5 | ### (c) 2006 Straylight/Edgeware | |
6 | ### | |
5ce5170c | 7 | |
5b1830f3 MW |
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. | |
5ce5170c MW |
25 | |
26 | cdef class URLEncode: | |
addc0c37 | 27 | """URLEncode([strictp = False], [laxp = False], [semip = False])""" |
5ce5170c MW |
28 | cdef url_ectx ctx |
29 | cdef dstr d | |
30 | ||
376ad06d | 31 | def __cinit__(me, *hunoz, **hukairz): |
5ce5170c MW |
32 | url_initenc(&me.ctx) |
33 | DCREATE(&me.d) | |
965caf5f MW |
34 | def __dealloc__(me): |
35 | dstr_destroy(&me.d) | |
5ce5170c MW |
36 | def __init__(me, strictp = False, laxp = False, semip = False): |
37 | cdef unsigned f | |
38 | f = 0 | |
39 | if strictp: | |
965caf5f | 40 | f |= URLF_STRICT |
5ce5170c | 41 | if laxp: |
965caf5f | 42 | f |= URLF_LAX |
5ce5170c | 43 | if semip: |
965caf5f | 44 | f |= URLF_SEMI |
5ce5170c | 45 | me.ctx.f = f |
965caf5f | 46 | def encode(me, object name, object value): |
addc0c37 | 47 | """UE.encode(NAME, VALUE): encode a key/value pair""" |
965caf5f | 48 | url_enc(&me.ctx, &me.d, TEXT_PTR(name), TEXT_PTR(value)) |
5ce5170c | 49 | return me |
965caf5f MW |
50 | @property |
51 | def result(me): | |
addc0c37 | 52 | """UE.result -> STR: the encoded string""" |
965caf5f MW |
53 | return TEXT_FROMSTRLEN(me.d.buf, me.d.len) |
54 | @property | |
55 | def strictp(me): | |
addc0c37 | 56 | """UE.strictp -> BOOL: strictly escape non-alphanumerics?""" |
965caf5f MW |
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): | |
addc0c37 | 66 | """UE.laxp -> BOOL: only escape obviously unsafe characters?""" |
965caf5f MW |
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): | |
addc0c37 | 76 | """UE.semip -> BOOL: separate key/value pairs with semicolons?""" |
965caf5f MW |
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 | |
5ce5170c MW |
84 | |
85 | cdef class URLDecode: | |
addc0c37 | 86 | """URLDecode(STR, [semip = False]): iterator over (KEY, VALUE) pairs""" |
5ce5170c MW |
87 | cdef url_dctx ctx |
88 | cdef char *p | |
89 | ||
376ad06d | 90 | def __cinit__(me, *hunoz, **hukairz): |
965caf5f | 91 | me.p = NULL |
5ce5170c | 92 | url_initdec(&me.ctx, me.p) |
965caf5f MW |
93 | def __dealloc__(me): |
94 | xfree(me.p) | |
95 | def __init__(me, object string, semip = False): | |
5ce5170c MW |
96 | cdef unsigned f |
97 | f = 0 | |
98 | if semip: | |
965caf5f | 99 | f |= URLF_SEMI |
5ce5170c | 100 | xfree(me.p) |
965caf5f | 101 | me.p = xstrdup(TEXT_PTR(string)) |
5ce5170c MW |
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: | |
965caf5f MW |
116 | nn = TEXT_FROMSTRLEN(n.buf, n.len) |
117 | vv = TEXT_FROMSTRLEN(v.buf, v.len) | |
5ce5170c MW |
118 | dstr_destroy(&n) |
119 | dstr_destroy(&v) | |
120 | if not anyp: | |
121 | raise StopIteration | |
122 | return nn, vv | |
965caf5f MW |
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 | |
5ce5170c | 133 | |
5b1830f3 | 134 | ###----- That's all, folks -------------------------------------------------- |