| 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 | cdef url_ectx ctx |
| 28 | cdef dstr d |
| 29 | |
| 30 | def __cinit__(me, *hunoz, **hukairz): |
| 31 | url_initenc(&me.ctx) |
| 32 | DCREATE(&me.d) |
| 33 | def __init__(me, strictp = False, laxp = False, semip = False): |
| 34 | cdef unsigned f |
| 35 | f = 0 |
| 36 | if strictp: |
| 37 | f = f | URLF_STRICT |
| 38 | if laxp: |
| 39 | f = f | URLF_LAX |
| 40 | if semip: |
| 41 | f = f | URLF_SEMI |
| 42 | me.ctx.f = f |
| 43 | def encode(me, char *name, char *value): |
| 44 | url_enc(&me.ctx, &me.d, name, value) |
| 45 | return me |
| 46 | property result: |
| 47 | def __get__(me): |
| 48 | return PyString_FromStringAndSize(me.d.buf, me.d.len) |
| 49 | property strictp: |
| 50 | def __get__(me): |
| 51 | return _tobool(me.ctx.f & URLF_STRICT) |
| 52 | def __set__(me, val): |
| 53 | if val: |
| 54 | me.ctx.f = me.ctx.f | URLF_STRICT |
| 55 | else: |
| 56 | me.ctx.f = me.ctx.f & ~URLF_STRICT |
| 57 | property laxp: |
| 58 | def __get__(me): |
| 59 | return _tobool(me.ctx.f & URLF_LAX) |
| 60 | def __set__(me, val): |
| 61 | if val: |
| 62 | me.ctx.f = me.ctx.f | URLF_LAX |
| 63 | else: |
| 64 | me.ctx.f = me.ctx.f & ~URLF_LAX |
| 65 | property semip: |
| 66 | def __get__(me): |
| 67 | return _tobool(me.ctx.f & URLF_SEMI) |
| 68 | def __set__(me, val): |
| 69 | if val: |
| 70 | me.ctx.f = me.ctx.f | URLF_SEMI |
| 71 | else: |
| 72 | me.ctx.f = me.ctx.f & ~URLF_SEMI |
| 73 | def __del__(me): |
| 74 | dstr_destroy(&me.d) |
| 75 | |
| 76 | cdef class URLDecode: |
| 77 | cdef url_dctx ctx |
| 78 | cdef char *p |
| 79 | |
| 80 | def __cinit__(me, *hunoz, **hukairz): |
| 81 | me.p = xstrdup('') |
| 82 | url_initdec(&me.ctx, me.p) |
| 83 | def __init__(me, char *string, semip = False): |
| 84 | cdef unsigned f |
| 85 | f = 0 |
| 86 | if semip: |
| 87 | f = f | URLF_SEMI |
| 88 | xfree(me.p) |
| 89 | me.p = xstrdup(string) |
| 90 | me.ctx.p = me.p |
| 91 | me.ctx.f = f |
| 92 | def __iter__(me): |
| 93 | return me |
| 94 | def __next__(me): |
| 95 | cdef dstr n |
| 96 | cdef dstr v |
| 97 | cdef object nn |
| 98 | cdef object vv |
| 99 | cdef int anyp |
| 100 | DCREATE(&n) |
| 101 | DCREATE(&v) |
| 102 | anyp = url_dec(&me.ctx, &n, &v) |
| 103 | if anyp: |
| 104 | nn = PyString_FromStringAndSize(n.buf, n.len) |
| 105 | vv = PyString_FromStringAndSize(v.buf, v.len) |
| 106 | dstr_destroy(&n) |
| 107 | dstr_destroy(&v) |
| 108 | if not anyp: |
| 109 | raise StopIteration |
| 110 | return nn, vv |
| 111 | property semip: |
| 112 | def __get__(me): |
| 113 | return _tobool(me.ctx.f & URLF_SEMI) |
| 114 | def __set__(me, val): |
| 115 | if val: |
| 116 | me.ctx.f = me.ctx.f | URLF_SEMI |
| 117 | else: |
| 118 | me.ctx.f = me.ctx.f & ~URLF_SEMI |
| 119 | def __del__(me): |
| 120 | xfree(me.p) |
| 121 | |
| 122 | ###----- That's all, folks -------------------------------------------------- |