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